evaluate method
- SymbolTable table
override
Evaluates this AST node and generates corresponding LLVM IR code.
This is the main method that each AST node must implement to:
- Perform semantic analysis (type checking, symbol resolution)
- Generate LLVM IR instructions for the node's operation
- Return the result value (if this is an expression)
Parameters:
table
: The current symbol table for variable and function lookups
Returns: The result of evaluating this node (type depends on node type)
Throws:
- Exception for semantic errors (undefined variables, type mismatches, etc.)
- Various specific exceptions for different error conditions
Note: The base implementation throws "Not implemented" - concrete subclasses must override this method with their specific evaluation logic.
Implementation
@override
void evaluate(SymbolTable table) {
var conditionResult = condition.evaluate(table);
//cant be array
//has to be int
if (conditionResult.type is! PrimitiveType ||
conditionResult.type.primitiveType != PrimitiveTypes.int) {
throw Exception("Condition must be int");
}
Node.addIrLine(
"%conditionCast.$id = icmp ne i64 ${conditionResult.regName}, 0");
Node.addIrLine(
"br i1 %conditionCast.$id, label %then.$id, label %else.$id");
Node.addIrlLabel("then.$id");
thenBlock.evaluate(table);
final elseNode = elseBlock;
Node.addIrLine("br label %end.$id");
Node.endIrLabel();
Node.addIrlLabel("else.$id");
if (elseNode != null) {
elseNode.evaluate(table);
}
Node.addIrLine("br label %end.$id");
Node.endIrLabel();
Node.addIrLine("end.$id:");
}