Repository navigation
Expand file tree
/
Copy pathSemantics.java
More file actions
125 lines (111 loc) · 4.49 KB
/
Copy pathSemantics.java
File metadata and controls
125 lines (111 loc) · 4.49 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
// Following is the semantics class:
// The meaning M of a Statement is a State
// The meaning M of a Expression is a Value
public class Semantics {
State M (Program p) {
return M (p.body, initialState(p.decpart));
}
State initialState (Declarations d) {
State state = new State();
Value intUndef = new IntValue();
for (Declaration decl : d)
state.put(decl.v, Value.mkValue(decl.t));
return state;
}
State M (Statement s, State state) {
if (s instanceof Skip) return M((Skip)s, state);
if (s instanceof Assignment) return M((Assignment)s, state);
if (s instanceof Conditional) return M((Conditional)s, state);
if (s instanceof Loop) return M((Loop)s, state);
if (s instanceof Block) return M((Block)s, state);
throw new IllegalArgumentException("should never reach here");
}
State M (Skip s, State state) {
return state;
}
State M (Assignment a, State state) {
return state.onion(a.target, M (a.source, state));
}
State M (Block b, State state) {
for (Statement s : b.members)
state = M (s, state);
return state;
}
State M (Conditional c, State state) {
if (M(c.test, state).boolValue( ))
return M (c.thenbranch, state);
else
return M (c.elsebranch, state);
}
State M (Loop l, State state) {
if (M (l.test, state).boolValue( ))
return M(l, M (l.body, state));
else return state;
}
Value applyBinary (Operator op, Value v1, Value v2) {
StaticTypeCheck.check( ! v1.isUndef( ) && ! v2.isUndef( ),
"reference to undef value");
if (op.val.equals(Operator.INT_PLUS))
return new IntValue(v1.intValue( ) + v2.intValue( ));
if (op.val.equals(Operator.INT_MINUS))
return new IntValue(v1.intValue( ) - v2.intValue( ));
if (op.val.equals(Operator.INT_TIMES))
return new IntValue(v1.intValue( ) * v2.intValue( ));
if (op.val.equals(Operator.INT_DIV))
return new IntValue(v1.intValue( ) / v2.intValue( ));
// student exercise
throw new IllegalArgumentException("should never reach here");
}
Value applyUnary (Operator op, Value v) {
StaticTypeCheck.check( ! v.isUndef( ),
"reference to undef value");
if (op.val.equals(Operator.NOT))
return new BoolValue(!v.boolValue( ));
else if (op.val.equals(Operator.INT_NEG))
return new IntValue(-v.intValue( ));
else if (op.val.equals(Operator.FLOAT_NEG))
return new FloatValue(-v.floatValue( ));
else if (op.val.equals(Operator.I2F))
return new FloatValue((float)(v.intValue( )));
else if (op.val.equals(Operator.F2I))
return new IntValue((int)(v.floatValue( )));
else if (op.val.equals(Operator.C2I))
return new IntValue((int)(v.charValue( )));
else if (op.val.equals(Operator.I2C))
return new CharValue((char)(v.intValue( )));
throw new IllegalArgumentException("should never reach here");
}
Value M (Expression e, State state) {
if (e instanceof Value)
return (Value)e;
if (e instanceof Variable)
return (Value)(state.get(e));
if (e instanceof Binary) {
Binary b = (Binary)e;
return applyBinary (b.op,
M(b.term1, state), M(b.term2, state));
}
if (e instanceof Unary) {
Unary u = (Unary)e;
return applyUnary(u.op, M(u.term, state));
}
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 = StaticTypeCheck.typing(prog.decpart);
// map.display(); // student exercise
StaticTypeCheck.V(prog);
Program out = TypeTransformer.T(prog, map);
System.out.println("Output AST");
// out.display(); // student exercise
Semantics semantics = new Semantics( );
State state = semantics.M(out);
System.out.println("Final State");
// state.display( ); // student exercise
}
}