annotate python/c3/semantics.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents b01429a5d695
children 003c8a976fff
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 from . import astnodes
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
2 from .scope import Scope, topScope
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 175
diff changeset
3 from ppci import CompilerError
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 class Semantics:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 """ This class constructs the AST from parser input """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 def __init__(self, diag):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8 self.diag = diag
169
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 167
diff changeset
9 def reinit(self):
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 167
diff changeset
10 # Set mod to empty package:
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 167
diff changeset
11 self.mod = astnodes.Package('unnamed')
ee0d30533dae Added more tests and improved the diagnostic update
Windel Bouwman
parents: 167
diff changeset
12 self.mod.scope = Scope(topScope)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
13 def addSymbol(self, s):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
14 if self.curScope.hasSymbol(s.name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
15 msg = 'Redefinition of {0}'.format(s.name)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 175
diff changeset
16 raise CompilerError(msg, s.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
17 else:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
18 self.curScope.addSymbol(s)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 def handlePackage(self, name, loc):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 self.mod = astnodes.Package(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 self.mod.loc = loc
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
22 self.mod.scope = self.curScope = Scope(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
23 def actOnVarDef(self, name, loc, t, ival):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
24 s = astnodes.Variable(name, t)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
25 s.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
26 self.addSymbol(s)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
27 def actOnConstDef(self, name, loc, t, val):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
28 s = astnodes.Constant(name, t, val)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
29 s.loc = loc
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
30 self.addSymbol(s)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
31 def actOnFuncDef1(self, name, loc):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
32 self.curFunc = astnodes.Function(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
33 self.curFunc.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
34 self.addSymbol(self.curFunc)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
35 self.curScope = self.curFunc.scope = Scope(self.curScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
36 def actOnParameter(self, name, loc, t):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
37 p = astnodes.Variable(name, t)
175
a51b3c956386 Added function call in expressions
Windel Bouwman
parents: 169
diff changeset
38 p.isParameter = True
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
39 p.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
40 self.addSymbol(p)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
41 return p
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
42 def actOnFuncDef2(self, parameters, returntype, body):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
43 self.curFunc.body = body
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
44 paramtypes = [p.typ for p in parameters]
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
45 self.curFunc.typ = astnodes.FunctionType(paramtypes, returntype)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
46 self.curFunc = None
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
47 self.curScope = self.curScope.parent
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
48 def actOnType(self, tok):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
49 # Try to lookup type, in case of failure return void
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
50 pass
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
51 def actOnDesignator(self, tname, loc):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
52 d = astnodes.Designator(tname)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
53 d.scope = self.curScope
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
54 d.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
55 return d
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
56 def actOnBinop(self, lhs, op, rhs, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
57 bo = astnodes.Binop(lhs, op, rhs)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
58 bo.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
59 return bo
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
60 def actOnNumber(self, num, loc):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 152
diff changeset
61 n = astnodes.Literal(num)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
62 n.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
63 return n
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
64 def actOnVariableUse(self, d, loc):
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
65 vu = astnodes.VariableUse(d)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
66 vu.loc = loc
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
67 return vu
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
68 def actOnAssignment(self, lval, rval, loc):
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
69 a = astnodes.Assignment(lval, rval)
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
70 a.loc = loc
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
71 return a
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
72 def actOnFunctionCall(self, func, args, loc):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
73 fc = astnodes.FunctionCall(func, args)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
74 fc.loc = loc
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 165
diff changeset
75 return fc
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
76 def actOnIfStatement(self, cond, yes, no, loc):
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
77 i = astnodes.IfStatement(cond, yes, no)
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
78 i.loc = loc
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
79 return i