[Sarnold]: I created a Tcl implementation in [JavaScript]. The idea came from the need I felt to process documentation in wiki form in a browser - i.e., store manuals as wiki markup ''directly'' on the server, and let some JavaScript code process it. This idea is not new; many AJAX applications already do it. But remember, Tcl was created as a configuration language, and I need this type of language. But I wonder if I will ever have the time to finish it. '''JsTcl''' - this implementation is far from being complete. I began it by looking at [Picol]'s parser code, and converting it into JavaScript. '''Downloads''' You will need: * [http://sarnold.free.fr/pub/jstcl/tcl02.js], the Tcl implementation. * wiki-resident copy: [tcl.js], [tcl.js 0.2] * [http://sarnold.free.fr/pub/jstcl/jstcl.html], an on-line demo with the official test suite (download the test suite at [http://sarnold.free.fr/pub/jstcl/testsuite.tcl]). '''Supported platforms (browsers?)''' This implementation was tested with Firefox 2.0.0.3 and IE 7.0 on [Microsoft Windows] XP. puts {Really, Tcl runs everywhere!} '''Differences with standard Tcl''' Like in [Picol], we have no [expr]. Instead, we have: + - * / % = != which operate on integers/reals, and not and or which operate on booleans (there *is* a boolean type in this implementation). ---- [RS]: [tcl.js] is indeed a lovely baby, though so far poorly equipped (e.g., no loop commands like [for], [foreach], [while]...). I had to add this.registerEasyCommand("<", function (interp, args) { this.requireExactArgc(args, 3); var a = args[1].getNumber(); var b = args[2].getNumber(); return (a < b); }); or, simpler, this.registerEasyCommand("<", function (interp, args) { this.requireExactArgc(args, 3); return (args[1].getNumber() < args[2].getNumber()); }); to get one of my favorite test cases through (ignore the backslash before * - Wiki is over-aggressive with that): proc fac x { if [< $x 2] {return 1} \* $x [fac [- $x 1]] } puts "fac 5 = [fac 5]" But it wasn't hard - just a matter of copy'n'paste :^) On IE 5.50, the demo page didn't work, but it did in Opera 7.54 (except for [source].. which lacked a good treatment for XMLHTTP). In any case, putting the test code right into jstcl.html works fine. Or using IE 6 on Win XP :^) My latest version is [tcl.js 0.2] ---- [RS]: Playing more with Javascript, it's fun to see how similar it is to Tcl in spirit, even though different. For instance, to roughly approximate the [clock] command, it just took these few lines of wrapping code: this.registerSubCommand("clock", "format", function (interp, args) { var now = new Date(); now.setTime(args[1]); return now.toString(); }); this.registerSubCommand("clock", "scan", function (interp, args) { return Date.parse(args[1]); }); this.registerSubCommand("clock", "seconds", function (interp, args) { return (new Date()).valueOf(); }); or this, one up the meta ladder: JS evaluates Tcl that evaluates JS: this.registerCommand("jseval", function (interp, args) { return eval(args[1].toString()); }); A language that has [eval] can't be all bad :^) Testing: puts [jseval "6 * 7 + Math.min(2,1,0);" ] 42 This allows us to add Tcl commands as [proc]s which probably aren't even so inefficient, as ultimately the JS interpreter eats its own food: proc sqrt x {jseval "Math.sqrt($x);"} ---- [LV] 2007 Nov 14 Note that I ran the jstcl.html test suite using Windows IE 6 and Windows Firefox 2.0 and the tcl.js 0.2 worked fine with the test suite there. ---- [CMcC] 16Nov07 added a "dom" command which implements a half-assed interface to http://jquery.com/ giving a DOM interface. By including the jquery script, it allows stuff like this: dom "

Hello

" appendTo body; dom #fred css border "1px solid gray" which adds a paragraph to the end of the document and puts a gray border around it. this.registerCommand("dom", function (interp, args) { var selector = args[1].toString(); var fn = args[2].toString(); args = args.slice(3); for (var i = 0; i < args.length; i++) args[i] = args[i].toString(); if (args.length == 1) var code = args[0]; else var code = args.join("\",\""); var script="$(\"" + selector + "\")." + fn + "(\"" + code + "\")"; //alert(script); eval(script).toString(); return "dom " + selector; }); So cool. Note: if you need to quote something in one of the args, best to use js's apostrophe quoting ---- See also: [Picol] ---- !!!!!! %| [Category Language] | [Category Tcl Implementations] |% !!!!!!