evaluate method

  1. @override
LangVal<LangType> evaluate(
  1. 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:

  1. Perform semantic analysis (type checking, symbol resolution)
  2. Generate LLVM IR instructions for the node's operation
  3. 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 func = SymbolTable.getFunction(identifier.nodeValue);
  if (func == null) {
    throw Exception("Function ${identifier.nodeValue} not found");
  }
  final argValues = args.evaluate(table);
  final argTypes = func.funcDec.params.map((e) => e.type.nodeValue).toList();
  if (argValues.length != argTypes.length) {
    throw Exception("Argument count mismatch");
  }
  for (var i = 0; i < argValues.length; i++) {
    if (argValues[i].type != argTypes[i]) {
      throw Exception("Argument type mismatch");
    }
  }
  final argStr = argValues.map((e) => "${e.type.irType} ${e.regName}").join(", ");
  Node.addIrLine("%call.$id = call ${func.funcDec.returnType.nodeValue.irType} @${func.name}($argStr)");
  return LangVal("%call.$id", func.funcDec.returnType.nodeValue);
}