annotate python/c3/codegenerator.py @ 156:1b4a85bdd99c

change types
author Windel Bouwman
date Sun, 03 Mar 2013 15:50:34 +0100
parents b28a11c01dbe
children 8f3924b6076e
rev   line source
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
1 import ir
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
2 from . import astnodes
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
3
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
4 def genModule(pkg):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
5 m = ir.Module(pkg.name)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
6 for s in pkg.scope:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
7 if type(s) is astnodes.Variable:
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
8 genGlobal(m, s)
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
9 elif type(s) is astnodes.Function:
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
10 genFunction(m, s)
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
11 else:
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
12 print(s)
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
13 return m
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
14
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
15 def genGlobal(m, var):
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
16 v = ir.Value()
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
17 v.name = var.name
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
18 m.Globals.append(v)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
19
156
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
20 def genFunction(m, fnc):
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
21 f = ir.Function()
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
22 m.Globals.append(f)
1b4a85bdd99c change types
Windel Bouwman
parents: 155
diff changeset
23
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
24 class CodeGenerator:
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
25 """ Generates intermediate code """
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
26 def gencode(self, ast):
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
27 print('Code generator')
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
28 assert type(ast) is astnodes.Package
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 151
diff changeset
29 return genModule(ast)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents:
diff changeset
30