annotate python/c3/typecheck.py @ 186:46d62dadd61b

Improved testsuite
author Windel Bouwman
date Sat, 25 May 2013 14:26:25 +0200
parents 4348da5ca307
children c1ccb1cb4cef
rev   line source
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
1 from .astnodes import *
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
2 from .scope import *
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
3 from .visitor import Visitor
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
4
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
5 def equalTypes(a, b):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
6 """ Compare types a and b for equality. Not equal until proven otherwise. """
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
7 if type(a) is type(b):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
8 if type(a) is BaseType:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
9 return a.name == b.name
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
10 return False
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
11
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
12 class TypeChecker:
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
13 def __init__(self, diag):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
14 self.diag = diag
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
15 self.visitor = Visitor(self.precheck, self.check2)
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
16 def error(self, msg, loc):
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
17 """ Wrapper that registers the message and marks the result invalid """
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
18 self.diag.error(msg, loc)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
19 self.ok = False
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
20 def checkPackage(self, pkg):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
21 self.ok = True
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
22 self.visitor.visit(pkg)
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
23 return self.ok
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
24 def precheck(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
25 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
26 def check2(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
27 if type(sym) is Function:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
28 pass
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
29 elif type(sym) in [IfStatement, WhileStatement]:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
30 if not equalTypes(sym.condition.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
31 self.error('Condition must be of type {0}'.format(boolType), sym.condition.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
32 elif type(sym) is Assignment:
164
e023d3ce1d63 Fix to loc of assignment
Windel Bouwman
parents: 163
diff changeset
33 if not equalTypes(sym.lval.typ, sym.rval.typ):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
34 self.error('Cannot assign {0} to {1}'.format(sym.rval.typ, sym.lval.typ), sym.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
35 elif type(sym) is ReturnStatement:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
36 pass
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
37 elif type(sym) is FunctionCall:
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
38 if sym.proc:
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
39 # Check arguments:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
40 ngiv = len(sym.args)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
41 ptypes = sym.proc.typ.parametertypes
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
42 nreq = len(ptypes)
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
43 if ngiv != nreq:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
44 self.error('Function {2}: {0} arguments required, {1} given'.format(nreq, ngiv, sym.proc.name), sym.loc)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
45 else:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
46 for a, at in zip(sym.args, ptypes):
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
47 if not equalTypes(a.typ, at):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
48 self.error('Got {0}, expected {1}'.format(a.typ, at), a.loc)
167
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
49 # determine return type:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
50 sym.typ = sym.proc.typ.returntype
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
51 else:
0b5b2ee6b435 Added 2 unit tests
Windel Bouwman
parents: 166
diff changeset
52 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
53 elif type(sym) is VariableUse:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
54 if sym.target:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
55 sym.typ = sym.target.typ
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
56 else:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
57 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
58 elif type(sym) is Literal:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
59 if type(sym.val) is int:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
60 sym.typ = intType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
61 elif type(sym.val) is float:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
62 sym.typ = doubleType
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
63 elif type(sym.val) is bool:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
64 sym.typ = boolType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
65 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
66 self.error('Unknown literal type', sym.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
67 elif type(sym) is Binop:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
68 if sym.op in ['+', '-', '*', '/']:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
69 if equalTypes(sym.a.typ, sym.b.typ):
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
70 if equalTypes(sym.a.typ, intType):
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
71 sym.typ = sym.a.typ
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
72 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
73 self.error('Can only add integers', sym.loc)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
74 sym.typ = intType
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
75 else:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
76 # assume void here? TODO: throw exception!
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
77 sym.typ = intType
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
78 self.error('Types unequal', sym.loc)
170
4348da5ca307 Cleanup of ir dir
Windel Bouwman
parents: 168
diff changeset
79 elif sym.op in ['>', '<', '==', '<=', '>=']:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
80 sym.typ = boolType
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
81 if not equalTypes(sym.a.typ, sym.b.typ):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
82 self.error('Types unequal', sym.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
83 elif sym.op in ['or', 'and']:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
84 sym.typ = boolType
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
85 if not equalTypes(sym.a.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
86 self.error('Must be {0}'.format(boolType), sym.a.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
87 if not equalTypes(sym.b.typ, boolType):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
88 self.error('Must be {0}'.format(boolType), sym.b.loc)
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
89 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
90 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
91 print('unknown binop', sym.op)
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
92 elif type(sym) is Variable:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
93 # check initial value type:
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
94 # TODO
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
95 pass
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
96 elif type(sym) is Constant:
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
97 if not equalTypes(sym.typ, sym.value.typ):
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
98 self.error('Cannot assign {0} to {1}'.format(sym.value.typ, sym.typ), sym.loc)
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
99
168
49f1ab80d040 Added awesome icons
Windel Bouwman
parents: 167
diff changeset
100 elif type(sym) in [EmptyStatement, CompoundStatement, Package, Function, FunctionType]:
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
101 pass
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
102 else:
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 170
diff changeset
103 raise Exception('Unknown type check {0}'.format(sym))
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
104