Check-in [4f3db7f982]
Overview
Comment:added several file commands, fixed backslash substitutions
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 4f3db7f982775837fa0e627dcee01572741dfb19
User & Date: suchenwi on 2013-11-23 21:28:45
Other Links: manifest | tags
Context
2013-11-24
08:24
added [file join], [file split] check-in: 07c2ea91bc user: suchenwi tags: trunk
2013-11-23
21:28
added several file commands, fixed backslash substitutions check-in: 4f3db7f982 user: suchenwi tags: trunk
2013-11-20
20:58
added [catch] check-in: 3d61c6fe53 user: suchenwi tags: trunk
Changes

Modified tcl053.js from [368d355e33] to [623beb5ebc].

1
2
3
4
5
6
7
8
9
10
11
12
13

14
15


16
17
18
19
20
21
22
23
24

25
26
27
28
29
30


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48







49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
1
2
3
4
5
6
7
8
9
10
11
12
13
14


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46





47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
62
63
64
65
66
67













+
-
-
+
+









+





-
+
+













-
-
-
-
-
+
+
+
+
+
+
+







-







/* =================================================== -*- C++ -*-
 * tcl.js "A Tcl implementation in Javascript"
 *
 * Released under the same terms as Tcl itself.
 * (BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
 *
 * Based on Picol by Salvatore Sanfilippo (<http://antirez.com/page/picol>)
 * (c) Stéphane Arnold 2007
 * Richard Suchenwirth 2007, 2013: cleanup, additions
 * vim: syntax=javascript autoindent softtabwidth=4
 */
// 'use strict'; // breaks some tests, like expr 0376, for loop
var _step = 0; // set to 1 for debugging
if(process.env["DEBUG"] == 1) _step = 1;
var fs = require('fs');
var puts = console.log;
var fs    = require('fs');
var puts  = console.log; // saves a lot of typing... ;^)

