Tuesday, January 11, 2011

Code Reuse

One of the advantages of building a model around a programming language and compiling source code to the model is reuse. For example, the Ctrl syntax allows the loop condition (or loop test) in a for loop to take on any of the classes in the model that extend RuleCondition:

i == 10EqualCondition
i != 10NotEqualCondition
i < 10LessThanCondition
i <= 10LessOrEqualCondition
i > 10GreaterThanCondition
i >= 10GreaterOrEqualCondition

When used as the loop test inside a for loop, these expressions evaluate the same way as when they are used inside an if statement because the evaluation model code behind the expressions is the same, regardless of the statement type and context:
for (int i = 10; i != 10; i--) {
    ...
}

if (i != 10) {
    ...    
}
In both examples above, the i != 10 expression compiles to an instance of NotEqualCondition whose evaluate() method compares the value of the "i" variable to the value on the right side of the expression, and returns true if they are not equal.

No comments:

Post a Comment