diff options
Diffstat (limited to 'js/scripting-lang/baba-yaga-c/ROADMAP.md')
-rw-r--r-- | js/scripting-lang/baba-yaga-c/ROADMAP.md | 685 |
1 files changed, 68 insertions, 617 deletions
diff --git a/js/scripting-lang/baba-yaga-c/ROADMAP.md b/js/scripting-lang/baba-yaga-c/ROADMAP.md index 991ac2f..aba5e4d 100644 --- a/js/scripting-lang/baba-yaga-c/ROADMAP.md +++ b/js/scripting-lang/baba-yaga-c/ROADMAP.md @@ -1,632 +1,83 @@ -# Baba Yaga C Implementation - Complete Roadmap +# Baba Yaga C Implementation - Focused Roadmap -## Executive Summary +## Current Status +- ✅ **Core Language**: Complete and stable (24/27 tests passing) +- ✅ **Table Pattern Matching**: Fixed and working +- ✅ **When Expressions**: Fixed and working +- ✅ **Computed Table Keys**: Fixed and working (Task 1.1 complete) +- ❌ **3 Remaining Issues**: Pattern expressions, table namespace, memory fixes needed -**Current Status**: ✅ **Core Functionality Complete** - Major language features implemented -**Overall Progress**: ~90% Complete -**Test Results**: 26/26 basic tests PASS, 60/66 standard library tests PASS, 4/27 JavaScript tests PASS -**Estimated Completion**: 1 week +## Implementation Plan -## Language Overview +### **Phase 1: Parser Extensions (High Impact)** -Baba Yaga is a functional programming language with combinator-based architecture where all operators translate to function calls. The C implementation provides a high-performance, memory-efficient interpreter that maintains compatibility with the JavaScript reference implementation. +#### **Task 1.1: Computed Table Keys** (Test 15) ✅ **COMPLETE** +**Issue**: `{(1 + 1): "two"}` not supported +**Solution**: Extended table key parsing with expression support +**Implementation**: Added `TOKEN_LPAREN` detection and expression parsing logic +**Result**: Test 15 now passes, 24/27 tests passing -### Key Features -- **Function Application**: Juxtaposition-based (`f x` means `apply(f, x)`) -- **Function References**: `@` operator for function references and expressions (`@function` and `@(expression)`) -- **Pattern Matching**: `when` expressions for conditional logic with wildcard support -- **User-Defined Functions**: `name : params -> body;` syntax -- **Tables**: APL-inspired data structures (planned) -- **Combinator Foundation**: All operations become function calls -- **Multiple Statements**: Semicolon-separated statement sequences +#### **Task 1.2: Multi-value Pattern Expressions** (Test 22) 🔄 **IN PROGRESS** +**Issue**: `when (x % 2) (y % 2) is` not supported +**Current**: Parse error - expression parsing consumes too many tokens +**Next**: Implement bounded expression parsing or sequence pattern matching - - -## Current Implementation Status - -### ✅ COMPLETED - Core Functionality - -#### Phase 1: Foundation (COMPLETE) -- **Value System**: ✅ Complete with all data types (number, string, boolean, table, function, nil) -- **Table Implementation**: ✅ Immutable hash table with reference counting -- **Memory Management**: ✅ Reference counting for tables and functions -- **Function Representation**: ✅ Native and user function support - -#### Phase 2: Lexer (COMPLETE) -- **Tokenization**: ✅ Complete with all token types including IO operations -- **Special Cases**: ✅ Unary/binary minus handling, function references (`@function`) -- **Error Handling**: ✅ Line/column information in error messages -- **Performance**: ✅ Efficient tokenization for large files - -#### Phase 3: Parser (COMPLETE) -- **AST Construction**: ✅ Complete AST node types for all language constructs -- **Operator Precedence**: ✅ Proper precedence handling with combinator translation -- **Combinator Translation**: ✅ All operators translate to function calls -- **Pattern Matching**: ✅ Complete pattern parsing with `when` expressions -- **Function Definitions**: ✅ User-defined function parsing with `->` syntax -- **Function References**: ✅ `@` operator parsing for both `@function` and `@(expression)` - -#### Phase 4: Interpreter Foundation (COMPLETE) -- **Scope Management**: ✅ Complete scope system with variable lookup and assignment -- **Function Calling**: ✅ Native function calling with proper argument passing -- **Standard Library**: ✅ Complete standard library with 20+ functions -- **Error Handling**: ✅ Runtime error detection and reporting infrastructure - -#### Phase 5: Enhanced Interpreter (COMPLETE) -- **Full AST Execution**: ✅ Complete expression evaluation pipeline -- **Function References**: ✅ @ operator support for function references and expressions -- **Pattern Matching**: ✅ Complete `when` expression evaluation with wildcard support -- **User-Defined Functions**: ✅ Function creation, parameter binding, and execution -- **Basic Integration**: ✅ Complete lexer → parser → interpreter pipeline - -#### Phase 6: Multiple Statement Support (COMPLETE) -- **Sequence Parsing**: ✅ Parse semicolon-separated statements into sequence nodes -- **Statement Execution**: ✅ Execute statements in sequence, return last value -- **Variable Scoping**: ✅ Variables defined in earlier statements available in later ones -- **Real-world Usage**: ✅ Language now supports practical multi-statement programs - -## 🔴 CRITICAL ISSUES TO FIX - -### 1. Multiple Statement Parsing (CRITICAL) ✅ **COMPLETED** -**Current Status**: ✅ All statements executed in sequence -**Impact**: ✅ Unblocks real-world usage -**Priority**: ✅ RESOLVED -**Effort**: ✅ Completed - -**Implementation**: -- ✅ Implemented `NODE_SEQUENCE` AST node type -- ✅ Created `parser_parse_statements()` for semicolon-separated parsing -- ✅ Updated interpreter to execute statement sequences -- ✅ Added sequence node accessor functions -- ✅ Updated test suite to reflect working functionality - -**Example Working**: -```c -// ✅ "x : 10; y : 20; add x y" → executes all three statements, returns 30 -// ✅ "a : 5; b : 3; c : 2; add a @multiply b c" → returns 11 -``` - -### 2. JavaScript Test Suite Compatibility (HIGH PRIORITY) 🔄 **IN PROGRESS** -**Current Status**: 4/27 tests pass (15% success rate) -**Priority**: HIGH -**Effort**: 1 week - -**Major Missing Features**: - -#### Pattern Matching (Critical - 8+ tests) ✅ **COMPLETED** -- `when` expressions: `when x is 42 then "correct" _ then "wrong"` -- **Status**: ✅ Fully implemented and working -- **Features**: Basic patterns, multiple patterns, wildcard (`_`) support, variable assignment context -- **Fix Applied**: Added `TOKEN_KEYWORD_WHEN`, `TOKEN_KEYWORD_IS`, `TOKEN_KEYWORD_THEN` to function call parsing stop tokens -- **Impact**: Unblocks tests 01, 07, 21, and integration tests -- **Result**: Pattern matching now works in all contexts - -#### Function Definitions (High - 3+ tests) ✅ **COMPLETED** -- User-defined functions: `name : params -> body;` -- **Status**: ✅ Fully implemented and working -- **Features**: Function definition parsing, function storage, parameter binding, function execution -- **Working**: Identity functions, simple parameter binding, `@` operator support -- **Issue**: Function calls within function bodies (e.g., `add x y`) return `nil` instead of expected result -- **Root Cause**: Function call arguments in function bodies are not being evaluated to their values -- **Impact**: Unblocks basic function tests (test 06 passes), but complex function bodies still fail -- **Result**: Function definitions work for simple cases, complex cases need debugging - -#### Tables (High - 5+ tests) -- Data structures: `data : {a: 1, b: 2};` -- **Impact**: Blocks tests 09, 12, 17 -- **Effort**: 3-4 hours - -#### Advanced Features (Medium - 8+ tests) -- Each combinator, embedded functions, via operator, etc. -- **Impact**: Blocks tests 18-23 -- **Effort**: 3-4 hours - -**Tasks**: -- [x] Implement pattern matching (`when` expressions) ✅ -- [x] Debug variable assignment context for `when` expressions ✅ -- [x] Add user-defined function syntax (`->`) ✅ -- [x] Implement `@` operator for function references and expressions ✅ -- [ ] Debug function calls within function bodies (CRITICAL - blocks complex functions) -- [ ] Implement table data structures -- [ ] Add advanced functional programming features (each combinator, via operator, etc.) - -### 3. Standard Library Issues (MEDIUM PRIORITY) ✅ **MOSTLY COMPLETED** -**Current Status**: 60/66 tests pass (91% success rate) -**Priority**: MEDIUM -**Effort**: 1-2 days - -**Specific Failures**: - -#### Type Checking (3 failures) -- `equals` function: Missing type mismatch error for non-number arguments -- `and` function: Missing type mismatch error for non-boolean arguments -- `not` function: Missing type mismatch error for non-boolean arguments -- **Root Cause**: Type validation not implemented for these functions - -**Tasks**: -- [ ] Add type validation to `equals` function -- [ ] Add type validation to `and` function -- [ ] Add type validation to `not` function - -#### Precision Issues (3 failures) -- `divide` function: Insufficient precision for floating point division -- `pow` function: Insufficient precision for square root and negative powers -- **Root Cause**: Limited floating point precision in output formatting - -**Tasks**: -- [ ] Fix `..out` function to output only once -- [ ] Fix `..assert` function to return false instead of failing -- [ ] Implement `..in` function - -#### Type Checking (3 failures) -- Comparison operators: Missing type mismatch errors -- Logical operators: Missing type mismatch errors -- **Impact**: Inconsistent error handling - -**Tasks**: -- [ ] Add type checking for comparison operators -- [ ] Add type checking for logical operators -- [ ] Improve error messages for type mismatches - -#### Precision Issues (3 failures) -- Floating point division: Limited precision -- Square root: Limited precision -- Negative power: "Error: Execution failed" - -**Tasks**: -- [ ] Fix floating point division precision -- [ ] Fix square root precision -- [ ] Fix negative power handling - -## 🟡 MEDIUM PRIORITY ISSUES - -### 3. User-Defined Functions (MEDIUM) -**Current Status**: Not implemented -**Priority**: MEDIUM -**Effort**: 3-4 days - -**Required Features**: -- Function body storage in AST -- Parameter binding during calls -- Local scope creation -- Recursive function support - -**Tasks**: -- [ ] Implement function body storage in AST -- [ ] Add parameter binding during function calls -- [ ] Create local scope creation for function execution -- [ ] Support recursive function calls -- [ ] Add stack overflow protection - -### 4. Pattern Matching Evaluation (MEDIUM) -**Current Status**: Basic parsing complete, evaluation pending -**Priority**: MEDIUM -**Effort**: 2-3 days - -**Required Features**: -- Boolean expression evaluation in patterns -- Multiple value patterns -- Pattern guards - -**Tasks**: -- [ ] Implement pattern matching engine -- [ ] Add boolean expression evaluation in patterns -- [ ] Support multiple value patterns -- [ ] Add wildcard pattern support -- [ ] Implement enhanced case statements - -### 5. Enhanced Error Handling (MEDIUM) -**Current Status**: Basic error detection -**Priority**: MEDIUM -**Effort**: 1-2 days - -**Required Features**: -- Specific error messages with context -- Line/column numbers in errors -- Source code snippets -- Stack traces for function calls - -**Tasks**: -- [ ] Add specific error messages (division by zero, undefined variable, etc.) -- [ ] Add error context (line/column numbers, source snippets) -- [ ] Add error recovery where possible -- [ ] Improve error message formatting - -## 🔵 LOW PRIORITY ISSUES - -### 6. Memory Leak Testing (LOW) -**Current Status**: Not tested -**Priority**: LOW -**Effort**: 1 day - -**Tasks**: -- [ ] Add valgrind to build system -- [ ] Create `make memcheck` target -- [ ] Test all components for leaks -- [ ] Fix any memory issues found -- [ ] Add memory usage monitoring - -### 7. Performance Optimization (LOW) -**Current Status**: Basic performance -**Priority**: LOW -**Effort**: 1-2 days - -**Tasks**: -- [ ] Profile critical execution paths -- [ ] Optimize memory allocation patterns -- [ ] Add performance benchmarks -- [ ] Optimize AST accessor functions - -### 8. Documentation and Testing (LOW) -**Current Status**: Basic documentation -**Priority**: LOW -**Effort**: 1 day - -**Tasks**: -- [ ] Complete API documentation -- [ ] Add comprehensive test suite -- [ ] Create architecture documentation -- [ ] Add usage examples - -## Current State - For New Team Members - -### What's Working ✅ - -#### Core Language Features -- **Basic Expressions**: Numbers, strings, booleans, variables -- **Arithmetic Operations**: `add`, `subtract`, `multiply`, `divide`, `modulo`, `pow` -- **Comparison Operations**: `equals`, `not_equals`, `less`, `greater`, etc. -- **Logical Operations**: `and`, `or`, `xor`, `not` -- **Function Calls**: Native function calling with proper argument evaluation -- **Function References**: `@function` and `@(expression)` syntax -- **Variable Assignment**: `name : value;` syntax -- **Multiple Statements**: Semicolon-separated sequences execute correctly - -#### Advanced Features -- **Pattern Matching**: `when` expressions with wildcard (`_`) support -- **User-Defined Functions**: `name : params -> body;` syntax -- **Higher-Order Functions**: `apply`, `compose` working correctly -- **IO Operations**: `..out`, `..assert` working correctly - -#### Test Results -- **Basic Tests**: 26/26 PASS (100%) -- **Standard Library**: 60/66 PASS (91%) -- **JavaScript Compatibility**: 4/27 PASS (15%) - -### What's Partially Working ⚠️ - -#### User-Defined Functions -- ✅ Function definition parsing works -- ✅ Function storage in scope works -- ✅ Parameter binding works -- ✅ Simple function bodies work (e.g., `x -> x`) -- ❌ Complex function bodies fail (e.g., `x y -> add x y` returns `nil`) - -**Example Working**: -```c -identity_func : x -> x; // ✅ Works -identity_func 42; // ✅ Returns 42 -``` - -**Example Failing**: -```c -add_func : x y -> add x y; // ❌ Function body returns nil -add_func 3 4; // ❌ Returns nil instead of 7 -``` - -### What's Not Implemented ❌ - -#### Tables -- Table literals: `{a: 1, b: 2}` -- Table access: `table.key` or `table["key"]` -- Table operations: `keys`, `values`, `size` - -#### Advanced Functional Features -- Each combinator: `each function list` -- Via operator: `via function value` -- Embedded functions: Functions within expressions -- Function composition with multiple functions - -#### Syntax Differences from JavaScript -- **Infix Operators**: JavaScript uses `a + b`, C uses `add a b` -- **Assertion Syntax**: JavaScript uses `..assert sum = 13`, C uses `..assert equals sum 13` - -### Critical Issues to Address - -#### 1. Function Calls in Function Bodies (HIGH PRIORITY) -**Problem**: Function calls like `add x y` within function bodies return `nil` -**Root Cause**: Function call arguments are not being evaluated to their values -**Impact**: Blocks complex user-defined functions -**Files to Check**: `src/interpreter.c` function call evaluation, `src/function.c` user function execution - -#### 2. Table Implementation (MEDIUM PRIORITY) -**Problem**: Table data structures not implemented -**Impact**: Blocks tests 09, 12, 17 -**Files to Check**: `src/table.c` (exists but needs implementation) - -#### 3. Advanced Combinators (MEDIUM PRIORITY) -**Problem**: Each combinator, via operator not implemented -**Impact**: Blocks tests 18-23 -**Files to Check**: `src/stdlib.c` for combinator implementations - -### Code Architecture - -#### Key Files -- `src/lexer.c`: Tokenization (✅ Complete) -- `src/parser.c`: AST construction (✅ Complete) -- `src/interpreter.c`: Expression evaluation (✅ Complete) -- `src/function.c`: Function management (✅ Complete) -- `src/scope.c`: Variable scoping (✅ Complete) -- `src/stdlib.c`: Standard library (✅ Complete) -- `src/table.c`: Table implementation (❌ Needs work) - -#### Key Data Structures -- `Value`: Union type for all values (number, string, boolean, function, table, nil) -- `ASTNode`: Abstract syntax tree nodes -- `Scope`: Variable scope with parent-child relationships -- `FunctionValue`: Function representation with native/user distinction - -#### Key Functions -- `baba_yaga_execute()`: Main execution entry point -- `interpreter_evaluate_expression()`: Core evaluation logic -- `baba_yaga_function_call()`: Function calling mechanism -- `scope_define()` / `scope_get()`: Variable management - -### Testing Strategy - -#### Test Suites -- `run_tests.sh`: Basic functionality tests (26 tests, all pass) -- `test_stdlib.sh`: Standard library tests (66 tests, 60 pass) -- `run_comprehensive_tests.sh`: JavaScript compatibility tests (27 tests, 4 pass) - -#### Debugging Commands -```bash -# Build with debug info -make debug - -# Test specific features -./bin/baba-yaga 'add 3 4;' # Basic function call -./bin/baba-yaga '@(add 3 4);' # Function reference -./bin/baba-yaga 'x : 42; x;' # Variable assignment -./bin/baba-yaga 'when 42 is 42 then "yes";' # Pattern matching -./bin/baba-yaga 'f : x -> x; f 42;' # User-defined function - -# Run test suites -./run_tests.sh -./test_stdlib.sh -./run_comprehensive_tests.sh -``` - -## Implementation Roadmap - -### Week 1: Critical Fixes - -#### Days 1-2: Multiple Statement Parsing (CRITICAL) -**Dependencies**: None -**Success Criteria**: -- `"x : 10; y : 20; add x y"` executes all statements -- Variable scoping works across statements -- Return value is from last statement - -#### Days 3-5: Standard Library Fixes (HIGH) -**Dependencies**: Multiple statement parsing -**Success Criteria**: -- All 66 standard library tests pass -- Higher-order functions work correctly -- IO operations behave as expected -- Type errors are properly reported - -### Week 2: Advanced Features - -#### Days 1-3: User-Defined Functions (MEDIUM) -**Dependencies**: Standard library fixes -**Success Criteria**: -- User-defined functions can be created and called -- Parameters are properly bound -- Local variables work correctly -- Recursive functions work - -#### Days 4-5: Pattern Matching and Polish (MEDIUM/LOW) -**Dependencies**: User-defined functions -**Success Criteria**: -- Complex when expressions work -- Boolean patterns evaluate correctly -- Multi-parameter patterns work -- Pattern guards function properly -- No memory leaks detected -- Comprehensive error messages - -## Test Results Analysis - -### ✅ Passing Tests (90/119 = 76%) -- **Basic Functionality**: 26/26 tests PASS (100%) -- **Standard Library**: 60/66 tests PASS (91%) -- **JavaScript Compatibility**: 4/27 tests PASS (15%) - -#### Detailed Breakdown - -**Basic Tests (26/26 PASS)**: -- Arithmetic operations, function calls, variable assignment -- Multiple statements, higher-order functions, IO operations -- Error handling, pattern matching basics - -**Standard Library Tests (60/66 PASS)**: -- Core arithmetic: `add`, `subtract`, `multiply`, `divide`, `modulo`, `pow` -- Comparison: `equals`, `not_equals`, `less`, `greater`, etc. -- Logical: `and`, `or`, `xor`, `not` -- Higher-order: `apply`, `compose` -- IO: `..out`, `..assert` - -**JavaScript Compatibility Tests (4/27 PASS)**: -- ✅ `02_arithmetic_operations`: Basic arithmetic working -- ✅ `04_logical_operators`: Logical operations working -- ✅ `06_function_definitions`: Basic function definitions working -- ✅ `10_standard_library`: Standard library working - -### ❌ Failing Tests (29/119 = 24%) - -#### Standard Library Failures (6/66): -- **Type Checking**: 3 failures (equals, and, not type mismatches) -- **Precision**: 3 failures (division, sqrt, negative power) - -#### JavaScript Compatibility Failures (23/27): -- **Syntax Differences**: Most failures due to infix vs prefix operator syntax -- **Missing Features**: Tables, advanced combinators, embedded functions -- **Function Body Issues**: Complex function bodies returning `nil` - -## Success Metrics - -### Current Status -- ✅ Basic functionality: 26/26 tests PASS (100%) -- ⚠️ Standard library: 60/66 tests PASS (91%) -- ✅ Multiple statements: Fully working -- ✅ Higher-order functions: Fully working -- ✅ IO operations: Fully working -- ✅ Pattern matching: Fully working -- ✅ Function definitions: Basic cases working -- ✅ Function references: `@` operator working -- ⚠️ Error handling: Basic only -- ❌ Tables: Not implemented -- ❌ Advanced combinators: Not implemented - -### Target Status (End of Week 1) -- ✅ All basic functionality: 26/26 tests PASS (100%) -- ✅ Standard library: 66/66 tests PASS (100%) -- ✅ Function definitions: Complex cases working -- ✅ Tables: Basic implementation -- ✅ Advanced combinators: Core implementation -- ✅ JavaScript compatibility: 15+/27 tests PASS -- ✅ Error handling: Comprehensive -- ✅ Memory management: Leak-free - -## Quick Start Commands - -### Build and Test -```bash -# Build with debug info -make debug - -# Test basic functionality (all working ✅) -./bin/baba-yaga '5 + 3;' # Should output: 8 -./bin/baba-yaga '10 - 3;' # Should output: 7 -./bin/baba-yaga '6 * 7;' # Should output: 42 -./bin/baba-yaga 'x : 42; x;' # Should output: 42 -./bin/baba-yaga 'add 5 3;' # Should output: 8 -./bin/baba-yaga '@multiply 2 3;' # Should output: 6 -./bin/baba-yaga 'add 5 @multiply 3 4;' # Should output: 17 - -# Multiple statements now working ✅ -./bin/baba-yaga 'x : 10; y : 20; add x y;' # ✅ Executes all statements, returns 30 - -# Run test suite -./run_tests.sh -./test_stdlib.sh - -# Check for memory leaks -valgrind --leak-check=full ./bin/baba-yaga '5 + 3;' -``` - -## Risk Assessment - -### High Risk -- **Memory Leaks**: Complex memory management in C -- **Mitigation**: Comprehensive testing with valgrind - -### Medium Risk -- **Performance Issues**: Can optimize after functionality complete -- **Cross-Platform Issues**: Test on multiple platforms - -### Low Risk -- **Complex Nested Function References**: Known limitation, not blocking -- **Multiple Statement Parsing**: ✅ Completed successfully - -## Conclusion - -The Baba Yaga C implementation is in excellent shape with 76% of functionality working correctly. Major language features including pattern matching, function definitions, and function references are fully implemented and working. The critical blocker of multiple statement parsing has been resolved, unlocking real-world usage. - -### Key Achievements -- ✅ **Pattern Matching**: Complete `when` expression support with wildcard patterns -- ✅ **Function Definitions**: User-defined functions with parameter binding -- ✅ **Function References**: `@` operator for both `@function` and `@(expression)` syntax -- ✅ **Multiple Statements**: Semicolon-separated statement sequences -- ✅ **Core Language**: All basic operations and standard library functions working - -### Remaining Work -- 🔧 **Function Body Debugging**: Fix function calls within function bodies returning `nil` -- 📊 **Tables**: Implement table data structures and operations -- 🔄 **Advanced Combinators**: Implement each combinator, via operator, etc. -- 🎯 **JavaScript Compatibility**: Address syntax differences and missing features - -The implementation follows the same architecture as the JavaScript version, ensuring consistency and maintainability. The C version provides better performance and memory efficiency while maintaining the same language semantics. - -**Next Action**: Debug function calls within function bodies to enable complex user-defined functions. - -**Estimated completion for full implementation**: 1 week remaining. - -## Immediate Next Steps for New Team Members - -### 1. Debug Function Calls in Function Bodies (CRITICAL - 1-2 days) -**Problem**: `add_func : x y -> add x y; add_func 3 4` returns `nil` instead of `7` - -**Investigation Steps**: -1. Add debug output to `src/interpreter.c` in `NODE_FUNCTION_CALL` case -2. Check if function call arguments are being evaluated correctly -3. Verify that `x` and `y` are being resolved to their values in the function scope -4. Test with simple cases: `f : x -> x; f 42` (works) vs `f : x -> add x 1; f 41` (fails) - -**Files to Modify**: -- `src/interpreter.c`: Add debug output to function call evaluation -- `src/function.c`: Check user function execution logic - -### 2. Implement Tables (MEDIUM - 2-3 days) -**Problem**: Table literals and operations not implemented +#### **Task 1.2: Multi-value Pattern Expressions** (Test 22) +**Issue**: `when (x % 2) (y % 2) is` not supported +**Current**: Only literal patterns in multi-value +**Fix**: Extend pattern parsing in `parser_parse_when_pattern` **Implementation Steps**: -1. Implement table literal parsing in `src/parser.c` -2. Add table operations to `src/table.c` -3. Update `src/stdlib.c` with table functions (`keys`, `values`, `size`) -4. Test with: `data : {a: 1, b: 2}; data.a` +1. Modify pattern detection logic (lines ~2640-2670 in parser.c) +2. Add support for `TOKEN_LPAREN` as valid pattern start +3. Parse expression patterns using `parser_parse_expression` +4. Test with `when (x % 2) (y % 2) is` -**Files to Modify**: -- `src/parser.c`: Add `NODE_TABLE` parsing -- `src/table.c`: Implement table operations -- `src/stdlib.c`: Add table utility functions +### **Phase 2: Runtime Fixes (Medium Impact)** -### 3. Implement Advanced Combinators (MEDIUM - 2-3 days) -**Problem**: Each combinator, via operator not implemented +#### **Task 2.1: Table Namespace Debugging** (Test 17) +**Issue**: `Error: Execution failed` in table operations +**Current**: `t.*` functions implemented but failing +**Fix**: Debug existing implementation **Implementation Steps**: -1. Add `each` combinator to `src/stdlib.c` -2. Add `via` operator support -3. Test with: `each add [1, 2, 3]` and `via add 5` - -**Files to Modify**: -- `src/stdlib.c`: Add combinator implementations -- `src/parser.c`: Add combinator parsing if needed +1. Add debug output to `stdlib_t_map`, `stdlib_t_filter`, etc. +2. Run test 17 with `DEBUG=4` to identify specific failure +3. Fix parameter validation or table iteration logic +4. Test with table enhancement operations -### 4. Fix Standard Library Issues (LOW - 1 day) -**Problem**: 6 standard library tests failing +#### **Task 2.2: Pattern Matching Memory** (Integration Test 02) +**Issue**: Segmentation fault in complex patterns +**Current**: Memory corruption in pattern matching +**Fix**: Add memory debugging and fix recursion **Implementation Steps**: -1. Add type checking to `equals`, `and`, `not` functions -2. Fix floating point precision issues -3. Run `./test_stdlib.sh` to verify fixes - -**Files to Modify**: -- `src/stdlib.c`: Add type validation and fix precision - -### Getting Started Commands -```bash -# Build and test current state -make debug -./run_comprehensive_tests.sh - -# Debug function body issue -./bin/baba-yaga 'f : x -> add x 1; f 41;' - -# Test pattern matching (working) -./bin/baba-yaga 'when 42 is 42 then "yes" _ then "no";' - -# Test function references (working) -./bin/baba-yaga '@(add 3 4);' -``` \ No newline at end of file +1. Add memory debugging to `interpreter_evaluate_when_expression` +2. Check for infinite recursion in pattern matching +3. Fix memory allocation/deallocation in pattern evaluation +4. Test with complex pattern matching scenarios + +### **Phase 3: Validation** +- Re-run comprehensive test suite +- Target: 27/27 tests passing +- Verify no regressions + +## Technical Notes + +### **Parser Architecture** +- Table parsing: `parser_parse_primary` → `TOKEN_LBRACE` case +- Pattern parsing: `parser_parse_when_pattern` → multi-parameter detection +- Both need expression support in parentheses + +### **Standard Library** +- `t.*` functions: Already implemented in `stdlib.c` (lines ~804-950) +- Functions: `t.map`, `t.filter`, `t.reduce`, `t.set`, `t.delete`, `t.merge`, `t.length`, `t.has` +- Issue: Likely parameter validation or table iteration + +### **Memory Management** +- Pattern matching: Uses recursion for nested patterns +- Potential: Stack overflow or memory corruption +- Solution: Add bounds checking and memory debugging + +## Next Action +**Proceed with Task 1.2** (Multi-value Pattern Expressions) - next high-impact parser extension. \ No newline at end of file |