comparison 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
comparison
equal deleted inserted replaced
148:e5263f74b287 149:74241ca312cc
3 class Scope: 3 class Scope:
4 """ A scope contains all symbols in a scope """ 4 """ A scope contains all symbols in a scope """
5 def __init__(self, parent=None): 5 def __init__(self, parent=None):
6 self.symbols = {} 6 self.symbols = {}
7 self.parent = parent 7 self.parent = parent
8 def __iter__(self):
9 return iter(self.symbols.values())
8 def getType(self, name): 10 def getType(self, name):
9 t = self.getSymbol(name) 11 t = self.getSymbol(name)
10 print(t) 12 print(t)
11 assert isinstance(t, astnodes.Type) 13 assert isinstance(t, astnodes.Type)
12 return t 14 return t
37 def handlePackage(self, name, loc): 39 def handlePackage(self, name, loc):
38 self.mod = astnodes.Package(name) 40 self.mod = astnodes.Package(name)
39 self.mod.loc = loc 41 self.mod.loc = loc
40 self.mod.scope = self.curScope = Scope() 42 self.mod.scope = self.curScope = Scope()
41 createBuiltins(self.curScope) 43 createBuiltins(self.curScope)
42 def handleBinop(self, lhs, op, rhs): 44 def actOnBinop(self, lhs, op, rhs, loc):
43 pass 45 bo = astnodes.Binop(lhs, op, rhs)
44 def actOnLocal(self, t, name, ival): 46 bo.loc = loc
45 s = astnodes.Variable(name, t, False) 47 return bo
48 def actOnNumber(self, num, loc):
49 n = astnodes.Constant(num)
50 n.loc = loc
51 return n
52 def actOnVarDef(self, name, loc, t, ival):
53 s = astnodes.Variable(name, t)
54 s.loc = loc
46 self.curScope.addSymbol(s) 55 self.curScope.addSymbol(s)
56 def actOnFuncDef1(self, name, loc):
57 self.curFunc = astnodes.Procedure(name)
58 self.curFunc.loc = loc
59 self.curScope.addSymbol(self.curFunc)
60 self.curScope = self.curFunc.scope = Scope(self.curScope)
61 def actOnFuncDef2(self, parameters, returntype, body):
62 self.curFunc.body = body
63 self.curFunc.typ = astnodes.FunctionType(parameters, returntype)
64 self.curFunc = None
65 self.curScope = self.curScope.parent
47 def actOnType(self, tok): 66 def actOnType(self, tok):
48 # Try to lookup type, in case of failure return void 67 # Try to lookup type, in case of failure return void
49 pass 68 pass
50 69