-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest.js
More file actions
472 lines (454 loc) · 16.4 KB
/
test.js
File metadata and controls
472 lines (454 loc) · 16.4 KB
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
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
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
134
135
136
137
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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
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
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
468
469
470
471
472
/*jslint plusplus: true, white: true, nomen: true */
import * as parser from './es6/parser.js';
import TclInterp from './es6/interp.js';
import tclobj from './es6/tclobject.js';
import types from './es6/types.js';
import * as dom from './demos/dom.js';
import BoolObj from './es6/objtype_bool.js';
var now, scriptobj;
if (window.performance === undefined) {
now = function(){
return Date.now();
};
} else if (window.performance.now !== undefined) {
now = function(){return window.performance.now();}
} else if (window.performance.webkitNow !== undefined) {
now = function(){return window.performance.webkitNow();};
}
function show_script(commands, node) {
var command, first, word, i, j;
function show_tokens(tokens, tnode, isscript) {
var k, token, classname;
for (k=0; k<tokens.length; k++) {
token = tokens[k];
if (token[0] === parser.SCRIPT) {
show_script(token[1], tnode, true);
} else if (token[0] === parser.INDEX) {
show_tokens(token[1], dom.create('span', {
className: 'tok_INDEX'
}, tnode), false);
} else {
classname = 'tok tok_'+parser.tokenname[token[0]];
if (isscript && token[0] === parser.TEXT && first) {
classname += ' tok_commandname';
first = false;
}
dom.create('span', {
className: classname,
title: parser.tokenname[token[0]] + (token[2] !== undefined ? ': "'+token[2]+'"' : '')
}, tnode, token[1]);
}
}
}
for (i=0; i<commands.length; i++) {
command = commands[i];
first = true;
for (j=0; j<command.length; j++) {
word = command[j];
show_tokens(word, node, true);
}
}
}
function run(script, setup) {
var interp = new TclInterp(),
obj = tclobj.AsObj(script),
commands = obj.GetParsedScript(), node, outputnode;
if (setup) {
setup(interp);
}
/*
dom.create('pre', {
innerHTML: '<h4>Exec Parse</h4>'+obj.GetExecParse(interp)
}, 'output');
*/
node = dom.create('pre', {}, 'output');
show_script(commands[1], node);
outputnode = dom.create('pre', {
className: 'script_output'
}, 'output');
interp.registerCommand('puts', function(args){
var message;
interp.checkArgs(args, [1, 2], "?-nonewline? string");
if (args.length === 2) {
message = args[1] + '\n';
} else {
message = args[2];
}
dom.create('span', {}, outputnode, message);
});
interp.registerCommand('getstring', function(args, interp){
interp.checkArgs(args, 0, '');
return 'result of getstring';
});
interp.registerCommand('get string', function(args, interp){
interp.checkArgs(args, 0, '');
return 'result of get string';
});
interp.registerCommand('say_o', function(args, interp){
interp.checkArgs(args, 0, '');
return 'o';
});
interp.registerAsyncCommand('bar', function(c, args, interp){
interp.checkArgs(args, 0, '');
setTimeout(function(){
c('delayed result');
}, 2000);
});
var before, after;
before = now();
interp.TclEval(obj, function(result){
var usec;
after = now();
usec = (after - before) * 1000;
dom.create('span', {className: 'timing'}, outputnode, Math.round(usec)+' microseconds per iteration\n');
if (result.code === types.OK) {
console.log('Got ok: ', result, ' string concat: "'+result+'", '+Math.round(usec)+' microseconds');
dom.create('span', {className: 'tclresult'}, outputnode, result.result+'\n');
} else {
console.log('Got error: ', result, ': "'+result.result+'"');
dom.create('span', {className: 'tclerror'}, outputnode, result.result+'\n');
}
});
console.log('TclEval returned');
}
function expr(str) {
var obj = tclobj.AsObj(str), interp = new TclInterp();
interp.registerCommand('get_num', function(){
return 43;
});
interp.set_var('a', 6);
interp.set_array('b', 'x', 40);
interp.set_array('b', 'y', 4);
interp.set_array('b', '43', 4);
console.log('evaluating expression: {'+str+'}');
console.log('parser.parse_expr:', obj.GetExprParse());
console.log('stack:', obj.GetExprStack());
interp.TclExpr(obj, function(res){
console.log('result:', res);
});
console.log('TclExpr returned');
}
dom.onclick('test1', function(){
run('set a [getstring; list 2]\nputs "($a)"');
});
dom.onclick('test2', function(){
run('set a [getstring; list 2]\nputs {($a)}');
});
dom.onclick('test3', function(){
run('set a [get\\ string; list \\u306f]\nputs ({$a})');
});
dom.onclick('test4', function(){
run('#comment 1\nset a(foo) [get\\ string; list \\u306f]\nputs "(hello index foo of a: $a(foo))"');
});
dom.onclick('test5', function(){
run('#comment 1\nset a(foo) [get\\ string; list \\u306f\n# comment two\n]\nputs "(hello index foo of a: $a(foo))"');
});
scriptobj = tclobj.AsObj('#comment 1\nset o 0;set a(fo0\\ o) [get\\ string; list \\u306f\n# comment two\n]\nputs "(hello index foo of a: $a(f[say_o]${o} o)), again: (${a(fo0 o)})"\nputs [bar]; set x {final result};');
scriptobj.GetParsedScript();
console.log('scriptobj tostring: ('+scriptobj.toString()+')');
dom.onclick('test6', function(){
run(scriptobj);
});
dom.onclick('list1', function(){
run('list foo bar');
});
dom.onclick('test7', function(){
run('set d {a A b B c C}; dict get $d b');
});
dom.onclick('test8', function(){
run('set d {a A b B c C}; dict keys $d');
});
dom.onclick('test9', function(){
run('set d {a A b B c C}; dict set d b Updated; set d');
});
dom.onclick('test10', function(){
run('set d {a A b B c C}; dict merge {x X a oldA y Y c oldC} $d');
});
dom.onclick('test11', function(){
run('set d [dict create a A b B c C]; dict merge {x X a oldA y Y c oldC} $d');
});
dom.onclick('test12', function(){
run('puts [dict exists {a {b B} c C} a b]; puts [dict exists {a {a B} c C} a x]');
});
dom.onclick('test13', function(){
//run('set a+b c; puts "hello $a+b"');
//expr('$a+-min(3, 4)+[get_num]-$b(y)');
//expr('10 - 5');
//expr('$a+-min(3, 4)+[get_num]-$b([get_num]) eq "42 [get_num]"');
expr('$a+-min(3, 4)+[get_num]-$b([get_num]) eq {42}');
//expr('6 + -3 + 43 - 4');
//expr('2+-min(3, 4)+[get_num]');
});
dom.onclick('expr2', function(){
expr('"2012-12-30" <= "2013-01-17"');
});
dom.onclick('expr3', function(){
expr('"2013-01-17" <= "2012-12-30"');
});
dom.onclick('cs1', function(){
run('if {[getstring] eq "result of getstring"} {puts "then body"} else {puts "else body"}');
});
dom.onclick('cs2', function(){
run('set acc 0; for {set i 0} {$i < 100} {incr i} {puts "loop body: $i"; if {$i % 2 == 0} continue; if {$i > 9} break; incr acc $i}; puts $acc');
});
dom.onclick('cs3', function(){
run('set acc 0; set i 10; puts "i before: $i"; while {[incr i -1]} {puts "loop i: $i"; incr acc $i}; puts $acc');
});
dom.onclick('cs4', function(){
run('foreach e {1 2 3} {puts "loop body, e: ($e)"}');
run('foreach {k v} {a 1 b 2 c 3} e {x y} {puts "loop body, k: ($k), v: ($v), e: ($e)"}');
});
dom.onclick('cs5', function(){
run('puts [lmap e {1 2 3} {expr {$e*2}}]');
});
dom.onclick('cf1', function(){
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo}} {puts "v in proc: ($v), d: ($d)"; set v updated}; p 1; puts "v in global, after: ($v)"');
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo}} {puts "v in proc: ($v), d: ($d)"; set v updated}; p; puts "v in global, after: ($v)"');
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo}} {puts "v in proc: ($v), d: ($d)"; set v updated}; p 1 2 3; puts "v in global, after: ($v)"');
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo}} {puts "v in proc: ($v), d: ($d)"; set v updated}; p 1 2; puts "v in global, after: ($v)"');
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo} args} {puts "v in proc: ($v), d: ($d), args: ($args)"; set v updated}; p 1 2; puts "v in global, after: ($v)"');
run('set v global; puts "v in global, before: ($v)"; proc p {v {d foo} args} {puts "v in proc: ($v), d: ($d), args: ($args)"; set v updated}; p 1 2 3 4; puts "v in global, after: ($v)"');
});
dom.onclick('cf2', function(){
//run('proc foo {a b} {return "$a-$b"}; foo 1 2; foo 3 4');
run('proc newcmd {a b} {return "$a-$b"}; set i 0; newcmd 1 $i; incr i; newcmd 1 $i');
run('proc newcmd {a b} {return "$a-$b"}; for {set i 0} {$i < 2} {incr i} {newcmd 1 $i}');
});
dom.onclick('prof1', function(){
run('for {set i 0} {$i < 10000} {incr i} nop', function(I){
I.registerCommand('nop', function(){});
});
});
dom.onclick('prof2', function(){
//run('proc newcmd {a b} {return "$a-$b"}');
//run('proc newcmd {a b} {set b}');
//run('proc newcmd {a b} {}');
//run('for {set i 0} {$i < 10000} {incr i} {set lastres [newcmd 1 $i]}; set lastres');
run('proc newcmd {a b} {return "$a-$b"}; for {set i 0} {$i < 10000} {incr i} {newcmd 1 $i}');
run('proc nop args {}; for {set i 0} {$i < 10000} {incr i} nop');
run('proc nop {} {}; for {set i 0} {$i < 10000} {incr i} nop');
run('proc nop i {}; for {set i 0} {$i < 10000} {incr i} {nop $i}');
run('proc nop i {}; for {set i 0} {$i < 10000} {incr i} {nop 1}');
run('proc nop {{i {}}} {}; for {set i 0} {$i < 10000} {incr i} nop');
run('proc nop args {}; for {set i 0} {$i < 10000} {incr i} {nop $i}');
});
dom.onclick('prof3', function(){
run('for {set i 0} {$i < 10000} {incr i} nop', function(I){
var trueObj = new BoolObj(true),
falseObj = new BoolObj(false),
trueRes = new I.TclResult(I.types.OK, trueObj),
falseRes = new I.TclResult(I.types.OK, falseObj);
I.registerCommand('nop', function(){});
I.registerAsyncCommand('for', function(c, args){
I.checkArgs(args, 4, 'start test next body');
var start = args[1], test = args[2], next = args[3], body = args[4];
var vn = 'i', tv = 10000;
function t(c){
var v, r;
v = I.get_scalar(vn),
r = v.GetInt() < tv;
//return c(new I.TclResult(I.types.OK, r));
//return c(new I.TclResult(I.types.OK, r ? trueObj : falseObj));
return c(r ? trueRes : falseRes);
}
return I.exec(start, function(res){
if (res.code !== types.OK) {return c(res);}
return function loop(){
return t(function(res){
if (res.code !== types.OK) {return c(res);}
if (!(res.result.GetBool())) {return c();}
return I.exec(body, function(res){
switch (res.code) {
case types.CONTINUE:
case types.OK:
return I.exec(next, function(res){
if (res.code !== types.OK) {return c(res);}
return loop;
});
case types.BREAK:
return c();
default:
return c(res);
}
});
});
};
});
});
});
});
dom.onclick('prof4', function(){
var before, after, i, outputnode;
outputnode = dom.create('pre', {
className: 'script_output'
}, 'output');
function nop(f){return f;}
before = now();
var f, i = 10000;
function loop(){
if (i-- <= 0) return;
return nop(loop);
}
f = loop;
while (typeof f === "function") {f = f();}
after = now();
dom.create('span', {className: 'timing'}, outputnode, (after-before)+' microseconds\n');
});
dom.onclick('str1', function(){
run('string length "hello, world"');
});
dom.onclick('str2', function(){
run('string map -nocase {Foo FOO bar bAR b *b*} "hello foo baR world baz"');
run('string map {foo FOO bar bAR b *b*} "hello foo bar world baz"');
});
dom.onclick('str3', function(){
run('string trim " hello,\nworld\n\t "');
run('string trim "hello,\nworld\n\t "');
run('string trim " hello,\nworld"');
run('string trim "hello,\nworld"');
run('string trim "/hello,\nworld|" /|');
});
dom.onclick('str4', function(){
run('string trimleft " hello,\nworld\n\t "');
run('string trimleft "hello,\nworld\n\t "');
run('string trimleft " hello,\nworld"');
run('string trimleft "hello,\nworld"');
run('string trimleft "/hello,\nworld|" /|');
});
dom.onclick('str5', function(){
run('string trimright " hello,\nworld\n\t "');
run('string trimright "hello,\nworld\n\t "');
run('string trimright " hello,\nworld"');
run('string trimright "hello,\nworld"');
run('string trimright "/hello,\nworld|" /|');
});
dom.onclick('str6', function(){
run('string tolower "hello, World"');
run('string tolower "hello, World" 4');
run('string tolower "hello, World" 4 4+3');
run('string tolower "hello, World" 4 end');
run('string tolower "hello, World" 4 end-2');
});
dom.onclick('str7', function(){
run('string toupper "hello, World"');
run('string toupper "hello, World" 4');
run('string toupper "hello, World" 4 4+3');
run('string toupper "hello, World" 4 end');
run('string toupper "hello, World" 4 end-2');
});
dom.onclick('str8', function(){
run('string totitle "hello, WORLD"');
run('string totitle "hello, WORLD" 4');
run('string totitle "hello, WORLD" 4 4+3');
run('string totitle "hello, WORLD" 4 end');
run('string totitle "hello, WORLD" 4 end-2');
});
dom.onclick('str9', function(){
run('string bytelength "hello, world"');
run('string bytelength "は"');
});
dom.onclick('str10', function(){
run('string first bar "foo bar baz"');
run('string first bat "foo bar baz"');
run('string first bar "foo bar baz" 4');
run('string first bar "foo bar baz" 6');
});
dom.onclick('str11', function(){
run('string last bar "foo bar baz"');
run('string last bat "foo bar baz"');
run('string last bar "foo bar baz" 4');
run('string last bar "foo bar baz" 2');
run('string last bar "foo bar baz" end-2');
});
dom.onclick('str12', function(){
run('string index "hello, world" 0');
run('string index "hello, world" 4');
run('string index "hello, world" end');
run('string index "hello, world" end-1');
});
dom.onclick('str13', function(){
run('string range "hello, world" -1 100');
run('string range "hello, world" 4 end');
run('string range "hello, world" 0 end-1');
run('string range "hello, world" 0 0+5');
});
dom.onclick('str14', function(){
run('string reverse "hello, world"');
});
dom.onclick('str15', function(){
run('string repeat "xy" 4');
run('string repeat "xy" -1');
});
dom.onclick('str16', function(){
run('string replace "hello, world" 0 end');
run('string replace "hello, world" 0 end NEW');
run('string replace "hello, world" -1 100 NEW');
run('string replace "hello, world" 1 100 NEW');
run('string replace "hello, world" 1 5 NEW');
run('string replace "hello, world" 1 end-2 NEW');
run('string replace "hello, world" end-8 end-2 NEW');
});
dom.onclick('str17', function(){
run('string match *lo,* "hello, world"');
run('string match *lo!* "hello, world"');
run('string match *lo, "hello, world"');
run('string match lo,* "hello, world"');
run('string match lo, "hello, world"');
run('string match {*[lx]lo? *} "hello, world"');
run('string match {*[ax]lo? *} "hello, world"');
run('string match -nocase *lo,* "HELLO, WORLD"');
run('string match -nocase *lo!* "HELLO, WORLD"');
run('string match -nocase *lo, "HELLO, WORLD"');
run('string match -nocase lo,* "HELLO, WORLD"');
run('string match -nocase lo, "HELLO, WORLD"');
run('string match -nocase {*[lx]lo? *} "HELLO, WORLD"');
run('string match -nocase {*[ax]lo? *} "HELLO, WORLD"');
});
dom.onclick('str18', function(){
run('string wordstart "foo bar baz" 3');
run('string wordstart "foo bar baz" 2');
run('string wordstart "foo bar baz" 4');
run('string wordstart "foo bar baz" 0');
run('string wordstart "foo bar baz" -2');
run('string wordstart "foo bar baz" 20');
});
dom.onclick('str19', function(){
run('string wordend "foo bar baz" -2');
run('string wordend "foo bar baz" 3');
run('string wordend "foo bar baz" 2');
run('string wordend "foo bar baz" 4');
run('string wordend "foo bar baz" 10');
run('string wordend "foo bar baz" 20');
});
dom.onclick('str20', function(){
run('string compare foo bar');
run('string compare foo foo');
run('string compare bar foo');
});
dom.onclick('str21', function(){
run('string equal foo bar');
run('string equal foo foo');
run('string equal foo Foo');
run('string equal -nocase foo Foo');
run('string equal -length 2 foo fox');
run('string equal -length 3 foo fox');
});
dom.onclick('str22', function(){
run('string is alnum ""');
run('string is alnum -strict ""');
run('string is alnum "foobar"');
run('string is alnum "foo,bar"');
run('string is alnum "foo42bar"');
run('string is alnum "foo bar"');
run('string is alnum "fooBar"');
run('set fv notset; string is alnum -failindex fv ""; set fv');
run('set fv notset; string is alnum -failindex fv -strict ""; set fv');
run('set fv notset; string is alnum -failindex fv "foobar"; set fv');
run('set fv notset; string is alnum -failindex fv "foo,bar"; set fv');
run('set fv notset; string is alnum -failindex fv "foo42bar"; set fv');
run('set fv notset; string is alnum -failindex fv "foo bar"; set fv');
run('set fv notset; string is alnum -failindex fv "fooBar"; set fv');
});
dom.onclick('cmdredef1', function(){
run('proc foo {} {return initial}; puts "1: [foo]"; proc foo {} {return changed}; puts "2: [foo]"');
});