aboutsummaryrefslogtreecommitdiff
path: root/make-c-ext.tcl
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2011-05-30 12:01:12 +1000
committerSteve Bennett <steveb@workware.net.au>2011-06-10 14:00:34 +1000
commitdbe7729d6b03470bf91de94a998b1c2e56b4ee3b (patch)
treebbb4d36fb9d7e43cdcb5a2d1d298963ea0970dd9 /make-c-ext.tcl
parent2d1f4f8041c487fcdef221d0c9c75b9c00d7225e (diff)
downloadjimtcl-dbe7729d6b03470bf91de94a998b1c2e56b4ee3b.zip
jimtcl-dbe7729d6b03470bf91de94a998b1c2e56b4ee3b.tar.gz
jimtcl-dbe7729d6b03470bf91de94a998b1c2e56b4ee3b.tar.bz2
Switch to a Tcl version of make-c-ext
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'make-c-ext.tcl')
-rw-r--r--make-c-ext.tcl42
1 files changed, 42 insertions, 0 deletions
diff --git a/make-c-ext.tcl b/make-c-ext.tcl
new file mode 100644
index 0000000..29f1107
--- /dev/null
+++ b/make-c-ext.tcl
@@ -0,0 +1,42 @@
+#!/usr/bin/env tclsh
+
+# Usage: make-c-ext.tcl source.tcl >jim-source.c
+
+# Converts a Tcl source file into C source suitable
+# for loading as a static extension.
+
+lassign $argv source
+
+if {![string match *.tcl $source]} {
+ error "Source $source is not a .tcl file"
+}
+
+# Read the Tcl source and convert to C
+# Note that no lines are removed in order to preserve line numbering
+set sourcelines {}
+set f [open $source]
+while {[gets $f buf] >= 0} {
+ # Remove comment lines
+ regsub {^[ \t]*#.*$} $buf "" buf
+ # Escape quotes and backlashes
+ set buf [string map [list \\ \\\\ \" \\"] $buf]
+ lappend sourcelines \"$buf\\n\"
+}
+close $f
+
+lappend lines {/* autogenerated - do not edit */}
+lappend lines {#include <jim.h>}
+lappend lines "static const char basename\[\] = \"[file tail $source]\";"
+lappend lines "static const char pkgname\[\] = \"[file rootname [file tail $source]]\";"
+
+lappend lines {static const char source[] = }
+lappend lines {*}$sourcelines \;
+
+lappend lines "int Jim_[file rootname [file tail $source]]Init(Jim_Interp *interp)"
+lappend lines \
+{{
+ if (Jim_PackageProvide(interp, pkgname, "1.0", JIM_ERRMSG)) return JIM_ERR;
+ return Jim_Eval_Named(interp, source, basename, 1);
+}}
+
+puts [join $lines \n]