AST Evaluator Implementation Notes
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
- Basic literal evaluation (strings, numbers, booleans)
- Variable reference resolution
- Binary operation evaluation (+, -, *, /, ==, !=, etc.)
- Assignment statement execution
- Function call execution (import, exec, print)
- 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