Mercurial > lcfOS
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 | 1 from . import astnodes |
150 | 2 from .scope import Scope, topScope |
194 | 3 from ppci import CompilerError |
148 | 4 |
5 class Semantics: | |
6 """ This class constructs the AST from parser input """ | |
7 def __init__(self, diag): | |
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 | 13 def addSymbol(self, s): |
14 if self.curScope.hasSymbol(s.name): | |
15 msg = 'Redefinition of {0}'.format(s.name) | |
194 | 16 raise CompilerError(msg, s.loc) |
150 | 17 else: |
18 self.curScope.addSymbol(s) | |
148 | 19 def handlePackage(self, name, loc): |
20 self.mod = astnodes.Package(name) | |
21 self.mod.loc = loc | |
150 | 22 self.mod.scope = self.curScope = Scope(topScope) |
23 def actOnVarDef(self, name, loc, t, ival): | |
24 s = astnodes.Variable(name, t) | |
25 s.loc = loc | |
26 self.addSymbol(s) | |
163 | 27 def actOnConstDef(self, name, loc, t, val): |
28 s = astnodes.Constant(name, t, val) | |
29 s.loc = loc | |
30 self.addSymbol(s) | |
150 | 31 def actOnFuncDef1(self, name, loc): |
32 self.curFunc = astnodes.Function(name) | |
33 self.curFunc.loc = loc | |
34 self.addSymbol(self.curFunc) | |
35 self.curScope = self.curFunc.scope = Scope(self.curScope) | |
36 def actOnParameter(self, name, loc, t): | |
37 p = astnodes.Variable(name, t) | |
175 | 38 p.isParameter = True |
150 | 39 p.loc = loc |
40 self.addSymbol(p) | |
41 return p | |
42 def actOnFuncDef2(self, parameters, returntype, body): | |
43 self.curFunc.body = body | |
167 | 44 paramtypes = [p.typ for p in parameters] |
45 self.curFunc.typ = astnodes.FunctionType(paramtypes, returntype) | |
150 | 46 self.curFunc = None |
47 self.curScope = self.curScope.parent | |
48 def actOnType(self, tok): | |
49 # Try to lookup type, in case of failure return void | |
50 pass | |
51 def actOnDesignator(self, tname, loc): | |
52 d = astnodes.Designator(tname) | |
53 d.scope = self.curScope | |
54 d.loc = loc | |
55 return d | |
149 | 56 def actOnBinop(self, lhs, op, rhs, loc): |
57 bo = astnodes.Binop(lhs, op, rhs) | |
58 bo.loc = loc | |
59 return bo | |
60 def actOnNumber(self, num, loc): | |
163 | 61 n = astnodes.Literal(num) |
149 | 62 n.loc = loc |
63 return n | |
165 | 64 def actOnVariableUse(self, d, loc): |
164 | 65 vu = astnodes.VariableUse(d) |
165 | 66 vu.loc = loc |
164 | 67 return vu |
68 def actOnAssignment(self, lval, rval, loc): | |
69 a = astnodes.Assignment(lval, rval) | |
70 a.loc = loc | |
71 return a | |
167 | 72 def actOnFunctionCall(self, func, args, loc): |
73 fc = astnodes.FunctionCall(func, args) | |
74 fc.loc = loc | |
75 return fc | |
165 | 76 def actOnIfStatement(self, cond, yes, no, loc): |
77 i = astnodes.IfStatement(cond, yes, no) | |
78 i.loc = loc | |
79 return i |