Mercurial > lcfOS
annotate python/c3/typecheck.py @ 227:82dfe6a32717
Fixed tests
author | Windel Bouwman |
---|---|
date | Fri, 12 Jul 2013 17:42:39 +0200 |
parents | 240111e0456f |
children | 7f18ed9b6b7e |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 from .scope import * | |
3 from .visitor import Visitor | |
4 | |
5 def equalTypes(a, b): | |
226 | 6 """ Compare types a and b for equality. Not equal until proven otherwise. """ |
7 # Recurse into named types: | |
8 if type(a) is DefinedType: | |
9 return equalTypes(a.typ, b) | |
10 if type(b) is DefinedType: | |
11 return equalTypes(a, b.typ) | |
12 # Compare for structural equivalence: | |
13 if type(a) is type(b): | |
14 if type(a) is BaseType: | |
15 return a.name == b.name | |
16 elif type(a) is PointerType: | |
17 return equalTypes(a.ptype, b.ptype) | |
227 | 18 elif type(a) is StructureType: |
19 if len(a.mems) != len(b.mems): | |
20 return False | |
21 for amem, bmem in zip(a.mems, b.mems): | |
22 if not equalTypes(amem.typ, bmem.typ): | |
23 return False | |
24 return True | |
25 else: | |
26 raise Exception('Type compare not implemented') | |
226 | 27 return False |
150 | 28 |
222 | 29 def canCast(fromT, toT): |
30 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): | |
31 return True | |
32 elif fromT is intType and isinstance(toT, PointerType): | |
33 return True | |
34 return False | |
35 | |
150 | 36 class TypeChecker: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
37 def __init__(self, diag): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
38 self.diag = diag |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
39 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
40 def error(self, msg, loc): |
186 | 41 """ Wrapper that registers the message and marks the result invalid """ |
42 self.diag.error(msg, loc) | |
43 self.ok = False | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
44 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
45 def checkPackage(self, pkg): |
186 | 46 self.ok = True |
215 | 47 visitor = Visitor() |
48 visitor.visit(pkg, f_post=self.check2) | |
186 | 49 return self.ok |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
50 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
51 def check2(self, sym): |
225 | 52 if type(sym) in [IfStatement, WhileStatement]: |
53 if not equalTypes(sym.condition.typ, boolType): | |
54 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) | |
55 elif type(sym) is Assignment: | |
221 | 56 if not equalTypes(sym.lval.typ, sym.rval.typ): |
57 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) | |
58 if not sym.lval.lvalue: | |
59 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc) | |
60 #if sym.rval.lvalue: | |
61 # self.error('Right hand side must be an rvalue', sym.rval.loc) | |
225 | 62 elif type(sym) is ReturnStatement: |
63 pass | |
64 elif type(sym) is FunctionCall: | |
166 | 65 if sym.proc: |
167 | 66 # Check arguments: |
67 ngiv = len(sym.args) | |
68 ptypes = sym.proc.typ.parametertypes | |
69 nreq = len(ptypes) | |
70 if ngiv != nreq: | |
186 | 71 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
167 | 72 else: |
73 for a, at in zip(sym.args, ptypes): | |
74 if not equalTypes(a.typ, at): | |
186 | 75 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) |
167 | 76 # determine return type: |
77 sym.typ = sym.proc.typ.returntype | |
78 else: | |
79 sym.typ = intType | |
225 | 80 elif type(sym) is VariableUse: |
221 | 81 sym.lvalue = True |
225 | 82 if type(sym.target) is Variable: |
221 | 83 sym.typ = sym.target.typ |
84 else: | |
225 | 85 print('warning {} has no target, defaulting to int'.format(sym)) |
221 | 86 sym.typ = intType |
225 | 87 elif type(sym) is Literal: |
221 | 88 sym.lvalue = False |
89 if type(sym.val) is int: | |
90 sym.typ = intType | |
91 elif type(sym.val) is float: | |
92 sym.typ = doubleType | |
93 elif type(sym.val) is bool: | |
94 sym.typ = boolType | |
95 else: | |
225 | 96 raise Exception('Unknown literal type'.format(sym.val)) |
97 elif type(sym) is Unop: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
98 if sym.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
99 sym.typ = PointerType(sym.a.typ) |
221 | 100 sym.lvalue = False |
225 | 101 else: |
102 raise Exception('Unknown unop {0}'.format(sym.op)) | |
103 elif type(sym) is Deref: | |
104 # pointer deref | |
105 sym.lvalue = True | |
106 # check if the to be dereferenced variable is a pointer type: | |
226 | 107 ptype = sym.ptr.typ |
108 if type(ptype) is DefinedType: | |
109 ptype = ptype.typ | |
110 if type(ptype) is PointerType: | |
111 sym.typ = ptype.ptype | |
225 | 112 else: |
226 | 113 self.error('Cannot dereference non-pointer type {}'.format(ptype), sym.loc) |
225 | 114 sym.typ = intType |
115 elif type(sym) is FieldRef: | |
116 basetype = sym.base.typ | |
117 sym.lvalue = True | |
226 | 118 if type(basetype) is DefinedType: |
119 basetype = basetype.typ | |
225 | 120 if type(basetype) is StructureType: |
121 if basetype.hasField(sym.field): | |
122 sym.typ = basetype.fieldType(sym.field) | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
123 else: |
226 | 124 self.error('{} does not contain field {}'.format(basetype, sym.field), sym.loc) |
221 | 125 sym.typ = intType |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
126 else: |
225 | 127 self.error('Cannot select field {} of non-structure type {}'.format(sym.field, basetype), sym.loc) |
128 sym.typ = intType | |
129 elif type(sym) is Binop: | |
227 | 130 sym.lvalue = False |
131 if sym.op in ['+', '-', '*', '/']: | |
132 if equalTypes(sym.a.typ, sym.b.typ): | |
133 if equalTypes(sym.a.typ, intType): | |
134 sym.typ = sym.a.typ | |
135 else: | |
136 self.error('Can only add integers', sym.loc) | |
137 sym.typ = intType | |
138 else: | |
139 # assume void here? TODO: throw exception! | |
140 sym.typ = intType | |
141 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
142 elif sym.op in ['>', '<', '==', '<=', '>=']: | |
143 sym.typ = boolType | |
144 if not equalTypes(sym.a.typ, sym.b.typ): | |
145 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
146 elif sym.op in ['or', 'and']: | |
147 sym.typ = boolType | |
148 if not equalTypes(sym.a.typ, boolType): | |
149 self.error('Must be {0}'.format(boolType), sym.a.loc) | |
150 if not equalTypes(sym.b.typ, boolType): | |
151 self.error('Must be {0}'.format(boolType), sym.b.loc) | |
152 elif sym.op in ['|', '&']: | |
153 sym.typ = intType | |
154 sym.lvalue = False | |
155 if equalTypes(sym.a.typ, sym.b.typ): | |
156 if not equalTypes(sym.a.typ, intType): | |
157 self.error('Can only add integers', sym.loc) | |
158 else: | |
159 self.error('Types unequal {} != {}'.format(sym.a.typ, sym.b.typ), sym.loc) | |
163 | 160 else: |
227 | 161 raise Exception('Unknown binop {0}'.format(sym.op)) |
225 | 162 elif type(sym) is Variable: |
165 | 163 # check initial value type: |
164 # TODO | |
165 pass | |
225 | 166 elif type(sym) is TypeCast: |
222 | 167 if canCast(sym.a.typ, sym.to_type): |
168 sym.typ = sym.to_type | |
169 else: | |
170 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type)) | |
225 | 171 elif type(sym) is Constant: |
172 if not equalTypes(sym.typ, sym.value.typ): | |
173 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) | |
174 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement, DefinedType]: | |
165 | 175 pass |
225 | 176 else: |
186 | 177 raise Exception('Unknown type check {0}'.format(sym)) |
150 | 178 |