Writing an interpreter
We will now write an interpreter for our Expressions DSL. The idea is that this interpreter, given an AbstractElement
, returns a Java object, which represents the evaluation of that element. Of course, we want the object with the result of the evaluation to be of the correct Java type; that is, if we evaluate a boolean expression, the corresponding object should be a Java boolean object.
Such an interpreter will be recursive, since to evaluate an expression, we must first evaluate its sub-expressions and then compute the result.
When implementing the interpreter we make the assumption that the passed AbstractElement
is valid. Therefore, we will not check for null sub-expressions. We will assume that all variable references are resolved, and we will assume that all the sub-expressions are well-typed. For example, if we evaluate an And
expression, we assume that the objects resulting from the evaluation of its sub-expressions are Java Boolean
objects.
Note
We write the...