annotate python/c3/semantics.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents 74241ca312cc
children b73bc14a3aa3
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
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
3 from ppci.errors import CompilerException
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
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
9 def addSymbol(self, s):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
10 if self.curScope.hasSymbol(s.name):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
11 msg = 'Redefinition of {0}'.format(s.name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
12 self.diag.diag(CompilerException(msg, s.loc))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
13 else:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
14 self.curScope.addSymbol(s)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 def handlePackage(self, name, loc):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 self.mod = astnodes.Package(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 self.mod.loc = loc
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
18 self.mod.scope = self.curScope = Scope(topScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
19 def actOnVarDef(self, name, loc, t, ival):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
20 s = astnodes.Variable(name, t)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
21 s.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
22 self.addSymbol(s)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
23 def actOnFuncDef1(self, name, loc):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
24 self.curFunc = astnodes.Function(name)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
25 self.curFunc.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
26 self.addSymbol(self.curFunc)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
27 self.curScope = self.curFunc.scope = Scope(self.curScope)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
28 def actOnParameter(self, name, loc, t):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
29 p = astnodes.Variable(name, t)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
30 p.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
31 p.parameter = True
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
32 self.addSymbol(p)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
33 return p
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
34 def actOnFuncDef2(self, parameters, returntype, body):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
35 self.curFunc.body = body
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
36 self.curFunc.typ = astnodes.FunctionType(parameters, returntype)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
37 self.curFunc = None
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
38 self.curScope = self.curScope.parent
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
39 def actOnType(self, tok):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
40 # Try to lookup type, in case of failure return void
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
41 pass
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
42 def actOnDesignator(self, tname, loc):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
43 d = astnodes.Designator(tname)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
44 d.scope = self.curScope
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
45 d.loc = loc
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
46 return d
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
47 def actOnBinop(self, lhs, op, rhs, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
48 bo = astnodes.Binop(lhs, op, rhs)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
49 bo.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
50 return bo
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
51 def actOnNumber(self, num, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
52 n = astnodes.Constant(num)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
53 n.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
54 return n
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
55 def actOnVariableUse(self, d):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
56 return astnodes.VariableUse(d)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
57