Mercurial > lcfOS
annotate python/c3/semantics.py @ 170:4348da5ca307
Cleanup of ir dir
author | Windel Bouwman |
---|---|
date | Fri, 29 Mar 2013 17:33:17 +0100 |
parents | ee0d30533dae |
children | a51b3c956386 |
rev | line source |
---|---|
148 | 1 from . import astnodes |
150 | 2 from .scope import Scope, topScope |
148 | 3 |
4 class Semantics: | |
5 """ This class constructs the AST from parser input """ | |
6 def __init__(self, diag): | |
7 self.diag = diag | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
167
diff
changeset
|
8 def reinit(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
167
diff
changeset
|
9 # Set mod to empty package: |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
167
diff
changeset
|
10 self.mod = astnodes.Package('unnamed') |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
167
diff
changeset
|
11 self.mod.scope = Scope(topScope) |
150 | 12 def addSymbol(self, s): |
13 if self.curScope.hasSymbol(s.name): | |
14 msg = 'Redefinition of {0}'.format(s.name) | |
152 | 15 self.diag.error(msg, s.loc) |
150 | 16 else: |
17 self.curScope.addSymbol(s) | |
148 | 18 def handlePackage(self, name, loc): |
19 self.mod = astnodes.Package(name) | |
20 self.mod.loc = loc | |
150 | 21 self.mod.scope = self.curScope = Scope(topScope) |
22 def actOnVarDef(self, name, loc, t, ival): | |
23 s = astnodes.Variable(name, t) | |
24 s.loc = loc | |
25 self.addSymbol(s) | |
163 | 26 def actOnConstDef(self, name, loc, t, val): |
27 s = astnodes.Constant(name, t, val) | |
28 s.loc = loc | |
29 self.addSymbol(s) | |
150 | 30 def actOnFuncDef1(self, name, loc): |
31 self.curFunc = astnodes.Function(name) | |
32 self.curFunc.loc = loc | |
33 self.addSymbol(self.curFunc) | |
34 self.curScope = self.curFunc.scope = Scope(self.curScope) | |
35 def actOnParameter(self, name, loc, t): | |
36 p = astnodes.Variable(name, t) | |
37 p.loc = loc | |
38 p.parameter = True | |
39 self.addSymbol(p) | |
40 return p | |
41 def actOnFuncDef2(self, parameters, returntype, body): | |
42 self.curFunc.body = body | |
167 | 43 paramtypes = [p.typ for p in parameters] |
44 self.curFunc.typ = astnodes.FunctionType(paramtypes, returntype) | |
150 | 45 self.curFunc = None |
46 self.curScope = self.curScope.parent | |
47 def actOnType(self, tok): | |
48 # Try to lookup type, in case of failure return void | |
49 pass | |
50 def actOnDesignator(self, tname, loc): | |
51 d = astnodes.Designator(tname) | |
52 d.scope = self.curScope | |
53 d.loc = loc | |
54 return d | |
149 | 55 def actOnBinop(self, lhs, op, rhs, loc): |
56 bo = astnodes.Binop(lhs, op, rhs) | |
57 bo.loc = loc | |
58 return bo | |
59 def actOnNumber(self, num, loc): | |
163 | 60 n = astnodes.Literal(num) |
149 | 61 n.loc = loc |
62 return n | |
165 | 63 def actOnVariableUse(self, d, loc): |
164 | 64 vu = astnodes.VariableUse(d) |
165 | 65 vu.loc = loc |
164 | 66 return vu |
67 def actOnAssignment(self, lval, rval, loc): | |
68 a = astnodes.Assignment(lval, rval) | |
69 a.loc = loc | |
70 return a | |
167 | 71 def actOnFunctionCall(self, func, args, loc): |
72 fc = astnodes.FunctionCall(func, args) | |
73 fc.loc = loc | |
74 return fc | |
165 | 75 def actOnIfStatement(self, cond, yes, no, loc): |
76 i = astnodes.IfStatement(cond, yes, no) | |
77 i.loc = loc | |
78 return i |