Mercurial > lcfOS
annotate python/c3/typecheck.py @ 220:3f6c30a5d234
Major change in expression parsing to enable pointers and structs
author | Windel Bouwman |
---|---|
date | Sat, 06 Jul 2013 21:32:20 +0200 |
parents | c1ccb1cb4cef |
children | 848c4b15fd0b |
rev | line source |
---|---|
163 | 1 from .astnodes import * |
2 from .scope import * | |
3 from .visitor import Visitor | |
4 | |
5 def equalTypes(a, b): | |
6 """ Compare types a and b for equality. Not equal until proven otherwise. """ | |
7 if type(a) is type(b): | |
8 if type(a) is BaseType: | |
9 return a.name == b.name | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
10 elif type(a) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
11 return equalTypes(a.ptype, b.ptype) |
163 | 12 return False |
150 | 13 |
14 class TypeChecker: | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
15 def __init__(self, diag): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
16 self.diag = diag |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
17 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
18 def error(self, msg, loc): |
186 | 19 """ Wrapper that registers the message and marks the result invalid """ |
20 self.diag.error(msg, loc) | |
21 self.ok = False | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
22 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
23 def checkPackage(self, pkg): |
186 | 24 self.ok = True |
215 | 25 visitor = Visitor() |
26 visitor.visit(pkg, f_post=self.check2) | |
186 | 27 return self.ok |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
28 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
29 def check2(self, sym): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
30 if type(sym) in [IfStatement, WhileStatement]: |
163 | 31 if not equalTypes(sym.condition.typ, boolType): |
186 | 32 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) |
150 | 33 elif type(sym) is Assignment: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
34 if type(sym.lval.typ) is PointerType and sym.rval.typ == intType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
35 print('special case, int to pointer is ok for now') |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
36 # TODO: add cast instruction? |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
37 elif not equalTypes(sym.lval.typ, sym.rval.typ): |
186 | 38 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) |
150 | 39 elif type(sym) is ReturnStatement: |
163 | 40 pass |
167 | 41 elif type(sym) is FunctionCall: |
166 | 42 if sym.proc: |
167 | 43 # Check arguments: |
44 ngiv = len(sym.args) | |
45 ptypes = sym.proc.typ.parametertypes | |
46 nreq = len(ptypes) | |
47 if ngiv != nreq: | |
186 | 48 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
167 | 49 else: |
50 for a, at in zip(sym.args, ptypes): | |
51 if not equalTypes(a.typ, at): | |
186 | 52 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) |
167 | 53 # determine return type: |
54 sym.typ = sym.proc.typ.returntype | |
55 else: | |
56 sym.typ = intType | |
163 | 57 elif type(sym) is VariableUse: |
58 if sym.target: | |
59 sym.typ = sym.target.typ | |
60 else: | |
165 | 61 sym.typ = intType |
163 | 62 elif type(sym) is Literal: |
63 if type(sym.val) is int: | |
64 sym.typ = intType | |
65 elif type(sym.val) is float: | |
66 sym.typ = doubleType | |
166 | 67 elif type(sym.val) is bool: |
68 sym.typ = boolType | |
163 | 69 else: |
186 | 70 self.error('Unknown literal type', sym.loc) |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
71 elif type(sym) is Unop: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
72 if sym.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
73 sym.typ = PointerType(sym.a.typ) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
74 elif sym.op == '*': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
75 # pointer deref |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
76 if type(sym.a.typ) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
77 sym.typ = sym.a.typ.ptype |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
78 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
79 self.error('Cannot dereference non-pointer type {}'.format(sym.a.typ), sym.loc) |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
80 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
81 print('unknown unop', sym.op) |
150 | 82 elif type(sym) is Binop: |
163 | 83 if sym.op in ['+', '-', '*', '/']: |
84 if equalTypes(sym.a.typ, sym.b.typ): | |
170 | 85 if equalTypes(sym.a.typ, intType): |
86 sym.typ = sym.a.typ | |
87 else: | |
186 | 88 self.error('Can only add integers', sym.loc) |
170 | 89 sym.typ = intType |
163 | 90 else: |
165 | 91 # assume void here? TODO: throw exception! |
92 sym.typ = intType | |
186 | 93 self.error('Types unequal', sym.loc) |
170 | 94 elif sym.op in ['>', '<', '==', '<=', '>=']: |
165 | 95 sym.typ = boolType |
96 if not equalTypes(sym.a.typ, sym.b.typ): | |
186 | 97 self.error('Types unequal', sym.loc) |
166 | 98 elif sym.op in ['or', 'and']: |
99 sym.typ = boolType | |
100 if not equalTypes(sym.a.typ, boolType): | |
186 | 101 self.error('Must be {0}'.format(boolType), sym.a.loc) |
166 | 102 if not equalTypes(sym.b.typ, boolType): |
186 | 103 self.error('Must be {0}'.format(boolType), sym.b.loc) |
163 | 104 else: |
105 sym.typ = voidType | |
106 print('unknown binop', sym.op) | |
165 | 107 elif type(sym) is Variable: |
108 # check initial value type: | |
109 # TODO | |
110 pass | |
166 | 111 elif type(sym) is Constant: |
112 if not equalTypes(sym.typ, sym.value.typ): | |
186 | 113 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) |
168 | 114 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]: |
165 | 115 pass |
163 | 116 else: |
186 | 117 raise Exception('Unknown type check {0}'.format(sym)) |
150 | 118 |