dict exists: Improve performance when when key not found
[jimtcl.git] / make-c-ext.tcl
blobfd1a056bada0c7188d25488cefc4607d3e40a934
1 #!/usr/bin/env tclsh
3 # Usage: make-c-ext.tcl source.tcl >jim-source.c
5 # Converts a Tcl source file into C source suitable
6 # for loading as a static extension.
8 lassign $argv source
10 if {![string match *.tcl $source]} {
11 error "Source $source is not a .tcl file"
14 # Read the Tcl source and convert to C
15 # Note that no lines are removed in order to preserve line numbering
16 set sourcelines {}
17 set f [open $source]
18 while {[gets $f buf] >= 0} {
19 # Remove comment lines
20 regsub {^[ \t]*#.*$} $buf "" buf
21 # Escape quotes and backlashes
22 set buf [string map [list \\ \\\\ \" \\"] $buf]
23 lappend sourcelines \"$buf\\n\"
25 close $f
27 lappend lines {/* autogenerated - do not edit */}
28 lappend lines {#include <jim.h>}
29 set basename [file tail $source]
30 set pkgname [file rootname $basename]
32 lappend lines "int Jim_${pkgname}Init(Jim_Interp *interp)"
33 lappend lines "\{"
34 lappend lines "\tif (Jim_PackageProvide(interp, \"$pkgname\", \"1.0\", JIM_ERRMSG)) return JIM_ERR;"
35 lappend lines "\treturn Jim_EvalSource(interp, \"$basename\", 1, [join $sourcelines \n]);"
36 lappend lines "\}"
38 puts [join $lines \n]