comparison python/c3/typecheck.py @ 221:848c4b15fd0b

pointers
author Windel Bouwman
date Mon, 08 Jul 2013 22:21:44 +0200
parents 3f6c30a5d234
children c3f1ce8b638f
comparison
equal deleted inserted replaced
220:3f6c30a5d234 221:848c4b15fd0b
29 def check2(self, sym): 29 def check2(self, sym):
30 if type(sym) in [IfStatement, WhileStatement]: 30 if type(sym) in [IfStatement, WhileStatement]:
31 if not equalTypes(sym.condition.typ, boolType): 31 if not equalTypes(sym.condition.typ, boolType):
32 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) 32 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc)
33 elif type(sym) is Assignment: 33 elif type(sym) is Assignment:
34 if type(sym.lval.typ) is PointerType and sym.rval.typ == intType: 34 if not equalTypes(sym.lval.typ, sym.rval.typ):
35 print('special case, int to pointer is ok for now') 35 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
36 # TODO: add cast instruction? 36 if not sym.lval.lvalue:
37 elif not equalTypes(sym.lval.typ, sym.rval.typ): 37 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc)
38 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) 38 #if sym.rval.lvalue:
39 # self.error('Right hand side must be an rvalue', sym.rval.loc)
39 elif type(sym) is ReturnStatement: 40 elif type(sym) is ReturnStatement:
40 pass 41 pass
41 elif type(sym) is FunctionCall: 42 elif type(sym) is FunctionCall:
42 if sym.proc: 43 if sym.proc:
43 # Check arguments: 44 # Check arguments:
53 # determine return type: 54 # determine return type:
54 sym.typ = sym.proc.typ.returntype 55 sym.typ = sym.proc.typ.returntype
55 else: 56 else:
56 sym.typ = intType 57 sym.typ = intType
57 elif type(sym) is VariableUse: 58 elif type(sym) is VariableUse:
58 if sym.target: 59 sym.lvalue = True
59 sym.typ = sym.target.typ 60 if sym.target:
60 else: 61 sym.typ = sym.target.typ
61 sym.typ = intType 62 else:
63 sym.typ = intType
62 elif type(sym) is Literal: 64 elif type(sym) is Literal:
63 if type(sym.val) is int: 65 sym.lvalue = False
64 sym.typ = intType 66 if type(sym.val) is int:
65 elif type(sym.val) is float: 67 sym.typ = intType
66 sym.typ = doubleType 68 elif type(sym.val) is float:
67 elif type(sym.val) is bool: 69 sym.typ = doubleType
68 sym.typ = boolType 70 elif type(sym.val) is bool:
69 else: 71 sym.typ = boolType
70 self.error('Unknown literal type', sym.loc) 72 else:
73 self.error('Unknown literal type', sym.loc)
71 elif type(sym) is Unop: 74 elif type(sym) is Unop:
72 if sym.op == '&': 75 if sym.op == '&':
73 sym.typ = PointerType(sym.a.typ) 76 sym.typ = PointerType(sym.a.typ)
77 sym.lvalue = False
74 elif sym.op == '*': 78 elif sym.op == '*':
75 # pointer deref 79 # pointer deref
80 sym.lvalue = True
76 if type(sym.a.typ) is PointerType: 81 if type(sym.a.typ) is PointerType:
77 sym.typ = sym.a.typ.ptype 82 sym.typ = sym.a.typ.ptype
78 else: 83 else:
84 sym.typ = intType
79 self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc) 85 self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc)
80 else: 86 else:
81 print('unknown unop', sym.op) 87 print('unknown unop', sym.op)
82 elif type(sym) is Binop: 88 elif type(sym) is Binop:
89 sym.lvalue = False
83 if sym.op in ['+', '-', '*', '/']: 90 if sym.op in ['+', '-', '*', '/']:
84 if equalTypes(sym.a.typ, sym.b.typ): 91 if equalTypes(sym.a.typ, sym.b.typ):
85 if equalTypes(sym.a.typ, intType): 92 if equalTypes(sym.a.typ, intType):
86 sym.typ = sym.a.typ 93 sym.typ = sym.a.typ
87 else: 94 else:
99 sym.typ = boolType 106 sym.typ = boolType
100 if not equalTypes(sym.a.typ, boolType): 107 if not equalTypes(sym.a.typ, boolType):
101 self.error('Must be {0}'.format(boolType), sym.a.loc) 108 self.error('Must be {0}'.format(boolType), sym.a.loc)
102 if not equalTypes(sym.b.typ, boolType): 109 if not equalTypes(sym.b.typ, boolType):
103 self.error('Must be {0}'.format(boolType), sym.b.loc) 110 self.error('Must be {0}'.format(boolType), sym.b.loc)
111 elif sym.op in ['|', '&']:
112 sym.typ = intType
113 # TODO: elaborate?
104 else: 114 else:
105 sym.typ = voidType 115 sym.typ = voidType
106 print('unknown binop', sym.op) 116 print('unknown binop', sym.op)
107 elif type(sym) is Variable: 117 elif type(sym) is Variable:
108 # check initial value type: 118 # check initial value type: