-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
56 lines (45 loc) · 1.18 KB
/
State.java
File metadata and controls
56 lines (45 loc) · 1.18 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
import java.util.*;
// State as stack
// <id, val>
class Pair {
Identifier id;
Value val;
Pair (Identifier id, Value v) {
this.id = id;
this.val = v;
}
}
class State extends Stack<Pair> {
public State( ) { }
public State(Identifier id, Value val) {
push(id, val);
}
public State push(Identifier id, Value val) {
super.push(new Pair(id, val));
return this;
}
public int lookup (Identifier v) {
for (int i=size()-1; i>=0; i--)
if (v.equals(((Pair)get(i)).id))
return i;
for (int i= 0; i <= size()-1; i++)
if (v.equals(((Pair)get(i)).id))
return i;
return -1;
}
// get the value of a variable in a State
public Value get (Identifier id) {
int i = lookup(id);
if (i == -1)
return null;
Pair p = (Pair)(get(i));
return (Value)(p.val);
}
public State set(Identifier id, Value val) {
int i = lookup(id);
if (i != -1)
super.set(i, new Pair(id, val)); // ArrayList.set
//System.out.println(val);
return this;
}
}