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