Monday, January 10, 2011

For Loop Evaluated

Focused almost all of my attention today writing and lightly exercising the Java code in the Ctrl model that supports the evaluation of a for loop. As mentioned in a previous post, Ctrl source code compiles to an instance of RuleStatement which is a base class in the language evaluation model.

The Ctrl is interpreted in the sense that statements read from source code are translated by ANTLR-generated parsers directly into a Java object called a RuleStatement to be evaluated in memory by the Ctrl interpreter. There is no intermediate representation, other than a fully initialized RuleStatement, that is used to execute Ctrl code.

In the model, as a descendant of RuleStatement, ForStatement overloads evaluate(). Within this method, ForStatement initializes the loop variable, evaluates the loop condition, processes the loop counter, and returns a boolean value, or throws an EvaluationException.

Having implemented evaluate(), I was able to eval-test two types of for loops, one that counts up, and another that counts down:
for (int i = 0; i < 10; i++) {
  ...
}

for (int i = 10; i > 0; i--) {
  ...
}
Both loops evaluate as expected, the value of the "i" variable increases or decreases by 1 with each pass through the loop and the body of the loop executes 10 times. This being the case, the for loop evaluation code I wrote today seems to be implemented correctly.

No comments:

Post a Comment