︙ | | | ︙ | |
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);
}
}
this.eval2 = function(code) {
this.code = this.OK;
var parser = new TclParser(code);
var args = [];
var first = true;
|
<
>
|
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 += "...";
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
|
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("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;
|
>
>
>
>
>
>
>
>
>
|
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
|
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];
if (args.length == 3) interp.setVar(name, args[2]);
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';}
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 = " ";
if (args.length == 3) sep = args[2].toString();
var res = [], e;
var tmp = str.split(sep);
for(var i in tmp) {
e = tmp[i];
if(e == "") e = "{}";
res.push(e);
}
return res.join(" ");
});
this.registerSubCommand("string", "compare", function (interp, args) {
this.arity(args, 3);
var a = args[1].toString();
var b = args[2].toString();
|
>
|
|
>
|
<
|
|
|
|
|
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, 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';}
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() : " ";
var res = [], element;
var tmp = str.split(sep);
for(var i in tmp) {
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
|
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 = "";
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
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.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
|
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.insidequote = false;
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 ("$[".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.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 "\\":
if (this.len >= 2) this.feedSequence();
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.insidequote = false;
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 == "\\") { // 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.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 "\\":
if (this.len >= 2) this.feedSequence();
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
|
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 == "{") {
blevel++;
} else if (this.cur == "}") {
if (blevel != 0) blevel--;
}
this.feedchar();
}
|
|
|
|
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 == "{") {
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
|
this.setPos(this.end+1);
return this.OK;
}
this.parseBrace = function () {
var level = 1;
this.feedcharstart();
while (true) {
if (this.len > 1 && this.cur == "\\") {
this.feedSequence();
} else 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;
}
|
|
>
|
|
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 == "\\") {
this.feedSequence();
} else*/
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
|
}
if (this.cur == "#" && this.type == this.EOL) {
this.parseComment();
continue;
}
return this.parseString();
}
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 = {};
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+"'");
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) {
|
>
|
>
|
|
>
|
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);
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 = 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) {
|
︙ | | | ︙ | |