forked from browserengineering/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
872 lines (801 loc) · 32.6 KB
/
compile.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
#!/usr/bin/env python3
import ast
import json
import warnings
import outlines
INDENT = 2
class AST39(ast.NodeTransformer):
def visit_Num(self, node):
return ast.Constant(node.n)
def visit_Str(self, node):
return ast.Constant(node.s)
def visit_NameConstant(self, node):
return ast.Constant(node.value)
def visit_Ellipsis(self, node):
return ast.Constant(node)
def visit_ExtSlice(self, node):
return ast.Tuple([self.generic_visit(d) for d in node.dims])
def visit_Index(self, node):
return node.value
@classmethod
def parse(cls, str, name='<unknown>'):
tree = ast.parse(str, name)
if hasattr(ast, "NameConstant"):
return ast.fix_missing_locations(cls().visit(tree))
else:
return tree
@staticmethod
def unparse(tree, explain=False):
if hasattr(ast, "unparse"):
return ast.unparse(tree)
elif explain:
return "/* Please convert to Python: " + ast.dump(tree) + " */"
else:
return ast.dump(tree)
class UnsupportedConstruct(AssertionError): pass
class MissingHint(Exception):
def __init__(self, tree, key, hint, error=None):
if not error:
self.message = f"Could not find {key} key for `{AST39.unparse(tree)}`"
else:
self.message = error
self.key = key
self.tree = tree
self.hint = hint
ISSUES = []
WRAP_DISABLED = False
def test_mode():
global WRAP_DISABLED
WRAP_DISABLED = True
def catch_issues(f):
def wrapped(tree, *args, **kwargs):
try:
try:
return f(tree, *args, **kwargs)
except AssertionError as e:
if WRAP_DISABLED: raise e
return find_hint(tree, "js", error=e)
except MissingHint as e:
if WRAP_DISABLED: raise e
ISSUES.append(e)
return "/* " + AST39.unparse(tree) + " */"
return wrapped
HINTS = []
def read_hints(f):
global HINTS
hints = json.load(f)
for h in hints:
assert "line" not in h or isinstance(h["line"], int)
assert "code" in h
s = AST39.parse(h["code"])
assert isinstance(s, ast.Module)
assert len(s.body) == 1
assert isinstance(s.body[0], ast.Expr)
h["ast"] = s.body[0].value
h["used"] = False
HINTS = hints
def find_hint(t, key, error=None):
for h in HINTS:
if "line" in h and h["line"] != t.lineno: continue
if ast.dump(h["ast"]) != ast.dump(t): continue
if key not in h: continue
break
else:
hint = {"line": t.lineno, "code": AST39.unparse(t, explain=True), key: "???"}
raise MissingHint(t, key, hint, error=error)
h["used"] = True
return h[key]
def check_args(args, ctx):
assert not args.vararg, ast.dump(args)
assert not args.kwonlyargs, ast.dump(args)
assert not args.kw_defaults, ast.dump(args)
assert not args.kwarg, ast.dump(args)
out = []
defaults = ([None] * len(args.args) + args.defaults)[-len(args.args):]
for i, (arg, default) in enumerate(zip(args.args, defaults)):
assert not arg.annotation
if ctx.type == "class" and i == 0:
assert arg.arg == "self"
else:
if default:
out.append(arg.arg + " = " + compile_expr(default, ctx))
else:
out.append(arg.arg)
return out
RENAME_METHODS = {
"lower": "toLowerCase",
"upper": "toUpperCase",
"strip": "trim",
"append": "push",
"pop": "pop",
"startswith": "startsWith",
"endswith": "endsWith",
"find": "indexOf",
"copy": "slice",
}
RENAME_FNS = {
"int": "parseInt",
"float": "parseFloat",
"print": "console.log",
}
# These are filled in as import statements are read
RT_IMPORTS = []
LAB_IMPORT_FNS = []
LAB_IMPORT_CONSTANTS = []
LAB_IMPORT_CLASSES = []
LIBRARY_METHODS = [
# socket
"connect",
"wrap_socket",
"send",
"makefile",
"readline",
"read",
"close",
# tkinter
"pack",
"bind",
"delete",
"create_text",
"create_rectangle",
"create_line",
"create_polygon",
# tkinter.font
"metrics",
"measure",
# stuff the compiler needs
"toString",
"init",
]
OUR_FNS = []
OUR_CLASSES = []
OUR_CONSTANTS = []
OUR_METHODS = []
OUR_SYNC_METHODS = ["__repr__", "__init__"]
FILES = []
EXPORTS = []
def load_outline_for_class(ol, class_name):
for item in ol:
if isinstance(item, outlines.Class):
if not item.name == class_name:
continue
OUR_CLASSES.append(item.name)
for subitem in item.fns:
if isinstance(subitem, outlines.Const): continue
elif isinstance(subitem, outlines.Function):
OUR_METHODS.append(subitem.name)
else:
raise ValueError(subitem)
def load_outline(ol):
for item in ol:
if isinstance(item, outlines.IfMain): continue
elif isinstance(item, outlines.Const):
OUR_CONSTANTS.extend(item.names)
elif isinstance(item, outlines.Function):
OUR_FNS.append(item.name)
elif isinstance(item, outlines.Class):
OUR_CLASSES.append(item.name)
for subitem in item.fns:
if isinstance(subitem, outlines.Const): continue
elif isinstance(subitem, outlines.Function):
OUR_METHODS.append(subitem.name)
else:
raise ValueError(subitem)
else:
raise ValueError(item)
THEIR_STUFF = set(LIBRARY_METHODS) | set(RENAME_METHODS) | set(RENAME_FNS)
OUR_STUFF = set(OUR_FNS) | set(OUR_METHODS) | set(OUR_CLASSES) | set(OUR_CONSTANTS)
mixed_types = set(OUR_FNS) & set(OUR_CLASSES)
assert not mixed_types, f"Names defined as both class and function: {mixed_types}"
our_their = (set(LIBRARY_METHODS) | set(RENAME_METHODS)) & set(OUR_METHODS)
assert not our_their, f"Methods defined by both our code and libraries: {our_their}"
our_their = set(RENAME_FNS) & set(OUR_FNS)
assert not our_their, f"Functions defined by our code shadow builtins: {our_their}"
def compile_method(base, name, args, ctx):
base_js = compile_expr(base, ctx)
args_js = [compile_expr(arg, ctx) for arg in args]
if name == "bind": # Needs special handling due to "this"
assert len(args) == 2
return base_js + ".bind(" + args_js[0] + ", (e) => " + args_js[1] + "(e))"
elif name == "makefile":
assert len(args) == 2
return "await " + base_js + ".makefile(" + ", ".join(args_js) + ")"
elif name in LIBRARY_METHODS:
return base_js + "." + name + "(" + ", ".join(args_js) + ")"
elif name in OUR_METHODS:
if name in OUR_SYNC_METHODS:
return base_js + "." + name + "(" + ", ".join(args_js) + ")"
else:
return "await " + base_js + "." + name + "(" + ", ".join(args_js) + ")"
elif name in RENAME_METHODS:
return base_js + "." + RENAME_METHODS[name] + "(" + ", ".join(args_js) + ")"
elif isinstance(base, ast.Name) and base.id == "self":
return base_js + "." + name + "(" + ", ".join(args_js) + ")"
elif base_js in RT_IMPORTS:
return base_js + "." + name + "(" + ", ".join(args_js) + ")"
elif name == "format":
assert isinstance(base, ast.Constant)
assert isinstance(base.value, str)
parts = base.value.split("{}")
assert len(parts) == len(args) + 1
out = ""
for part, arg in zip(parts, [None] + args_js):
assert "{" not in part
if arg: out += " + " + arg
if part: out += " + " + compile_expr(ast.Constant(part), ctx)
return "(" + out[3:] + ")"
elif name == "encode":
assert len(args) == 1
assert isinstance(args[0], ast.Constant)
assert args[0].value == "utf8"
return base_js
elif name == "extend":
assert len(args) == 1
return "Array.prototype.push.apply(" + base_js + ", " + args_js[0] + ")"
elif name == "join":
assert len(args) == 1
return args_js[0] + ".join(" + base_js + ")"
elif name == "isspace":
assert len(args) == 0
return "/^\s*$/.test(" + base_js + ")"
elif name == "isalnum":
assert len(args) == 0
return "/^[a-zA-Z0-9]+$/.test(" + base_js + ")"
elif name == "items":
assert len(args) == 0
return "Object.entries(" + base_js + ")"
elif name == "get":
assert 1 <= len(args) <= 2
default = args_js[1] if len(args) == 2 else "null"
return "(" + base_js + "?.[" + args_js[0] + "] ?? " + default + ")"
elif name == "split":
assert 0 <= len(args) <= 2
if len(args) == 0:
return base_js + ".trim().split(/\s+/)"
elif len(args) == 1:
return base_js + ".split(" + args_js[0] + ")"
else:
return "pysplit(" + base_js + ", " + args_js[0] + ", " + args_js[1] + ")"
elif name == "rsplit":
assert len(args) == 2
return "pyrsplit(" + base_js + ", " + args_js[0] + ", " + args_js[1] + ")"
elif name == "count":
assert len(args) == 1
return base_js + ".split(" + args_js[0] + ").length - 1"
else:
raise UnsupportedConstruct()
def compile_function(name, args, ctx):
args_js = [compile_expr(arg, ctx) for arg in args]
if name in RENAME_FNS:
return RENAME_FNS[name] + "(" + ", ".join(args_js) + ")"
elif name in OUR_FNS or name in LAB_IMPORT_FNS:
return "await " + name + "(" + ", ".join(args_js) + ")"
elif name in OUR_CLASSES:
return "await (new " + name + "()).init(" + ", ".join(args_js) + ")"
elif name == "str":
assert len(args) == 1
return args_js[0] + ".toString()"
elif name == "len":
assert len(args) == 1
return args_js[0] + ".length"
elif name == "ord":
assert len(args) == 1
return args_js[0] + ".charCodeAt(0)"
elif name == "isinstance":
assert len(args) == 2
return args_js[0] + " instanceof " + args_js[1]
elif name == "sum":
assert len(args) == 1
return args_js[0] + ".reduce((a, v) => a + v, 0)"
elif name == "max":
assert 1 <= len(args) <= 2
if len(args) == 1:
return args_js[0] + ".reduce((a, v) => Math.max(a, v))"
else:
return "Math.max(" + args_js[0] + ", " + args_js[1] + ")"
elif name == "breakpoint":
assert isinstance(args[0], ast.Constant)
assert isinstance(args[0].value, str)
return "await breakpoint.event(" + ", ".join(args_js) + ")"
elif name == "min":
assert 1 <= len(args) <= 2
if len(args) == 1:
return args_js[0] + ".reduce((a, v) => Math.min(a, v))"
else:
return "Math.min(" + args_js[0] + ", " + args_js[1] + ")"
elif name == "repr":
assert len(args) == 1
return args_js[0] + ".toString()"
elif name == "open":
assert len(args) == 1
assert isinstance(args[0], ast.Str)
FILES.append(args[0].s)
return "filesystem.open(" + args_js[0] + ")"
elif name == "enumerate":
assert len(args) == 1
return args_js[0] + ".entries()"
else:
raise UnsupportedConstruct()
def op2str(op):
if isinstance(op, ast.Add): return "+"
elif isinstance(op, ast.Sub): return "-"
elif isinstance(op, ast.USub): return "-"
elif isinstance(op, ast.Mult): return "*"
elif isinstance(op, ast.Div): return "/"
elif isinstance(op, ast.Not): return "!"
elif isinstance(op, ast.Gt): return ">"
elif isinstance(op, ast.Lt): return "<"
elif isinstance(op, ast.GtE): return ">="
elif isinstance(op, ast.LtE): return "<="
elif isinstance(op, ast.Eq): return "==="
elif isinstance(op, ast.NotEq): return "!=="
elif isinstance(op, ast.And): return "&&"
elif isinstance(op, ast.Or): return "||"
else:
raise UnsupportedConstruct()
def lhs_targets(tree):
if isinstance(tree, ast.Name):
return set([tree.id])
elif isinstance(tree, ast.Tuple):
return set().union(*[lhs_targets(t) for t in tree.elts])
elif isinstance(tree, ast.Attribute):
return set()
elif isinstance(tree, ast.Subscript):
return set()
else:
raise UnsupportedConstruct()
def compile_lhs(tree, ctx):
targets = lhs_targets(tree)
for target in targets:
if target not in ctx:
ctx[target] = {"is_class": False}
return compile_expr(tree, ctx)
class Context(dict):
def __init__(self, type, parent):
super().__init__(self)
self.type = type
self.parent = parent
def __contains__(self, i):
return (super().__contains__(i)) or (i in self.parent)
def __getitem__(self, i):
if super().__contains__(self, i):
return super().__getitem__(i)
else:
return self.parent[i]
def is_global_constant(self, i):
if self.type == "module":
if super().__contains__(i):
return not super().__getitem__(i)["is_class"]
return True
elif super().__contains__(i):
return False
else:
return self.parent.is_global_constant(i)
@catch_issues
def compile_expr(tree, ctx):
if isinstance(tree, ast.Subscript):
lhs = compile_expr(tree.value, ctx)
if isinstance(tree.slice, ast.Slice):
assert not tree.slice.step
lower = tree.slice.lower and compile_expr(tree.slice.lower, ctx)
upper = tree.slice.upper and compile_expr(tree.slice.upper, ctx)
if lower and upper:
return lhs + ".slice(" + lower + ", " + upper + ")"
elif upper:
return lhs + ".slice(0, " + upper + ")"
elif lower:
return lhs + ".slice(" + lower + ")"
else:
return lhs + ".slice()"
else:
rhs = compile_expr(tree.slice, ctx)
if rhs == "(-1)":
return lhs + "[" + lhs + ".length - 1]"
else:
return lhs + "[" + rhs + "]"
elif isinstance(tree, ast.Call):
args = tree.args[:]
if tree.keywords:
names = []
vals = []
for kwarg in tree.keywords:
assert kwarg.arg
names.append(ast.Constant(kwarg.arg))
vals.append(kwarg.value)
args += [ast.Dict(names, vals)]
if isinstance(tree.func, ast.Attribute):
return "(" + compile_method(tree.func.value, tree.func.attr, args, ctx) + ")"
elif isinstance(tree.func, ast.Name) and tree.func.id == "sorted":
assert len(tree.args) == 1
assert len(tree.keywords) == 1
assert tree.keywords[0].arg == 'key'
assert isinstance(tree.keywords[0].value, ast.Name)
base = compile_expr(args[0], ctx)
return "(" + base + ".slice().sort(comparator(" + tree.keywords[0].value.id + ")))"
elif isinstance(tree.func, ast.Name):
return "(" + compile_function(tree.func.id, args, ctx) + ")"
else:
raise UnsupportedConstruct()
elif isinstance(tree, ast.UnaryOp):
rhs = compile_expr(tree.operand, ctx)
if isinstance(tree.op, ast.Not): rhs = "truthy(" + rhs + ")"
return "(" + op2str(tree.op) + rhs + ")"
elif isinstance(tree, ast.BinOp):
lhs = compile_expr(tree.left, ctx)
rhs = compile_expr(tree.right, ctx)
if isinstance(tree.op, ast.FloorDiv):
return "Math.trunc(" + lhs + " / " + rhs + ")"
else:
return "(" + lhs + " " + op2str(tree.op) + " " + rhs + ")"
elif isinstance(tree, ast.BoolOp):
parts = ["truthy("+compile_expr(val, ctx)+")" for val in tree.values]
return "(" + (" " + op2str(tree.op) + " ").join(parts) + ")"
elif isinstance(tree, ast.Compare):
lhs = compile_expr(tree.left, ctx)
conjuncts = []
for op, comp in zip(tree.ops, tree.comparators):
rhs = compile_expr(comp, ctx)
if (isinstance(op, ast.In) or isinstance(op, ast.NotIn)):
negate = isinstance(op, ast.NotIn)
if isinstance(comp, ast.Str):
cmp = "===" if negate else "!=="
conjuncts.append("(" + rhs + ".indexOf(" + lhs + ") " + cmp + " -1)")
elif isinstance(comp, ast.List):
assert isinstance(tree.left, ast.Name) or \
(isinstance(tree.left, ast.Subscript) and
isinstance(tree.left.value, ast.Name))
op = " !== " if negate else " === "
parts = [lhs + op + compile_expr(v, ctx) for v in comp.elts]
conjuncts.append("(" + (" && " if negate else " || ").join(parts) + ")")
else:
t = find_hint(tree, "type")
assert t in ["str", "dict", "list"]
cmp = "===" if negate else "!=="
if t in ["str", "list"]:
conjuncts.append("(" + rhs + ".indexOf(" + lhs + ") " + cmp + " -1)")
elif t == "dict":
conjuncts.append("(typeof " + rhs + "[" + lhs + "] " + cmp + " \"undefined\")")
elif isinstance(op, ast.Eq) and \
(isinstance(comp, ast.List) or isinstance(tree.left, ast.List)):
conjuncts.append("(JSON.stringify(" + lhs + ") === JSON.stringify(" + rhs + "))")
else:
conjuncts.append("(" + lhs + " " + op2str(op) + " " + rhs + ")")
lhs = rhs
if len(conjuncts) == 1:
return conjuncts[0]
else:
return "(" + " && ".join(conjuncts) + ")"
elif isinstance(tree, ast.IfExp):
test = compile_expr(tree.test, ctx)
ift = compile_expr(tree.body, ctx)
iff = compile_expr(tree.orelse, ctx)
return "(" + test + " ? " + ift + " : " + iff + ")"
elif isinstance(tree, ast.ListComp):
assert len(tree.generators) == 1
gen = tree.generators[0]
out = compile_expr(gen.iter, ctx)
ctx2 = Context("expr", ctx)
arg = compile_lhs(gen.target, ctx2)
assert not gen.is_async
for if_clause in gen.ifs:
e = compile_expr(if_clause, ctx2)
out += ".filter((" + arg + ") => " + e + ")"
e = compile_expr(tree.elt, ctx2)
out += ".map((" + arg + ") => " + e + ")"
return out
elif isinstance(tree, ast.Attribute):
base = compile_expr(tree.value, ctx)
return base + "." + tree.attr
elif isinstance(tree, ast.Dict):
assert all(isinstance(k, ast.Str) for k in tree.keys)
pairs = [compile_expr(k, ctx) + ": " + compile_expr(v, ctx) for k, v in zip(tree.keys, tree.values)]
return "{" + ", ".join(pairs) + "}"
elif isinstance(tree, ast.Tuple) or isinstance(tree, ast.List):
return "[" + ", ".join([compile_expr(a, ctx) for a in tree.elts]) + "]"
elif isinstance(tree, ast.Name):
if tree.id == "self":
return "this"
elif tree.id in RT_IMPORTS or tree.id in LAB_IMPORT_CLASSES:
return tree.id
elif ctx.is_global_constant(tree.id) or tree.id in LAB_IMPORT_CONSTANTS:
return "constants.{}".format(tree.id)
elif tree.id in ctx:
return tree.id
else:
raise AssertionError(f"Could not find variable {tree.id}")
elif isinstance(tree, ast.Constant):
if isinstance(tree.value, str):
return compile_str(tree.value)
elif isinstance(tree.value, bool):
return "true" if tree.value else "false"
elif isinstance(tree.value, int):
return repr(tree.value)
elif isinstance(tree.value, float):
return repr(tree.value)
elif tree.value is None:
return "null"
else:
raise UnsupportedConstruct()
else:
raise UnsupportedConstruct()
def compile_str(s):
out = repr(s)
if out[0] == out[-1] == "'" and '"' not in out:
out = '"' + out[1:-1] + '"'
return out
def flatten_ifs(tree):
parts = [(tree.test, tree.body)]
while len(tree.orelse) == 1 and isinstance(tree.orelse[0], ast.If):
tree = tree.orelse[0]
parts.append((tree.test, tree.body))
if tree.orelse:
parts.append((None, tree.orelse))
return parts
@catch_issues
def compile(tree, ctx, indent=0):
if isinstance(tree, ast.Import):
assert len(tree.names) == 1
assert not tree.names[0].asname
name = tree.names[0].name
ctx[name] = {"is_class": False}
RT_IMPORTS.append(name)
return " " * indent + "// Please configure the '" + name + "' module"
elif isinstance(tree, ast.ImportFrom):
assert tree.level == 0
assert tree.module
assert all(name.asname is None for name in tree.names)
names = [name.name for name in tree.names]
filename = "src/{}.py".format(tree.module)
with open(filename) as file:
outline = outlines.outline(AST39.parse(file.read(), filename))
to_import = []
to_bind = []
for name in names:
if name.isupper(): # Global constant
LAB_IMPORT_CONSTANTS.append(name)
to_import.append("constants as {}_constants".format(tree.module))
to_bind.append(name)
elif name[0].isupper(): # Class
LAB_IMPORT_CLASSES.append(name)
load_outline_for_class(outline, name)
to_import.append(name)
else: # function
LAB_IMPORT_FNS.append(name)
to_import.append(name)
import_line = "import {{ {} }} from \"./{}.js\";".format(", ".join(sorted(set(to_import))), tree.module)
out = " " * indent + import_line
for const in to_bind:
out += "\n" + " " * indent + "constants.{} = {}_constants.{};".format(const, tree.module, const)
return out
elif isinstance(tree, ast.ClassDef):
assert not tree.bases
assert not tree.keywords
assert not tree.decorator_list
ctx[tree.name] = {"is_class": True}
ctx2 = Context("class", ctx)
parts = [compile(part, indent=indent + INDENT, ctx=ctx2) for part in tree.body]
EXPORTS.append(tree.name)
return " " * indent + "class " + tree.name + " {\n" + "\n\n".join(parts) + "\n}"
elif isinstance(tree, ast.FunctionDef):
assert not tree.decorator_list
assert not tree.returns
args = check_args(tree.args, ctx)
ctx2 = Context("function", ctx)
for arg in tree.args.args:
ctx2[arg.arg] = True
body = "\n".join([compile(line, indent=indent + INDENT, ctx=ctx2) for line in tree.body])
if tree.name == "__init__":
# JS constructors cannot be async, so we move that to a builder method
assert ctx.type == "class"
def_line = " " * indent + "async init(" + ", ".join(args) + ") {\n"
ret_line = "\n" + " " * (indent + INDENT) + "return this;"
last_line = "\n" + " " * indent + "}"
return def_line + body + ret_line + last_line
elif tree.name == "__repr__":
# This actually defines a 'toString' operator
assert ctx.type == "class"
def_line = " " * indent + "toString(" + ", ".join(args) + ") {\n"
last_line = "\n" + " " * indent + "}"
return def_line + body + last_line
else:
if ctx.type == "module":
EXPORTS.append(tree.name)
kw = "" if ctx.type == "class" else "function "
def_line = kw + tree.name + "(" + ", ".join(args) + ") {\n"
if ctx.type != "class" or tree.name not in OUR_SYNC_METHODS:
def_line = "async " + def_line
last_line = "\n" + " " * indent + "}"
return " " * indent + def_line + body + last_line
elif isinstance(tree, ast.Expr) and ctx.type == "module" and \
isinstance(tree.value, ast.Constant) and isinstance(tree.value.value, str):
cmt = " " * indent + "// "
return cmt + tree.value.value.strip("\n").replace("\n", "\n" + cmt)
elif isinstance(tree, ast.Expr):
return " " * indent + compile_expr(tree.value, ctx) + ";"
elif isinstance(tree, ast.Assign):
assert len(tree.targets) == 1
targets = lhs_targets(tree.targets[0])
ins = set([target in ctx for target in targets])
if True in ins and False in ins:
kw = "let " + ", ".join([target for target in targets if target not in ctx]) + "; "
elif ctx.type in ["class"]: kw = ""
elif False in ins and ctx.type != "module":
kw = "let "
else: kw = ""
lhs = compile_lhs(tree.targets[0], ctx)
rhs = compile_expr(tree.value, ctx)
return " " * indent + kw + lhs + " = " + rhs + ";"
elif isinstance(tree, ast.AugAssign):
targets = lhs_targets(tree.target)
for target in targets:
assert target in ctx
lhs = compile_lhs(tree.target, ctx)
rhs = compile_expr(tree.value, ctx)
return " " * indent + lhs + " " + op2str(tree.op) + "= " + rhs + ";"
elif isinstance(tree, ast.Assert):
test = compile_expr(tree.test, ctx)
msg = compile_expr(tree.msg, ctx) if tree.msg else ""
return " " * indent + "if (!truthy(" + test + ")) throw Error(" + msg + ");"
elif isinstance(tree, ast.Return):
ret = compile_expr(tree.value, ctx) if tree.value else None
return " " * indent + "return" + (" " + ret if ret else "") + ";"
elif isinstance(tree, ast.While):
assert not tree.orelse
test = compile_expr(tree.test, ctx)
out = " " * indent + "while (" + test + ") {\n"
out += "\n".join([compile(line, indent=indent + INDENT, ctx=ctx) for line in tree.body])
out += "\n" + " " * indent + "}"
return out
elif isinstance(tree, ast.For):
assert not tree.orelse
ctx2 = Context("for", ctx)
lhs = compile_lhs(tree.target, ctx2)
rhs = compile_expr(tree.iter, ctx)
body = "\n".join([compile(line, indent=indent + INDENT, ctx=ctx2) for line in tree.body])
fstline = " " * indent + "for (let " + lhs + " of " + rhs + ") {\n"
return fstline + body + "\n" + " " * indent + "}"
elif isinstance(tree, ast.If) and ctx.type == "module":
test = tree.test
assert isinstance(test, ast.Compare)
assert isinstance(test.left, ast.Name)
assert test.left.id == "__name__"
assert len(test.comparators) == 1
if isinstance(test.comparators[0], ast.Str):
s = test.comparators[0].s
else:
assert isinstance(test.comparators[0], ast.Constant)
assert isinstance(test.comparators[0].value, str)
s = test.comparators[0].value
assert s == "__main__"
assert len(test.ops) == 1
assert isinstance(test.ops[0], ast.Eq)
return " " * indent + "// Requires a test harness"
elif isinstance(tree, ast.If):
if not tree.orelse and tree.test.lineno == tree.body[0].lineno:
assert len(tree.body) == 1
ctx2 = Context(ctx.type, ctx)
test = compile_expr(tree.test, ctx)
body = compile(tree.body[0], indent=indent, ctx=ctx2)
return " " * indent + "if (truthy(" + test + ")) " + body.strip()
else:
parts = flatten_ifs(tree)
out = " " * indent
# This block handles variables defined in all branches of an if statement
ctxs = []
for test, body in parts:
ctx2 = Context(ctx.type, ctx)
ctxs.append(ctx2)
for line in body: compile(line, ctx=ctx2)
intros = set.intersection(*[set(ctx2) for ctx2 in ctxs]) - set(ctx)
if intros:
for name in intros: ctx[name] = {"is_class": False}
out += "let " + ",".join(intros) + ";\n" + " " * indent
for i, (test, body) in enumerate(parts):
ctx2 = Context(ctx.type, ctx)
body_js = "\n".join([compile(line, indent=indent + INDENT, ctx=ctx2) for line in body])
if not i and test:
test_js = compile_expr(test, ctx)
out += "if (truthy(" + test_js + ")) {\n"
elif i and test:
test_js = compile_expr(test, ctx)
out += " else if (truthy(" + test_js + ")) {\n"
elif not test:
out += " else {\n"
out += body_js + "\n"
out += " " * indent + "}"
return out
elif isinstance(tree, ast.Try):
assert not tree.orelse
assert not tree.finalbody
assert len(tree.handlers) == 1
out = " " * indent + "try {\n"
ctx2 = Context(ctx.type, ctx)
body_js = "\n".join([compile(line, indent=indent + INDENT, ctx=ctx2) for line in tree.body])
out += body_js + "\n"
out += " " * indent + "} catch {\n"
handler = tree.handlers[0]
assert not handler.name
ctx3 = Context(ctx.type, ctx)
if handler.type:
assert isinstance(handler.type, ast.Name)
assert handler.type.id.endswith("Error")
catch_js = "\n".join([compile(line, indent=indent + INDENT, ctx=ctx3) for line in handler.body])
out += catch_js + "\n"
out += " " * indent + "}"
return out
elif isinstance(tree, ast.With):
assert not tree.type_comment
assert len(tree.items) == 1
item = tree.items[0]
var = item.optional_vars if item.optional_vars else ast.Name("_ctx")
assert isinstance(var, ast.Name)
out = compile(ast.Assign([var], item.context_expr), indent=indent, ctx=ctx) + "\n"
out += "\n".join([compile(line, indent=indent, ctx=ctx) for line in tree.body]) + "\n"
out += compile(ast.Expr(ast.Call(ast.Attribute(var, "close"), [], [])),
indent=indent, ctx=ctx)
return out
elif isinstance(tree, ast.Continue):
return " " * indent + "continue;"
elif isinstance(tree, ast.Break):
return " " * indent + "break;"
elif isinstance(tree, ast.Pass):
return ""
else:
raise UnsupportedConstruct()
def compile_module(tree, name, use_js_modules):
assert isinstance(tree, ast.Module)
ctx = Context("module", {})
items = [compile(item, indent=0, ctx=ctx) for item in tree.body]
exports = ""
rt_imports = ""
render_imports = ""
constants_export = "const constants = {};"
if use_js_modules:
if len(EXPORTS) > 0:
exports = "export {{ {} }};".format(", ".join(EXPORTS))
imports_str = "import {{ {} }} from \"./{}.js\";"
rt_imports_arr = [ 'breakpoint', 'comparator', 'filesystem', 'pysplit', 'pyrsplit', 'truthy' ]
rt_imports_arr += set([ mod.split(".")[0] for mod in RT_IMPORTS])
rt_imports = imports_str.format(", ".join(rt_imports_arr), "rt")
constants_export = "export " + constants_export
return "{}\n{}\n{}\n\n{}".format(
exports, rt_imports, constants_export, "\n\n".join(items))
if __name__ == "__main__":
import sys, os
import argparse
MIN_PYTHON = (3, 7)
if sys.version_info < MIN_PYTHON:
sys.exit("Python %s.%s or later is required.\n" % MIN_PYTHON)
parser = argparse.ArgumentParser(description="Compiles each chapter's Python code to JavaScript")
parser.add_argument("--hints", default=None, type=argparse.FileType())
parser.add_argument("--indent", default=2, type=int)
parser.add_argument("--use-js-modules", action="store_true", default=False)
parser.add_argument("python", type=argparse.FileType())
parser.add_argument("javascript", type=argparse.FileType("w"))
args = parser.parse_args()
name = os.path.basename(args.python.name)
assert name.endswith(".py")
if args.hints: read_hints(args.hints)
INDENT = args.indent
tree = AST39.parse(args.python.read(), args.python.name)
load_outline(outlines.outline(tree))
js = compile_module(tree, name[:-len(".py")], args.use_js_modules)
for fn in FILES:
path = os.path.join(os.path.dirname(args.python.name), fn)
with open(path) as f:
js += "\nfilesystem.register(" + repr(fn) + ", " + json.dumps(f.read()) + ");\n"
args.javascript.write(js)
issues = 0
for i in ISSUES:
print(i.message)
if i.hint:
print(" Hint:", json.dumps(i.hint), file=sys.stderr)
issues += 1
for h in HINTS:
if h["used"]: continue
h2 = h.copy()
del h2["used"]
del h2["ast"]
print(f"Unused hint: {json.dumps(h2)}", file=sys.stderr)
issues += 1
sys.exit(issues)