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) {
  if (nodeValue != BoolOperator.not) {
    throw Exception("Invalid operator for BoolUnOp");
  }
  final childResult = child.evaluate(table);
  //cant be array
  //has to be int
  if (childResult.type is! PrimitiveType ||
      childResult.type.primitiveType != PrimitiveTypes.int) {
    throw Exception("Not operantion can only be applied to int");
  }

  //if child is not 0 convert to 0 else convert to 1
  Node.addIrLine("%boolIsZero.$id = icmp eq i64 ${childResult.regName}, 0");
  Node.addIrLine("%boolUnOp.$id = zext i1 %boolIsZero.$id to i64");
  return LangVal("%boolUnOp.$id", const PrimitiveType(PrimitiveTypes.int));
}