annotate python/c3/semantics.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents
children 74241ca312cc
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
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
8 def getType(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 t = self.getSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 print(t)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 assert isinstance(t, astnodes.Type)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 return t
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13 def getSymbol(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 if name in self.symbols:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 return self.symbols[name]
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
16 # Look for symbol:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 if self.parent:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 return self.parent.getSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
19 raise CompilerException("Symbol {0} not found".format(name), name.loc)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 def hasSymbol(self, name):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
21 if name in self.symbols:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
22 return True
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
23 if self.parent:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
24 return self.parent.hasSymbol(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
25 return False
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
26
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 def addSymbol(self, sym):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28 self.symbols[sym.name] = sym
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
29
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 def createBuiltins(scope):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
31 scope.addSymbol(astnodes.BaseType('int'))
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
32
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
33 class Semantics:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
34 """ This class constructs the AST from parser input """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
35 def __init__(self, diag):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 self.diag = diag
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 def handlePackage(self, name, loc):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38 self.mod = astnodes.Package(name)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
39 self.mod.loc = loc
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
40 self.mod.scope = self.curScope = Scope()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
41 createBuiltins(self.curScope)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 def handleBinop(self, lhs, op, rhs):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
44 def actOnLocal(self, t, name, ival):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
45 s = astnodes.Variable(name, t, False)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
46 self.curScope.addSymbol(s)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
47 def actOnType(self, tok):
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
48 # Try to lookup type, in case of failure return void
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
49 pass
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
50