Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathutils.py
More file actions
Latest commit
1655 lines (1136 loc) · 38.6 KB
/
Copy pathutils.py
File metadata and controls
1655 lines (1136 loc) · 38.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
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
importos
importsys
fromsysimportplatform
importast
fromastimport*
fromdataclassesimportdataclass
# move these to the compilers, use a method with overrides -Jeremy
builtin_functions= \
{'input_int', 'print',
'arity',
'len',
'array_len', 'array_load', 'array_store',
'any_load', 'any_load_unsafe', 'any_store', 'any_store_unsafe',
'any_len', 'make_any',
'exit',
'is_tuple_proxy', 'project_tuple', 'proxy_tuple_load', 'proxy_tuple_len',
'is_array_proxy', 'project_array', 'proxy_array_load', 'proxy_array_len',
'proxy_array_store',
}
tag_is_array_right_shift=62
tag_is_proxy_right_shift=63
################################################################################
# repr for classes in the ast module
################################################################################
indent_amount=2
defindent_stmt():
return" "*indent_amount
defindent():
globalindent_amount
indent_amount+=2
defdedent():
globalindent_amount
indent_amount-=2
defstr_Module(self):
indent()
body=''.join([str(s) forsinself.body])
dedent()
returnbody
Module.__str__=str_Module
defrepr_Module(self):
return'Module('+repr(self.body) +')'
Module.__repr__=repr_Module
defstr_Expr(self):
returnindent_stmt() +str(self.value)
Expr.__str__=str_Expr
defrepr_Expr(self):
returnindent_stmt() +'Expr('+repr(self.value) +')'
Expr.__repr__=repr_Expr
defstr_Pass(self):
returnindent_stmt() +'pass'
Pass.__str__=str_Pass
defrepr_Pass(self):
returnindent_stmt() +'Pass()'
Pass.__repr__=repr_Pass
defstr_Assign(self):
returnindent_stmt() +str(self.targets[0]) +' = '+str(self.value)
Assign.__str__=str_Assign
defrepr_Assign(self):
returnindent_stmt() +'Assign('+repr(self.targets) +', '+repr(self.value) +')'
Assign.__repr__=repr_Assign
defstr_AnnAssign(self):
returnindent_stmt() +str(self.target) +' : '+str(self.annotation) +' = '+str(self.value)
AnnAssign.__str__=str_AnnAssign
defrepr_AnnAssign(self):
returnindent_stmt() +'AnnAssign('+repr(self.target) +', ' \
+repr(self.annotation) +', '+repr(self.value) +repr(self.simple) +')'
AnnAssign.__repr__=repr_AnnAssign
defstr_Return(self):
returnindent_stmt() +'return '+str(self.value)
Return.__str__=str_Return
defrepr_Return(self):
returnindent_stmt() +'Return('+repr(self.value) +')'
Return.__repr__=repr_Return
defstr_Name(self):
ifhasattr(self, 'has_type'):
returnself.id+':'+str(self.has_type)
else:
returnself.id
Name.__str__=str_Name
defrepr_Name(self):
return'Name('+repr(self.id) +')'
Name.__repr__=repr_Name
defstr_Constant(self):
returnstr(self.value)
Constant.__str__=str_Constant
defrepr_Constant(self):
return'Constant('+repr(self.value) +')'
Constant.__repr__=repr_Constant
defstr_Add(self):
return'+'
Add.__str__=str_Add
defrepr_Add(self):
return'Add()'
Add.__repr__=repr_Add
defstr_Sub(self):
return'-'
Sub.__str__=str_Sub
defrepr_Sub(self):
return'Sub()'
Sub.__repr__=repr_Sub
defstr_Mult(self):
return'*'
Mult.__str__=str_Mult
defrepr_Mult(self):
return'Mult()'
Mult.__repr__=repr_Mult
defstr_And(self):
return'and'
And.__str__=str_And
defrepr_And(self):
return'And()'
And.__repr__=repr_And
defstr_Or(self):
return'or'
Or.__str__=str_Or
defrepr_Or(self):
return'Or()'
Or.__repr__=repr_Or
defstr_BinOp(self):
return'('+str(self.left) +' '+str(self.op) +' '+str(self.right) +')'
BinOp.__str__=str_BinOp
defrepr_BinOp(self):
return'BinOp('+repr(self.left) +', '+repr(self.op) +', '+repr(self.right) +')'
BinOp.__repr__=repr_BinOp
defstr_BoolOp(self):
return'('+str(self.values[0]) +' '+str(self.op) +' '+str(self.values[1]) +')'
BoolOp.__str__=str_BoolOp
defrepr_BoolOp(self):
returnrepr(self.values[0]) +' '+repr(self.op) +' '+repr(self.values[1])
BoolOp.__repr__=repr_BoolOp
defstr_USub(self):
return'-'
USub.__str__=str_USub
defrepr_USub(self):
return'USub()'
USub.__repr__=repr_USub
defstr_Not(self):
return'not'
Not.__str__=str_Not
defrepr_Not(self):
return'Not()'
Not.__repr__=repr_Not
defstr_UnaryOp(self):
returnstr(self.op) +'('+str(self.operand) +')'
UnaryOp.__str__=str_UnaryOp
defrepr_UnaryOp(self):
return'UnaryOp('+repr(self.op) +', '+repr(self.operand) +')'
UnaryOp.__repr__=repr_UnaryOp
defstr_Call(self):
returnstr(self.func) \
+'('+', '.join([str(arg) forarginself.args]) +')'
Call.__str__=str_Call
defrepr_Call(self):
return'Call('+repr(self.func) +', '+repr(self.args) +')'
Call.__repr__=repr_Call
defstr_If(self):
header=indent_stmt() +'if '+str(self.test) +':\n'
indent()
thn=''.join([str(s) forsinself.body])
els=''.join([str(s) forsinself.orelse])
dedent()
returnheader+thn+'\n'+indent_stmt() +'else:\n'+els
If.__str__=str_If
defrepr_If(self):
return'If('+repr(self.test) +', '+repr(self.body) +', '+repr(self.orelse) +')'
If.__repr__=repr_If
defstr_IfExp(self):
return'('+str(self.body) +' if '+str(self.test) + \
' else '+str(self.orelse) +')'
IfExp.__str__=str_IfExp
defrepr_IfExp(self):
return'IfExp('+repr(self.test) +', '+repr(self.body) + \
', '+repr(self.orelse) +')'
IfExp.__repr__=repr_IfExp
defstr_While(self):
header=indent_stmt() +'while '+str(self.test) +':\n'
indent()
body=''.join([str(s) forsinself.body])
dedent()
returnheader+body
While.__str__=str_While
defrepr_While(self):
return'While('+repr(self.test) +', '+repr(self.body) +', '+repr(self.orelse) +')'
While.__repr__=repr_While
defstr_Compare(self):
returnstr(self.left) +' '+str(self.ops[0]) +' '+str(self.comparators[0])
Compare.__str__=str_Compare
defrepr_Compare(self):
return'Compare('+repr(self.left) +', '+repr(self.ops) +', ' \
+repr(self.comparators) +')'
Compare.__repr__=repr_Compare
defstr_Eq(self):
return'=='
Eq.__str__=str_Eq
defrepr_Eq(self):
return'Eq()'
Eq.__repr__=repr_Eq
defstr_NotEq(self):
return'!='
NotEq.__str__=str_NotEq
defrepr_NotEq(self):
return'NotEq()'
NotEq.__repr__=repr_NotEq
defstr_Lt(self):
return'<'
Lt.__str__=str_Lt
defrepr_Lt(self):
return'Lt()'
Lt.__repr__=repr_Lt
defstr_LtE(self):
return'<='
LtE.__str__=str_Lt
defrepr_LtE(self):
return'LtE()'
LtE.__repr__=repr_LtE
defstr_Gt(self):
return'>'
Gt.__str__=str_Gt
defrepr_Gt(self):
return'Gt()'
Gt.__repr__=repr_Gt
defstr_GtE(self):
return'>='
GtE.__str__=str_GtE
defrepr_GtE(self):
return'GtE()'
GtE.__repr__=repr_GtE
defstr_Is(self):
return'is'
Is.__str__=str_Is
defrepr_Is(self):
return'Is()'
Is.__repr__=repr_Is
defstr_Tuple(self):
return'('+', '.join([str(e) foreinself.elts]) +',)'
Tuple.__str__=str_Tuple
defrepr_Tuple(self):
return'Tuple('+repr(self.elts) +')'
Tuple.__repr__=repr_Tuple
defstr_List(self):
return'['+', '.join([str(e) foreinself.elts]) +']'
ast.List.__str__=str_List
defrepr_List(self):
return'List('+repr(self.elts) +')'
ast.List.__repr__=repr_List
defstr_Subscript(self):
returnstr(self.value) +'['+str(self.slice) +']'
Subscript.__str__=str_Subscript
defrepr_Subscript(self):
return'Subscript('+repr(self.value) +', '+repr(self.slice) \
+', '+repr(self.ctx) +')'
Subscript.__repr__=repr_Subscript
defstr_FunctionDef(self):
ifisinstance(self.args, ast.arguments):
params=', '.join([a.arg+' : '+str(a.annotation) forainself.args.args])
else:
params=', '.join([x+' : '+str(t) for (x, t) inself.args])
indent()
ifisinstance(self.body, list):
body=''.join([str(s) forsinself.body])
elifisinstance(self.body, dict):
body=''
for (l, ss) inself.body.items():
body+=l+':\n'
indent()
body+='\n'.join([str(s) forsinss])
body+='\n'
dedent()
else:
body=''
dedent()
returnindent_stmt() +'def '+self.name+'('+params+')'+ \
' -> '+str(self.returns) +':\n'+body+'\n'
defrepr_FunctionDef(self):
return'FunctionDef('+self.name+','+repr(self.args) +','+ \
repr(self.body) +')'
FunctionDef.__str__=str_FunctionDef
FunctionDef.__repr__=repr_FunctionDef
defstr_Lambda(self):
ifisinstance(self.args, ast.arguments):
params=', '.join([a.argforainself.args.args])
else:
params=', '.join([xforxinself.args])
body=str(self.body)
return'(lambda '+params+': '+body+')'
defrepr_Lambda(self):
return'Lambda('+repr(self.args) +','+repr(self.body) +')'
Lambda.__str__=str_Lambda
Lambda.__repr__=repr_Lambda
defstr_ImportFrom(self):
returnindent_stmt() +'from'+' X '+'import'+' Y\n'
defrepr_ImportFrom(self):
return'ImportFrom()'
ImportFrom.__str__=str_ImportFrom
ImportFrom.__repr__=repr_ImportFrom
################################################################################
# __eq__ and __hash__ for classes in the ast module
################################################################################
defeq_Name(self, other):
ifisinstance(other, Name):
returnself.id==other.id
else:
returnFalse
Name.__eq__=eq_Name
defhash_Name(self):
returnhash(self.id)
Name.__hash__=hash_Name
################################################################################
# Generating unique names
################################################################################
name_id=0
defgenerate_name(name):
globalname_id
ls=name.split('.')
new_id=name_id
name_id+=1
returnls[0] +'.'+str(new_id)
################################################################################
# AST classes
################################################################################
classType:
pass
defmake_assigns(bs):
return [Assign([x], rhs) for (x, rhs) inbs]
defmake_begin(bs, e):
iflen(bs) >0:
returnBegin([Assign([x], rhs) for (x, rhs) inbs], e)
else:
returne
@dataclass
classCast(expr):
body: expr
source: Type
target: Type
__match_args__= ("body", "source", "target")
def__str__(self):
return'('+str(self.body) +' : '+str(self.source) +' => '+str(self.target) +')'
# A lambda expression whose parameters are annotated with types.
@dataclass
classAnnLambda(expr):
params: list[tuple[str, Type]]
returns: Type
body: expr
__match_args__= ("params", "returns", "body")
def__str__(self):
return'lambda ['+ \
', '.join([x+':'+str(t) for (x, t) inself.params]) +'] -> ' \
+str(self.returns) +': '+str(self.body)
# Instantiate a generic function
@dataclass
classInst(expr):
generic: expr
type_args: dict[str, Type]
__match_args__= ("generic", "type_args")
def__str__(self):
returnstr(self.generic) +str(self.type_args)
# An uninitialized value of a given type.
# Needed for boxing local variables.
@dataclass
classUninitialized(expr):
ty: Type
__match_args__= ("ty",)
def__str__(self):
return'uninit['+str(self.ty) +']'
@dataclass
classCProgram:
__match_args__= ("body",)
body: list[stmt]
def__str__(self):
result=''
for (l, ss) inself.body.items():
result+=l+':\n'
indent()
result+=''.join([str(s) forsinss]) +'\n'
dedent()
returnresult
@dataclass
classCProgramDefs:
defs: list[stmt]
__match_args__= ("defs",)
def__str__(self):
return'\n'.join([str(d) fordinself.defs]) +'\n'
@dataclass
classGoto(stmt):
label: str
__match_args__= ("label",)
def__str__(self):
returnindent_stmt() +'goto '+self.label+'\n'
@dataclass
classAllocate(expr):
length: int
ty: Type
__match_args__= ("length", "ty")
def__str__(self):
return'allocate('+str(self.length) +','+str(self.ty) +')'
@dataclass
classAllocateArray(expr):
length: int
ty: Type
__match_args__= ("length", "ty")
def__str__(self):
return'allocate_array('+str(self.length) +','+str(self.ty) +')'
@dataclass
classAllocateClosure(expr):
length: int
ty: Type
arity: int
__match_args__= ("length", "ty", "arity")
def__str__(self):
return'alloc_clos('+str(self.length) +','+str(self.ty) \
+','+str(self.arity) +')'
@dataclass
classUncheckedCast(expr):
exp: expr
ty: Type
__match_args__= ("exp","ty")
@dataclass
classCollect(stmt):
size: int
__match_args__= ("size",)
def__str__(self):
returnindent_stmt() +'collect('+str(self.size) +')\n'
@dataclass
classBegin(expr):
__match_args__= ("body", "result")
body: list[stmt]
result: expr
def__str__(self):
indent()
stmts='\n'.join([str(s) forsinself.body])
end='\n'+indent_stmt() +'produce '+str(self.result) +'\n'
dedent()
return'{\n'+stmts+end+'}'
@dataclass
classGlobalValue(expr):
name: str
__match_args__= ("name",)
def__str__(self):
returnstr(self.name)
@dataclass(eq=True)
classIntType(Type):
def__str__(self):
return'int'
@dataclass(eq=True)
classBoolType(Type):
def__str__(self):
return'bool'
@dataclass(eq=True)
classVoidType(Type):
def__str__(self):
return'void'
@dataclass(eq=True)
classBottom(Type):
def__str__(self):
return'bottom'
@dataclass(eq=True)
classTupleType(Type):
types: list[Type]
__match_args__= ("types",)
def__str__(self):
return'tuple['+','.join([str(p) forpinself.types]) +']'
@dataclass(eq=True)
classListType(Type):
elt_type: Type
__match_args__= ("elt_type",)
def__str__(self):
return'list['+str(self.elt_type) +']'
@dataclass(eq=True)
classFunctionType(Type):
param_types: list[Type]
ret_type: Type
__match_args__= ("param_types", "ret_type")
def__str__(self):
return'Callable[['+','.join([str(p) forpinself.param_types]) +']' \
+', '+str(self.ret_type) +']'
@dataclass(eq=True)
classGenericVar(Type):
id: str
__match_args__= ("id",)
def__str__(self):
returnstr(self.id)
@dataclass(eq=True)
classAllType(Type):
params: list[str]
typ: Type
__match_args__= ("params", "typ")
def__str__(self):
return'∀ '+','.join([str(p) forpinself.params]) +'(' \
+str(self.typ) +')'
@dataclass(eq=True)
classFunRef(expr):
name: str
arity: int
__match_args__= ("name", "arity")
def__str__(self):
return'{'+self.name+'}'
@dataclass
classTailCall(stmt):
func: expr
args: list[expr]
__match_args__= ("func", "args")
def__str__(self):
returnindent_stmt() +'tail '+str(self.func) +'('+', '.join([str(e) foreinself.args]) +')\n'
# like a Tuple, but also stores the function's arity
@dataclass
classClosure(expr):
arity: int
args: list[expr]
__match_args__= ("arity", "args")
def__str__(self):
ifhasattr(self, 'has_type'):
typ=':'+str(self.has_type)
else:
typ=''
return'closure{'+repr(self.arity) +'}('+', '.join([str(e) foreinself.args]) +')'+typ
@dataclass
classInject(expr):
value: expr
typ: Type
__match_args__= ("value", "typ")
def__str__(self):
return'inject('+str(self.value) +', '+str(self.typ) +')'
@dataclass
classProject(expr):
value: expr
typ: Type
__match_args__= ("value", "typ")
def__str__(self):
return'project('+str(self.value) +', '+str(self.typ) +')'
@dataclass
classTagOf(expr):
value: expr
__match_args__= ("value",)
def__str__(self):
return'tagof('+str(self.value) +')'
@dataclass
classValueOf(expr):
value: expr
typ: Type
__match_args__= ("value", "typ")
def__str__(self):
return'valueof('+str(self.value) +', '+str(self.typ) +')'
@dataclass(eq=True)
classAnyType(Type):
def__str__(self):
return'Any'
@dataclass(eq=True)
classProxyOrTupleType(Type):
elt_types: list[Type]
def__str__(self):
return'POrTuple['+','.join([str(t) fortinself.elt_types]) +']'
@dataclass(eq=True)
classProxyOrListType(Type):
elt_type: Type
def__str__(self):
return'POrList['+str(self.elt_type) +']'
@dataclass(eq=True)
classTupleProxy(expr):
value: expr
reads: list[expr]
source: Type
target: Type
def__str__(self):
return'tuple_proxy('+str(self.value) +', '+str(self.source) \
+', '+str(self.target) +')'
@dataclass(eq=True)
classRawTuple(expr):
elts: list[expr]
def__str__(self):
return'[['+','.join([str(e) foreinself.elts]) +']]'
@dataclass(eq=True)
classListProxy(expr):
value: expr
read: expr
write: expr
source: Type
target: Type
def__str__(self):
return'array_proxy('+str(self.value) +', '+str(self.source) \
+', '+str(self.target) +')'
@dataclass(eq=True)
classInjectTuple(expr):
value: expr
def__str__(self):
return'inject_tuple['+str(self.value) +']'
@dataclass(eq=True)
classInjectTupleProxy(expr):
value: expr
typ: Type
def__str__(self):