-
Notifications
You must be signed in to change notification settings - Fork 2
/
Calculator.java
70 lines (63 loc) · 2.46 KB
/
Calculator.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
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
public class Calculator {
public static class CalculatorVisitor extends AbstractParseTreeVisitor<Integer> implements CALCVisitor<Integer> {
@Override
public Integer visitExpr(CALCParser.ExprContext context) {
int left = visit(context.factor());
if (context.expr() != null) {
int right = visit(context.expr());
switch (context.op.getText()) {
case "+":
return left + right;
case "-":
return left - right;
default:
throw new IllegalStateException("Missing operand: " + context.op.getText());
}
} else {
return left;
}
}
@Override
public Integer visitFactor(CALCParser.FactorContext context) {
int left = visit(context.term());
if (context.factor() != null) {
int right = visit(context.factor());
switch (context.op.getText()) {
case "*":
return left * right;
case "/":
return left / right;
default:
throw new IllegalStateException("Missing operand: " + context.op.getText());
}
} else {
return left;
}
}
@Override
public Integer visitTerm(CALCParser.TermContext context) {
if (context.expr() != null) {
return visit(context.expr());
} else {
return Integer.valueOf(context.NUMBER().getText());
}
}
}
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Expects exactly one expression");
return;
}
ANTLRInputStream input = new ANTLRInputStream(args[0]);
CALCLexer lexer = new CALCLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
CALCParser parser = new CALCParser(tokens);
ParseTree tree = parser.expr();
CalculatorVisitor visitor = new CalculatorVisitor();
System.out.println(visitor.visit(tree));
}
}