-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvalVisitor.java
121 lines (100 loc) · 3.62 KB
/
EvalVisitor.java
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
package wikify;
import java.util.HashMap;
import java.util.Map;
public class EvalVisitor extends WikiBaseVisitor<Integer> {
/** "memory" for our calculator; variable/value pairs go here */
Map<String, DataType> memory = new HashMap<String, DataType>();
/** ID '=' expr NEWLINE */
@Override
public Integer visitAssign(WikiParser.AssignContext ctx) {
String id = ctx.ID().getText(); // id is left-hand side of '='
int value = visit(ctx.expr()); // compute value of expression on right
DataType dt = new DataType(value);
memory.put(id, dt); // store it in our memory
return value;
}
/** expr op=(ADD|SUB) term */
@Override
public Integer visitAddSub(WikiParser.AddSubContext ctx) {
int left = visit(ctx.expr()); // get value of left subexpression
int right = visit(ctx.term()); // get value of right subexpression
if ( ctx.op.getType() == WikiParser.ADD ) //if add
return left + right;
return left - right; // must be SUB
}
/** INT */
@Override
public Integer visitInteger(WikiParser.IntegerContext ctx) {
return Integer.valueOf(ctx.INT().getText());
}
/** ID */
@Override
public Integer visitVar(WikiParser.VarContext ctx) {
String id = ctx.ID().getText();
if ( memory.containsKey(id) ) {
DataType dt = memory.get(id);
if(dt.getType() == DataType.INT)
return dt.getInt();
}
//some kind of error
return 0;
}
/** term op=(MUL|DIV) fact */
@Override
public Integer visitMulDiv(WikiParser.MulDivContext ctx) {
int left = visit(ctx.term()); // get value of left subexpression
int right = visit(ctx.fact()); // get value of right subexpression
if ( ctx.op.getType() == WikiParser.MUL ) //if MUL
return left * right;
return left / right; // must be DIV
}
/** '(' expr ')' */
@Override
public Integer visitParens(WikiParser.ParensContext ctx) {
return visit(ctx.expr()); // return child expr's value
}
/** PRINT ID */
@Override
public Integer visitPrintId(WikiParser.PrintIdContext ctx) {
String id = ctx.ID().getText();
DataType dt;
if ( memory.containsKey(id) ) {
dt = memory.get(id);
if(dt.getType() == DataType.STR)
System.out.println(dt.getString());
if(dt.getType() == DataType.INT)
System.out.println(dt.getInt());
}
return 0;
}
/** PRINT STRING */
@Override
public Integer visitPrintString(WikiParser.PrintStringContext ctx) {
String word = ctx.STRING().getText();
word = word.substring(1, word.length() - 1);
word = replaceEscapeSeq(word);
System.out.print(word);
return 0;
}
//Note I wrote this because java's replaceAll would not recognize \\n
private String replaceEscapeSeq(String word)
{
char escape[] = {'n', 'r', 't', '"', '\\'};
char seq[] = {'\n', '\r', '\t', '\"', '\\'};
int i;
for(i = 1; i < word.length(); i++)
{
for(int k = 0; k < 5; k++)
{
if(word.charAt(i) == escape[k] && word.charAt(i-1) == '\\')
{
String suffix = word.substring(i+ 1, word.length());
String prefix = word.substring(0, i - 1);
prefix = prefix.concat(Character.toString(seq[k]));
word = prefix.concat(suffix);
}
}
}
return word;
}
}