-
Notifications
You must be signed in to change notification settings - Fork 17
/
jispy.py
1423 lines (1303 loc) · 69.2 KB
/
jispy.py
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
#############################################################################
# #
# Jispy: A JavaScript interpreter in Python #
# Copyright (c) 2017 Polydojo, Inc. #
# #
# #
# #
# This Source Code Form is subject to the terms of the Mozilla Public #
# License, v. 2.0. If a copy of the MPL was not distributed with this #
# file, You can obtain one at http://mozilla.org/MPL/2.0/. #
# #
#############################################################################
import re;
import time;
import inspect;
import sys;
import math;
import random;
isa = isinstance;
#############################################################
# LEXICAL ANALYSIS #
#############################################################
class Name(str): pass; # for holding identifier names
class Symbol(str): pass; # for holding keywords and ops
def makeSymbolTable(addLiterals):
"Creates a symbol table."
table = {}; # private (via closure)
if addLiterals is True:
table = {'true': True, 'false': False, 'null': None}; # Note: hash('x') is the same as hash(Symbol('x'))
class SymbolTable(object):
def __call__(self, s):
if s not in table:
table[s] = Symbol(s);
return table[s];
def __contains__(self, s):
return s in table;
def __repr__(self):
return str(table);
return SymbolTable();
iKeywords = '''
var if else while for break function return true false null
'''.split(); # list of implemented keywords
uKeywords = '''
case class catch const continue debugger default delete do
export extends finally import in instanceof let new super
switch this throw try typeof void with yield
enum
implements package protected static interface private public
Infinity NaN undefined
'''.split(); # list of unimplemented keywords (and globals)
keywords = iKeywords + uKeywords;
sym = makeSymbolTable(addLiterals = True); # true, false, null
map(sym, iKeywords);
map(sym, list('()[]!*/%+-><=,;{:}'));
map(sym, '>= <= === !== && ||'.split());
map(sym, '+= -='.split());
# Note: '.' (dot) is not a symbol
badsym = makeSymbolTable(addLiterals = False);
map(badsym, '== != << >> *= /= %= ++ --'.split());
map(badsym, uKeywords);
#############################################################
class LJErr(Exception): pass;
class LJSyntaxErr(LJErr): pass;
class LJTypeErr(LJErr): pass;
class LJRuntimeErr(LJErr): pass;
class LJReferenceErr(LJErr): pass;
class LJKeyErr(LJErr): pass;
class LJIndexErr(LJErr): pass;
class LJAssertionErr(LJErr): pass;
def isNameLike(s):
"Returns if a token is name-LIKE. 'if' IS name like."
if s == '_' or s == '$': return True;
return re.findall(r'[a-z]\w*', s) == [s] and s[-1] != '_';
def isValidName(s):
"Returns if a token is a valid identifier name."
return s not in keywords and isNameLike(s);
def strNum(n):
"Converts floats to (possibly int-like) strings."
return str(int(n)) if n == round(n) else str(n);
def isDecimal(s):
"Checks if a number-token is decimal (non-octal)."
if s == '0' or any(map(s.startswith, '0e 0E 0.'.split())):
return True;
return not s.startswith('0');
def lex(s):
"Breaks input into a list of tokens. (Lexical Analysis)"
spacer = [
# Introducing spaces:
('=', ' = '), (',', ' , '), (';', ' ; '),
('(', ' ( '), ('[', ' [ '), ('{', ' { '),
(')', ' ) '), (']', ' ] '), ('}', ' } '),
('!', ' ! '), ('*', ' * '), ('/', ' / '),
('%', ' % '), ('+', ' + '), ('-', ' - '),
('>', ' > '), ('<', ' < '), (':', ' : '),
('&&', ' && '), ('||', ' || '),
# Removing unwanted spaces:
('> =', '>='), ('< =', '<='),
('= = =', '==='), ('! = =', '!=='),
('+ =', '+='), ('- =', '-='),
# Bad (EVIL) symbols (these raise syntax errors):
('= =', '=='), ('! =', '!='),
('+ +', '++'), ('- -', '--'),
('> >', '>>'), ('< <', '<<'),
('* =', '*='), ('/ =', '/='), ('% =', '%=')#,
];
tokens = []; # Output. Filled by enclosed functions.
###
def segmentifyLine(s):
"Helps segmentify() with segmenting a single line." # segmentifyLine() uses an FSM which looks as follows:
mode = 'code'; quote = None; #
ans = ['']; # .--> (CODE) <--> STRING
for i in xrange(len(s)): #
if mode == 'code' and quote is None: # CODE is the start state and the only accepting state.
if s[i] in ['"', "'"]:
mode = 'string';
quote = '"' if s[i] == '"' else "'";
ans.append(quote);
elif s[i] == '/' and i + 1 < len(s) and s[i+1] == '/':
return ans;
else:
ans[-1] += s[i];
elif mode == 'string' and quote in ['"', "'"]: # A segment is a un-quoted code snippet
prev = ans[-1][-1]; # or a quoted-string snippet.
ans[-1] += s[i];
if s[i] == quote and prev != '\\':
mode = 'code';
ans.append('');
quote = None;
else:
assert False;
if mode != 'code':
raise LJSyntaxErr('EOL while scanning string literal');
return ans;
def segmentify(s):
"Segments input into code-segements & string-segments."
segments = [];
for line in s.splitlines():
if line and not line.isspace():
segments += segmentifyLine(line);
return segments;
def isPropertyName(s):
"Helps subscriptify() tell if `s` is a valid key." # TODO: Currently, `a = {}; a.function = 1;` is legal;
return re.findall(r'\w+', s) == [s]; # BUT, `a = {function : 1};` is illegal. FIX THIS.
def subscriptify(s):
"Helps handleDot() change dots to subscripts."
if isPropertyName(s):
toAppend = [sym('['), s, sym(']')];
map(tokens.append, toAppend);
else:
raise LJSyntaxErr('illegal refinement ' + s);
def handleDot(x):
"Converts dot-refinements to subscript-refinements." # The parser cannot handle dot-refinements (yet);
if x[-1] == '.':
raise LJSyntaxErr('unexpected trailing . (dot) ' + x); # TODO: Support trailing dots.
elif x[0] == '.':
if tokens == []:
raise LJSyntaxErr('unexpected refinement ' + x);
# otherwise...
poken = tokens[-1]; # previous token
if not (poken in '})]' or isValidName(poken)): # Note: `in` is weaker than `is`.
raise LJSyntaxErr('unexpected token ' + x + poken); # Thus,
# otherwise... # poken in [sym('}'), sym(')'), sym(']')]
splitLi = x[1 : ].split('.'); # is not used.
map(subscriptify, splitLi);
else: # if x[0] != '.'
splitLi = x.split('.');
baseId = splitLi[0]; # base/first identifier
if isValidName(baseId):
tokens.append(Name(baseId))
map(subscriptify, splitLi[1 : ]);
else:
raise LJSyntaxErr('unexpected token ' + baseId);
def atomizeNS(x):
"Identify (& return) bools, numbers, strings & names."
if x in uKeywords:
raise LJSyntaxErr('unexpected keyword ' + x);
elif x in badsym:
raise LJSyntaxErr('unexpected token ' + x);
elif x in sym:
tokens.append(sym(x));
elif isValidName(x):
tokens.append(Name(x));
else:
try:
tmp = float(x);
if isDecimal(x): tokens.append(tmp);
else:
raise LJSyntaxErr('illegal (octal) number ' + x);
except ValueError: # SyntaxError is left uncaught.
if '.' in x:
handleDot(x);
else:
raise LJSyntaxErr('unexpected token ' + x);
def lexNS(code):
"Tokenizes a non-string, i.e. un-quoted code snippet."
for tup in spacer:
code = code.replace(tup[0], tup[1]);
map(atomizeNS, code.split());
def lexS(s):
"Shaves off quotes and decodes backslash escapes."
assert s[0] == s[-1] and s[-1] in ['"', "'"];
tokens.append(s[1 : -1].decode('string_escape'));
for seg in segmentify(s):
if seg.startswith('"') or seg.startswith("'"):
lexS(seg);
else:
lexNS(seg);
return [sym('var?')] + tokens; # UNINTERNED symbol, indicating that a var statement may follow.
#############################################################
# SYNTACTIC ANALYSIS #
#############################################################
def eMsgr(li):
"Helps print error messages."
err_repr = lambda x: '' if x == 'var?' else lj_repr(x);
return ' ... ' + ' '.join(map(err_repr, li[:20]));
def gmb(seq, i): # Get Matching (Closing) Bracket
"Get index of right bracket, matching left bracket at i."
brackets = {
sym('('): sym(')'),
sym('['): sym(']'),
sym('{'): sym('}')#,
};
left = seq[i];
right = brackets[left];
count = 0;
for j in xrange(i, len(seq), 1):
if seq[j] == left: count += 1;
elif seq[j] == right: count -= 1;
if count == 0: return j;
raise LJSyntaxErr('unbalanced bracket' + eMsgr(seq[i:]));
def topSplit(li, symbo): # Consider: `add(1, sub(2, 3));`
"Splits input list on the TOP-LEVEL non-bracket symbol." # The comma right after 2 is not a top-level comma. (Its nested.)
if not li: return li; # empty list; # On the other hand, the comma right after 1 is.
oBrackets = [sym('('), sym('{'), sym('[')];
ans = [];
i, j = 0, 0;
while j < len(li):
if li[j] in oBrackets:
j = gmb(li, j); # ignore commas in bracket pairs
elif li[j] is symbo:
ans.append(li[i : j]);
i, j = j+1, j+1;
elif j == len(li) - 1:
ans.append(li[i : j + 1]);
break;
else:
j += 1;
return ans;
def isTopIn(li, symbo):
"Tells if symbo is a top-level-symbol in list li."
return len(topSplit(li, symbo)) != 1;
def topIndex(li, j, symbo): # error thrower # Bugfix:
"Gives index of TOP-LEVEL occurence of symbo, starting at j." # Originally, topIndex accepted a list of symbols `syms` to
oBrackets = [sym('('), sym('{'), sym('[')]; # to split on. If a non-list x was passed, it'd set syms = [x].
if symbo in oBrackets: # Splitting was accomplished by checking `if li[j] in syms`
raise Exception(''); # internal error # as opposed to the current check `if li[j] is symbo`
while j < len(li): # The `in` test is insufficient as it cannot tell the difference
if li[j] in oBrackets: # between types str and Symbol. The `is` test is necessary.
j = gmb(li, j); # ignore semis in bracket pairs # Luckily, topSplit was always called on a single symbol.
elif li[j] is symbo: # The bug was thus, easily fixed by changing the test is `is`.
return j;
else:
j += 1;
raise LJSyntaxErr('expected ' + symbo + eMsgr(li));
#############################################################
class Function(object): # for holding function values # Functions can be completely parsed during sytactic analysis.
def __init__(self, params, tree, iTokens): # There is no need to wait for interpreting.
self.params = params; # This is not true about literal objects and arrays.
self.tree = tree;
self.iTokens = iTokens;
self.crEnv = None; # creation ENVironment # However, the crEnv of a function can be know only at rumtime.
def __str__ (self): # So, for now, we set it to None;
return '...function %s %s...' % \
(str(self.params), str(self.tree));
__repr__ = __str__ # uncomment for debugging
def yacc(tokens):
"Builds an AST from a list of tokens. (Syntactic Analysis)"
tree = []; # AST (Abstract Syntax Tree)
def parseFunction(expLi, k): # form: ... function ( a , b ) { ... } ...
"Helps parseExp() to parse function expressions." # indices: k lp rp lc rc
try:
lp = expLi.index(sym('('), k);
assert lp == k + 1;
rp = gmb(expLi, lp);
lc = expLi.index(sym('{'), rp);
assert lc == rp + 1;
rc = gmb(expLi, lc);
assert rc > lc + 1; # ensuring non-empty block
paramsWithCommas = expLi[lp+1 : rp]; # Do _NOT_ to call topSplit(), as each param _MUST_ be a Name
params = [];
for p in paramsWithCommas:
if isa(p, Name): params.append(p);
else: assert p == sym(',');
except (ValueError, AssertionError):
#print 'func ExpLI = ', expLi;
raise LJSyntaxErr('bad function literal');
iTokens = [sym('var?')] + expLi[lc + 1 : rc]; # list of body tokens (non-alias)
func = Function(params, yacc(iTokens), iTokens);
expLi = expLi[ : k] + [func] + expLi[rc + 1 : ]; # DO NOT DIRECTLY RETURN THE RHS EXPRESSION. It is less readable.
return expLi; # Substitute an instance of Function in place of function literal
def parseExp(expLi):
"Tells functions from rest and parses them." # Remaining parts in an expression like operators etc.
while True: # are dealt with during interpreting
#print 'expLi = ', expLi, '\n';
funky = map(lambda x: x is sym('function'), expLi);
#print ; print;
#print 'expLi = ', expLi;
#print 'funky = ', funky;
#print ; print;
if any(funky): # Do NOT use `while sym("function") in expLi`
k = funky.index(True); # as the `in` operator cannot tell the difference between
expLi = parseFunction(expLi, k); # 'function' the Symbol and "function" the str.
else:
break;
return expLi;
def parseVar(tokens, j): # form: ... var a = 10 , b = 20 ; ...
"Helps yacc() in parsing var statements." # indices: j semiPos
if j == 0 or tokens[j - 1] != sym('var?'):
raise LJSyntaxErr('unexpected var statement'); # In JS, var statements may occur anywhere.
semiPos = topIndex(tokens, j, sym(';')); # This may create an illusion of block-scope, which JS lacks.
subtokens = tokens[j+1 : semiPos]; # As a remedy, Jispy allows at most one var statement per scope,
inits = topSplit(subtokens, sym(',')); # and, if used, it must be the very first statement in the scope.
for init in inits: # The uninterned symbol `var?` is used to restrict var statements.
try:
assert len(init) >= 3; # eg. init: a = 10 + 10
assert type(init[0]) is Name; # indices: 0 1 2 3 4
assert init[1] is sym('=');
except AssertionError:
raise LJSyntaxErr('illegal var statement');
rhsParsedExp = parseExp(init[2 : ]);
tree.append(['init', init[0], rhsParsedExp]);
return semiPos + 1;
def parseIf(tokens, j, tree=tree, stmtName = 'if'): # form: ... if ( a == 20 ) { ... } ...
"Helps yacc(..) in parsing if statements." # indices: j lp rp lc rc
try:
lp = tokens.index(sym('('), j); # left paren # Note: `while` & `else if` are syntactically similar to pure `if`.
assert lp == j + 1; # We shall thus use parseIf() to parse `else if` and `while`.
rp = gmb(tokens, lp); # right paren # In doing so, we mustn't wrongly mutate the AST.
assert rp > lp + 1; # non-empty # Instead, a dummy-tree will be passed in to parseIf()
lc = tokens.index(sym('{'), rp);# left curly
assert lc == rp + 1;
rc = gmb(tokens, lc); # right curly
assert rc > lc + 1; # non-empty block
except (ValueError, AssertionError):
raise LJSyntaxErr('illegal %s statement' % stmtName);
cond = parseExp(tokens[lp + 1 : rp]);
code = yacc(tokens[lc + 1 : rc]);
tree.append(['if-ladder', cond, code]) # An if-ladder generically represents a any VALID combination
return rc + 1; # of if and else constructs. (This obviously includes `else if`.)
def parseElseIf(tokens, j): # form: ... else if ( a !== 0 ) { ... } ...
"Helps parseElse() in parsing else-if statements." # index: j
tempTree = []; # dummy-tree for passing to parseIf()
j = parseIf(tokens[:], j+1, tempTree, 'else if'); # The if-ladder, which was created on seeing `if`,
[_, cond, code] = tempTree[0]; # is mutated as follows on seeing `else if`:
tree[-1].append(cond); # [if-ladder cond0 cond0] --> [if-ladder cond0 code0 cond1 code1]
tree[-1].append(code); # Where cond0, code0 were previously seen; cond1, code1 were just seen.
return j;
def parsePureElse(tokens, j): # form: ... else { ... } ...
"Helps parseElse() in parsing pure-else stmts." # indices: j lc rc
try:
lc = tokens.index(sym('{'), j);
assert lc == j + 1;
rc = gmb(tokens, lc);
except (ValueError, AssertionError):
raise LJSyntaxErr('illegal else statement');
cond = [sym('true')]; # alwyas truthy. # The if-ladder, (which was previously created,)
code = yacc(tokens[lc+1 : rc]); # is mutated by adding a condition which is always true:
tree[-1].append(cond); # [if-ladder cond0 code0 ] --> [if-ladder cond0 code0 TrueCond code1]
tree[-1].append(code); # TrueCond is always true, which makes pure `else`,
return rc + 1; # the semantic equivalent of `else if (true)`.
def parseElse(tokens, j): # Relies on parseElseIf() and parsePureElse() above.
"Helps yacc() in parsing else statements."
if tree == [] or tree[-1][0] != 'if-ladder':
raise LJSyntaxErr('misplaced else statement');
if j == len(tokens) - 1 :
raise LJSyntaxErr('unexpected else (last token)');
if tokens[j+1] == 'if':
return parseElseIf(tokens[:], j);
else:
return parsePureElse(tokens[:], j);
def parseWhile(tokens, j): # form: ... while ( a > 1 ) { ... } ...
"Helps yacc() in parsing while statements." # indices: j lp rp lc rc
tokens[j] = sym('if'); # make while look like if.
tempTree = []; # dummy-tree
j = parseIf(tokens[:], j, tempTree, 'while'); # Note:
[_, cond, code] = tempTree[0]; # param `tokens` supplied to parseWhile is a
tree.append(['while', cond, code]); # NON-ALIAS-copy of that supplied to yacc;
return j; # We may hence freely make changes in it.
def parseReturn(tokens, j): # form: ... return 1 + 1 + 1 ; ...
"Helps yacc() in parsing return statements." # indices: j semiPos
semiPos = topIndex(tokens, j, sym(';'));
parsedReturnExp = parseExp(tokens[j+1 : semiPos]);
tree.append(['return', parsedReturnExp]);
return semiPos + 1;
def parseBreak(tokens, j): # form: ... break ; ...
"Helps yacc() in parsing break statements." # indices: j j+1
try: assert tokens[j + 1] is sym(';');
except AssertionError:
raise LJSyntaxErr('expected ; (semicolon) after break');
tree.append(['break']);
return j + 2;
def checkLhsExp(expLi): # expLi is lhsExpLi
"Helps parseAssign() in checking LHS of assignment."
try:
assert len(expLi) >= 1;
assert type(expLi[0]) is Name;
if len(expLi) > 1:
j = -1; # last index
while True:
assert expLi[j] is sym(']');
ls = gmob(expLi, j); # Left Square [ gmob() <--> Get Matching Opening Bracket
if expLi[ls - 1] is sym(']'): # cascaded indexing
j = ls - 1; # Right square ]
else:
assert ls - 1 == 0;
break;
except AssertionError:
raise LJTypeErr('illegal LHS in assignment' + eMsgr(expLi));
return None;
def parseAssign(stmt, tree=tree): # Relies on checkLhsExp # form: a[0] = 1 + b + c ;
"Helps yacc() in parsing assignment statements." # indices: 0 eqSign
try:
assert type(stmt[0]) is Name; # Note: parseAssign() was written when shorthand assignments
eqSign = stmt.index(sym('=')); # were NOT supported. As a result, it doen't (currently)
lhsExp = parseExp(stmt[ : eqSign]); # handle them. A separate function parseShorthandAssign()
rhsExp = parseExp(stmt[eqSign + 1 : ]); # has been written to do so exclusively, which
checkLhsExp(lhsExp); # converts a shorthand assignment to an assignment
except AssertionError: # via parseAssign().
raise LJTypeErr('illegal LHS in assignment' + eMsgr(stmt));
tree.append(['assign', lhsExp, rhsExp]);
return None;
def parseShortAssign(stmt, tree=tree): # form: k[i] += 1 ;
"Helps yacc() in parsing short-hand assignments." # indices: 0
assert sym('+=') in stmt or sym('-=') in stmt;
if sym('+=') in stmt:
short = sym('+=');
else:
short = sym('-=');
symPos = stmt.index(short);
xtmt = stmt[: symPos] + [sym('=')] + stmt[symPos+1 :]; # Make the shorthand assignment LOOK LIKE an assignment.
tempTree = []; # dummy-tree
parseAssign(xtmt, tempTree); # Checks legality of assignment, and hence of shorhand assignment.
[_, lhsExp, rhsExp] = tempTree[0];
pOrM = sym(short[0]); # sym('+') or sym('-'); i.e. Plus or Minus
rhsExp = lhsExp + [pOrM] + rhsExp; # Convertin `lhs += rhs` to `lhs = lhs + rhs`
tree.append(['assign', lhsExp, rhsExp]);
def sepForCls(tokens, j): #form: ... for ( i = 0 ; i < 10 ; i += 1 ) { ... } ...
"Helps parseFor() w/ separating sub-clauses in for." #indices: j lp s1 s2 rp lc rc
try:
lp = tokens.index(sym('('), j);
assert lp == j + 1; # Notes:
rp = gmb(tokens, lp); # 1. We shall convert the for loop to an equivalent while loop
assert rp > lp + 1; # ( ..non-empty.. ) # 2. The equivalent while loop of the loop shown above is:
lc = tokens.index(sym('{'), rp); # ... i = 0 ; while (i < 10) { ... i += 1 } ...
assert lc == rp + 1; # where `...` is assumend to remain the same.
rc = gmb(tokens, lc); # 3. It is necessary to verify that the increment-clause
assert rc > lc + 1; # non-empty block # of for `i += 1` is NOT a disruptive statement.
s1 = tokens.index(sym(';'), lp, rp); # It must be an assignment. (Pure JS is less restrictive.)
s2 = tokens.index(sym(';'), s1 + 1, rp); # 4. Assignment & increment clauses within (parens of) for
assert s2 > s1 + 1; # may not include function values; as tokens.index()
assert sym(';') not in tokens[s2 + 1 : rp]; # is used as opposed to topIndex().
except (ValueError, AssertionError):
raise LJSyntaxErr('illegal for statement');
asgnCl = tokens[lp + 1 : s1]; # ASSiGNment CLause, yet to be parsed
condCl = tokens[s1 + 1 : s2];
incrCl = tokens[s2 + 1 : rp];
codeCl = tokens[lc + 1 : rc];
return (asgnCl, condCl, incrCl, codeCl, rc); # Each clause is yet to be parsed.
def parseForAssignments(asgnClause):
"Helps in parsing assignment and increment clauses."
tempTree = [];
asgns = topSplit(asgnClause, sym(','));
#print 'split asgns = ', asgns;
for asg in asgns:
if sym('=') in asg:
parseAssign(asg, tempTree);
elif sym('+=') in asg or sym('-=') in asg:
parseShortAssign(asg, tempTree);
else:
raise LJSyntaxErr('illegal for statement');
return tempTree;
def parseFor(tokens, j):
"Helps yacc(..) in parsing for stmt (as while)."
(asgnCl, condCl, incrCl, codeCl, rc) = sepForCls(tokens, j);
pAsgns = parseForAssignments(asgnCl); # parsed assignments (from assignment clause)
pCond = parseExp(condCl); # parsed condition
pIncrs = parseForAssignments(incrCl); # only for error checking
pCode = yacc(codeCl) + pIncrs;# + incrCl + [sym(';')]); # parsed code; the UNPARSED increment clause is appended
map(tree.append, pAsgns); # Assignment is placed before while (in our parse tree)
tree.append(['while', pCond, pCode]); # equivalent while loop
return rc + 1;
def parseExpStmt(stmt):
"Helps yacc(..) parse exp-stmts like `print('Hi!');`"
if stmt[0] is sym('function'):
raise LJSyntaxErr('unexpected token function'); # functions, unless wrapped in parens, are illegal pure-expressions
tree.append(['exp-stmt', parseExp(stmt)]);
return None;
j = 0;
while j < len(tokens): # Here, we look at the a given token `tok`.
tok = tokens[j]; # If it is special, like 'if', 'var', 'while' etc.,
if tok is sym('var?'): # we ship all the tokens, along with the index `j`
j += 1; # of the current token, to a helper like parseIf()
elif tok is sym('var'): #
j = parseVar(tokens[:], j); # In doing so, we send a non-alias-copy of `tokens`
elif tok is sym('if'): # by simplying slicing it to all of itself `tokens[:]`.
j = parseIf(tokens[:], j); # Sending a non-alias-copy is important as the helper
elif tok is sym('else'): # may mutate tokens, making debugging difficult (if not impossible).
j = parseElse(tokens[:], j); #
elif tok is sym('while'): # The helper, when its done parsing, returns the index of
j = parseWhile(tokens[:], j); # the NEXT token to be considered.
elif tok is sym('return'): #
j = parseReturn(tokens[:], j); # The AST is mutated by the helper function.
elif tok is sym('break'): # We needn't worry about that here.
j = parseBreak(tokens[:], j);
elif tok is sym('for'):
j = parseFor(tokens[:], j);
else:
#print 'current token = ', tok;
semiPos = topIndex(tokens, j, sym(';'));
stmt = tokens[j : semiPos];
if not stmt:
#pass;
raise LJSyntaxErr('empty statement');
#if sym('=') in stmt:
# parseAssign(stmt);
#elif sym('+=') in stmt or sym('-=') in stmt:
# parseShortAssign(stmt);
if isTopIn(stmt, sym('=')):
parseAssign(stmt);
elif isTopIn(stmt, sym('+=')) or isTopIn(stmt, sym('-=')):
parseShortAssign(stmt);
else:
parseExpStmt(stmt);
j = semiPos + 1;
return tree;
#############################################################
# SEMANTIC ANALYSIS #
#############################################################
def gmob(li, i): # GetMatchingOpeningBacket. Cousin of gmb()
"Get index of left bracket, matching right bracket at i."
if i < 0: i = len(li) - abs(i); # allows -ive indexing
brackets = {
sym(')'): sym('('),
sym(']'): sym('['),
sym('}'): sym('{')#,
};
right = li[i];
left = brackets[right];
count = 0;
for j in xrange(i, -1, -1): # i may be -ive
if li[j] == right : count += 1;
elif li[j] == left : count -= 1;
if count == 0: return j;
raise LJSyntaxErr('unbalanced bracket ' + right);
#def cloneLi(li):
# "Creates a NON-ALIAS clone of a (possibly nested) list."
# ans = [];
# for elt in li:
# if type(elt) is list:
# ans.append(cloneLi(elt));
# else:
# ans.append(elt);
# return ans;
def cloneTree (iTree):
"";
oTree = [];
for iNode in iTree:
if type(iNode) is Function:
oNode = Function(*map(cloneTree, [iNode.params, iNode.tree, iNode.iTokens]));
elif type(iNode) is list:
oNode = cloneTree(iNode);
else:
oNode = iNode;
oTree.append(oNode);
return oTree;
cloneLi = cloneTree; # Backward compatible alias.
def isFalsy(val): # Python and JS have (slightly) different ideas of falsehood.
"Tells if a value is falsy." # [] and {} are falsy in python, but truthy is JS.
if not hasattr(val, '__call__'):
assert type(val) in [bool, float, str, list, dict, Function, type(None)];
return val in [False, 0.0, '', None];
def isTruthy(val):
"Tells is a value is truthy."
return not isFalsy(val);
def makeEnvClass(maxDepth=None): # maxDepth is private
class Env(dict):
"Defines a scope/context for execution."
def __init__(self, params=[], args=[], parent=None):
if len(params) != len(args): raise Exception(); # internal error
self.update(zip(params, args));
self.parent = parent;
self.isGlobal = (parent is None);
if self.isGlobal: self.depth = 0;
else: self.depth = self.parent.depth + 1;
if maxDepth and self.depth >= maxDepth:
raise LJRuntimeErr('maximum scope depth exceeded');
if (self is self.parent):
raise Exception(); # internal error
def getEnv(self, key): # used (mostly) internally
"Returns environment in which a variable appears."
d = 'Global' if self.isGlobal else self;
#print '\nlooking for %s in %s' % (key, d);
if key in self:
return self;
elif not self.isGlobal:
return self.parent.getEnv(key);
raise LJReferenceErr('%s is not defined' % key);
def init(self, key, value):
"Initializes a variable in the currect environment."
if key not in self: self[key] = value;
else:
raise LJReferenceErr('%s is already defined' % key);
def assign(self, key, value):
"Resets the value of a variable in its own environment."
self.getEnv(key)[key] = value;
def lookup(self, key):
"Looks up the value of a variable. (convenience)"
return self.getEnv(key)[key];
def makeChild(self, params=[], args=[]):
"Creates an Env with current Env `self` as parent."
return Env(params=params, args=args, parent=self);
def setDepth(self, depth):
self.depth = depth;
if maxDepth and self.depth >= maxDepth:
raise LJRuntimeErr('maximum call depth exceeded');
#def show(self):
# "Helps with debugging."
# out = '\n';
# for k in self:
# if k not in 'type str len keys write writeln'.split():
# out += '\t\t%s : %s\n' % (k, self[k]);
# return out;
return Env;
class LJJump(Exception):
def __str__(self):
return 'unexpected jump statement';
class LJReturn(LJJump): pass;
class LJBreak(LJJump): pass;
def lj_repr(x):
if x is None: return 'null';
if type(x) is bool: return 'true' if x else 'false';
if type(x) is float:
if x == round(x): return str(int(x));
else: return str(x);
if type(x) is str:
return '"' + x.replace('"', '\\"') + '"';
if type(x) in [Name, Symbol]: return x;
if type(x) is list:
if not x: return '[]' # corner case (see shaving)
out = '[';
for elt in x: out += lj_repr(elt) + ', ';
out = out[ : -2]; # shave off trailing ", "
return out + ']';
if type(x) is dict:
if not x: return '{}'; # corner case
out = '{';
for k in x: out += lj_repr(k) + ': ' + lj_repr(x[k]) + ', ';
out = out[ : -2]; # shave off trailing ", "
return out + '}';
if type(x) is Function:
return 'function (' + ', '.join(x.params) + ') { ' + ' '.join(map(lj_repr, x.iTokens[1:])) + ' }';
if inspect.isfunction(x):
return 'function () { [native code] }'
assert False;
#############################################################
def run(tree, env, maxLoopTime=None, writer=None):
"Executes parsed code in an environment `env`."
# -------------------------------------------------------
# *********************************************
def eval(expLi, env):
"Evaluates an expression in an environment."
# - - - - - - - - - - - - - - - - - - - - - - - - - -
def setFuncCreationEnvs(expLi):
"Set the creation Env (crEnv) for all functions."
for elt in expLi:
if type(elt) is Function and elt.crEnv == None: # elt.crEnv may have been previously set due to the recursive
elt.crEnv = env; # nature of eval().
return expLi; # Not required, but all other functions return expLi.
def subNames(expLi):
"Substitutes identifier names w/ their values."
#print 'entering subNames, expLi = ', expLi;
count = 0; # counts { and }
for j in xrange(len(expLi)):
tok = expLi[j];
if tok is sym('{'): count += 1; # Keys in objects `{a: "apple"}` are NOT substituted
elif tok is sym('}'): count -= 1;
elif type(tok) is Name and count == 0:
expLi[j] = env.lookup(tok); # env was passed to eval
return expLi;
def subObject(expLi, j): # form: ... { ... keyX : valueExpX , keyY : valueExpY ... }
"Substitutes an object literal w/ a py-dict." # indices: j rc
#print 'entering subObject, expLi = ', expLi;
rc = gmb(expLi, j);
inner = expLi[j + 1 : rc];
pairs = topSplit(inner, sym(','));
obj = {};
for pair in pairs: # pair form: keyA : x + y + factorial ( 5 )
try: # indices: 0 1 2 3 4 5 6 7 8 9
assert len(pair) >= 3;
key = pair[0];
if type(key) is Name: key = str(key);
assert type(key) is str; # JS keys MUST be strings. Numbers & booleans are coerced to strings.
assert pair[1] is sym(':');
except AssertionError :
raise LJSyntaxErr('illegal object literal' + eMsgr(expLi));
valueLi = pair[2 : ];
obj[key] = eval(valueLi, env);
expLi = expLi[ : j] + [obj] + expLi[rc + 1 : ];
return expLi;
def subArray(expLi, j): # form: ... [ 1 + 1 + 1 , expB , ... , expN ] ...
"Substitutes an array literal w/ a py-list." # indices: j rs
#print 'entering subArray, expLi = ', expLi;
rs = gmb(expLi, j);
inner = expLi[j + 1 : rs];
expLists = topSplit(inner, sym(',')); # each expression is represented by a py-list of tokens
arr = [];
for eLi in expLists:
try: assert len(eLi) >= 1; # try-except used over `if` to maintain consistency.
except AssertionError: # In the future, there may be more assertions.
raise LJSyntaxErr('illegal array literal');
arr.append(eval(eLi, env));
expLi = expLi[ : j] + [arr] + expLi[rs + 1 : ];
return expLi;
def subObjsAndArrs(expLi):
"Substitute literal objects and arrays."
#print 'entering sOA, expLi = ' + expLi;
j = 0;
while j < len(expLi):
tok = expLi[j];
if tok is sym('{'):
expLi = subObject(expLi, j);
elif tok is sym('['):
if j == 0: # corner case (array literal)
expLi = subArray(expLi, j);
else:
pok = expLi[j - 1]; # Previous tOK
indexyTypes = [dict, list, str];
pokIsIndexy = type(pok) in indexyTypes;
if pokIsIndexy or pok in [sym(']'), sym(')')]:
pass; # indexing operation # indexing operations are handeled by refine(..)
else: # array literal
expLi = subArray(expLi, j);
else:
pass; # ignore other tokens
j += 1; # even if expLi changes, incr j by 1
return expLi;
def refineObject(obj, key):
"Helps with object refinements."
if type(key) is not str:
raise LJTypeErr('object keys must be strings');
if key not in obj:
raise LJKeyErr(key);
return obj[key]; # intermediate result
def refineListy(li, ind):
"Helps with list and string refinements."
msg = 'array' if type(li) is list else 'string';
if type(ind) is not float:
raise LJTypeErr(msg + ' indices must be numbers ... ' + lj_repr(i));
elif ind < 0:
raise LJTypeErr(msg + ' indices must be non-negative ... ' + lj_repr(i));
elif ind != round(ind):
raise LJTypeErr(msg + ' indices must integers ... ' + lj_repr(i));
elif ind >= len(li):
raise LJIndexErr(msg + ' index out of range ... ' + lj_repr(ind));
return li[int(ind)]; # intermediate result
def refine(expLi, j): # form: ... <py-dict-or-list> [ 10 ] ...
"Performs a refinement on object/array." # indices: j rs
#print 'entering refine, expLi = ', expLi;
def getRetExpLi(inter):
return expLi[ :j-1] + [inter] + expLi[rs+1: ];
ob = expLi[j - 1]; # object, array or string
assert type(ob) in [dict, list, str];
rs = gmb(expLi, j);
if rs == j + 1:
raise LJSyntaxErr('illegal refinement');
inner = expLi[j + 1 : rs];
ki = eval(inner, env); # short for Key/Index
if type(ob) is dict:
inter = refineObject(ob, ki);
else: # ob in [list, str]: (see assert)
inter = refineListy(ob, ki);
return getRetExpLi(inter);
def invokeFunction(func, args):
"Helps invokes non-native functions."
if len(args) != len(func.params):
raise LJTypeErr('incorrect no. of arguments ... (%s)' % lj_repr(args)[1:-1]);
if func.crEnv is None: raise Exception(); # internal error
assert func.crEnv is not None;
newEnv = func.crEnv.makeChild(func.params, args); # A function is executed in its environ of creation
newEnv.setDepth(env.depth + 1); # Depth of newEnv is changed to invocation_env's depth + 1
treeClone = cloneTree(func.tree); # shields func.tree from being mutated
#print 'bodyClone = ', bodyClone, '\n';
try:
run(treeClone, newEnv, maxLoopTime, writer);
#print('about to exit invokeFunc...`try` block w/ no return');
except LJReturn as r:
#print('in except block of invokeFunc.. due to return, retval = %s' % r.args[0]);
inter = r.args[0];
return inter; # intermediate result
raise LJTypeErr('non-returning function');
def invokePyFunction(func, args):
"Helps invoke python's function."
nParams = len(inspect.getargspec(func)[0]); # number of parameters
if len(args) != nParams:
raise LJTypeErr('incorrect no. of arguments');
inter = func(*args);
types = [bool, float, str, list, dict, Function, type(None)];
if type(inter) in types or inspect.isfunction(inter):
return inter; # intermediate result
#print func;
raise Exception('non-returning native function');
def invoke(expLi, j): # form: ... <Function> ( 1, "king", ... , [0] ) ...
"Helps perform function calls." # indices: j rp
#print 'entering invoke, expLi = ', expLi;
def getRetExpLi(inter):
return expLi[ :j-1] + [inter] + expLi[rp+1:];
func = expLi[j - 1];
assert type(func) is Function or inspect.isfunction(func);
rp = gmb(expLi, j);
inner = expLi[j + 1 : rp];
augInner = [sym('[')] + inner + [sym(']')];
args = subArray(augInner, 0)[0]; # subArray returns an expLi. Here, the returned expLi will contain a single list
if type(func) is Function:
inter = invokeFunction(func, args);
else: # pythonic function
inter = invokePyFunction(func, args);
return getRetExpLi(inter);
def solveGroup(expLi, j): # form: ... ( a + b ) ...
"Helps with parentheses based grouping." # indices: j
#print 'entering solveGroup, expLi = ', expLi;
rp = gmb(expLi, j);
inner = expLi[j+1 : rp];
gVal = eval(inner, env); # Grouping's Value
expLi = expLi[ : j] + [gVal] + expLi[rp+1 : ];
return expLi;
def refine_invoke_and_group(expLi):
"Performs refinements and invocations."
#print 'entering r_i_and_g, expLi = ', expLi;
entered = cloneLi(expLi);
j = 0;
def isFunction(x) :
return type(x) is Function or inspect.isfunction(x);
while j < len(expLi):
tok = expLi[j];
if tok is sym('['):
expLi = refine(expLi, j);
# j = j; # no increment
elif tok is sym('(') and j == 0: # corner case
expLi = solveGroup(expLi, j);
j += 1;
elif tok is sym('('):
prev = expLi[j - 1];
if isFunction(prev):
expLi = invoke(expLi, j);
# j = j; # no increment
else:
expLi = solveGroup(expLi, j);
j += 1;
else:
j += 1; # ignore other tokens
if expLi == [['a', 'c', 'b'], [0.0]]:
pass;
#print 'entering rig expLi = ', entered;
#print 'leaving rig expLi = ', expLi;
return expLi;
def unop(expLi, op, j): # form: ... op value ...
"Evaluates a single unary expression like !true." # indices: j
#print 'entering unop, expLi = ', expLi;
try:
valExpLi = [expLi[j + 1]];
except ValueError:
raise LJSyntaxErr('unexpected unary operator' + op);
if op is sym('!'):
inter = not isTruthy(eval(valExpLi, env));
expLi = expLi[ : j] + [inter] + expLi[j+2 : ];
elif op is sym('-'):
inter = eval(valExpLi, env);
if type(inter) is not float:
raise LJTypeErr('bad operand for unary -');
expLi = expLi[ : j] + [-inter] + expLi[j+2 : ];
elif op is sym('+'):
inter = eval(valExpLi, env);
if type(inter) in [str, float]:
try: interF = float(inter); # Note: `isDecimal` is not useful here.
except ValueError:
raise LJTypeErr('bad operand for unary +');
else:
raise LJTypeErr('bad operand for unary +');
expLi = expLi[ : j] + [interF] + expLi[j+2 : ];
return expLi;
def indiBinop(a, op, b):
"Helps binop(..) with type-independent operators."
#print 'entering indiBinop, expLi = ', expLi;
isT = isTruthy; # Note: JS and python have different ideas of falsehood
def eqeqeq(x, y): # Note: In python, `1.0 is 1.0` --> True
if type(x) != type(y) : return False; # But, `a = 1.0; b = 1.0; a is b` --> False
if type(y) in [bool, float, str, type(None)]: # Thus `is` in py is NOT the same as `===` in JS
return x == y;
refTypes = [list, dict, Function];
assert type(y) in refTypes or inspect.isfunction(y);
return x is y;
return { # pythonic switch statement
sym('==='): eqeqeq,
sym('!=='): lambda x, y: not eqeqeq(x, y),
sym('&&'): lambda x, y: y if isT(x) else x,
sym('||'): lambda x, y: x if isT(x) else y#,
}[op](a, b);
def strNumBinop(a, op, b):
"Helps binop(..) with string and number operations."
#print 'entering strNumBinop, expLi = ', expLi;
return {
sym('>='): lambda x, y: x >= y, # Note: `1 < "king"` is `True` in python but `false` in JS
sym('<='): lambda x, y: x <= y, # Thus, type equality IS necessary for meaningful use of these ops.
sym('>'): lambda x, y: x > y,
sym('<'): lambda x, y: x < y,
sym('+'): lambda x, y: x + y,
}[op](a, b);
def numBinop(a, op, b):
"Helps binop(..) with number operations."
#print 'entering numBinop, expLi = ', expLi;