155
|
1 import ir
|
|
2 from . import astnodes
|
151
|
3
|
155
|
4 def genModule(pkg):
|
|
5 m = ir.Module(pkg.name)
|
|
6 for s in pkg.scope:
|
|
7 if type(s) is astnodes.Variable:
|
|
8 genGlobal(m, s)
|
156
|
9 elif type(s) is astnodes.Function:
|
|
10 genFunction(m, s)
|
|
11 else:
|
|
12 print(s)
|
155
|
13 return m
|
|
14
|
|
15 def genGlobal(m, var):
|
|
16 v = ir.Value()
|
|
17 v.name = var.name
|
|
18 m.Globals.append(v)
|
151
|
19
|
156
|
20 def genFunction(m, fnc):
|
|
21 f = ir.Function()
|
|
22 m.Globals.append(f)
|
|
23
|
151
|
24 class CodeGenerator:
|
|
25 """ Generates intermediate code """
|
|
26 def gencode(self, ast):
|
155
|
27 print('Code generator')
|
|
28 assert type(ast) is astnodes.Package
|
|
29 return genModule(ast)
|
151
|
30
|