Checking for undeclared variables
To find undeclared variables, check the symbol table on each variable that’s used for assignment or dereferencing. These reads and writes of memory occur in the executable statements and the expressions whose values are computed within those statements. Given a syntax tree, how do you find them?
The answer is to use tree traversals that look for IDENTIFIER
tokens but only when they are in executable statements within blocks of code. To go about this, start from the top with a tree traversal that just finds the blocks of code. In Jzero, this is a traversal that finds the bodies of methods.
Identifying the bodies of methods
The check_codeblocks()
method traverses the tree from the top to find all the method bodies, which is where the executable code is in Jzero. For every method declaration it finds, it calls another method called check_block()
on that method’s body. In tree.icn
, the Unicon version is:
method check_codeblocks...