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