Skip to content

Commit faa0a38

Browse files
author
Eble
committed
Expression Evaluator Skeleton
1 parent 0ada69c commit faa0a38

6 files changed

Lines changed: 107 additions & 2 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com;
2+
3+
public class ExpressionEvaluatorMain {
4+
5+
public static void main(String[] args) throws Exception {
6+
com.compiler.Lexer lexer = new com.compiler.Lexer();
7+
com.compiler.ExpressionEvaluator evaluator = new com.compiler.ExpressionEvaluator(lexer);
8+
int result = evaluator.eval("4+5");
9+
System.out.println(result);
10+
}
11+
12+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com;
22

33

4-
public class ParserExample {
4+
public class ParserExampleMain {
55
com.compiler.Lexer m_lexer;
66

77
void parsePrint() throws Exception {
@@ -26,7 +26,7 @@ void parse(String input) throws Exception {
2626
}
2727

2828
public static void main(String[] args) throws Exception {
29-
ParserExample parser = new ParserExample();
29+
ParserExampleMain parser = new ParserExampleMain();
3030
parser.parse("""
3131
PRINT 1;
3232
PRINT 2;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.compiler;
2+
3+
public class ExpressionEvaluator implements ExpressionEvaluatorIntf {
4+
private Lexer m_lexer;
5+
6+
public ExpressionEvaluator(Lexer lexer) {
7+
m_lexer = lexer;
8+
}
9+
10+
@Override
11+
public int eval(String val) throws Exception {
12+
m_lexer.init(val);
13+
return getQuestionMarkExpr();
14+
}
15+
16+
int getParantheseExpr() throws Exception {
17+
Token curToken = m_lexer.lookAhead();
18+
m_lexer.expect(Token.Type.INTEGER);
19+
return Integer.valueOf(curToken.m_value);
20+
}
21+
22+
int getDashExpr() throws Exception {
23+
return getParantheseExpr();
24+
}
25+
26+
int getUnaryExpr() throws Exception {
27+
return getDashExpr();
28+
}
29+
30+
int getMulDivExpr() throws Exception {
31+
return getUnaryExpr();
32+
}
33+
34+
int getPlusMinusExpr() throws Exception {
35+
int result = getMulDivExpr();
36+
return result;
37+
}
38+
39+
int getBitAndOrExpr() throws Exception {
40+
return getPlusMinusExpr();
41+
}
42+
43+
int getShiftExpr() throws Exception {
44+
return getBitAndOrExpr();
45+
}
46+
47+
int getCompareExpr() throws Exception {
48+
return getShiftExpr();
49+
}
50+
51+
int getAndOrExpr() throws Exception {
52+
return getCompareExpr();
53+
}
54+
55+
int getQuestionMarkExpr() throws Exception {
56+
return getAndOrExpr();
57+
}
58+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.compiler;
2+
3+
public interface ExpressionEvaluatorIntf {
4+
5+
/**
6+
* evaluates the expression val and returns result as integer
7+
*/
8+
int eval(String val) throws Exception;
9+
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.compiler;
2+
3+
public class TestExpressionEvaluatorBase {
4+
5+
public int evalExpression(String input) throws Exception {
6+
Lexer lexer = new Lexer();
7+
ExpressionEvaluatorIntf evaluator = new ExpressionEvaluator(lexer);
8+
int result = evaluator.eval(input);
9+
return result;
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.compiler;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class TestPlusMinusExpr extends TestExpressionEvaluatorBase {
8+
9+
@Test
10+
public void testPlusMinusExpr() throws Exception {
11+
assertEquals(5, evalExpression("5"));
12+
}
13+
14+
}

0 commit comments

Comments
 (0)