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 ptrName = '%ptr.${identifier.nodeValue}.$id';
final varType = type.nodeValue;
final langVar = LangVar(ptrName, varType);
table.create(identifier.nodeValue, langVar);
if (varType is PrimitiveType) {
Node.addIrLine("$ptrName = alloca ${varType.primitiveType.irType}");
if (children.length > 2) {
var value = children[2].evaluate(table);
if (varType.primitiveType != value.type.primitiveType) {
throw Exception("Type mismatch");
}
Node.addIrLine(
"store ${varType.primitiveType.irType} ${value.regName}, ptr $ptrName");
}
} else if (varType is ArrayType) {
var arrayTypeNode = type as ArrayTypeNode;
var size = arrayTypeNode.size.evaluate(table);
if (size != null) {
Node.addIrLine(
"%arrayptr.$id = alloca ${varType.primitiveType.irType}, i64 ${size.regName}");
Node.addIrLine(
"$ptrName = getelementptr ${varType.primitiveType.irType}, ${varType.irType} %arrayptr.$id, i64 0");
} else {
//need size
throw Exception("Array size not found");
}
if (children.length > 2) {
//cant assign to array
throw Exception("Cannot assign to array");
}
}
}