add new node types to the visitors
This commit is contained in:
@@ -21,24 +21,38 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(MultiplicativeExpression e) {
|
public Value visit(AdditionExpression e) {
|
||||||
Value a = e.lhs.welcome(this);
|
Value lhs = e.lhs.welcome(this);
|
||||||
Value b = e.rhs.welcome(this);
|
Value rhs = e.rhs.welcome(this);
|
||||||
return new Value(a.asInteger() * b.asInteger());
|
return new Value(lhs.asInteger() + rhs.asInteger());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(AdditiveExpression e) {
|
public Value visit(SubstractionExpression e) {
|
||||||
Value a = e.lhs.welcome(this);
|
Value lhs = e.lhs.welcome(this);
|
||||||
Value b = e.rhs.welcome(this);
|
Value rhs = e.rhs.welcome(this);
|
||||||
return new Value(a.asInteger() + b.asInteger());
|
return new Value(lhs.asInteger() - rhs.asInteger());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value visit(MultiplicationExpression e) {
|
||||||
|
Value lhs = e.lhs.welcome(this);
|
||||||
|
Value rhs = e.rhs.welcome(this);
|
||||||
|
return new Value(lhs.asInteger() * rhs.asInteger());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value visit(DivisionExpression e) {
|
||||||
|
Value lhs = e.lhs.welcome(this);
|
||||||
|
Value rhs = e.rhs.welcome(this);
|
||||||
|
return new Value(lhs.asInteger() / rhs.asInteger());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Value visit(ModuloExpression e) {
|
public Value visit(ModuloExpression e) {
|
||||||
Value a = e.lhs.welcome(this);
|
Value lhs = e.lhs.welcome(this);
|
||||||
Value b = e.rhs.welcome(this);
|
Value rhs = e.rhs.welcome(this);
|
||||||
return new Value(a.asInteger() % b.asInteger());
|
return new Value(lhs.asInteger() % rhs.asInteger());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,7 +66,7 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
Value result = this.env.get(e.name);
|
Value result = this.env.get(e.name);
|
||||||
|
|
||||||
if (result == null) {
|
if (result == null) {
|
||||||
throw new RuntimeException("Variable with name " +e.name + " not found.");
|
throw new RuntimeException("Variable with name " + e.name + " not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -123,7 +137,7 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
|
|
||||||
// Stelle sicher, dass die Länge der argumente und parameter übereinstimmen
|
// Stelle sicher, dass die Länge der argumente und parameter übereinstimmen
|
||||||
if (e.arguments.length != func.parameters.length) {
|
if (e.arguments.length != func.parameters.length) {
|
||||||
throw new RuntimeException("Error with function call " +e.name + ": Number of parameters wrong");
|
throw new RuntimeException("Error with function call " + e.name + ": Number of parameters wrong");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Baue ein neues environment
|
// Baue ein neues environment
|
||||||
@@ -147,7 +161,7 @@ public class EvalVisitor implements Visitor<Value> {
|
|||||||
public Value visit(Program e) {
|
public Value visit(Program e) {
|
||||||
// Funktionsdefinitionen für die Auswertung
|
// Funktionsdefinitionen für die Auswertung
|
||||||
// von Funktionsaufrufen speichern
|
// von Funktionsaufrufen speichern
|
||||||
for (var funcDef: e.funcs) {
|
for (var funcDef : e.funcs) {
|
||||||
this.funcs.put(funcDef.name, funcDef);
|
this.funcs.put(funcDef.name, funcDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,20 +6,9 @@ import java.util.Map;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
import de.hsrm.compiler.Klang.nodes.Block;
|
import de.hsrm.compiler.Klang.nodes.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
|
import de.hsrm.compiler.Klang.nodes.expressions.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.Program;
|
import de.hsrm.compiler.Klang.nodes.statements.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.AdditiveExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.FunctionCall;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.IntegerExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.ModuloExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.MultiplicativeExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.NegateExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.Variable;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.IfStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.PrintStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.ReturnStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.VariableAssignment;
|
|
||||||
|
|
||||||
public class GenASM implements Visitor<Void> {
|
public class GenASM implements Visitor<Void> {
|
||||||
|
|
||||||
@@ -81,25 +70,61 @@ public class GenASM implements Visitor<Void> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(Variable e) {
|
public Void visit(Variable e) {
|
||||||
this.ex.write(" movq " +this.env.get(e.name) +"(%rbp), %rax\n");
|
this.ex.write(" movq " + this.env.get(e.name) + "(%rbp), %rax\n");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(MultiplicativeExpression e) {
|
public Void visit(AdditionExpression e) {
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
this.ex.write(" movq %rax, %rbx\n");
|
this.ex.write(" pushq %rax\n");
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
|
this.ex.write(" popq %rbx\n");
|
||||||
|
this.ex.write(" addq %rbx, %rax\n");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(SubstractionExpression e) {
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
this.ex.write(" pushq %rax\n");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
this.ex.write(" popq %rbx\n");
|
||||||
|
this.ex.write(" subq %rbx, %rax\n");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(MultiplicationExpression e) {
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
this.ex.write(" pushq %rax\n");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
this.ex.write(" popq %rbx\n");
|
||||||
this.ex.write(" imulq %rbx, %rax\n");
|
this.ex.write(" imulq %rbx, %rax\n");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(AdditiveExpression e) {
|
public Void visit(DivisionExpression e) {
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
this.ex.write(" movq %rax, %rbx\n");
|
this.ex.write(" pushq %rax\n");
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
this.ex.write(" addq %rbx, %rax\n");
|
this.ex.write(" movq %rax, %rbx\n");
|
||||||
|
this.ex.write(" popq %rax\n");
|
||||||
|
this.ex.write(" xor %rdx, %rdx\n"); // clear upper part of division
|
||||||
|
this.ex.write(" idiv %rbx\n"); // %rax/%rbx, quotient now in %rax
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(ModuloExpression e) {
|
||||||
|
this.ex.write(" pushq %rax\n");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
this.ex.write(" movq %rax, %rbx\n");
|
||||||
|
this.ex.write(" popq %rax\n");
|
||||||
|
this.ex.write(" xor %rdx, %rdx\n"); // clear upper part of division
|
||||||
|
this.ex.write(" idiv %rbx\n"); // %rax/%rbx, remainder now in %rdx
|
||||||
|
this.ex.write(" movq %rdx, %rax\n");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,17 +135,6 @@ public class GenASM implements Visitor<Void> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visit(ModuloExpression e) {
|
|
||||||
e.rhs.welcome(this);
|
|
||||||
this.ex.write(" movq %rax, %rbx\n");
|
|
||||||
e.lhs.welcome(this);
|
|
||||||
this.ex.write(" xor %rdx, %rdx\n"); // clear remainder register
|
|
||||||
this.ex.write(" idiv %ebx\n"); // %rax/%rbx, remainder now in %rdx
|
|
||||||
this.ex.write(" movq %rdx, %rax\n");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(IfStatement e) {
|
public Void visit(IfStatement e) {
|
||||||
int then = ++lCount;
|
int then = ++lCount;
|
||||||
@@ -205,7 +219,7 @@ public class GenASM implements Visitor<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Reserviere Platz auf dem Stack für jede lokale variable
|
// Reserviere Platz auf dem Stack für jede lokale variable
|
||||||
for (String lok_var: vars) {
|
for (String lok_var : vars) {
|
||||||
offset -= 8;
|
offset -= 8;
|
||||||
this.ex.write(" pushq $0\n");
|
this.ex.write(" pushq $0\n");
|
||||||
this.env.put(lok_var, offset);
|
this.env.put(lok_var, offset);
|
||||||
|
|||||||
@@ -2,20 +2,9 @@ package de.hsrm.compiler.Klang.visitors;
|
|||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.hsrm.compiler.Klang.nodes.Block;
|
import de.hsrm.compiler.Klang.nodes.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.FunctionDefinition;
|
import de.hsrm.compiler.Klang.nodes.expressions.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.Program;
|
import de.hsrm.compiler.Klang.nodes.statements.*;
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.AdditiveExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.FunctionCall;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.IntegerExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.ModuloExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.MultiplicativeExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.NegateExpression;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.expressions.Variable;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.IfStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.PrintStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.ReturnStatement;
|
|
||||||
import de.hsrm.compiler.Klang.nodes.statements.VariableAssignment;
|
|
||||||
|
|
||||||
class GetVars implements Visitor<Void> {
|
class GetVars implements Visitor<Void> {
|
||||||
|
|
||||||
@@ -36,14 +25,35 @@ class GetVars implements Visitor<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(MultiplicativeExpression e) {
|
public Void visit(AdditionExpression e) {
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(AdditiveExpression e) {
|
public Void visit(SubstractionExpression e) {
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(MultiplicationExpression e) {
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(DivisionExpression e) {
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(ModuloExpression e) {
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
return null;
|
return null;
|
||||||
@@ -55,13 +65,6 @@ class GetVars implements Visitor<Void> {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visit(ModuloExpression e) {
|
|
||||||
e.lhs.welcome(this);
|
|
||||||
e.rhs.welcome(this);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(IfStatement e) {
|
public Void visit(IfStatement e) {
|
||||||
e.cond.welcome(this);
|
e.cond.welcome(this);
|
||||||
|
|||||||
@@ -72,33 +72,60 @@ public class PrettyPrintVisitor implements Visitor<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(MultiplicativeExpression e) {
|
public Void visit(AdditionExpression e) {
|
||||||
e.lhs.welcome(this);
|
ex.write("(");
|
||||||
ex.write(" * ");
|
|
||||||
e.rhs.welcome(this);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Void visit(AdditiveExpression e) {
|
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
ex.write(" + ");
|
ex.write(" + ");
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
|
ex.write(")");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(NegateExpression e) {
|
public Void visit(SubstractionExpression e) {
|
||||||
ex.write(" - ");
|
ex.write("(");
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
|
ex.write(" - ");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
ex.write(")");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(MultiplicationExpression e) {
|
||||||
|
ex.write("(");
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
ex.write(" * ");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
ex.write(")");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(DivisionExpression e) {
|
||||||
|
ex.write("(");
|
||||||
|
e.lhs.welcome(this);
|
||||||
|
ex.write(" / ");
|
||||||
|
e.rhs.welcome(this);
|
||||||
|
ex.write(")");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Void visit(ModuloExpression e) {
|
public Void visit(ModuloExpression e) {
|
||||||
|
ex.write("(");
|
||||||
e.lhs.welcome(this);
|
e.lhs.welcome(this);
|
||||||
ex.write(" % ");
|
ex.write(" % ");
|
||||||
e.rhs.welcome(this);
|
e.rhs.welcome(this);
|
||||||
|
ex.write(")");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void visit(NegateExpression e) {
|
||||||
|
ex.write(" - ");
|
||||||
|
e.lhs.welcome(this);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ import de.hsrm.compiler.Klang.nodes.statements.*;
|
|||||||
public interface Visitor<R> {
|
public interface Visitor<R> {
|
||||||
R visit(IntegerExpression e);
|
R visit(IntegerExpression e);
|
||||||
R visit(Variable e);
|
R visit(Variable e);
|
||||||
R visit(MultiplicativeExpression e);
|
R visit(AdditionExpression e);
|
||||||
R visit(AdditiveExpression e);
|
R visit(SubstractionExpression e);
|
||||||
R visit(NegateExpression e);
|
R visit(MultiplicationExpression e);
|
||||||
|
R visit(DivisionExpression e);
|
||||||
R visit(ModuloExpression e);
|
R visit(ModuloExpression e);
|
||||||
|
R visit(NegateExpression e);
|
||||||
R visit(IfStatement e);
|
R visit(IfStatement e);
|
||||||
R visit(PrintStatement e);
|
R visit(PrintStatement e);
|
||||||
R visit(VariableAssignment e);
|
R visit(VariableAssignment e);
|
||||||
|
|||||||
Reference in New Issue
Block a user