Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
147:4e79484a9d47 | 148:e5263f74b287 |
---|---|
1 from . import astnodes | |
2 | |
3 class Scope: | |
4 """ A scope contains all symbols in a scope """ | |
5 def __init__(self, parent=None): | |
6 self.symbols = {} | |
7 self.parent = parent | |
8 def getType(self, name): | |
9 t = self.getSymbol(name) | |
10 print(t) | |
11 assert isinstance(t, astnodes.Type) | |
12 return t | |
13 def getSymbol(self, name): | |
14 if name in self.symbols: | |
15 return self.symbols[name] | |
16 # Look for symbol: | |
17 if self.parent: | |
18 return self.parent.getSymbol(name) | |
19 raise CompilerException("Symbol {0} not found".format(name), name.loc) | |
20 def hasSymbol(self, name): | |
21 if name in self.symbols: | |
22 return True | |
23 if self.parent: | |
24 return self.parent.hasSymbol(name) | |
25 return False | |
26 | |
27 def addSymbol(self, sym): | |
28 self.symbols[sym.name] = sym | |
29 | |
30 def createBuiltins(scope): | |
31 scope.addSymbol(astnodes.BaseType('int')) | |
32 | |
33 class Semantics: | |
34 """ This class constructs the AST from parser input """ | |
35 def __init__(self, diag): | |
36 self.diag = diag | |
37 def handlePackage(self, name, loc): | |
38 self.mod = astnodes.Package(name) | |
39 self.mod.loc = loc | |
40 self.mod.scope = self.curScope = Scope() | |
41 createBuiltins(self.curScope) | |
42 def handleBinop(self, lhs, op, rhs): | |
43 pass | |
44 def actOnLocal(self, t, name, ival): | |
45 s = astnodes.Variable(name, t, False) | |
46 self.curScope.addSymbol(s) | |
47 def actOnType(self, tok): | |
48 # Try to lookup type, in case of failure return void | |
49 pass | |
50 |