add context analysis and custom visitors

This commit is contained in:
2019-11-04 17:35:15 +01:00
parent bef26434c6
commit c26cb6ddf5
22 changed files with 355 additions and 123 deletions

View File

@@ -0,0 +1,66 @@
package de.hsrm.compiler.Klang.visitors;
import de.hsrm.compiler.Klang.Value;
import de.hsrm.compiler.Klang.nodes.expressions.*;
import de.hsrm.compiler.Klang.nodes.statements.*;
public class EvalVisitor implements Visitor<Value> {
@Override
public Value visit(IntegerExpression e) {
return new Value(e.value);
}
@Override
public Value visit(MultiplicativeExpression e) {
Value a = e.lhs.welcome(this);
Value b = e.rhs.welcome(this);
return new Value(a.asInteger() * b.asInteger());
}
@Override
public Value visit(AdditiveExpression e) {
Value a = e.lhs.welcome(this);
Value b = e.rhs.welcome(this);
return new Value(a.asInteger() + b.asInteger());
}
@Override
public Value visit(ModuloExpression e) {
Value a = e.lhs.welcome(this);
Value b = e.rhs.welcome(this);
return new Value(a.asInteger() % b.asInteger());
}
@Override
public Value visit(NegateExpression e) {
Value a = e.lhs.welcome(this);
return new Value(-a.asInteger());
}
@Override
public Value visit(IfStatement e) {
// In the future we have to make sure that the
// value is actually a type that we can use as boolean
Value condition = e.cond.welcome(this);
if (condition.asInteger() != 0) {
e.then.welcome(this);
} else if(e.alt != null) {
e.alt.welcome(this);
}
return null;
}
@Override
public Value visit(PrintStatement e) {
Value value = e.expression.welcome(this);
// In the future we have to determine of which type the value is
// before calling an "asX()" method
System.out.println(value.asInteger());
return null;
}
}

View File

@@ -0,0 +1,14 @@
package de.hsrm.compiler.Klang.visitors;
import de.hsrm.compiler.Klang.nodes.expressions.*;
import de.hsrm.compiler.Klang.nodes.statements.*;
public interface Visitor<R> {
R visit(IntegerExpression e);
R visit(MultiplicativeExpression e);
R visit(AdditiveExpression e);
R visit(NegateExpression e);
R visit(ModuloExpression e);
R visit(IfStatement e);
R visit(PrintStatement e);
}