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
LangVal evaluate(SymbolTable table) {
final leftResult = left.evaluate(table);
final rightResult = right.evaluate(table);
//cant be array
if (leftResult.type is ArrayType || rightResult.type is ArrayType) {
throw Exception("Cannot apply binary operator to array");
}
if (leftResult.type.primitiveType != PrimitiveTypes.int ||
rightResult.type.primitiveType != PrimitiveTypes.int) {
throw Exception("BoolBinOp can only be applied to int");
}
//should always return 0 or 1 in i64
switch (nodeValue) {
case BoolOperator.and:
Node.addIrLine(
"%and.$id = and i64 ${leftResult.regName}, ${rightResult.regName}");
Node.addIrLine("%logic.$id = icmp ne i64 %and.$id, 0");
Node.addIrLine("%boolBinOp.$id = zext i1 %logic.$id to i64");
break;
case BoolOperator.or:
Node.addIrLine(
"%and.$id = or i64 ${leftResult.regName}, ${rightResult.regName}");
Node.addIrLine("%logic.$id = icmp ne i64 %and.$id, 0");
Node.addIrLine("%boolBinOp.$id = zext i1 %logic.$id to i64");
break;
default:
throw Exception("Unknown operator: $nodeValue");
}
return LangVal("%boolBinOp.$id", const PrimitiveType(PrimitiveTypes.int));
}