Repository navigation
Expand file tree
/
Copy pathStaticTypeCheck.java
More file actions
139 lines (125 loc) · 4.85 KB
/
Copy pathStaticTypeCheck.java
File metadata and controls
139 lines (125 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// StaticTypeCheck.java
import java.util.*;
// Static type checking for Clite is defined by the functions
// V and the auxiliary functions typing and typeOf. These
// functions use the classes in the Abstract Syntax of Clite.
public class StaticTypeCheck {
public static TypeMap typing (Declarations d) {
TypeMap map = new TypeMap();
for (Declaration di : d)
map.put (di.v, di.t);
return map;
}
public static void check(boolean test, String msg) {
if (test) return;
System.err.println(msg);
System.exit(1);
}
public static void V (Declarations d) {
for (int i=0; i<d.size() - 1; i++)
for (int j=i+1; j<d.size(); j++) {
Declaration di = d.get(i);
Declaration dj = d.get(j);
check( ! (di.v.equals(dj.v)),
"duplicate declaration: " + dj.v);
}
}
public static void V (Program p) {
V (p.decpart);
V (p.body, typing (p.decpart));
}
public static Type typeOf (Expression e, TypeMap tm) {
if (e instanceof Value) return ((Value)e).type;
if (e instanceof Variable) {
Variable v = (Variable)e;
check (tm.containsKey(v), "undefined variable: " + v);
return (Type) tm.get(v);
}
if (e instanceof Binary) {
Binary b = (Binary)e;
if (b.op.ArithmeticOp( ))
if (typeOf(b.term1,tm)== Type.FLOAT)
return (Type.FLOAT);
else return (Type.INT);
if (b.op.RelationalOp( ) || b.op.BooleanOp( ))
return (Type.BOOL);
}
if (e instanceof Unary) {
Unary u = (Unary)e;
if (u.op.NotOp( )) return (Type.BOOL);
else if (u.op.NegateOp( )) return typeOf(u.term,tm);
else if (u.op.intOp( )) return (Type.INT);
else if (u.op.floatOp( )) return (Type.FLOAT);
else if (u.op.charOp( )) return (Type.CHAR);
}
throw new IllegalArgumentException("should never reach here");
}
public static void V (Expression e, TypeMap tm) {
if (e instanceof Value)
return;
if (e instanceof Variable) {
Variable v = (Variable)e;
check( tm.containsKey(v)
, "undeclared variable: " + v);
return;
}
if (e instanceof Binary) {
Binary b = (Binary) e;
Type typ1 = typeOf(b.term1, tm);
Type typ2 = typeOf(b.term2, tm);
V (b.term1, tm);
V (b.term2, tm);
if (b.op.ArithmeticOp( ))
check( typ1 == typ2 &&
(typ1 == Type.INT || typ1 == Type.FLOAT)
, "type error for " + b.op);
else if (b.op.RelationalOp( ))
check( typ1 == typ2 , "type error for " + b.op);
else if (b.op.BooleanOp( ))
check( typ1 == Type.BOOL && typ2 == Type.BOOL,
b.op + ": non-bool operand");
else
throw new IllegalArgumentException("should never reach here");
return;
}
// student exercise
throw new IllegalArgumentException("should never reach here");
}
public static void V (Statement s, TypeMap tm) {
if ( s == null )
throw new IllegalArgumentException( "AST error: null statement");
if (s instanceof Skip) return;
if (s instanceof Assignment) {
Assignment a = (Assignment)s;
check( tm.containsKey(a.target)
, " undefined target in assignment: " + a.target);
V(a.source, tm);
Type ttype = (Type)tm.get(a.target);
Type srctype = typeOf(a.source, tm);
if (ttype != srctype) {
if (ttype == Type.FLOAT)
check( srctype == Type.INT
, "mixed mode assignment to " + a.target);
else if (ttype == Type.INT)
check( srctype == Type.CHAR
, "mixed mode assignment to " + a.target);
else
check( false
, "mixed mode assignment to " + a.target);
}
return;
}
// student exercise
throw new IllegalArgumentException("should never reach here");
}
public static void main(String args[]) {
Parser parser = new Parser(new Lexer(args[0]));
Program prog = parser.program();
// prog.display(); // student exercise
System.out.println("\nBegin type checking...");
System.out.println("Type map:");
TypeMap map = typing(prog.decpart);
// map.display(); // student exercise
V(prog);
} //main
} // class StaticTypeCheck