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
|
|
10 return False
|
150
|
11
|
|
12 class TypeChecker:
|
|
13 def __init__(self, diag):
|
|
14 self.diag = diag
|
186
|
15 def error(self, msg, loc):
|
|
16 """ Wrapper that registers the message and marks the result invalid """
|
|
17 self.diag.error(msg, loc)
|
|
18 self.ok = False
|
150
|
19 def checkPackage(self, pkg):
|
186
|
20 self.ok = True
|
215
|
21 visitor = Visitor()
|
|
22 visitor.visit(pkg, f_post=self.check2)
|
186
|
23 return self.ok
|
163
|
24 def check2(self, sym):
|
|
25 if type(sym) is Function:
|
|
26 pass
|
165
|
27 elif type(sym) in [IfStatement, WhileStatement]:
|
163
|
28 if not equalTypes(sym.condition.typ, boolType):
|
186
|
29 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc)
|
150
|
30 elif type(sym) is Assignment:
|
164
|
31 if not equalTypes(sym.lval.typ, sym.rval.typ):
|
186
|
32 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
|
150
|
33 elif type(sym) is ReturnStatement:
|
163
|
34 pass
|
167
|
35 elif type(sym) is FunctionCall:
|
166
|
36 if sym.proc:
|
167
|
37 # Check arguments:
|
|
38 ngiv = len(sym.args)
|
|
39 ptypes = sym.proc.typ.parametertypes
|
|
40 nreq = len(ptypes)
|
|
41 if ngiv != nreq:
|
186
|
42 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc)
|
167
|
43 else:
|
|
44 for a, at in zip(sym.args, ptypes):
|
|
45 if not equalTypes(a.typ, at):
|
186
|
46 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
|
167
|
47 # determine return type:
|
|
48 sym.typ = sym.proc.typ.returntype
|
|
49 else:
|
|
50 sym.typ = intType
|
163
|
51 elif type(sym) is VariableUse:
|
|
52 if sym.target:
|
|
53 sym.typ = sym.target.typ
|
|
54 else:
|
165
|
55 sym.typ = intType
|
163
|
56 elif type(sym) is Literal:
|
|
57 if type(sym.val) is int:
|
|
58 sym.typ = intType
|
|
59 elif type(sym.val) is float:
|
|
60 sym.typ = doubleType
|
166
|
61 elif type(sym.val) is bool:
|
|
62 sym.typ = boolType
|
163
|
63 else:
|
186
|
64 self.error('Unknown literal type', sym.loc)
|
150
|
65 elif type(sym) is Binop:
|
163
|
66 if sym.op in ['+', '-', '*', '/']:
|
|
67 if equalTypes(sym.a.typ, sym.b.typ):
|
170
|
68 if equalTypes(sym.a.typ, intType):
|
|
69 sym.typ = sym.a.typ
|
|
70 else:
|
186
|
71 self.error('Can only add integers', sym.loc)
|
170
|
72 sym.typ = intType
|
163
|
73 else:
|
165
|
74 # assume void here? TODO: throw exception!
|
|
75 sym.typ = intType
|
186
|
76 self.error('Types unequal', sym.loc)
|
170
|
77 elif sym.op in ['>', '<', '==', '<=', '>=']:
|
165
|
78 sym.typ = boolType
|
|
79 if not equalTypes(sym.a.typ, sym.b.typ):
|
186
|
80 self.error('Types unequal', sym.loc)
|
166
|
81 elif sym.op in ['or', 'and']:
|
|
82 sym.typ = boolType
|
|
83 if not equalTypes(sym.a.typ, boolType):
|
186
|
84 self.error('Must be {0}'.format(boolType), sym.a.loc)
|
166
|
85 if not equalTypes(sym.b.typ, boolType):
|
186
|
86 self.error('Must be {0}'.format(boolType), sym.b.loc)
|
163
|
87 else:
|
|
88 sym.typ = voidType
|
|
89 print('unknown binop', sym.op)
|
165
|
90 elif type(sym) is Variable:
|
|
91 # check initial value type:
|
|
92 # TODO
|
|
93 pass
|
166
|
94 elif type(sym) is Constant:
|
|
95 if not equalTypes(sym.typ, sym.value.typ):
|
186
|
96 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
|
166
|
97
|
168
|
98 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]:
|
165
|
99 pass
|
163
|
100 else:
|
186
|
101 raise Exception('Unknown type check {0}'.format(sym))
|
150
|
102
|