Skip to content

Commit 55a2eb6

Browse files
authored
Merge pull request #24 from Sagichnicht04/main
Dash AST
2 parents 81efbfa + 56ace10 commit 55a2eb6

3 files changed

Lines changed: 45 additions & 1 deletion

File tree

src/main/java/com/compiler/ExpressionParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ ASTExprNode getVariableExpr() throws Exception {
3434
}
3535

3636
ASTExprNode getDashExpr() throws Exception {
37-
return getParantheseExpr();
37+
ASTExprNode result = getParantheseExpr();
38+
while (m_lexer.lookAhead().m_type == Type.TDASH) {
39+
Token curToken = m_lexer.lookAhead();
40+
m_lexer.advance();
41+
ASTExprNode operand = getParantheseExpr();
42+
result = new ASTTDashNode(result, operand);
43+
}
44+
return result;
3845
}
3946

4047
ASTExprNode getUnaryExpr() throws Exception {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.compiler.ast;
2+
3+
import com.compiler.TokenIntf;
4+
5+
import java.io.OutputStreamWriter;
6+
7+
public class ASTTDashNode extends ASTExprNode {
8+
ASTExprNode m_operand0;
9+
ASTExprNode m_operand1;
10+
com.compiler.TokenIntf.Type m_operator = TokenIntf.Type.TDASH;
11+
12+
public ASTTDashNode(ASTExprNode operand0, ASTExprNode operand1) {
13+
m_operand0 = operand0;
14+
m_operand1 = operand1;
15+
}
16+
17+
public int eval() {
18+
int operand0 = m_operand0.eval();
19+
int operand1 = m_operand1.eval();
20+
return (int) Math.pow(operand0,operand1);
21+
}
22+
23+
public void print(OutputStreamWriter outStream, String indent) throws Exception {}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.compiler;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class TestTDashExpr extends TestExpressionParserBase {
8+
@Test
9+
public void testTDash() throws Exception {
10+
assertEquals((int) Math.pow(16, 2), evalExpression("2^4^2"));
11+
}
12+
}
13+

0 commit comments

Comments
 (0)