AST Evaluator Implementation Notes

Current Status

  • โœ… Complete AST node system with all node types
  • โœ… Enhanced lexer producing proper token streams
  • โœ… Recursive descent parser building AST from tokens
  • ๐Ÿ”„ Need AST evaluator to execute parsed AST

Evaluator Architecture

Core Evaluation Function

typedef struct {
    store* variables;           // Variable storage
    processor_context* ctx;     // XMD processor context
    char* output_buffer;        // Output accumulator
    size_t output_size;         // Buffer size
    bool has_error;            // Error flag
    char* error_message;       // Error details
} ast_evaluator;

Evaluation Strategy

  • ast_evaluate() - Main entry point for any AST node
  • Type-specific evaluation for each AST_* node type
  • Return values stored in unified result structure
  • Integration with existing variable store system

Integration Points

Replace string parsing in:

  • process_set.c:96-180 โ†’ ast_evaluate_assignment()
  • Expression evaluation โ†’ ast_evaluate_expression()
  • Function calls โ†’ ast_evaluate_function_call()

Value System

typedef struct {
    enum { VAL_STRING, VAL_NUMBER, VAL_BOOLEAN } type;
    union {
        char* string_value;
        double number_value; 
        bool boolean_value;
    } value;
} ast_value;

Implementation Priority

  1. Basic literal evaluation (strings, numbers, booleans)
  2. Variable reference resolution
  3. Binary operation evaluation (+, -, *, /, ==, !=, etc.)
  4. Assignment statement execution
  5. Function call execution (import, exec, print)
  6. Integration with existing XMD variable system

Memory Management

  • AST nodes are read-only during evaluation
  • Evaluator creates new ast_value results
  • Proper cleanup of temporary values
  • Integration with existing variable reference counting

Error Handling

  • Detailed error messages with source locations
  • Type checking during evaluation
  • Undefined variable detection
  • Division by zero and other runtime errors