annotate python/c3/typecheck.py @ 150:4ae0e02599de

Added type check start and analyze phase
author Windel Bouwman
date Fri, 01 Mar 2013 16:53:22 +0100
parents
children b73bc14a3aa3
rev   line source
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
1 from .astnodes import BaseType, Variable, Designator, Function
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
2 from .astnodes import CompoundStatement, Assignment, VariableUse
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
3 from .astnodes import Binop, Unop, Constant
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
4 from .astnodes import IfStatement, WhileStatement, ReturnStatement
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
5 from .astnodes import FunctionType, BaseType
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
6 from . import astnodes
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
7 from ppci.errors import CompilerException
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
8 from .scope import topScope
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
9
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
10 class TypeChecker:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
11 def __init__(self, diag):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
12 self.diag = diag
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
13 def err(self, msg, loc):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
14 self.diag.diag(CompilerException(msg, loc))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
15 def checkPackage(self, pkg):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
16 for s in pkg.scope:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
17 self.check(s)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
18 def resolveDesignator(self, d):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
19 if d.scope.hasSymbol(d.tname):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
20 return d.scope.getSymbol(d.tname)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
21 else:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
22 msg = 'Cannot resolve name {0}'.format(d.tname)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
23 self.err(msg, d.loc)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
24 def check(self, sym):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
25 if type(sym) is Variable:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
26 if type(sym.typ) is Designator:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
27 sym.typ = self.resolveDesignator(sym.typ)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
28 elif type(sym) is Function:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
29 for s in sym.scope:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
30 self.check(s)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
31 self.check(sym.typ)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 self.check(sym.body)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
33 elif type(sym) is CompoundStatement:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
34 for s in sym.statements:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
35 self.check(s)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
36 elif type(sym) is IfStatement:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
37 self.check(sym.condition)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
38 print(sym.condition)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
39 self.check(sym.truestatement)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
40 self.check(sym.falsestatement)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
41 elif type(sym) is VariableUse:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
42 if type(sym.target) is Designator:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
43 sym.target = self.resolveDesignator(sym.target)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
44 elif type(sym) is Assignment:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
45 self.check(sym.lval)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
46 self.check(sym.rval)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
47 elif type(sym) is ReturnStatement:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
48 self.check(sym.expr)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
49 elif type(sym) is Constant:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
50 if type(sym.value) is int:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
51 sym.typ = topScope.getSymbol('int')
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
52 elif type(sym) is FunctionType:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
53 if type(sym.returntype) is Designator:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
54 sym.returntype = self.resolveDesignator(sym.returntype)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
55 self.check(sym.returntype)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
56 elif type(sym) is Binop:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
57 self.check(sym.a)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
58 self.check(sym.b)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
59 if type(sym.a) is Constant and type(sym.b) is Constant:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
60 # Possibly fold expression
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
61 pass
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
62