diff --git a/src/main/java/interpreter/Evaluator.java b/src/main/java/interpreter/Evaluator.java index 15a3c3c..9f4b357 100644 --- a/src/main/java/interpreter/Evaluator.java +++ b/src/main/java/interpreter/Evaluator.java @@ -34,23 +34,22 @@ takes an `Expression` in the constructor and provides a method `eval()` which ev the given expression and returns the result as an integer. For a given `expression` of type `Expression` it can be used as follows - Evaluator evaluator = new Evaluator(expression) - System.out.println(evaluator.eval()); + Evaluator evaluator = new Evaluator(expression, valuation) + System.out.println(evaluator.getValue()); The evaluation function `eval` takes the variable valuation `v`, which is passed on recursively. As the valuation is not changed during the evaluation process, it can be stored in a global variable which is not changed. */ public class Evaluator extends Visitor { - - final Expression expression; - final Map valuation = new HashMap(); + private final int value; + private final Map valuation = new HashMap(); public Evaluator(Expression expression, Map valuation) { - this.expression = expression; this.valuation.putAll(valuation); + value = visit(expression); } - public int eval() { - return visit(expression); + public int getValue() { + return value; } /*! diff --git a/src/main/java/interpreter/Interpreter.java b/src/main/java/interpreter/Interpreter.java index 121df8c..6be42eb 100644 --- a/src/main/java/interpreter/Interpreter.java +++ b/src/main/java/interpreter/Interpreter.java @@ -49,8 +49,7 @@ The semantic function `sem` passes along the variable valuation `v`. In the `Int as global variable `valuation`. Because of the in-order execution this global variable always represents the state of the `v` passed to the semantic function `sem`. */ public class Interpreter extends Visitor { - final Program program; - final Map valuation = new HashMap(); + private final Map valuation = new HashMap(); public Map getValuation() { Map result = new HashMap(); @@ -59,7 +58,6 @@ public class Interpreter extends Visitor { } public Interpreter(Program program) { - this.program = program; visit(program); } @@ -68,7 +66,7 @@ public class Interpreter extends Visitor { */ public void visitAssignment(Assignment assignment) { Evaluator evaluator = new Evaluator(assignment.expression, valuation); - valuation.put(assignment.identifier.name, evaluator.eval()); + valuation.put(assignment.identifier.name, evaluator.getValue()); } /*! @@ -86,7 +84,7 @@ public class Interpreter extends Visitor { */ public void visitConditional(Conditional conditional) { Evaluator evaluator = new Evaluator(conditional.condition, valuation); - if (evaluator.eval() != 0) { + if (evaluator.getValue() != 0) { visit(conditional.thenCase); } else { visit(conditional.elseCase); @@ -100,7 +98,7 @@ public class Interpreter extends Visitor { */ public void visitLoop(Loop loop) { Evaluator evaluator = new Evaluator(loop.condition, valuation); - if (evaluator.eval() != 0) { + if (evaluator.getValue() != 0) { visit(new Composition(loop.program, loop)); } } diff --git a/src/test/java/interpreter/EvaluatorTest.java b/src/test/java/interpreter/EvaluatorTest.java index ef87dc9..d264edf 100644 --- a/src/test/java/interpreter/EvaluatorTest.java +++ b/src/test/java/interpreter/EvaluatorTest.java @@ -16,6 +16,6 @@ public class EvaluatorTest { valuation.put("y", 2); Expression expression = new Addition(new Identifier("x"), new Subtraction(new Identifier("y"), new Int(-4))); Evaluator evaluator = new Evaluator(expression, valuation); - assertEquals(13, evaluator.eval()); + assertEquals(13, evaluator.getValue()); } }