Mercurial > lcfOS
comparison python/c3/codegenerator.py @ 157:8f3924b6076e
Added some code generator things
author | Windel Bouwman |
---|---|
date | Sun, 03 Mar 2013 18:14:35 +0100 |
parents | 1b4a85bdd99c |
children | 9683a4cd848f |
comparison
equal
deleted
inserted
replaced
156:1b4a85bdd99c | 157:8f3924b6076e |
---|---|
16 v = ir.Value() | 16 v = ir.Value() |
17 v.name = var.name | 17 v.name = var.name |
18 m.Globals.append(v) | 18 m.Globals.append(v) |
19 | 19 |
20 def genFunction(m, fnc): | 20 def genFunction(m, fnc): |
21 f = ir.Function() | 21 ft = genType(fnc.typ) |
22 f = ir.Function(fnc.name, ft) | |
22 m.Globals.append(f) | 23 m.Globals.append(f) |
24 bb = ir.BasicBlock() | |
25 f.BasicBlocks.append(bb) | |
26 genCode(bb, fnc.body) | |
27 | |
28 def genCode(bb, code): | |
29 if type(code) is astnodes.CompoundStatement: | |
30 for s in code.statements: | |
31 genCode(bb, s) | |
32 elif type(code) is astnodes.Assignment: | |
33 genCode(bb, code.rval) | |
34 print('assign') | |
35 elif type(code) is astnodes.IfStatement: | |
36 genCode(bb, code.condition) | |
37 genCode(bb, code.truestatement) | |
38 print('If!') | |
39 elif type(code) is astnodes.Binop: | |
40 genCode(bb, code.a) | |
41 genCode(bb, code.b) | |
42 a = 1 | |
43 b = 2 | |
44 if code.op == '+': | |
45 bb.Instructions.append(ir.AddInstruction(a, b)) | |
46 else: | |
47 bb.Instructions.append(ir.BinaryOperator(code.op, a, b)) | |
48 elif type(code) is astnodes.Constant: | |
49 print('CST') | |
50 bb.Instructions.append(ir.ImmLoadInstruction(code.value)) | |
51 else: | |
52 print('Unknown:', code) | |
53 | |
54 def genType(t): | |
55 return ir.Type() | |
23 | 56 |
24 class CodeGenerator: | 57 class CodeGenerator: |
25 """ Generates intermediate code """ | 58 """ Generates intermediate code """ |
26 def gencode(self, ast): | 59 def gencode(self, ast): |
27 print('Code generator') | |
28 assert type(ast) is astnodes.Package | 60 assert type(ast) is astnodes.Package |
29 return genModule(ast) | 61 return genModule(ast) |
30 | 62 |