-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactor.java
More file actions
67 lines (61 loc) · 2.01 KB
/
Copy pathFactor.java
File metadata and controls
67 lines (61 loc) · 2.01 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
class Factor {
Id id;
int constant;
Expr expr;
void parse() {
if (Parser.scanner.currentToken() == Core.ID) {
this.id = new Id();
this.id.parse();
} else if (Parser.scanner.currentToken() == Core.CONST) {
this.constant = Parser.scanner.getCONST();
Parser.scanner.nextToken();
} else if (Parser.scanner.currentToken() == Core.LPAREN) {
Parser.scanner.nextToken();
this.expr = new Expr();
this.expr.parse();
Parser.expectedToken(Core.RPAREN);
Parser.scanner.nextToken();
} else {
System.out.println("ERROR: Expected ID, CONST, or LPAREN, recieved "
+ Parser.scanner.currentToken());
System.exit(0);
}
}
void semantic() {
if (this.id != null) {
this.id.semantic();
} else if (this.expr != null) {
this.expr.semantic();
}
}
void print() {
if (this.id != null) {
this.id.print();
} else if (this.expr != null) {
System.out.print("(");
this.expr.print();
System.out.print(")");
} else {
System.out.print(this.constant);
}
}
//Returns the int value of the factor
int execute() {
int value = 0;
if (this.id != null) {
if (GarbageCollector.varType.get(this.id.getString()).peek() == 0) {
value = this.id.executeValue();
} else if (GarbageCollector.varType.get(this.id.getString())
.peek() == 1)
{
int oldPositionInHeap = Executor.varGet(this.id.getString());
value = GarbageCollector.heap.get(oldPositionInHeap);
}
} else if (this.expr != null) {
value = this.expr.execute();
} else {
value = this.constant;
}
return value;
}
}