forked from hczhcz/puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lion.js
1422 lines (1191 loc) · 36.9 KB
/
lion.js
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
473
474
475
476
477
478
479
480
481
482
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
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
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
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
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
//////// utilities ////////
var lion = {
//////// builtin libraries ////////
core: {},
std: {
LIONJS: true,
// see envq in lion.core
envq: function (env, ast) {
throw Error('[LION] can not call envq in the standard library');
},
// see getq in lion.core
getq: function (env, ast) {
var name = ast[1];
if (Object.hasOwnProperty.call(lion.core, name)) {
// found in lion.core
return lion.core[name];
} else if (Object.hasOwnProperty.call(lion.std, name)) {
// found in lion.std
return lion.std[name];
} else {
// not found
throw Error('[LION] value not found: ' + name);
}
},
},
//////// constants ////////
W_DELAY: 1,
W_ARG_HAS_ENV: 2,
W_ARG_AS_ARR: 4,
//////// maps ////////
typeMap: {
Object: Object,
Function: Function,
Array: Array,
String: String,
Boolean: Boolean,
Number: Number,
Date: Date,
RegExp: RegExp,
Error: Error,
EvalError: EvalError,
RangeError: RangeError,
ReferenceError: ReferenceError,
SyntaxError: SyntaxError,
TypeError: TypeError,
URIError: URIError,
},
errorMap: {
Error: Error,
EvalError: EvalError,
RangeError: RangeError,
ReferenceError: ReferenceError,
SyntaxError: SyntaxError,
TypeError: TypeError,
URIError: URIError,
},
//////// wrapper functions ////////
// convert f([env, ]arg...) to g(env, ast) with calls
wrap: function (func, option) {
return function (env, ast) {
var args = [];
// scan arguments
var l = ast.length;
for (var i = 1; i < l; ++i) {
if (option & lion.W_DELAY) {
// make a function
args.push(function (target) {
return function () {
return lion.call(env, target);
};
} (ast[i]));
} else {
// call directly
args.push(
lion.call(env, ast[i])
);
}
}
if (option & lion.W_ARG_AS_ARR) {
if (option & lion.W_ARG_HAS_ENV) {
return func(env, args);
} else {
return func(args);
}
} else {
if (option & lion.W_ARG_HAS_ENV) {
args.unshift(env);
}
return func.apply(this, args);
}
};
},
// add checker to a function (for lion.core)
wrapCore: function (func, option) {
return function (env, ast) {
if (Object.hasOwnProperty.call(env, 'LIONJS')) {
return func(env, ast);
} else {
throw Error('[LION] core function is not allowed: ' + ast[0]);
}
};
},
// convert a native object to an environment
wrapObj: function (obj, option, envname) {
return {
LIONJS: true,
// see envq in lion.core
envq: function (env, ast) {
// return ['LIONSTD', ['getq', envname]];
return envname;
},
// see xgetq in lion.core
xgetq: function (env, ast) {
var name = ast[1];
if (Object.hasOwnProperty.call(obj, name)) {
return lion.wrap(obj[name], option);
} else {
// find from the standard library
return lion.coreFunc(lion.std, ['getq', name]);
}
},
};
},
// convert an object with prototype (a class-like object) to an environment
wrapClass: function (obj, option, envname) {
return {
LIONJS: true,
// see envq in lion.core
envq: function (env, ast) {
// return ['LIONSTD', ['getq', envname]];
return envname;
},
// see xgetq in lion.core
xgetq: function (env, ast) {
var name = ast[1];
if (Object.hasOwnProperty.call(obj.prototype, name)) {
return lion.wrap(function (args) {
var target = args.shift();
var map = {
String: 'string',
Boolean: 'boolean',
Number: 'number',
};
if (
target instanceof Object ?
// target instanceof obj : typeof target === typeof obj()
target instanceof obj : (
Object.hasOwnProperty.call(map, envname)
&& typeof target === typeof envname
)
) {
return obj.prototype[name].apply(target, args);
} else {
throw Error('[LION] bad access to object method: ' + name);
}
}, option | lion.W_ARG_AS_ARR);
} else {
// find from the standard library
return lion.coreFunc(lion.std, ['getq', name]);
}
},
};
},
//////// helper functions ////////
// add library functions
addFunc: function (env, pkg, hook, option) {
for (var i in pkg) {
if (i in env) {
throw Error('[LION] naming conflict in the library: ' + i);
} else {
env[i] = hook ? hook(pkg[i], option, i) : pkg[i];
}
}
},
// execute an AST by getting a callee
call: function (env, ast) {
if (ast instanceof Array) {
// is a function call
var callee = lion.call(env, ast[0]);
// call via env.callq
return lion.coreFunc(
env,
['callq', callee, ast]
);
} else {
// is an object
return ast;
}
},
// search core function in env and lion.core
coreFunc: function (env, ast) {
var name = ast[0];
if (Object.hasOwnProperty.call(env, name)) {
return lion.core['callq'](
env,
['callq', env[name], ast]
);
} else if (Object.hasOwnProperty.call(lion.core, name)) {
return lion.core[name](env, ast);
}
},
//////// interface functions ////////
// create a new environment
init: function () {
return {LIONJS: true};
},
// parse a string, execute it and return a string
exec: function (env, str) {
return JSON.stringify(lion.call(env, JSON.parse(str)));
},
// call lion.init and lion.exec
boot: function (str) {
return lion.exec(lion.init(), str);
},
};
//////// modularization support ////////
if (
typeof require === 'function'
&& typeof module === 'object'
&& typeof module.exports === 'object'
) {
// CommonJS / NodeJS
module.exports = lion;
} else if (
typeof define === 'function'
// && define['amd']
) {
// AMD / CMD
define(lion);
}
//////// core functions ////////
// core-level names:
// LIONJS
// callq
// envq
// getq
// xgetq
// setq
// delq
// parent
// caller
// callee
// callenv
lion.addFunc(lion.core, {
// execute an AST with a given callee
// proto: callq('callee, 'caller) -> result
callq: function (env, ast) {
// TODO: move something to lion.call
// and make this function overridable
var callee = ast[1];
var caller = ast[2];
if (typeof callee === 'string') {
// get the callee from the environment
var newcallee = lion.coreFunc(
env,
['getq', callee]
);
if (newcallee) {
// apply the callee
return lion.coreFunc(
env,
['callq', newcallee, caller]
);
} else {
// callee not found
throw Error('[LION] callee not found: ' + callee);
}
} else if (callee instanceof Function) {
// callee is a builtin function
return callee(env, caller);
} else if (callee instanceof Array) {
// callee is an AST
// call with a new environment
var newenv = {
LIONJS: true,
caller: caller,
callee: callee,
callenv: lion.coreFunc(env, ['envq']),
};
return lion.call(newenv, callee);
} else if (callee instanceof Object) {
// callee is an object
// use callee as the new environment
return lion.call(callee, caller[1]);
} else {
// callee is not callable
// return callee;
throw Error('[LION] callee is not callable: ' + callee);
}
},
// get current environment
// proto: envq() -> env
envq: function (env, ast) {
return env;
},
// get value from current environment or call xgetq
// proto: getq('name) -> value
getq: function (env, ast) {
var name = ast[1];
if (Object.hasOwnProperty.call(env, name)) {
// found
return env[name];
} else {
// not found
return lion.coreFunc(env, ['xgetq', name]);
}
},
// get value outside of current environment
// proto: xgetq('name) -> value
xgetq: function (env, ast) {
var name = ast[1];
if (Object.hasOwnProperty.call(env, 'parent')) {
// find from env's parent
return lion.coreFunc(env.parent, ['getq', name]);
} else {
// find from the standard library
return lion.coreFunc(lion.std, ['getq', name]);
}
},
// set value in current environment
// proto: setq('name, 'value) -> value
setq: function (env, ast) {
var name = ast[1];
var value = ast[2];
if ((name in env) && !Object.hasOwnProperty.call(env, name)) {
// js internal property
throw Error('[LION] name is not acceptable: ' + name);
} else {
return env[name] = value;
}
},
// remove value from current environment
// proto: delq('name) -> success
delq: function (env, ast) {
var name = ast[1];
if ((name in env) && !Object.hasOwnProperty.call(env, name)) {
// js internal property
throw Error('[LION] name is not acceptable: ' + name);
} else {
if (Object.hasOwnProperty.call(env, name)) {
// found
return delete env[name];
} else {
// not found
throw Error('[LION] value not found: ' + name);
}
}
},
}, lion.wrapCore);
//////// the standard library ////////
//// access & call ////
lion.addFunc(lion.std, {
// callq() with calling
// proto: callq(callee, caller) -> result
call: function (env, callee, caller) {
return lion.coreFunc(env, ['callq', callee, caller]);
},
// envq() with calling
// proto: env() -> env
env: function (env) {
return lion.coreFunc(env, ['envq']);
},
// getq() with calling
// proto: get(name) -> value
get: function (env, name) {
return lion.coreFunc(env, ['getq', name]);
},
// xgetq() with calling
// proto: xget(name) -> value
xget: function (env, name) {
return lion.coreFunc(env, ['xgetq', name]);
},
// setq() with calling
// proto: set(name, value) -> value
set: function (env, name, value) {
return lion.coreFunc(env, ['setq', name, value]);
},
// delq() with calling
// proto: del(name) -> success
del: function (env, name) {
return lion.coreFunc(env, ['delq', name]);
},
// set quoted value
// proto: var(name, value) -> 'value
var: function (env, name, value) {
return lion.coreFunc(env, ['setq', name, ['quote', value]]);
},
}, lion.wrap, lion.W_ARG_HAS_ENV);
lion.addFunc(lion.std, {
// return the AST
// proto: quote('ast) -> 'ast
quote: function (env, ast) {
return ast[1];
},
// just calling
// proto: pass(ast) -> (call)^1 -> result
pass: function (env, ast) {
return lion.call(env, ast[1]);
},
// lion.call() with wrap
// proto: eval($ast) -> (call)^2 -> result
eval: function (env, ast) {
return lion.call(env, lion.call(env, ast[1]));
},
// call a function and modify a value
// proto: mut(func, name, ...) -> new value
mut: function (env, ast) {
var name = lion.call(env, ast[2]);
var oldvalue = lion.coreFunc(env, ['getq', name]);
var args = ast.slice(1);
args[1] = oldvalue; // hack
var newvalue = lion.call(env, args);
return lion.coreFunc(env, ['setq', name, ['quote', newvalue]]);
},
});
lion.addFunc(lion.std, {
// string to AST (JSON only)
// proto: parse(str) -> ast
parse: function (json) {
return JSON.parse(json);
},
// AST to string (JSON only)
// proto: stringify(ast) -> str
stringify: function (ast) {
return JSON.stringify(ast);
},
}, lion.wrap);
//// function ////
lion.addFunc(lion.std, {
// execute and make quote
// proto: argcall('env, 'ast) -> 'called
argcall: function (env, ast) {
return ['quote', lion.call(ast[1], ast[2])];
},
// execute later
// proto: argpass('env, 'ast) -> 'pass(ast)
argpass: function (env, ast) {
return [ast[1], ['pass', ast[2]]];
},
// make quote
// proto: argquote('env, 'ast) -> 'ast
argquote: function (env, ast) {
return ['quote', ast[2]];
},
// do nothing
// proto: argraw('env, 'ast) -> ast
argraw: function (env, ast) {
return ast[2];
},
});
lion.addFunc(lion.std, {
// give name to arguments with wrap
// proto: setarg(wrapper, ...) -> caller
setarg: function (env, arr) { // TODO: rename?
// get arguments
var caller = lion.coreFunc(
env,
['getq', 'caller']
);
var callenv = lion.coreFunc(
env,
['getq', 'callenv']
);
var wrapper = arr[0];
for (var i = 1; i < arr.length; ++i) {
var arg = lion.call(
env, [wrapper, callenv, caller[i]]
);
lion.coreFunc(
env, ['setq', arr[i], arg]
);
}
return caller;
},
}, lion.wrap, lion.W_ARG_HAS_ENV | lion.W_ARG_AS_ARR);
lion.addFunc(lion.std, {
// make an anonymous function with closure
// proto: lambda(wrapper, ..., body) -> function
lambda: function (env, ast) {
var setparent = ['setq', 'parent', lion.coreFunc(env, ['envq'])];
var setarg = ['setarg'];
for (var i = 1; i < ast.length - 1; ++i) {
setarg.push(ast[i]);
}
return [
'do',
setparent,
setarg,
ast[ast.length - 1]
];
},
// make an anonymous function without closure
// proto: macro(wrapper, ..., body) -> function
macro: function (env, ast) {
var setarg = ['setarg'];
for (var i = 1; i < ast.length - 1; ++i) {
setarg.push(ast[i]);
}
return [
'do',
setarg,
ast[ast.length - 1]
];
},
});
//// control flows ////
lion.addFunc(lion.std, {
// conditional branch
// proto: cond(cond, action, ...) -> result
cond: function (arr) {
var l = arr.length;
for (var i = 0; i + 1 < l; i += 2) {
if (arr[i]()) {
return arr[i + 1]();
}
}
},
// switch-like branch
// proto: case(value, default, case, action, ...) -> result
case: function (arr) {
var l = arr.length;
var value = arr[0]();
for (var i = 2; i + 1 < l; i += 2) {
var target = arr[i]();
if (target instanceof Array) {
// multi cases
for (var j = 0; j < target.length; ++j) {
if (value == target[j]) {
return arr[i + 1]();
}
}
} else {
// one case
if (value == target) {
return arr[i + 1]();
}
}
}
// default branch
return arr[1]();
},
}, lion.wrap, lion.W_DELAY | lion.W_ARG_AS_ARR);
lion.addFunc(lion.std, {
// simple branch (if branch)
// proto: if(cond, then, else) -> result
if: function (cond, then_br, else_br) {
if (cond()) {
return then_br();
} else if (else_br) {
return else_br();
}
},
// simple loop
// proto: loop(count, body) -> all result
loop: function (count, body) {
var all = [];
for (var i = Math.floor(count()); i > 0; --i) {
all.push(body());
}
return all;
},
// for loop
// proto: for(init, cond, step, body) -> all result
for: function (init, cond, step, body) {
var all = [];
for (init(); cond(); step()) {
all.push(body());
}
return all;
},
// while loop
// proto: while(cond, body) -> all result
while: function (cond, body) {
var all = [];
while (cond()) {
all.push(body());
}
return all;
},
// until (do-while) loop
// proto: until(cond, body) -> all result
until: function (cond, body) {
var all = [];
do {
all.push(body());
} while (cond());
return all;
},
}, lion.wrap, lion.W_DELAY);
//// iteration ////
lion.addFunc(lion.std, {
// iteration loop (for-in loop) by index
// proto: forin(iter, data, body) -> all result
forin: function (env, iter, data, body) {
var name = iter();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var all = [];
for (var i in list) {
lion.coreFunc(env, ['setq', name, ['quote', i]]);
all.push(body());
}
return all;
},
// iteration loop (for-in loop) by value
// proto: each(iter, data, body) -> all result
each: function (env, iter, data, body) {
var name = iter();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var all = [];
for (var i in list) {
lion.coreFunc(env, ['setq', name, ['quote', list[Math.floor(i)]]]);
all.push(body());
}
return all;
},
// find the first value
// proto: find(iter, data, cond) -> first result
find: function (env, iter, data, cond) {
var name = iter();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
for (var i in list) {
lion.coreFunc(env, ['setq', name, ['quote', list[Math.floor(i)]]]);
if (cond()) {
return list[Math.floor(i)];
}
}
},
// filter values
// proto: filter(iter, data, cond) -> result list
filter: function (env, iter, data, cond) {
var name = iter();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var all = [];
for (var i in list) {
lion.coreFunc(env, ['setq', name, ['quote', list[Math.floor(i)]]]);
if (cond()) {
all.push(list[Math.floor(i)]);
}
}
return all;
},
// linear for loop
// proto: table(iter, begin, end, step, body) -> all result
table: function (env, iter, begin, end, step, body) {
var name = iter();
var all = [];
for (var i = begin(); i != end(); i += step()) {
lion.coreFunc(env, ['setq', name, ['quote', i]]);
all.push(body());
}
return all;
},
// generate linear values
// proto: range(begin, end, step)
range: function (env, begin, end, step) {
var all = [];
// notice: arguments are called dynamically
for (var i = begin(); i != end(); i += step()) {
all.push(i);
}
return all;
},
// left folding by value
// proto: foldl(iter1, iter2, data, body) -> result
foldl: function (env, iter1, iter2, data, body) {
var name1 = iter1();
var name2 = iter2();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var value = list[0];
for (var i = 1; i < list.length; ++i) {
lion.coreFunc(env, ['setq', name1, ['quote', value]]);
lion.coreFunc(env, ['setq', name2, ['quote', list[i]]]);
value = body();
}
return value;
},
// right folding by value
// proto: foldr(iter1, iter2, data, body) -> result
foldr: function (env, iter1, iter2, data, body) {
var name1 = iter1();
var name2 = iter2();
var list = data();
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var value = list[list.length - 1];
for (var i = list.length - 2; i >= 0; --i) {
lion.coreFunc(env, ['setq', name1, ['quote', list[i]]]);
lion.coreFunc(env, ['setq', name2, ['quote', value]]);
value = body();
}
return value;
},
}, lion.wrap, lion.W_DELAY | lion.W_ARG_HAS_ENV);
lion.addFunc(lion.std, {
// pass each value in a list to a function
// proto: map(func, list) -> all result
map: function (env, func, list) {
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var all = [];
for (var i = 0; i < list.length; ++i) {
all.push(lion.call(
env,
[
['quote', func],
['quote', list[i]],
['quote', i]
]
));
}
return all;
},
// left folding using a function
// proto: reducel(func, list) -> result
reducel: function (env, func, list) {
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var value = list[0];
for (var i = 1; i < list.length; ++i) {
value = lion.call(
env,
[
['quote', func],
['quote', value],
['quote', list[i]],
['quote', i - 1],
['quote', i]
]
);
}
return value;
},
// right folding using a function
// proto: reducer(func, list) -> result
reducer: function (env, func, list) {
if (!list instanceof Array) {
throw Error('[LION] bad type of list');
}
var value = list[list.length - 1];
for (var i = list.length - 2; i >= 0; --i) {
value = lion.call(
env,
[
['quote', func],
['quote', list[i]],
['quote', value],
['quote', i],
['quote', i + 1]
]
);
}
return value;
},
}, lion.wrap, lion.W_ARG_HAS_ENV);
//// exception ////
lion.addFunc(lion.std, {
// try structure
// proto: try(body, except, finally) -> result
try: function (env, body, except, finally_do) {
try {
return body();
} catch (e) {
lion.coreFunc(env, ['setq', 'exception', ['quote', e]]);
return except();
} finally {
if (finally_do) {
finally_do();
}
}
},
}, lion.wrap, lion.W_DELAY | lion.W_ARG_HAS_ENV);
lion.addFunc(lion.std, {
// throw statement
// proto: throw(err) -> never return
throw: function (err) {
throw err;
},
// error constructor
// proto: error(message, type) -> error object
error: function (message, type) {
if (Object.hasOwnProperty.call(lion.errorMap, type)) {
return lion.errorMap[type](message);
} else {
return Error(message);
}
},
}, lion.wrap);
//// operators ////
lion.addFunc(lion.std, {
// unary operators
// proto: op(a) -> op a (a op)
positive: function (a) {return +a;},
negative: function (a) {return -a;},
// '++': function (a) {return ++a;},
// '--': function (a) {return --a;},
// '+++': function (a) {return a++;},
// '---': function (a) {return a--;},
'~': function (a) {return ~a;},
typeof: function (a) {return typeof a;},
// binary operators
// proto: op(a, b) -> a op b
'+': function (a, b) {return a + b;},
'-': function (a, b) {return a - b;},
'*': function (a, b) {return a * b;},
'/': function (a, b) {return a / b;},
'%': function (a, b) {return a % b;},
'<': function (a, b) {return a < b;},
'>': function (a, b) {return a > b;},
'<=': function (a, b) {return a <= b;},
'>=': function (a, b) {return a >= b;},
'==': function (a, b) {return a == b;},
'!=': function (a, b) {return a != b;},
'===': function (a, b) {return a === b;},
'!==': function (a, b) {return a !== b;},