Forward references
You should know by now that parsing is only the first stage when implementing a DSL and that it cannot detect all the errors from the programs. We need to implement additional checks in a validator.
One important thing we need to check in our Expressions DSL is that an expression does not refer to a variable defined after the very expression. Using an identifier before its declaration is usually called a forward reference.
Therefore, this program should not be considered valid:
var i = j + 1 var j = 0
Since the initialization expression of i
refers to j
, which is defined after. Of course, this is a design choice. Since we want to interpret the expressions, it makes sense to interpret them in the order they are defined.
This strategy also avoids possible mutual dependency problems:
var i = j + 1 var j = i + 1
A variable which is initialized referring to itself is a special case of the preceding:
var i = i + 1
We want to avoid this because our interpreter would enter an endless...