comparison python/c3/typecheck.py @ 152:b73bc14a3aa3

Light coupling ide and c3 frontend
author Windel Bouwman
date Sat, 02 Mar 2013 09:56:12 +0100
parents 4ae0e02599de
children b28a11c01dbe
comparison
equal deleted inserted replaced
151:afc8c0207984 152:b73bc14a3aa3
2 from .astnodes import CompoundStatement, Assignment, VariableUse 2 from .astnodes import CompoundStatement, Assignment, VariableUse
3 from .astnodes import Binop, Unop, Constant 3 from .astnodes import Binop, Unop, Constant
4 from .astnodes import IfStatement, WhileStatement, ReturnStatement 4 from .astnodes import IfStatement, WhileStatement, ReturnStatement
5 from .astnodes import FunctionType, BaseType 5 from .astnodes import FunctionType, BaseType
6 from . import astnodes 6 from . import astnodes
7 from ppci.errors import CompilerException
8 from .scope import topScope 7 from .scope import topScope
9 8
10 class TypeChecker: 9 class TypeChecker:
11 def __init__(self, diag): 10 def __init__(self, diag):
12 self.diag = diag 11 self.diag = diag
13 def err(self, msg, loc):
14 self.diag.diag(CompilerException(msg, loc))
15 def checkPackage(self, pkg): 12 def checkPackage(self, pkg):
16 for s in pkg.scope: 13 for s in pkg.scope:
17 self.check(s) 14 self.check(s)
18 def resolveDesignator(self, d): 15 def resolveDesignator(self, d):
19 if d.scope.hasSymbol(d.tname): 16 if d.scope.hasSymbol(d.tname):
20 return d.scope.getSymbol(d.tname) 17 return d.scope.getSymbol(d.tname)
21 else: 18 else:
22 msg = 'Cannot resolve name {0}'.format(d.tname) 19 msg = 'Cannot resolve name {0}'.format(d.tname)
23 self.err(msg, d.loc) 20 self.diag.error(msg, d.loc)
24 def check(self, sym): 21 def check(self, sym):
25 if type(sym) is Variable: 22 if type(sym) is Variable:
26 if type(sym.typ) is Designator: 23 if type(sym.typ) is Designator:
27 sym.typ = self.resolveDesignator(sym.typ) 24 sym.typ = self.resolveDesignator(sym.typ)
28 elif type(sym) is Function: 25 elif type(sym) is Function: