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
|
|
31 elif type(sym) is ProcedureCall:
|
|
32 # Check arguments:
|
166
|
33 if sym.proc:
|
|
34 pass
|
163
|
35 # determine return type:
|
|
36 sym.typ = sym.proc.typ.returntype
|
|
37 elif type(sym) is VariableUse:
|
|
38 if sym.target:
|
|
39 sym.typ = sym.target.typ
|
|
40 else:
|
165
|
41 sym.typ = intType
|
163
|
42 elif type(sym) is Literal:
|
|
43 if type(sym.val) is int:
|
|
44 sym.typ = intType
|
|
45 elif type(sym.val) is float:
|
|
46 sym.typ = doubleType
|
166
|
47 elif type(sym.val) is bool:
|
|
48 sym.typ = boolType
|
163
|
49 else:
|
|
50 self.diag.error('Unknown literal type', sym.loc)
|
150
|
51 elif type(sym) is Binop:
|
163
|
52 if sym.op in ['+', '-', '*', '/']:
|
|
53 if equalTypes(sym.a.typ, sym.b.typ):
|
|
54 sym.typ = sym.a.typ
|
|
55 else:
|
165
|
56 # assume void here? TODO: throw exception!
|
|
57 sym.typ = intType
|
163
|
58 self.diag.error('Types unequal', sym.loc)
|
|
59 elif sym.op in ['>', '<']:
|
165
|
60 sym.typ = boolType
|
|
61 if not equalTypes(sym.a.typ, sym.b.typ):
|
163
|
62 self.diag.error('Types unequal', sym.loc)
|
166
|
63 elif sym.op in ['or', 'and']:
|
|
64 sym.typ = boolType
|
|
65 if not equalTypes(sym.a.typ, boolType):
|
|
66 self.diag.error('Must be {0}'.format(boolType), sym.a.loc)
|
|
67 if not equalTypes(sym.b.typ, boolType):
|
|
68 self.diag.error('Must be {0}'.format(boolType), sym.b.loc)
|
163
|
69 else:
|
|
70 sym.typ = voidType
|
|
71 print('unknown binop', sym.op)
|
165
|
72 elif type(sym) is Variable:
|
|
73 # check initial value type:
|
|
74 # TODO
|
|
75 pass
|
166
|
76 elif type(sym) is Constant:
|
|
77 if not equalTypes(sym.typ, sym.value.typ):
|
|
78 self.diag.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
|
|
79
|
|
80 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function]:
|
165
|
81 pass
|
163
|
82 else:
|
|
83 print('Unknown type check', sym)
|
150
|
84
|