Minor cleanup in JimParseVar()
[jimtcl.git] / make-c-ext.tcl
blob29f11072d34fc786f44c6d5aa775dd9018239e3b
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 lappend lines "static const char basename\[\] = \"[file tail $source]\";"
30 lappend lines "static const char pkgname\[\] = \"[file rootname [file tail $source]]\";"
32 lappend lines {static const char source[] = }
33 lappend lines {*}$sourcelines \;
35 lappend lines "int Jim_[file rootname [file tail $source]]Init(Jim_Interp *interp)"
36 lappend lines \
38 if (Jim_PackageProvide(interp, pkgname, "1.0", JIM_ERRMSG)) return JIM_ERR;
39 return Jim_Eval_Named(interp, source, basename, 1);
42 puts [join $lines \n]