-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssign.java
More file actions
113 lines (96 loc) · 3.55 KB
/
Copy pathAssign.java
File metadata and controls
113 lines (96 loc) · 3.55 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
class Assign implements Stmt {
Id id;
Id id2;
Expr expr;
int option;
@Override
public void parse() {
this.id = new Id();
this.id.parse();
Parser.expectedToken(Core.ASSIGN);
Parser.scanner.nextToken();
if (Parser.scanner.currentToken() == Core.NEW) {
this.option = 2;
Parser.scanner.nextToken();
this.expr = new Expr();
this.expr.parse();
Parser.expectedToken(Core.SEMICOLON);
Parser.scanner.nextToken();
} else if (Parser.scanner.currentToken() == Core.DEFINE) {
this.option = 3;
Parser.scanner.nextToken();
Parser.expectedToken(Core.ID);
this.id2 = new Id();
this.id2.parse();
Parser.expectedToken(Core.SEMICOLON);
Parser.scanner.nextToken();
} else {
this.option = 1;
this.expr = new Expr();
this.expr.parse();
Parser.expectedToken(Core.SEMICOLON);
Parser.scanner.nextToken();
}
}
@Override
public void semantic() {
this.id.semantic();
//correct or not for option1?
if (this.option == 1) {
this.expr.semantic();
} else if (this.option == 2) {
this.expr.semantic();
} else if (this.option == 3) {
this.id2.semantic();
}
}
@Override
public void print(int indent) {
for (int i = 0; i < indent; i++) {
System.out.print("\t");
}
this.id.print();
System.out.print("=");
if (this.option == 1) {
this.expr.print();
} else if (this.option == 2) {
System.out.print("new");
this.expr.print();
} else if (this.option == 3) {
System.out.print("define");
this.id2.print();
}
System.out.println(";");
}
@Override
public void execute() {
if (this.option == 1) {
this.id.executeAssign(this.expr.execute());
} else if (this.option == 2) {
//countReference-1 if x refers to other value
GarbageCollector.subCountReference(this.id.getString());
//1. insert the value of expression in the heap
int value = this.expr.execute();//value of expression
GarbageCollector.heap.add(value);
GarbageCollector.countTotalSpace++;
System.out.println("gc:" + GarbageCollector.countTotalSpace);
//2.change the call stack value of x to the position of heap
int newPositionInHeap = GarbageCollector.heap.size()-1;
Executor.varSet(this.id.getString(), newPositionInHeap);
//3. let the count number++
GarbageCollector.countReference.add(1);
} else if (this.option == 3) {
//countReference-1 if x refers to other value
GarbageCollector.subCountReference(this.id.getString());
//2.change the call stack value of id to the position of heap
Integer newPositionInHeap = Executor.varGet(this.id2.getString()); // position of x now on the heap
Executor.varSet(this.id.getString(), newPositionInHeap);
//3. let the count number++
if(newPositionInHeap!=null)
{
GarbageCollector.countReference.set(newPositionInHeap,
GarbageCollector.countReference.get(newPositionInHeap) + 1);
}
}
}
}