evaluate method
- SymbolTable table
override
Evaluates the array element access by computing the element address and loading its value.
First verifies that the identifier refers to an array, then evaluates the index expression, and finally generates LLVM IR to compute the element address using getelementptr and load the element value.
Parameters:
table
: The symbol table for variable lookup
Returns: A LangVal containing the loaded array element value
Throws:
Implementation
@override
LangVal evaluate(SymbolTable table) {
final varData = table.get(nodeValue);
if (varData == null) {
throw Exception("Variable $nodeValue not found");
}
if (varData.type is! ArrayType) {
throw Exception("Variable $nodeValue is not an array");
}
final indexResult = index.evaluate(table);
if (indexResult.type.primitiveType != PrimitiveTypes.int) {
throw Exception("Index must be int");
}
Node.addIrLine(
"%arrayPtr.$id = getelementptr ${varData.type.primitiveType.irType}, ${varData.type.irType} ${varData.ptrName}, i64 ${indexResult.regName}");
Node.addIrLine(
"%var$id = load ${varData.type.primitiveType.irType}, ptr %arrayPtr.$id");
return LangVal("%var$id", PrimitiveType(varData.type.primitiveType));
}