annotate python/c3/semantics.py @ 149:74241ca312cc

Fixes on parser and semantics
author Windel Bouwman
date Fri, 01 Mar 2013 11:43:52 +0100
parents e5263f74b287
children 4ae0e02599de
rev   line source
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
1 from . import astnodes
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
2
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3 class Scope:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4 """ A scope contains all symbols in a scope """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5 def __init__(self, parent=None):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
6 self.symbols = {}
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
7 self.parent = parent
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
8 def __iter__(self):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
9 return iter(self.symbols.values())
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 def getType(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 t = self.getSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 print(t)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 assert isinstance(t, astnodes.Type)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 return t
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 def getSymbol(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 if name in self.symbols:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 return self.symbols[name]
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 # Look for symbol:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 if self.parent:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 return self.parent.getSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 raise CompilerException("Symbol {0} not found".format(name), name.loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 def hasSymbol(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 if name in self.symbols:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 return True
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 if self.parent:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26 return self.parent.hasSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29 def addSymbol(self, sym):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 self.symbols[sym.name] = sym
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32 def createBuiltins(scope):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 scope.addSymbol(astnodes.BaseType('int'))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 class Semantics:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 """ This class constructs the AST from parser input """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 def __init__(self, diag):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 self.diag = diag
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 def handlePackage(self, name, loc):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 self.mod = astnodes.Package(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 self.mod.loc = loc
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 self.mod.scope = self.curScope = Scope()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 createBuiltins(self.curScope)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
44 def actOnBinop(self, lhs, op, rhs, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
45 bo = astnodes.Binop(lhs, op, rhs)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
46 bo.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
47 return bo
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
48 def actOnNumber(self, num, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
49 n = astnodes.Constant(num)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
50 n.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
51 return n
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
52 def actOnVarDef(self, name, loc, t, ival):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
53 s = astnodes.Variable(name, t)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
54 s.loc = loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
55 self.curScope.addSymbol(s)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
56 def actOnFuncDef1(self, name, loc):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
57 self.curFunc = astnodes.Procedure(name)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
58 self.curFunc.loc = loc
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
59 self.curScope.addSymbol(self.curFunc)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
60 self.curScope = self.curFunc.scope = Scope(self.curScope)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
61 def actOnFuncDef2(self, parameters, returntype, body):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
62 self.curFunc.body = body
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
63 self.curFunc.typ = astnodes.FunctionType(parameters, returntype)
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
64 self.curFunc = None
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
65 self.curScope = self.curScope.parent
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
66 def actOnType(self, tok):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
67 # Try to lookup type, in case of failure return void
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
69