Skip to content

Commit 12399c1

Browse files
author
Fynn Henck
committed
Implemented Interpreter for compare functions
1 parent f464841 commit 12399c1

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

src/main/java/com/compiler/ast/ASTCompareExprNode.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.compiler.ast;
22

3+
import com.compiler.InstrIntf;
34
import com.compiler.TokenIntf;
45

6+
import com.compiler.instr.InstrCompare;
57
import java.io.OutputStreamWriter;
68

79
public class ASTCompareExprNode extends ASTExprNode{
@@ -37,4 +39,14 @@ public void print(OutputStreamWriter outStream, String indent) throws Exception
3739
m_operand0.print(outStream, indent + " ");
3840
m_operand1.print(outStream, indent + " ");
3941
}
42+
43+
@Override
44+
public com.compiler.InstrIntf codegen(com.compiler.CompileEnvIntf env) {
45+
final InstrIntf operand0 = m_operand0.codegen(env);
46+
final InstrIntf operand1 = m_operand1.codegen(env);
47+
final InstrIntf compareExp = new InstrCompare(m_operator, operand0, operand1);
48+
49+
env.addInstr(compareExp);
50+
return compareExp;
51+
}
4052
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.compiler.instr;
2+
3+
import com.compiler.ExecutionEnvIntf;
4+
import com.compiler.InstrIntf;
5+
import com.compiler.TokenIntf;
6+
import com.compiler.TokenIntf.Type;
7+
import java.io.OutputStreamWriter;
8+
9+
public class InstrCompare extends InstrIntf {
10+
11+
protected TokenIntf.Type m_operator;
12+
protected InstrIntf m_operand0;
13+
protected InstrIntf m_operand1;
14+
15+
public InstrCompare(final Type m_operator, final InstrIntf m_operand0, final InstrIntf m_operand1) {
16+
this.m_operator = m_operator;
17+
this.m_operand0 = m_operand0;
18+
this.m_operand1 = m_operand1;
19+
}
20+
21+
@Override
22+
public void execute(ExecutionEnvIntf env) throws Exception {
23+
//ergebnis : m_value
24+
25+
switch (m_operator) {
26+
case GREATER:
27+
if(m_operand0.getValue() > m_operand1.getValue()) {
28+
m_value = 1;
29+
break;
30+
}
31+
m_value = 0;
32+
break;
33+
case LESS:
34+
if(m_operand0.getValue() < m_operand1.getValue()) {
35+
m_value = 1;
36+
break;
37+
}
38+
m_value = 0;
39+
break;
40+
case EQUAL:
41+
if(m_operand0.getValue() == m_operand1.getValue()) {
42+
m_value = 1;
43+
break;
44+
}
45+
m_value = 0;
46+
break;
47+
}
48+
49+
}
50+
51+
@Override
52+
public void trace(OutputStreamWriter os) throws Exception {
53+
os.write(
54+
String.format("%%%d = %s %%%d, %%%d\n",
55+
m_id, m_operator.toString(), m_operand0.getId(), m_operand1.getId()
56+
));
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.compiler;
2+
3+
import org.junit.Test;
4+
5+
public class InterpreterCompareTest extends InterpreterTestBase{
6+
7+
@Test
8+
public void compare() throws Exception {
9+
final String program = """
10+
{
11+
PRINT 1 > 2;
12+
}
13+
""";
14+
15+
testInterpreter(program, "0\n");
16+
}
17+
18+
@Test
19+
public void compareAgain() throws Exception {
20+
final String program = """
21+
{
22+
PRINT 4 > 1 == 1 > 0;
23+
}
24+
""";
25+
26+
testInterpreter(program, "1\n");
27+
}
28+
29+
30+
31+
32+
}

0 commit comments

Comments
 (0)