function TclInterp () {
    this.patchlevel = "0.5.3";
    this.callframe  = [{}];
    this.level      = 0;
    this.levelcall  = [];
    this.commands   = {};
    this.procs      = [];
    this.script     = "";
    this.getsing    = 0;
    this.OK  = 0;
    this.RET = 1;
    this.BRK = 2;
    this.CNT = 3;
    this.getVar = function(name) {
        var nm = name.toString();
        var nm  = name.toString();
	// no arrays supported yet, but a read-only exception for ::env()
        if (nm.match("^::env[(]")) nm = nm.substr(2);
        if (nm.match("^env[(]")) {
            var key = nm.substr(4,nm.length-5);
            var val = process.env[key];
        } else if (nm.match("^::")) {
            var val = this.callframe[0][nm.substr(2)]; // global
        } else {
            var val = this.callframe[this.level][name];
        }
        if (val == null) throw 'can\'t read "'+name+'": no such variable';
        return val;
    }
    this.setVar = function(name, val) {
        var nm = name.toString();
        if (nm.match("^::")) {
            this.callframe[0][nm.substr(2)] = val;
        } else {this.callframe[this.level][name] = val;}
        return val;
      var nm  = name.toString();
      if (val != null && val.toString().match(/\\/))
	val = eval("'"+val.toString()+"'");
      if (nm.match("^::")) {
	this.callframe[0][nm.substr(2)] = val;
      } else {this.callframe[this.level][name] = val;}
      return val;
    }
    this.setVar("argc",  process.argv.length-2);
    this.setVar("argv0", process.argv[1]);
    this.setVar("argv",  process.argv.slice(2));
    this.setVar("errorInfo", "");

    this.incrLevel = function() {
      //puts("going down from ("+this.level+"): "+this.levelcall);
      this.callframe[++this.level] = {};
      return this.level;
    }
    this.decrLevel = function() {
      this.callframe[this.level] = null;
      this.level--;
      if (this.level < 0) throw "Exit application";
102
103
104
105
106
107
108

109
110
111
112
113
114
115
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120







+







      } catch (e) {
	var msg = code.substr(0,128);
	if(msg.length >= 125) msg += "...";
	var msg = e+'\n        while executing\n"'+msg+'"';
	for(var i = this.level; i > 0; i--)
	  msg += '\n        invoked from within\n"'+this.levelcall[i]+'"'
	this.setVar("::errorInfo", msg);
	if(_step) puts("e: "+e);
	throw e;
      }
    }
    this.eval2 = function(code) {
      this.code  = this.OK;
      var parser = new TclParser(code);
      var args   = [];
168
169
170
171
172
173
174





175
176
177
178
179
180
181
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191







+
+
+
+
+







	  args[args.length-1] = args[args.length-1].toString() + text.toString();
	}
      }
      if (args.length > 0) result = this.call(args);
      return this.objectify(result);
    }
    //---------------------------------- Commands in alphabetical order
    /*this.registerCommand("after", function (interp, args) {
        this.arity(args, 3);
	var code = args[2].toString();
	setTimeout(args[1], function(code) {interp.eval(code)});
	});*/
    this.registerCommand("append", function (interp, args) {
        this.arity(args, 2, Infinity);
        var vname = args[1].toString();
	try {var str = interp.getVar(vname);} catch(e) {var str = "";}
	for (var i = 2; i < args.length; i++) str += args[i].toString();
        interp.setVar(vname, str);
        return str;
202
203
204
205
206
207
208
209
210




211
212
213
214
215
216
217
212
213
214
215
216
217
218


219
220
221
222
223
224
225
226
227
228
229







-
-
+
+
+
+







      });
    this.registerCommand("continue", function (interp, args) {
        interp.code = interp.CNT;
        return;
      });
    this.registerSubCommand("clock", "format", function (interp, args) {
        var now = new Date();
        now.setTime(args[1]);
        return now.toString();
        now.setTime(args[1]*1000);
        var ts = now.toString().split(" ");
	var tz = ts[6].toString().replace("(","").replace(")","");
	return ts[0]+" "+ts[1]+" "+ts[2]+" "+ts[4]+" "+tz+" "+ts[3];
      });
    this.registerSubCommand("clock", "milliseconds", function (interp, args) {
	var t = new Date();
	return t.valueOf();
      });
    this.registerSubCommand("clock", "scan", function (interp, args) {
        return Date.parse(args[1]);
356
357
358
359
360
361
362

363
364

365
366
367
368
369

370
371
372
373
374
375
376
377





378
379






380
381
382
































383
384
385
386
387
388
389
368
369
370
371
372
373
374
375
376

377
378
379
380
381

382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442







+

-
+




-
+








+
+
+
+
+


+
+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    var sqrt = Math.sqrt; // "publish" other Math.* functions as needed

    this.registerCommand("expr", function (interp, args) {
	var expression = args.slice(1).join(" ");
	return interp.expr(interp, expression);
      });
    this.expr = function (interp, expression) { // also used in for, if, while
      var mx;
      try {
	var mx = expression.match(/(\[.*\])/g);
	mx = expression.match(/(\[.*\])/g);
	for (var i in mx)
	  puts("have to deal with "+mx[i].toString());
      } catch(e) {puts(i+". exception: "+e);}
      mx = expression.match(/(\$[A-Za-z0-9_:]+)/g);
      for (i in mx) {
      for (var i in mx) {
	var val = interp.getVar(mx[i].slice(1)).toString();
	if(isNaN(val) || !isFinite(val)) val = '"'+val+'"';
	eval("var "+mx[i]+' = '+val);
      }
      var res = eval(expression);
      if(res == false) res = 0; else if(res == true) res = 1;
      return res;
    };
    this.registerSubCommand("file", "atime", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.atime.getTime()/1000;
      })
    this.registerSubCommand("file", "dirname", function (interp, args) {
        this.arity(args, 2);
	return interp.dirname(args[1].toString());
     });
    this.dirname = function(p) { // also used in [glob]
      //require("path"); //not working :(
      //return path.dirname(p.toString());
      if(p == ".") p = process.cwd();
	var path = args[1].toString().split("/");
	path.pop();
	return path.join("/");
      p = p.split("/"); 
      p.pop();
      if(p == "") return("/");
      return p.join("/");
    };
    this.registerSubCommand("file", "exists", function (interp, args) {
        this.arity(args, 2);
	var file = args[1].toString();
	try {var fd = fs.openSync(file,"r");} catch(e) {return 0;}
	fs.closeSync(fd);
	return 1;
     });
    this.registerSubCommand("file", "extension", function (interp, args) {
        this.arity(args, 2);
	var fn  = args[1].toString();
	var res = fn.split(".").pop();
	res = (res == fn)? "" : "."+res;
	return res;
     });
    this.registerSubCommand("file", "mtime", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.mtime.getTime()/1000;
      })
    this.registerSubCommand("file", "size", function (interp, args) {
        this.arity(args, 2);
	var stat = fs.statSync(args[1].toString());
	return stat.size;
      })
    this.registerSubCommand("file", "tail", function (interp, args) {
        this.arity(args, 2);
	return args[1].toString().split("/").pop();
      });
    this.registerCommand("for", function (interp, args) {
        this.arity(args, 5);
        interp.eval(args[1].toString());
        if(interp.code != interp.OK) return;
        var cond = args[2].toString();
        var step = args[3].toString();
430
431
432
433
434
435
436
437

438
439
440
441
442


443
444
445
446
447
448
449
450
451






















452
453
454
455
456
457





















458
459
460
461
462
463
464
465
466
467
483
484
485
486
487
488
489

490

491



492
493









494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515






516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536

537

538
539
540
541
542
543
544







-
+
-

-
-
-
+
+
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-

-







	if(fmt=="%x") {
	  var x = new Number(val);
	  return x.toString(16);
	} else if(fmt=="%X") {
	  var x = new Number(val);
	  return x.toString(16).toUpperCase();
	}
	else throw "unknown format";
	else throw "unknown format";	
	
      });
    /*
    this.registerCommand("gets", function (interp, args) {
        this.arity(args, 2, 3);
   this.registerCommand("gets0", function (interp, args) {
	this.arity(args, 2, 3);
	this.tmp = null;
	rl.on('line', itp.gets);
	while(true) {
	  if(this.tmp != null) break;
	};
	rl.on('line', this.getsevalputs);
        var reply = this.tmp;
        // = prompt(args[1],"");
        //process.stdin.resume();
	interp.getsing = 1;
	//interp.buf = "";
	//while(interp.buf == "") {
	//interp.timeout = setTimeout(function(){}, 10000);
	//  if(interp.getsing==0) break;
	//}
	return; // result will be in interp.buf when done
     });
   this.gets = function(char) {
     try {
       if(char.match(/foo[\r\n]/)) {
	 this.getsing = 0;
	 puts("received: "+this.buf);
       } else {
	 puts("<"+char+">"+this.getsing);
	 this.buf += char;
       }
     } catch(e) {puts(e)};
   }
   this.registerCommand("glob", function (interp, args) {
	this.arity(args, 2, Infinity);
	args.shift();
        //process.stdin.on('data', function(str) {
        //reply = str;
        //    });
        if(args[2] != null) {
	  interp.setVar(args[2],interp.objectify(reply));
	  return reply.length;
	var res    = [];
	var prefix = "";
	var dir    = ".";
	for (var arg in args) {
	  var path    = args[arg].toString();
	  if(path.match("[/]")) {
	     var dir    = interp.dirname(path);
	     var prefix = dir+"/";
	  }
	  var pattern = path.split("/").pop().replace(/[*]/g,".*");
	  var files   = fs.readdirSync(dir);
	  for (var i in files) {
	    if(files[i].match("^[.]")) continue;
	    if(files[i] == pattern) {res.push(files[i]);}
	    if(files[i].match("^"+pattern+"$")) {
	      var file = (dir == ".")? files[i] : dir+"/"+files[i];
	      res.push(file.replace(/\/\//,"/"));
	    }
	  }
	}
	return res;
        } else return reply;
      });
    */
    this.registerCommand("if", function (interp, args) {
        this.arity(args, 3, Infinity);
        var cond = args[1].toString();
        var test = interp.objectify(interp.expr(interp, cond));
        if (test.toBoolean()) return interp.eval(args[2].toString());
        if (args.length == 3) return;
        for (var i = 3; i < args.length; ) {
535
536
537
538
539
540
541
542

543
544
545
546
547
548
549
612
613
614
615
616
617
618

619
620
621
622
623
624
625
626







-
+







        return interp.mkList(interp.procs);
    });
    this.registerSubCommand("info", "script", function (interp, args) {
        return interp.script;
    });
    this.registerSubCommand("info", "vars", function (interp, args) {
	var res = [];
	for(i in interp.callframe[interp.level]) {
	for(var i in interp.callframe[interp.level]) {
	  try {
	    if(interp.getVar(i) != null) {res.push(i);}
	  } catch(e) {};
	}
	return res;
    });
    this.registerCommand("join", function (interp, args) {
597
598
599
600
601
602
603
604

605
606

607
608
609
610
611
612
613
674
675
676
677
678
679
680

681
682

683
684
685
686
687
688
689
690







-
+

-
+







      });
    this.registerCommand("llength", function (interp, args) {
        this.arity(args, 2);
        return args[1].toList().length;
      });
    this.registerCommand("lrange", function (interp, args) {
        this.arity(args, 4);
        var list    = interp.objectify(args[1]);
        var list  = interp.objectify(args[1]);
        var start = list.listIndex(args[2]);
        var end     = list.listIndex(args[3])+1;
        var end   = list.listIndex(args[3])+1;
        try {
	  return list.content.slice(start, end);
        } catch (e) {return [];}
      });
    this.registerCommand("lreverse", function (interp, args) {
        this.arity(args, 2);
        return args[1].toList().reverse();
691
692
693
694
695
696
697
698

699

700
701
702

703
704
705
706
707
708
709
768
769
770
771
772
773
774

775
776
777
778
779

780
781
782
783
784
785
786
787







-
+

+


-
+







        if (args.length == 3) interp.setVar(name, val);
        return interp.getVar(name);
    });
    this.registerCommand("source", function (interp, args) {
        this.arity(args, 2);
        interp.script = args[1].toString();
        try {
	  var data = fs.readFileSync(interp.script).toString();
	  var data = fs.readFileSync(interp.script,{encoding: 'utf8'}).toString();
        } catch(e) {
	  puts("e: "+e);
	  throw 'couldn\' read file "'+interp.script
	    +'": no such file or directory';}
	var res    = interp.eval(data);
	var res       = interp.eval(data);
	interp.script = "";
	return res;
      });
    this.registerCommand("split", function (interp, args) {
        this.arity(args, 2, 3);
        var str = args[1].toString();
        var sep = (args.length == 3)? args[2].toString() : " ";
884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
962
963
964
965
966
967
968

969
970
971
972
973
974
975
976







-
+







Tcl.isDecimal  = new RegExp("^[+\\-]?[1-9][0-9]*$");
Tcl.isHex      = new RegExp("^0x[0-9a-fA-F]+$");
Tcl.isOctal    = new RegExp("^[+\\-]?0[0-7]*$");
Tcl.isHexSeq   = new RegExp("[0-9a-fA-F]*");
Tcl.isOctalSeq = new RegExp("[0-7]*");
Tcl.isList     = new RegExp("[\\{\\} ]");
Tcl.isNested   = new RegExp("^\\{.*\\}$");
Tcl.getVar     = new RegExp("^[a-zA-Z0-9_]+", "g");
Tcl.getVar     = new RegExp("^[:a-zA-Z0-9_]+", "g");

Tcl.Proc = function (interp, args) {
   var priv = this.privdata;
   interp.incrLevel();
   var arglist = priv[0].toList();
   var body    = priv[1];
   var call    = []; 
920
921
922
923
924
925
926
927

928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948

949
950
951
952
953
954
955
998
999
1000
1001
1002
1003
1004

1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024


1025
1026
1027
1028
1029
1030
1031
1032







-
+



















-
-
+







       } else if (name == "args") {
           interp.setVar("args", new TclObject(args.slice(i, args.length)));
           break;
       }
       interp.setVar(name, interp.objectify(args[i]));
   }
   if (name == "args" && i+1 < arglist.length)
       throw "'args' should be the last argument";
     throw "'args' should be the last argument";
   try {
       var res = interp.eval(body);
       interp.code = interp.OK;
       interp.decrLevel();
       return res;
   } catch (e) {
       interp.decrLevel();
       throw "Tcl.Proc exception "+e;
   }
}
/** Manage subcommands */
Tcl.EnsembleCommand = function (interp, args) {
  var sub  = args[1].toString();
  var main = args.shift().toString()+" "+sub;
  args[0]  = main;
  var ens  = this.ensemble;
  if (ens == null) {
    throw "Not an ensemble command: "+main;
  } else if (ens[sub] == null) {
    
    var matches   = 0, lastmatch = "";
    var matches = 0, lastmatch = "";
    for (var i in ens) { // maybe unambiguous prefix?
      if (i.match("^"+sub) != null) {
	matches  += 1;
	lastmatch = i;
      } 
    }
    if(matches == 1) {
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114



1115
1116
1117
1118
1119
1120
1121
1182
1183
1184
1185
1186
1187
1188



1189
1190
1191
1192
1193
1194
1195
1196
1197
1198







-
-
-
+
+
+







function TclCommand(func, privdata) {
  if (func == null) throw "No such function";
  this.func     = func;
  this.privdata = privdata;
  this.ensemble = arguments[2];
  
  this.call = function(interp, args) {
    var r = (this.func)(interp, args);
    r = interp.objectify(r);
    return r;
    var res = (this.func)(interp, args);
    res = interp.objectify(res);
    return res;
  }
  this.arity = function (args, min, max) {
    if(max == undefined) max = min;
    if (args.length < min || args.length > max) {
      throw min + ".."+max + " words expected, got "+args.length;
    }
  } 
1137
1138
1139
1140
1141
1142
1143
1144

1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

1155
1156
1157
1158
1159
1160
1161
1162
1163

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180

1181
1182
1183
1184
1185
1186
1187
1214
1215
1216
1217
1218
1219
1220

1221
1222
1223
1224
1225
1226
1227
1228
1229
1230

1231
1232
1233
1234
1235
1236
1237
1238
1239

1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256

1257
1258
1259
1260
1261
1262
1263
1264







-
+









-
+








-
+
















-
+







  this.len         = text.length;
  this.type        = this.EOL;
  this.cur         = this.text.charAt(0);
  this.getText     = function () {
    return this.text.substring(this.start,this.end+1);
  }
  this.parseString = function () {
    var newword = (this.type==this.SEP ||
    var newword = (this.type == this.SEP ||
		   this.type == this.EOL || this.type == this.STR);
    if (newword && this.cur == "{") return this.parseBrace();
    else if (newword && this.cur == '"') {
      this.insidequote = true;
      this.feedchar();
    }
    this.start = this.index;
    while (true) {
      if (this.len == 0) {
	this.end = this.index-1;
	this.end  = this.index-1;
	this.type = this.ESC;
	return this.OK;
      }
      /*if (this.cur == "\\") { // works not :(
	if (this.len >= 2) this.feedSequence();
	}
	else*/ if ("$[ \t\n\r;".indexOf(this.cur)>=0) {
	if ("$[".indexOf(this.cur)>=0 || !this.insidequote) {
	  this.end = this.index-1;
	  this.end  = this.index-1;
	  this.type = this.ESC;
	  return this.OK;
	}
      }
      else if (this.cur == '"' && this.insidequote) {
	this.end  = this.index-1;
	this.type = this.ESC;
	this.feedchar();
	this.insidequote = false;
	return this.OK;
      }
      this.feedchar();
    }
    return this.OK;
  }
  this.parseList = function () {
    var level = 0;
    var level  = 0;
    this.start = this.index;
    while (true) {
      if (this.len == 0) {
	this.end  = this.index;
	this.type = this.EOL;
	return;
      }
1299
1300
1301
1302
1303
1304
1305
1306
1307

1308
1309
1310
1311
1312
1313
1314
1376
1377
1378
1379
1380
1381
1382


1383
1384
1385
1386
1387
1388
1389
1390







-
-
+







      }
      if (this.cur == "#" && this.type == this.EOL) {
	this.parseComment();
	continue;
      }
      return this.parseString();
    }
    puts("unreachable?");
    return this.OK; // unreached
    //return this.OK; // unreached
  }
  this.feedSequence = function () {
    //return;
    if (this.cur != "\\") throw "Invalid escape sequence";
    var cur = this.steal(1);
    puts("enter feedSequence, text: "+this.text+" cur: "+cur);
    var specials = {};
1360
1361
1362
1363
1364
1365
1366
1367

1368
1369
1370


1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387

1388
1389
1390


1391
1392
1393

1394
1395
1396



1397
1398
1399
1400
1401
1402




1403
1404
1405
1436
1437
1438
1439
1440
1441
1442

1443
1444


1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462

1463
1464
1465

1466
1467
1468
1469
1470
1471



1472
1473
1474






1475
1476
1477
1478










-
+

-
-
+
+
















-
+


-
+
+



+
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
-
-
-
    return word;
  }
  this.feedcharstart = function () {
    this.feedchar();
    this.start = this.index;
  }
  this.setPos = function (index) {
    var d = index-this.index;
    var d      = index-this.index;
    this.index = index;
    this.len -= d;
    this.cur = this.text.charAt(this.index);
    this.len  -= d;
    this.cur   = this.text.charAt(this.index);
  }
  this.feedchar = function () {
    this.index++;
    this.len--;
    if (this.len < 0)
      throw "End of file reached";
    this.cur = this.text.charAt(this.index);
  }
}
//------------------------------------- main Read-Eval-Print loop
var itp = new TclInterp();
var res;
process.argv.slice(2).forEach(function(cmd,index,array) {
       itp.eval(cmd);
     });
var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
var rl       = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('% ');
rl.prompt();
itp.getsevalputs = function(line) {
itp.gets = function(line) {
  if (itp.getsing == 0) {
    try {
      res = itp.eval(line.trim());
    } catch(e) {res = e;}
    if (itp.getsing == 0) {
    if(res != null && res.toString() != "" && res.toString().length) 
      puts(res.toString());
    rl.prompt();
      if(res != null && res.toString().length) 
	puts(res.toString());
      rl.prompt();
};
itp.gets = function(line) {
  puts("received "+line); 
  this.tmp = line;
}
rl.on('line', itp.getsevalputs
    }
  } else {itp.buf = line; itp.getsing = 0; rl.prompt();}
};
rl.on('line', itp.gets).on('close',function() {process.exit(0);});
).on('close',function() {
    process.exit(0);
  });

Modified test_tcljs.tcl from [da8fb3dc27] to [744eb2cb5e].

1
2
3
4
5
6
7
8
9
10
11

12
13

14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


36
37
38


39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


55


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
1
2
3
4
5
6
7
8
9
10

11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
26
27
28
29
30
31


32


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63
64
65
66
67

68
69
70
71
72
73
74










-
+


+




-
+












-
-

-
-
+
+



+
+

+














-
+
+

+
+







-







# test suite for TclJS
# This file is designed so it can also run in a tclsh. Some JavaScript goodies,
# like 1/0, sqrt(-1) were excluded from the tests.
# [clock format 0] was excluded because the timezone string differed.

set vars [info vars] ;# for later cleanup
set version 0.5.3
set total   0
set passed  0
set fail    0
puts "------------------------ [info script] patchlevel: [info patchlevel]"
puts "----- [info script] of [clock format [file mtime [info script]]], patchlevel: [info patchlevel]"

proc e.g. {cmd -> expected} {
    #puts $cmd
    incr ::total
    incr ::fail ;# to also count exceptions
    set res [uplevel 1 $cmd]
    if ![string equal $res $expected] {
    #if {$res != $expected} {} #should work, but wouldn't
	#if {$res != $expected} {}
	puts "**** $cmd -> $res, expected: $expected"
    } else {incr ::passed; incr ::fail -1}
}

# e.g. {exec echo hello} -> hello ;# needs blocking exec

e.g. {append new hello} -> hello
e.g. {set x foo}        -> foo
e.g. {append x bar}     -> foobar

proc sum args {expr [join $args +]}
e.g. {sum 1 2 3} -> 6
#native sum2 {function (interp, args) {return eval(args.join("+"));}}
#e.g. {sum2 2 3 4} -> 9

e.g. {catch foo msg} -> 1
e.g. {set msg} -> {invalid command name "foo"}
e.g. {catch foo msg}    -> 1
e.g. {set msg}          -> {invalid command name "foo"}
e.g. {catch {expr 7*6}} -> 0
e.g. {catch {expr 7*6} msg; set msg} -> 42

e.g. {clock format 0}   -> {Thu Jan 01 01:00:00 CET 1970}

e.g. {concat {a b} {c d}} -> {a b c d}
e.g. {concat $::version}  -> $version

e.g. {set d [dict create a 1 b 2 c 3]} -> {a 1 b 2 c 3}
e.g. {dict exists $d c} -> 1
e.g. {dict exists $d x} -> 0
e.g. {dict get $d b}    -> 2
e.g. {dict keys $d}     -> {a b c}
e.g. {dict set d b 5}   -> {a 1 b 5 c 3}
e.g. {dict set d x 7}   -> {a 1 b 5 c 3 x 7}
e.g. {dict unset d b}   -> {a 1 c 3 x 7}
e.g. {dict unset d x}   -> {a 1 c 3}
e.g. {dict unset d nix} -> {a 1 c 3}
e.g. {dict set dx a 1}  -> {a 1} ;# create new dict if not exists

e.g. {set home [file dirname [pwd]]; list} -> {}
e.g. {string equal [set env(HOME)] $home}   -> 1
e.g. {string equal [set env(HOME)] $home}  -> 1
# e.g. {string equal $::env(HOME) $home}  -> 1
e.g. {string equal [set ::env(HOME)] $home} -> 1
e.g. {file dirname /foo/bar/grill}          -> /foo/bar
e.g. {file tail    /foo/bar/grill}          -> grill

e.g. {expr 6*7}         -> 42
e.g. {expr {6 * 7 + 1}} -> 43
e.g. {set x 43}         -> 43
e.g. {expr {$x-1}}      -> 42
e.g. {expr $x-1}        -> 42
if ![info exists auto_path] { ;#these tests are not for a real tclsh
    e.g. {clock format 0} -> {Thu Jan 01 1970 01:00:00 GMT+0100 (CET)}
    e.g. {set i [expr 1/0]} -> Infinity
    e.g. {expr $i==$i+42}   -> 1
    e.g. {set n [expr sqrt(-1)]} -> NaN
    e.g. {expr $n == $n} -> 0
    e.g. {expr $n==$n}   -> 0
    e.g. {expr $n!=$n}   -> 1
    e.g. {info patchlevel} -> $version
90
91
92
93
94
95
96






97
98
99
100
101
102
103
104
105
106
107
108
109

110
111
112
113
114




115
116

117
118
119
120
121
122
123
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118

119
120




121
122
123
124
125

126
127
128
129
130
131
132
133







+
+
+
+
+
+












-
+

-
-
-
-
+
+
+
+

-
+







e.g. {expr {$x+1}} -> 4
e.g. {set x a; set y b; expr {$x == $y}} -> 0
e.g. {expr {$x != $y}} -> 1
e.g. {expr 43 % 5}     -> 3 
e.g. {set x -44; expr {-$x}} -> 44
e.g. {expr 1<<3} -> 8

e.g. {file dirname foo/bar/grill}  -> foo/bar
e.g. {file dirname /foo/bar/grill} -> /foo/bar
e.g. {file extension foo.txt}      -> .txt
e.g. {file extension Makefile}     -> ""
e.g. {file tail foo/bar/grill}     -> grill

set forres ""
e.g. {for {set i 0} {$i < 5} {incr i} {append forres $i}; set forres} -> 01234
e.g. {foreach i {a b c d e} {append foreachres $i}; set foreachres}   -> abcde

e.g. {format %x 255} -> ff
e.g. {format %X 254} -> FE

e.g. {set x 41}  -> 41
e.g. {incr x}    -> 42
e.g. {incr x 2}  -> 44
e.g. {incr x -3} -> 41

e.g. {info args e.g.} -> {cmd -> expected}
e.g. {info args e.g.}        -> {cmd -> expected}
e.g. {unset -nocomplain foo} -> {}
e.g. {info exists foo} -> 0
e.g. {set foo 42}      -> 42
e.g. {info exists foo} -> 1
e.g. {info level}      -> 0 ;# e.g. runs the command one level up
e.g. {info exists foo}       -> 0
e.g. {set foo 42}            -> 42
e.g. {info exists foo}       -> 1
e.g. {info level}            -> 0 ;# e.g. runs the command one level up
e.g. {proc f x {set y 0; info vars}} -> ""
e.g. {f 41}            -> {x y}
e.g. {f 41}                          -> {x y}
set tmp [f 40]; e.g. {lappend tmp z} -> {x y z}
e.g. {info args f}      -> x
e.g. {info body f}      -> {set y 0; info vars}
e.g. {info bod f}       -> {set y 0; info vars}

e.g. {join {a b c}}     -> {a b c}
e.g. {join {a b c} +}   -> {a+b+c}
138
139
140
141
142
143
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168













169
170



171
172
173
174
175
176
177
178
179

180
181
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167












168
169
170
171
172
173
174
175
176
177
178
179
180
181

182
183
184
185
186
187
188
189

190
191

192
193
194







+












-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+
+
+





-


-
+


e.g. {lsearch $x y}       -> -1
e.g. {lsort {z x y}}      -> {x y z}

e.g. {proc f args {expr [join $args +]}} -> ""
e.g. {f 1}     -> 1
e.g. {f 1 2}   -> 3
e.g. {f 1 2 3} -> 6
e.g. {proc f {arg b} {expr $arg*$b}; f 6 7} -> 42 ;# should work with 'args'

e.g. {regexp {X[ABC]Y} XAY}    -> 1
e.g. {regexp {X[ABC]Y} XDY}    -> 0
e.g. {regsub {[A-C]+} uBAAD x} -> uxD 

e.g. {split "a b  c d"}     -> {a b {} c d}
e.g. {split " a b  c d"}     -> {{} a b {} c d}
e.g. {split "a b  c d "}     -> {a b {} c d {}}
e.g. {split usr/local/bin /} -> {usr local bin}
e.g. {split /usr/local/bin /} -> {{} usr local bin}
e.g. {split abc ""}          -> {a b c}

e.g. {string compare a b}     -> -1
e.g. {string compare b a}     -> 1
e.g. {string compare b b}     -> 0
e.g. {string equal foo foo}   -> 1
e.g. {string equal foo bar}   -> 0
e.g. {string index abcde 2}   -> c
e.g. {string length ""}       -> 0
e.g. {string length foo}      -> 3
e.g. {string range hello 1 3} -> ell
e.g. {string tolower Tcl}     -> tcl
e.g. {string toupper Tcl}     -> TCL
e.g. {string trim " foo "}    -> foo
e.g. {string compare a b}       -> -1
e.g. {string compare b a}       -> 1
e.g. {string compare b b}       -> 0
e.g. {string equal foo foo}     -> 1
e.g. {string equal foo bar}     -> 0
e.g. {string index abcde 2}     -> c
e.g. {string length ""}         -> 0
e.g. {string length foo}        -> 3
e.g. {string range hello 1 3}   -> ell
e.g. {string range hello 1 end} -> ello
e.g. {string tolower Tcl}       -> tcl
e.g. {string toupper Tcl}       -> TCL
e.g. {string trim " foo "}      -> foo

e.g. {set x a.\x62.c} -> a.b.c ;# severe malfunction, breaks test suite operation :(
e.g. {set x a.\x62.c} -> a.b.c
e.g. {set e \u20ac} -> "€" ;# breaks in node v0.6.19, works in v0.10.22


puts "total $total tests, passed $passed, failed $fail"
#----------- clean up variables used in tests
foreach var [info vars] {
    set pos [lsearch $vars $var] ;# expr can't substitute commands yet
    set neq [string compare $var vars]
    if {$var != "vars" && $pos < 0} {unset $var}
}
unset vars var pos neq
unset vars var pos
puts "vars now: [info vars]"
puts "[llength [info commands]] commands implemented"