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 (including the initialization expression of a variable) 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:
i = j + 1 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:
i = j + 1 j = i + 1
A variable which is initialized referring to itself is a special case of the preceding:
i = i + 1
We want to avoid this because our...