浏览代码

Improve visitor usage

pull/1/head
Malte Schmitz 9 年前
父节点
当前提交
c16bdebdfd
共有 3 个文件被更改,包括 12 次插入15 次删除
  1. +7
    -8
      src/main/java/interpreter/Evaluator.java
  2. +4
    -6
      src/main/java/interpreter/Interpreter.java
  3. +1
    -1
      src/test/java/interpreter/EvaluatorTest.java

+ 7
- 8
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<Integer> {

final Expression expression;
final Map<String, Integer> valuation = new HashMap<String, Integer>();
private final int value;
private final Map<String, Integer> valuation = new HashMap<String, Integer>();

public Evaluator(Expression expression, Map<String, Integer> valuation) {
this.expression = expression;
this.valuation.putAll(valuation);
value = visit(expression);
}

public int eval() {
return visit(expression);
public int getValue() {
return value;
}

/*!


+ 4
- 6
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<String, Integer> valuation = new HashMap<String, Integer>();
private final Map<String, Integer> valuation = new HashMap<String, Integer>();

public Map<String, Integer> getValuation() {
Map<String, Integer> result = new HashMap<String, Integer>();
@@ -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));
}
}


+ 1
- 1
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());
}
}

正在加载...
取消
保存