comparison python/c3/typecheck.py @ 165:598d3888a11c

Added front class and fided AST view
author Windel Bouwman
date Fri, 22 Mar 2013 15:12:38 +0100
parents e023d3ce1d63
children da0087b82fbe
comparison
equal deleted inserted replaced
164:e023d3ce1d63 165:598d3888a11c
18 def precheck(self, sym): 18 def precheck(self, sym):
19 pass 19 pass
20 def check2(self, sym): 20 def check2(self, sym):
21 if type(sym) is Function: 21 if type(sym) is Function:
22 pass 22 pass
23 elif type(sym) is IfStatement: 23 elif type(sym) in [IfStatement, WhileStatement]:
24 print(sym.condition)
25 if not equalTypes(sym.condition.typ, boolType): 24 if not equalTypes(sym.condition.typ, boolType):
26 self.diag.error('Condition must be a boolean expression', sym.condition.loc) 25 self.diag.error('Condition must be a boolean expression', sym.condition.loc)
27 elif type(sym) is Assignment: 26 elif type(sym) is Assignment:
28 if not equalTypes(sym.lval.typ, sym.rval.typ): 27 if not equalTypes(sym.lval.typ, sym.rval.typ):
29 self.diag.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) 28 self.diag.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
36 sym.typ = sym.proc.typ.returntype 35 sym.typ = sym.proc.typ.returntype
37 elif type(sym) is VariableUse: 36 elif type(sym) is VariableUse:
38 if sym.target: 37 if sym.target:
39 sym.typ = sym.target.typ 38 sym.typ = sym.target.typ
40 else: 39 else:
41 sym.typ = voidType 40 sym.typ = intType
42 elif type(sym) is Literal: 41 elif type(sym) is Literal:
43 if type(sym.val) is int: 42 if type(sym.val) is int:
44 sym.typ = intType 43 sym.typ = intType
45 elif type(sym.val) is float: 44 elif type(sym.val) is float:
46 sym.typ = doubleType 45 sym.typ = doubleType
49 elif type(sym) is Binop: 48 elif type(sym) is Binop:
50 if sym.op in ['+', '-', '*', '/']: 49 if sym.op in ['+', '-', '*', '/']:
51 if equalTypes(sym.a.typ, sym.b.typ): 50 if equalTypes(sym.a.typ, sym.b.typ):
52 sym.typ = sym.a.typ 51 sym.typ = sym.a.typ
53 else: 52 else:
54 # assume void here? 53 # assume void here? TODO: throw exception!
55 sym.typ = voidType 54 sym.typ = intType
56 self.diag.error('Types unequal', sym.loc) 55 self.diag.error('Types unequal', sym.loc)
57 elif sym.op in ['>', '<']: 56 elif sym.op in ['>', '<']:
58 if equalTypes(sym.a.typ, sym.b.typ): 57 sym.typ = boolType
59 sym.typ = boolType 58 if not equalTypes(sym.a.typ, sym.b.typ):
60 else:
61 sym.typ = voidType
62 self.diag.error('Types unequal', sym.loc) 59 self.diag.error('Types unequal', sym.loc)
63 else: 60 else:
64 sym.typ = voidType 61 sym.typ = voidType
65 print('unknown binop', sym.op) 62 print('unknown binop', sym.op)
63 elif type(sym) is Variable:
64 # check initial value type:
65 # TODO
66 pass
67 elif type(sym) in [EmptyStatement, CompoundStatement, Package]:
68 pass
66 else: 69 else:
67 print('Unknown type check', sym) 70 print('Unknown type check', sym)
68 71