annotate python/c3/typecheck.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents b28a11c01dbe
children e023d3ce1d63
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)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
16 def checkPackage(self, pkg):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
17 self.visitor.visit(pkg)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
18 def precheck(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
19 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
20 def check2(self, sym):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
21 if type(sym) is Function:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
22 pass
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
23 elif type(sym) is IfStatement:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
24 print(sym.condition)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
25 if not equalTypes(sym.condition.typ, boolType):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
26 self.diag.error('Condition must be a boolean expression', sym.condition.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
27 elif type(sym) is Assignment:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
28 pass
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
29 elif type(sym) is ReturnStatement:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
30 pass
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
31 elif type(sym) is ProcedureCall:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
32 # Check arguments:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
33
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
34 # determine return type:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
35 sym.typ = sym.proc.typ.returntype
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
36 elif type(sym) is VariableUse:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
37 if sym.target:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
38 sym.typ = sym.target.typ
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
39 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
40 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
41 elif type(sym) is Literal:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
42 if type(sym.val) is int:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
43 sym.typ = intType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
44 elif type(sym.val) is float:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
45 sym.typ = doubleType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
46 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
47 self.diag.error('Unknown literal type', sym.loc)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
48 elif type(sym) is Binop:
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
49 if sym.op in ['+', '-', '*', '/']:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
50 if equalTypes(sym.a.typ, sym.b.typ):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
51 sym.typ = sym.a.typ
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
52 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
53 # assume void here?
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
54 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
55 self.diag.error('Types unequal', sym.loc)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
56 elif sym.op in ['>', '<']:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
57 if equalTypes(sym.a.typ, sym.b.typ):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
58 sym.typ = boolType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
59 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
60 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
61 self.diag.error('Types unequal', sym.loc)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
62 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
63 sym.typ = voidType
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
64 print('unknown binop', sym.op)
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
65 else:
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 155
diff changeset
66 print('Unknown type check', sym)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents:
diff changeset
67