Check-in [3d61c6fe53]
Overview
Comment:added [catch]
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 3d61c6fe53bb2dc412187dd18ec9a28fdbe70911
User & Date: suchenwi on 2013-11-20 20:58:57
Other Links: manifest | tags
Context
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
2013-11-19
20:21
now passing all 105 tests check-in: c9965b6d9d user: suchenwi tags: trunk
Changes

Modified tcl053.js from [7793cfca8e] to [368d355e33].

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

110
111
112
113
114
115
116
98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
114
115
116







-




+







    }
    this.eval = function (code) {
      try {
	return this.eval2(code);
      } catch (e) {
	var msg = code.substr(0,128);
	if(msg.length >= 125) msg += "...";
	puts(e);
	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);
	throw e;
      }
    }
    this.eval2 = function(code) {
      this.code  = this.OK;
      var parser = new TclParser(code);
      var args   = [];
      var first  = true;
179
180
181
182
183
184
185









186
187
188
189
190
191
192
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201







+
+
+
+
+
+
+
+
+







	for (var i = 2; i < args.length; i++) str += args[i].toString();
        interp.setVar(vname, str);
        return str;
      });
    this.registerCommand("break", function (interp, args) {
        interp.code = interp.BRK;
        return;
      });
    this.registerCommand("catch", function (interp, args) {
	this.arity(args, 2, 3);
	var code = args[1].toString();
	var res;
	var rc   = 0;
	try {res = interp.eval(code);} catch(e) {res = e; rc = 1;}
	if(args.length == 3) interp.setVar(args[2], res);
	return rc;
      });
    this.registerCommand("cd", function (interp, args) {
	this.arity(args, 1, 2);
	var dir = process.env.HOME;
	if (args.length == 2) dir = args[1].toString();
	process.chdir(dir);
	return;
674
675
676
677
678
679
680

681

682
683
684
685
686
687
688
689
690


691
692
693
694
695
696
697
698

699
700

701
702
703
704
705



706
707
708
709
710
711
712
683
684
685
686
687
688
689
690

691
692
693
694
695
696
697
698
699

700
701
702
703
704
705
706
707
708

709


710
711
712



713
714
715
716
717
718
719
720
721
722







+
-
+








-
+
+







-
+
-
-
+


-
-
-
+
+
+







        var r = args[1];
        interp.code = interp.RET;
        return r;
    });
    this.registerCommand("set", function (interp, args) {
        this.arity(args, 2, 3);
        var name = args[1];
	var val  = eval(args[2]);
        if (args.length == 3) interp.setVar(name, args[2]);
        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();
        } catch(e) {
	  throw 'couldn\' read file "'+interp.script+'": no such file or directory';}
	  throw 'couldn\' read file "'+interp.script
	    +'": no such file or directory';}
	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 = " ";
        var sep = (args.length == 3)? args[2].toString() : " ";
        if (args.length == 3) sep = args[2].toString();
	var res = [], e;
	var res = [], element;
        var tmp = str.split(sep);
	for(var i in tmp) {
	  e = tmp[i];
	  if(e == "") e = "{}";
	  res.push(e);
	  element = tmp[i];
	  if(element == "") element = "{}";
	  res.push(element);
	}
	return res.join(" ");
      });
    this.registerSubCommand("string", "compare", function (interp, args) {
        this.arity(args, 3);
	var a = args[1].toString();
	var b = args[2].toString();
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
826
827
828
829
830
831
832



































833
834
835
836
837
838
839







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-







	  return;
        }
        base = cmd[0].toString();
        cmd.shift();
        interp.registerSubCommand(base, cmd.join(" "), eval(args[2].toString()));
        return;
      });
    this.math = function (name, a, b) {
      switch (name) {
      case "+":  return a + b;
      case "-":  return a - b;
      case "*":  return a * b;
      case "/":  return a / b;
      case "%":  return a % b;
      case "<":  return a < b? "1":"0";
      case ">":  return a > b? "1":"0";
      case "==": return a == b? "1":"0";
      case "!=": return a != b? "1":"0";
      default:   throw "Unknown operator: '"+name+"'";
      }
    }
    var ops = ["+","-","*","/","%","<",">","==","!="];
    for (var i in ops)
      this.registerCommand(ops[i],function (interp, args) {
	  this.arity(args, 3);
	  var name = args[0].toString();
	  var a    = interp.objectify(args[1]);
	  var b    = interp.objectify(args[2]);
	  if (name == '==') 
	    return new TclObject(a.toString() == b.toString()?"1":"0","BOOL");
	  if (name == '!=') 
	    return new TclObject(a.toString() != b.toString()?"1":"0","BOOL");
	  
	  var x = a.getNumber();
	  var y = b.getNumber();
	  if (a.isInteger() && b.isInteger())
	    return new TclObject(interp.math(name, x, y),"INTEGER");
	  if (a.isReal() && b.isReal())
	    return new TclObject(interp.math(name, x, y),"REAL");
	  return new TclObject(interp.math(name, args[1].toString(), 
					   args[2].toString()));
        });
    this.mkList = function(x) {
      var list = [];
      for (var name in x) {list.push(name);}
      return list;
    }
    this.objectify = function (text) {
      if (text == null) text = "";
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
1188
1189
1190
1191
1192
1193
1194

1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214

1215
1216

1217
1218
1219
1220
1221
1222
1223
1125
1126
1127
1128
1129
1130
1131



1132
1133
1134
1135





1136
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
1188

1189
1190

1191
1192
1193
1194
1195
1196
1197
1198







-
-
-
+
+
+

-
-
-
-
-
+
+
+
+
+

















-
-
-
-
+
+
+
+







-
+



















-
+

-
+







  this.SEP = 0;
  this.STR = 1;
  this.EOL = 2;
  this.EOF = 3;
  this.ESC = 4;
  this.CMD = 5;
  this.VAR = 6;
  this.text  = text;
  this.start = 0;
  this.end   = 0;
  this.text        = text;
  this.start       = 0;
  this.end         = 0;
  this.insidequote = false;
  this.index = 0;
  this.len = text.length;
  this.type = this.EOL;
  this.cur = this.text.charAt(0);
  this.getText = function () {
  this.index       = 0;
  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 ||
		   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.type = this.ESC;
	return this.OK;
      }
      if (this.cur == "\\") {
	//if (this.len >= 2) this.feedSequence();
      }
      else if ("$[ \t\n\r;".indexOf(this.cur)>=0) {
      /*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.type = this.ESC;
	  return this.OK;
	}
      }
      else if (this.cur == '"' && this.insidequote) {
	this.end    = this.index-1;
	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;
    this.start = this.index;
    while (true) {
      if (this.len == 0) {
	this.end  = this.index;
	this.type = this.EOL;
	return;
      }
      switch (this.cur) {
      case "\\":
	/*case "\\":
	if (this.len >= 2) this.feedSequence();
	break;
	break;*/
      case " ": case "\t": case "\n": case "\r":
	if (level > 0) break;
	this.end  = this.index - 1;
	this.type = this.SEP;
	this.feedchar();
	return;
      case '{': level++; break;
1250
1251
1252
1253
1254
1255
1256
1257
1258


1259
1260
1261
1262
1263
1264
1265
1225
1226
1227
1228
1229
1230
1231


1232
1233
1234
1235
1236
1237
1238
1239
1240







-
-
+
+







    while (true) {
      if (this.len == 0) break;
      if (this.cur == "[" && blevel == 0)
	level++;
      else if (this.cur == "]" && blevel == 0) {
	level--;
	if (level == 0) break;
      } else if (this.cur == "\\") {
	this.feedSequence();
	//} else if (this.cur == "\\") {
	//this.feedSequence();
      } else if (this.cur == "{") {
	blevel++;
      } else if (this.cur == "}") {
	if (blevel != 0) blevel--;
      }
      this.feedchar();
    }
1279
1280
1281
1282
1283
1284
1285
1286

1287

1288

1289
1290
1291
1292
1293
1294
1295
1254
1255
1256
1257
1258
1259
1260

1261
1262
1263

1264
1265
1266
1267
1268
1269
1270
1271







-
+

+
-
+







    this.setPos(this.end+1);
    return this.OK;
  }
  this.parseBrace = function () {
    var level = 1;
    this.feedcharstart();
    while (true) {
      if (this.len > 1 && this.cur == "\\") {
      /*if (this.len > 1 && this.cur == "\\") {
	this.feedSequence();
	} else*/
      } else if (this.len == 0 || this.cur == "}") {
      if (this.len == 0 || this.cur == "}") {
	level--;
	if (level == 0 || this.len == 0) {
	  this.end = this.index-1;
	  if (this.len > 0) this.feedchar();
	  this.type = this.STR;
	  return this.OK;
	}
1323
1324
1325
1326
1327
1328
1329

1330
1331
1332
1333
1334
1335
1336

1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352

1353
1354


1355
1356
1357
1358

1359
1360
1361
1362
1363
1364
1365
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312

1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330


1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344







+






-
+
















+
-
-
+
+




+







      }
      if (this.cur == "#" && this.type == this.EOL) {
	this.parseComment();
	continue;
      }
      return this.parseString();
    }
    puts("unreachable?");
    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);
    puts("enter feedSequence, text: "+this.text+" cur: "+cur);
    var specials = {};
    specials.a = "\a";
    specials.b = "\b";
    specials.f = "\f";
    specials.n = "\n";
    specials.r = "\r";
    specials.t = "\t";
    specials.v = "\v";
    switch (cur) {
    case 'u':
      var hex = this.steal(4);
      if (hex != Tcl.isHexSeq.exec(hex))
	throw "Invalid unicode escape sequence: "+hex;
      cur = String.fromCharCode(parseInt(hex,16));
      break;
    case 'x':
      /*
      var hex = this.steal(2);
      puts("enter case x, hex: '"+hex+"'");
      var hex = cur; //this.steal(2);
      puts("enter case x, hex: '"+hex+"' cur: '"+cur+'"');
      if (hex != Tcl.isHexSeq.exec(hex))
	throw "Invalid unicode escape sequence: "+hex;
      cur = String.fromCharCode(parseInt(hex,16));
      //puts("hex: "+hex.toString()+" cur: "+cur);
      */
      break;
    case "a": case "b": case "f": case "n":
    case "r": case "t": case "v":
      cur = specials[cur];
      break;
    default:
      if ("0123456789".indexOf(cur) >= 0) {

Modified test_tcljs.tcl from [c15ca09a61] to [da8fb3dc27].

27
28
29
30
31
32
33





34
35
36
37
38
39
40
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45







+
+
+
+
+







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 {expr 7*6}} -> 0
e.g. {catch {expr 7*6} msg; set msg} -> 42

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

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}
70
71
72
73
74
75
76




77
78
79
80
81
82
83



84
85
86
87
88
89
90
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102







+
+
+
+







+
+
+







e.g. {set x 0xFF; expr {$x+0}} -> 255
e.g. {expr 0376}               -> 254
e.g. {set x 0375; expr $x}     -> 253
e.g. {expr {$x}}   -> 253
e.g. {expr 6 * 7}  -> 42
e.g. {expr 1 == 2} -> 0
e.g. {expr 1 < 2}  -> 1
e.g. {expr 1 > 2}  -> 0
e.g. {expr 1 != 2} -> 1
e.g. {expr 1 <= 2} -> 1
e.g. {expr 1 >= 2} -> 0
e.g. {expr !0}     -> 1
e.g. {expr !42}    -> 0
e.g. {set x 3}     -> 3
e.g. {expr $x+1}   -> 4
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

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
121
122
123
124
125
126
127





128
129
130
131
132
133
134
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151







+
+
+
+
+







e.g. {llength $x}         -> 4
e.g. {lindex $x 2}        -> c
e.g. {lrange $x 1 2}      -> {b c}
e.g. {lreverse {a b c}}   -> {c b a}
e.g. {lsearch $x b}       -> 1
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. {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}
146
147
148
149
150
151
152
153

154
155
156
157
158
159
160
161
162
163
164
163
164
165
166
167
168
169

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







-
+











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. {set x a.\x62.c} -> a.b.c ;# severe malfunction, breaks test suite operation :(
e.g. {set x a.\x62.c} -> a.b.c ;# severe malfunction, breaks test suite operation :(

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
puts "vars now: [info vars]"
puts "[llength [info commands]] commands implemented"