You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB

  1. /*!! Interpreter */
  2. /*!
  3. Evaluator
  4. =========
  5. The evaluator implements the semantics defined by the function `eval: Expr * V -> Z`, where `V = Id -> Z` is the set
  6. of all variable valuations to the set `Z` set of integers. `eval` is inductively defined as follows:
  7. eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
  8. eval(e1 "-" e2, v) = eval(e1, v) − eval(e2, v)
  9. eval(x, v) = v(x)
  10. eval(z, v) = z
  11. with
  12. - expressions `e`, `e1`, `e2`,
  13. - a variable valuation `v`,
  14. - and identifier `x` and
  15. - an integer `z`.
  16. */
  17. /*!- Header */
  18. package interpreter;
  19. import expression.*;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /*! The `Evaluator` implements the evaluation function defined above with the help of the
  23. [Visitor](${basePath}/src/main/java/interpreter/Visitor.java.html). The `Evaluator`
  24. takes an `Expression` in the constructor and provides a method `eval()` which evaluates
  25. the given expression and returns the result as an integer. For a given `expression` of type `Expression`
  26. it can be used as follows
  27. Evaluator evaluator = new Evaluator(expression, valuation)
  28. System.out.println(evaluator.getValue());
  29. The evaluation function `eval` takes the variable valuation `v`, which is passed on recursively. As the valuation
  30. is not changed during the evaluation process, it can be stored in a global variable which is not changed. */
  31. public class Evaluator extends Visitor<Integer> {
  32. private final int value;
  33. private final Map<String, Integer> valuation = new HashMap<String, Integer>();
  34. public Evaluator(Expression expression, Map<String, Integer> valuation) {
  35. this.valuation.putAll(valuation);
  36. value = visit(expression);
  37. }
  38. public int getValue() {
  39. return value;
  40. }
  41. /*!
  42. eval(e1 "+" e2, v) = eval(e1, v) + eval(e2, v)
  43. */
  44. public Integer visitAddition(Addition addition) {
  45. return visit(addition.leftHandSide) + visit(addition.rightHandSide);
  46. }
  47. /*!
  48. eval(e1 "-" e2, v) = eval(e1, v) - eval(e2, v)
  49. */
  50. public Integer visitSubtraction(Subtraction subtraction) {
  51. return visit(subtraction.leftHandSide) - visit(subtraction.rightHandSide);
  52. }
  53. /*!
  54. eval(x, v) = v(x)
  55. */
  56. public Integer visitInt(Int integer) {
  57. return integer.value;
  58. }
  59. public Integer visitIdentifier(Identifier identifier) {
  60. /*! Make sure that the identifier actually exists in the valuation and raise an exception otherwise. */
  61. if (valuation.containsKey(identifier.name)) {
  62. /*!
  63. eval(z, v) = z
  64. */
  65. return valuation.get(identifier.name);
  66. } else {
  67. throw new InterpreterException("Identifier " + identifier.name + " not found.");
  68. }
  69. }
  70. }