Mercurial > lcfOS
annotate python/c3/typecheck.py @ 223:85c8105318e7
Fixup of parser
author | Windel Bouwman |
---|---|
date | Tue, 09 Jul 2013 17:42:52 +0200 |
parents | c3f1ce8b638f |
children | 1c7364bd74c7 |
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 |
222 | 14 def canCast(fromT, toT): |
15 if isinstance(fromT, PointerType) and isinstance(toT, PointerType): | |
16 return True | |
17 elif fromT is intType and isinstance(toT, PointerType): | |
18 return True | |
19 return False | |
20 | |
150 | 21 class TypeChecker: |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
22 def __init__(self, diag): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
23 self.diag = diag |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
24 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
25 def error(self, msg, loc): |
186 | 26 """ Wrapper that registers the message and marks the result invalid """ |
27 self.diag.error(msg, loc) | |
28 self.ok = False | |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
29 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
30 def checkPackage(self, pkg): |
186 | 31 self.ok = True |
215 | 32 visitor = Visitor() |
33 visitor.visit(pkg, f_post=self.check2) | |
186 | 34 return self.ok |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
35 |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
36 def check2(self, sym): |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
37 if type(sym) in [IfStatement, WhileStatement]: |
163 | 38 if not equalTypes(sym.condition.typ, boolType): |
186 | 39 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc) |
150 | 40 elif type(sym) is Assignment: |
221 | 41 if not equalTypes(sym.lval.typ, sym.rval.typ): |
42 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) | |
43 if not sym.lval.lvalue: | |
44 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc) | |
45 #if sym.rval.lvalue: | |
46 # self.error('Right hand side must be an rvalue', sym.rval.loc) | |
150 | 47 elif type(sym) is ReturnStatement: |
163 | 48 pass |
167 | 49 elif type(sym) is FunctionCall: |
166 | 50 if sym.proc: |
167 | 51 # Check arguments: |
52 ngiv = len(sym.args) | |
53 ptypes = sym.proc.typ.parametertypes | |
54 nreq = len(ptypes) | |
55 if ngiv != nreq: | |
186 | 56 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
167 | 57 else: |
58 for a, at in zip(sym.args, ptypes): | |
59 if not equalTypes(a.typ, at): | |
186 | 60 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) |
167 | 61 # determine return type: |
62 sym.typ = sym.proc.typ.returntype | |
63 else: | |
64 sym.typ = intType | |
163 | 65 elif type(sym) is VariableUse: |
221 | 66 sym.lvalue = True |
67 if sym.target: | |
68 sym.typ = sym.target.typ | |
69 else: | |
70 sym.typ = intType | |
163 | 71 elif type(sym) is Literal: |
221 | 72 sym.lvalue = False |
73 if type(sym.val) is int: | |
74 sym.typ = intType | |
75 elif type(sym.val) is float: | |
76 sym.typ = doubleType | |
77 elif type(sym.val) is bool: | |
78 sym.typ = boolType | |
79 else: | |
80 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
|
81 elif type(sym) is Unop: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
82 if sym.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
83 sym.typ = PointerType(sym.a.typ) |
221 | 84 sym.lvalue = False |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
85 elif sym.op == '*': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
86 # pointer deref |
221 | 87 sym.lvalue = True |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
88 if type(sym.a.typ) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
89 sym.typ = sym.a.typ.ptype |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
90 else: |
221 | 91 sym.typ = intType |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
92 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
|
93 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
94 print('unknown unop', sym.op) |
150 | 95 elif type(sym) is Binop: |
221 | 96 sym.lvalue = False |
163 | 97 if sym.op in ['+', '-', '*', '/']: |
98 if equalTypes(sym.a.typ, sym.b.typ): | |
170 | 99 if equalTypes(sym.a.typ, intType): |
100 sym.typ = sym.a.typ | |
101 else: | |
186 | 102 self.error('Can only add integers', sym.loc) |
170 | 103 sym.typ = intType |
163 | 104 else: |
165 | 105 # assume void here? TODO: throw exception! |
106 sym.typ = intType | |
186 | 107 self.error('Types unequal', sym.loc) |
170 | 108 elif sym.op in ['>', '<', '==', '<=', '>=']: |
165 | 109 sym.typ = boolType |
110 if not equalTypes(sym.a.typ, sym.b.typ): | |
186 | 111 self.error('Types unequal', sym.loc) |
166 | 112 elif sym.op in ['or', 'and']: |
113 sym.typ = boolType | |
114 if not equalTypes(sym.a.typ, boolType): | |
186 | 115 self.error('Must be {0}'.format(boolType), sym.a.loc) |
166 | 116 if not equalTypes(sym.b.typ, boolType): |
186 | 117 self.error('Must be {0}'.format(boolType), sym.b.loc) |
221 | 118 elif sym.op in ['|', '&']: |
119 sym.typ = intType | |
120 # TODO: elaborate? | |
163 | 121 else: |
122 sym.typ = voidType | |
123 print('unknown binop', sym.op) | |
165 | 124 elif type(sym) is Variable: |
125 # check initial value type: | |
126 # TODO | |
127 pass | |
222 | 128 elif type(sym) is TypeCast: |
129 if canCast(sym.a.typ, sym.to_type): | |
130 sym.typ = sym.to_type | |
131 else: | |
132 self.error('Cannot cast {} to {}'.format(sym.a.typ, sym.to_type)) | |
166 | 133 elif type(sym) is Constant: |
134 if not equalTypes(sym.typ, sym.value.typ): | |
186 | 135 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) |
222 | 136 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType, ExpressionStatement]: |
165 | 137 pass |
163 | 138 else: |
186 | 139 raise Exception('Unknown type check {0}'.format(sym)) |
150 | 140 |