-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.py
544 lines (418 loc) · 19.5 KB
/
compiler.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
import ast
from node import Node, NodeType, toNode
from scope import Scope
from symbol import Symbol, SymbolKind
from codegen import CResult
from type_ import *
from typing import List
KEYWORDS = ['range', 'enumerate', 'zip', 'any', 'all', 'next', 'print']
def getType(scope: Scope, typeName: str) -> Type:
symbol = scope.findNested(typeName)
if symbol:
return symbol.resolvedType
def getAnnotationType(scope: Scope, annotation: Node) -> Type:
if annotation.nodeType == NodeType.NAME:
typeName = annotation.getText()
return getType(scope, typeName)
elif annotation.nodeType == NodeType.TUPLE:
tupleType = getType(scope, BuiltInType.TUPLE)
elts = annotation.getChild('elts')
elementTypes = [getAnnotationType(scope, elt) for elt in elts]
return Type(genericType=tupleType, elementTypes=elementTypes)
elif annotation.nodeType == NodeType.SUBSCRIPT:
genericType = getType(scope, annotation.getChild('value').getText())
slice_ = annotation.getChild('slice').getChild('value')
if slice_.nodeType == NodeType.TUPLE:
sliceElts = slice_.getChild('elts')
elementTypes = [getAnnotationType(scope, sliceElt) for sliceElt in sliceElts]
return Type(genericType=genericType, elementTypes=elementTypes)
else:
sliceType = getAnnotationType(scope, slice_)
return Type(genericType=genericType, elementTypes=[sliceType])
else:
raise Exception('Unknown node type:' + annotation.nodeType)
def getArgumentType(node: Node, index: int) -> Type:
annotation = node.getChild('annotation')
if annotation:
return getAnnotationType(node.parentScope, annotation)
elif index == 0:
return node.parentScope.classType
def getFunctionReturnType(node: Node) -> str:
name = node.getChild('name')
if name == '__init__':
return Type('void')
return_ = node.getChild('returns')
if return_:
if return_.nodeType == NodeType.NAME:
typeName = return_.getText()
return getType(node.parentScope, typeName)
elif return_.nodeType in [NodeType.LIST, NodeType.TUPLE]:
genericType = getType(node.parentScope, BuiltInType.TUPLE)
elts = return_.getChild('elts')
elementTypes = [getType(node.parentScope, elt.getText()) for elt in elts]
return Type(genericType=genericType, elementTypes=elementTypes)
elif return_.nodeType == NodeType.SUBSCRIPT:
genericType = getType(node.parentScope, return_.getChild('value').getText())
index = return_.getChild('slice').getChild('value')
if index.nodeType == NodeType.NAME:
elementTypes = [getType(node.parentScope, index.getText())]
elif index.nodeType == NodeType.TUPLE:
elts = index.getChild('elts')
elementTypes = [getType(node.parentScope, elt.getText()) for elt in elts]
else:
raise Exception('Unknown node type:' + index.nodeType)
return Type(genericType=genericType, elementTypes=elementTypes)
else:
raise Exception('Unknown node type:' + return_.nodeType)
def getExpressionType(node: Node) -> str:
if node.nodeType in [NodeType.NUM, NodeType.CONSTANT]:
val = node.getText()
if val.isdigit():
return Type(BuiltInType.INT32)
elif val.count('.') == 1 and val.replace('.', '').isdigit():
return Type(BuiltInType.FLOAT64)
elif val in ['True', 'False']:
return Type(BuiltInType.BOOL)
raise Exception('Cannot determine type for: ' , val)
if node.nodeType in NodeType.NAME:
name = node.getText()
if name in KEYWORDS:
return None
symbol = node.parentScope.findNested(name)
node.symbol = symbol
return symbol.resolvedType
if node.nodeType == NodeType.ATTR:
var = node.getChild('value')
resolve(var, node.parentScope)
type_ = var.resolvedType
genericType = type_.genericType
typeName = type_.name
genericTypeName = genericType.name if genericType else ''
attr = node.getChild('attr')
field = type_.getField(attr)
if field:
return field.type_
elif genericTypeName == BuiltInType.LIST:
if attr == 'get':
return type_.elementTypes[0]
elif genericTypeName == BuiltInType.DICT:
listType = getType(node.parentScope, BuiltInType.LIST)
if attr == 'keys':
return Type(genericType=listType, elementTypes=[type_.elementTypes[0]])
if attr == 'values':
return Type(genericType=listType, elementTypes=[type_.elementTypes[1]])
if attr == 'items':
dictItemType = getType(node.parentScope, BuiltInType.DICT_ITEM)
dictItemType = Type(genericType=dictItemType, elementTypes=type_.elementTypes)
return Type(genericType=listType, elementTypes=[dictItemType])
else:
raise Exception('Unknown attribute: ' + attr)
if node.nodeType == NodeType.CALL:
func = node.getChild('func')
if func.nodeType == NodeType.NAME:
func_name = func.getText()
if func_name == 'print':
return getType(node.parentScope, BuiltInType.VOID)
if func_name == 'range':
rangeType = getType(node.parentScope, BuiltInType.RANGE)
intType = Type(BuiltInType.INT32)
return Type(genericType=rangeType, elementTypes=[intType])
if func_name == 'enumerate':
args = node.getChild('args')
assert(len(args) == 1)
resolve(args[0], node.parentScope)
intType = Type(BuiltInType.INT32)
listType = getType(node.parentScope, BuiltInType.ENUMERATE)
tupleType = getType(node.parentScope, BuiltInType.TUPLE)
tupleType = Type(genericType=tupleType, elementTypes=[intType, args[0].resolvedType.elementTypes[0]])
return Type(genericType=listType, elementTypes=[tupleType])
if func_name in ['any', 'all']:
return Type(BuiltInType.BOOL)
if func_name == 'next':
args = node.getChild('args')
for arg in args:
resolve(arg, node.parentScope)
return args[0].resolvedType.elementTypes[0]
if func_name == 'zip':
args = node.getChild('args')
for arg in args:
resolve(arg, node.parentScope)
elementTypes=[arg.resolvedType.elementTypes[0] for arg in args]
listType = getType(node.parentScope, BuiltInType.ZIP)
tupleType = getType(node.parentScope, BuiltInType.TUPLE)
tupleType = Type(genericType=tupleType, elementTypes=elementTypes)
return Type(genericType=listType, elementTypes=[tupleType])
resolve(func, node.parentScope)
return func.resolvedType
elif func.nodeType == NodeType.ATTR:
var = func.getChild('value')
resolve(var, node.parentScope)
attr = func.getChild('attr')
if attr == 'get' and var.resolvedType.genericType and var.resolvedType.genericType.name == 'List':
return var.resolvedType.elementTypes[0]
symbol = node.parentScope.findNested(var.resolvedType.name + '__' + attr)
return symbol.resolvedType
else:
raise Exception('Unknown node type: ' + func.nodeType)
if node.nodeType == NodeType.IF_EXP:
left = node.getChild('body').getChild('items')
right = node.getChild('orelse').getChild('items')
resolve(left, node.parentScope)
resolve(right, node.parentScope)
return combineType(left.resolvedType, right.resolvedType)
if node.nodeType == NodeType.BIN_OP:
left = node.getChild('left')
right = node.getChild('right')
resolve(left, node.parentScope)
resolve(right, node.parentScope)
return combineType(left.resolvedType, right.resolvedType)
if node.nodeType == NodeType.UNARY_OP:
operand = node.getChild('operand')
op = node.getChild('op').nodeType
resolve(operand, node.parentScope)
return operand.resolvedType
if node.nodeType in [NodeType.BOOL_OP, NodeType.COMPARE]:
return Type(BuiltInType.BOOL)
if node.nodeType in [NodeType.LIST, NodeType.SET]:
genericType = getType(node.parentScope, node.nodeType)
elts = node.getChild('elts')
for elt in elts:
resolve(elt, node.parentScope)
if elts:
elementType = combineListTypes([elt.resolvedType for elt in elts])
return Type(genericType=genericType, elementTypes=[elementType])
return None
if node.nodeType in [NodeType.LIST_COMP, NodeType.GEN_EXP]:
generators = node.getChild('generators')
parentScope = node.parentScope
for gen in generators:
resolve(gen, parentScope)
parentScope = gen.scope
elt = node.getChild('elt')
resolve(elt, parentScope)
listType = getType(parentScope, BuiltInType.LIST)
return Type(genericType=listType, elementTypes=[elt.resolvedType])
if node.nodeType == NodeType.TUPLE:
tupleType = getType(node.parentScope, BuiltInType.TUPLE)
elts = node.getChild('elts')
for elt in elts:
resolve(elt, node.parentScope)
if elts:
return Type(genericType=tupleType, elementTypes=[elt.resolvedType for elt in elts])
return None
if node.nodeType == NodeType.DICT:
dictType = getType(node.parentScope, BuiltInType.DICT)
keys = node.getChild('keys')
values = node.getChild('values')
for k in keys:
resolve(k, node.parentScope)
for v in values:
resolve(v, node.parentScope)
if keys and values:
keyType = combineListTypes([k.resolvedType for k in keys])
valType = combineListTypes([v.resolvedType for v in values])
return Type(genericType=dictType, elementTypes=[keyType, valType])
return None
if node.nodeType == NodeType.SUBSCRIPT:
value = node.getChild('value')
resolve(value, node.parentScope)
type_ = value.resolvedType
if type_.name in GENERIC_TYPES:
genericType = type_
index = node.getChild('slice').getChild('value')
if index.nodeType == NodeType.NAME:
resolve(index, node.parentScope)
return Type(genericType=genericType, elementTypes=[index.resolvedType])
elif index.nodeType == NodeType.TUPLE:
elts = index.getChild('elts')
for elt in elts:
resolve(elt, node.parentScope)
return Type(genericType=genericType, elementTypes=[elt.resolvedType for elt in elts])
elif index.nodeType == NodeType.SUBSCRIPT:
resolve(index, node.parentScope)
return Type(genericType=genericType, elementTypes=[index.resolvedType])
else:
raise Exception('Unknown node type:' + index.nodeType)
else:
genericType = type_.genericType
genericTypeName = genericType.name if genericType else ''
if genericTypeName == BuiltInType.LIST:
if any(x in node.getChild('slice').children for x in ['lower', 'upper', 'step']):
return type_
else:
return type_.elementTypes[0]
if genericType == BuiltInType.DICT:
return type_.elementTypes[1]
if genericTypeName in [BuiltInType.TUPLE, BuiltInType.DICT_ITEM]:
index = node.getChild('slice').getChild('value')
if index.nodeType == NodeType.NUM:
n = index.getChild('n')
if 0 <= n < len(type_.elementTypes):
return type_.elementTypes[n]
else:
raise Exception('Index out of range')
else:
raise Exception('Not support variable index to ' + genericTypeName)
def resolveClass(node: Node, parentScope: Scope):
for child in node.getChildNodes():
if child.nodeType == NodeType.CLASS:
name = child.getChild('name')
type_ = Type(name, True)
symbol = Symbol(name, type_, SymbolKind.CLASS)
child.symbol = symbol
child.resolvedType = type_
parentScope.define(symbol)
scope = Scope(parentScope)
scope.classType = type_
child.scope = scope
def resolveFunction(node: Node, parentScope: Scope):
for child in node.getChildNodes():
if child.nodeType == NodeType.FUNCTION:
child.parentScope = parentScope
name = child.getChild('name')
if parentScope.classType:
if name != '__init__':
name = parentScope.classType.name + '__' + name
else:
name = parentScope.classType.name + name
type_ = getFunctionReturnType(child)
symbol = Symbol(name, type_)
child.symbol = symbol
child.resolvedType = type_
global_ = parentScope
while global_.parent != None:
global_ = global_.parent
global_.define(symbol)
scope = Scope(parentScope)
scope.isFunction = True
scope.classType = parentScope.classType
child.scope = scope
def resolveAssign(parentScope: Scope, target: Node, type_: Type, addVar=True):
target.parentScope = parentScope
target.resolvedType = type_
if target.nodeType == NodeType.NAME:
name = target.getText()
symbol = parentScope.findNested(name)
if not symbol:
symbol = Symbol(name, type_)
parentScope.define(symbol)
target.symbol = symbol
elif target.nodeType == NodeType.TUPLE:
elts = target.getChild('elts')
for i, elt in enumerate(elts):
resolveAssign(parentScope, elt, type_.elementTypes[i], addVar)
elif target.nodeType == NodeType.ATTR:
var_name = target.getChild('value').getText()
var_attr = target.getChild('attr')
tmp = target
while tmp != None and tmp.nodeType != NodeType.FUNCTION:
tmp = tmp.parent
if tmp and tmp.getChild('name') == '__init__':
args = tmp.getChild('args').getChild('args')
if args and args[0].getChild('arg') == var_name:
classType = tmp.parentScope.classType
classType.addField(var_attr, type_)
elif target.nodeType == NodeType.SUBSCRIPT:
pass
else:
raise Exception('Unknown node type:' + target.nodeType)
def resolve(node: Node, parentScope: Scope):
nodeType = node.nodeType
node.parentScope = parentScope
resolveClass(node, parentScope)
resolveFunction(node, parentScope)
if nodeType in [NodeType.FUNCTION, NodeType.CLASS]:
parentScope = node.scope
if not node.resolvedType:
if nodeType in [NodeType.ASSIGN, NodeType.ANN_ASSIGN]:
value = node.getChild('value')
resolve(value, parentScope)
type_ = value.resolvedType
if node.getChild('annotation'):
annotation = node.getChild('annotation')
type_ = getAnnotationType(parentScope, annotation)
if nodeType == NodeType.ASSIGN:
targets = node.getChild('targets')
else:
targets = [node.getChild('target')]
for target in targets:
resolveAssign(parentScope, target, type_)
elif node.isExpression():
node.resolvedType = getExpressionType(node)
elif nodeType == NodeType.WHILE:
scope = Scope(parentScope)
scope.isLoop = True
parentScope = node.scope = scope
elif nodeType in [NodeType.FOR, NodeType.COMP]:
scope = Scope(parentScope)
scope.isLoop = nodeType == NodeType.FOR
parentScope = node.scope = scope
target = node.getChild('target')
iter_ = node.getChild('iter')
resolve(iter_, parentScope)
type_ = iter_.resolvedType
resolveAssign(parentScope, target, type_.elementTypes[0], addVar=False)
elif nodeType == NodeType.BLOCK:
if node.parent.nodeType not in [NodeType.CLASS, NodeType.FUNCTION, NodeType.FOR, NodeType.WHILE]:
scope = Scope(parentScope)
parentScope = node.scope = scope
else:
parentScope = node.scope = node.parent.scope
elif nodeType == NodeType.ARGUMENTS:
args = node.getChild('args')
for i, arg in enumerate(args):
arg.parentScope = parentScope
type_ = getArgumentType(arg, i)
name = arg.getChild('arg')
symbol = Symbol(name, type_)
arg.resolvedType = type_
parentScope.define(symbol)
for child_node in node.getChildNodes():
resolve(child_node, parentScope)
def replaceListComp(node: Node):
if node.nodeType == NodeType.LIST_COMP:
pass
for child_node in node.getChildNodes():
replaceListComp(child_node)
def collectTupleTypes(node: Node, tupleTypes: List[Type]):
type_ = node.resolvedType
genericType = type_.genericType if type_ else None
if genericType and genericType.name == BuiltInType.TUPLE:
if all(x.name != type_.name for x in tupleTypes):
tupleTypes.append(type_)
for child in node.getChildNodes():
collectTupleTypes(child, tupleTypes)
def defineTupleTypes(global_: Node):
tupleTypes = []
collectTupleTypes(global_, tupleTypes)
for type_ in tupleTypes:
symbol = Symbol(type_.name, type_)
global_.scope.define(symbol)
class Compiler:
def __init__(self):
scope = Scope()
global_ = Node(None, 'Global')
global_.children['files'] = []
global_.scope = scope
self.global_ = global_
self.defineNativeTypes()
def defineNativeTypes(self):
for type_ in VALUE_TYPES + REFERENCE_TYPES + [BuiltInType.VOID, BuiltInType.TUPLE]:
is_class = type_ in REFERENCE_TYPES
symbol = Symbol(type_, Type(type_, is_class))
self.global_.scope.define(symbol)
def addInput(self, src: str):
global_ = self.global_
node = toNode(global_, ast.parse(src))
global_.children['files'].append(node)
replaceListComp(global_)
resolve(global_, global_.scope)
defineTupleTypes(global_)
#print(global_)
def cppEmit(self) -> str:
result = CResult()
for file in self.global_.getChildNodes():
result.emitDefinition(file)
for file in self.global_.getChildNodes():
result.emitImplement(file)
return result.code