Mercurial > lcfOS
annotate python/c3/typecheck.py @ 221:848c4b15fd0b
pointers
author | Windel Bouwman |
---|---|
date | Mon, 08 Jul 2013 22:21:44 +0200 |
parents | 3f6c30a5d234 |
children | c3f1ce8b638f |
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: |
221 | 34 if not equalTypes(sym.lval.typ, sym.rval.typ): |
35 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc) | |
36 if not sym.lval.lvalue: | |
37 self.error('No valid lvalue {}'.format(sym.lval), sym.lval.loc) | |
38 #if sym.rval.lvalue: | |
39 # self.error('Right hand side must be an rvalue', sym.rval.loc) | |
150 | 40 elif type(sym) is ReturnStatement: |
163 | 41 pass |
167 | 42 elif type(sym) is FunctionCall: |
166 | 43 if sym.proc: |
167 | 44 # Check arguments: |
45 ngiv = len(sym.args) | |
46 ptypes = sym.proc.typ.parametertypes | |
47 nreq = len(ptypes) | |
48 if ngiv != nreq: | |
186 | 49 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc) |
167 | 50 else: |
51 for a, at in zip(sym.args, ptypes): | |
52 if not equalTypes(a.typ, at): | |
186 | 53 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc) |
167 | 54 # determine return type: |
55 sym.typ = sym.proc.typ.returntype | |
56 else: | |
57 sym.typ = intType | |
163 | 58 elif type(sym) is VariableUse: |
221 | 59 sym.lvalue = True |
60 if sym.target: | |
61 sym.typ = sym.target.typ | |
62 else: | |
63 sym.typ = intType | |
163 | 64 elif type(sym) is Literal: |
221 | 65 sym.lvalue = False |
66 if type(sym.val) is int: | |
67 sym.typ = intType | |
68 elif type(sym.val) is float: | |
69 sym.typ = doubleType | |
70 elif type(sym.val) is bool: | |
71 sym.typ = boolType | |
72 else: | |
73 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
|
74 elif type(sym) is Unop: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
75 if sym.op == '&': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
76 sym.typ = PointerType(sym.a.typ) |
221 | 77 sym.lvalue = False |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
78 elif sym.op == '*': |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
79 # pointer deref |
221 | 80 sym.lvalue = True |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
81 if type(sym.a.typ) is PointerType: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
82 sym.typ = sym.a.typ.ptype |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
83 else: |
221 | 84 sym.typ = intType |
220
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
85 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
|
86 else: |
3f6c30a5d234
Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents:
215
diff
changeset
|
87 print('unknown unop', sym.op) |
150 | 88 elif type(sym) is Binop: |
221 | 89 sym.lvalue = False |
163 | 90 if sym.op in ['+', '-', '*', '/']: |
91 if equalTypes(sym.a.typ, sym.b.typ): | |
170 | 92 if equalTypes(sym.a.typ, intType): |
93 sym.typ = sym.a.typ | |
94 else: | |
186 | 95 self.error('Can only add integers', sym.loc) |
170 | 96 sym.typ = intType |
163 | 97 else: |
165 | 98 # assume void here? TODO: throw exception! |
99 sym.typ = intType | |
186 | 100 self.error('Types unequal', sym.loc) |
170 | 101 elif sym.op in ['>', '<', '==', '<=', '>=']: |
165 | 102 sym.typ = boolType |
103 if not equalTypes(sym.a.typ, sym.b.typ): | |
186 | 104 self.error('Types unequal', sym.loc) |
166 | 105 elif sym.op in ['or', 'and']: |
106 sym.typ = boolType | |
107 if not equalTypes(sym.a.typ, boolType): | |
186 | 108 self.error('Must be {0}'.format(boolType), sym.a.loc) |
166 | 109 if not equalTypes(sym.b.typ, boolType): |
186 | 110 self.error('Must be {0}'.format(boolType), sym.b.loc) |
221 | 111 elif sym.op in ['|', '&']: |
112 sym.typ = intType | |
113 # TODO: elaborate? | |
163 | 114 else: |
115 sym.typ = voidType | |
116 print('unknown binop', sym.op) | |
165 | 117 elif type(sym) is Variable: |
118 # check initial value type: | |
119 # TODO | |
120 pass | |
166 | 121 elif type(sym) is Constant: |
122 if not equalTypes(sym.typ, sym.value.typ): | |
186 | 123 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc) |
168 | 124 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]: |
165 | 125 pass |
163 | 126 else: |
186 | 127 raise Exception('Unknown type check {0}'.format(sym)) |
150 | 128 |