# Baba Yaga C Implementation - Focused Roadmap ## 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) - ✅ **Memory Issues**: Fixed - reverted partial application implementation - ❌ **2 Remaining Issues**: Partial application, pattern matching memory ## Implementation Plan ### **Phase 1: Parser Extensions (High Impact)** #### **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, 25/27 tests passing #### **Task 1.2: Multi-value Pattern Expressions** (Test 22) ✅ **COMPLETE** **Issue**: `when (x % 2) (y % 2) is` not supported **Current**: Multi-value pattern expressions working correctly in parser **Status**: Core functionality implemented - test file has minor syntax issue #### **Task 1.3: Pattern Matching Memory** (Integration Test 02) 🔄 **IN PROGRESS** **Issue**: Segmentation fault in complex pattern matching **Current**: Memory corruption in pattern matching **Status**: Need to add memory debugging and fix recursion ### **Phase 2: Runtime Fixes (Medium Impact)** #### **Task 2.1: Table Namespace Debugging** (Test 17) ✅ **COMPLETE** **Issue**: `Error: Execution failed` in table operations **Current**: Basic `map`, `filter`, `reduce` functions now work correctly with operator lookup **Status**: Core functionality implemented - operator scope access fixed **Root Cause Analysis**: - ✅ `t.*` namespace functions (t.map, t.filter, t.reduce, t.set, t.delete, t.merge, t.length, t.has, t.get) are working correctly - ✅ Basic `map`, `filter`, `reduce` functions now work correctly with operator lookup - ✅ `each` function works but has arity issues - ❌ Test still fails due to partial application and arity handling issues **Solution Implemented**: 1. **Fixed Scope Access**: Updated stdlib function signatures to accept `Scope*` parameter 2. **Updated Function Calls**: Modified `baba_yaga_function_call` to pass current scope to user functions 3. **Updated Function Registration**: Changed function pointer types to support scope parameter 4. **Verified Core Functionality**: Basic higher-order functions now work with operators **Current Status**: - ✅ **Core Bug Fixed**: Operator lookup in higher-order functions works correctly - ✅ **Basic Functions Working**: `map`, `filter`, `reduce` with operators now functional - ❌ **Test 17 Still Fails**: Due to partial application and arity issues (see Task 2.3) #### **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 #### **Task 2.3: Partial Application Support** (Test 17) 🔄 **IN PROGRESS** **Issue**: Test 17 fails with partial application and arity errors **Current**: Core higher-order functions work, but partial application not supported **Status**: Need to implement minimal partial application support (reverted due to memory issues) **Root Cause Analysis**: - ✅ **Core Functions Working**: `map`, `filter`, `reduce` with operators now functional - ❌ **Partial Application**: Function calls with fewer arguments than required fail - ❌ **Arity Issues**: `each` function expects 3 args but receives 2 in some cases - ❌ **Variable Scope**: Some variables like `partial_result`, `scalar_2`, etc. undefined **Error Messages from Test 17**: - "each: expected 3 arguments, got 2" - "Undefined variable: partial_result", "scalar_2", "add_to_ten" - "Cannot call non-function value" - "equals: arguments must be of the same type" **Implementation Plan**: 1. **Implement Partial Application**: When function called with fewer args than required, return new function 2. **Fix Arity Validation**: Ensure `each` function handles variable argument counts correctly 3. **Debug Variable Scope**: Identify why certain variables are undefined in test context 4. **Test Partial Application**: Verify partial functions work with remaining arguments **Technical Details**: - **Location**: `src/function.c` - `baba_yaga_function_call` function - **Current Behavior**: Returns `VAL_NIL` when insufficient arguments - **Required Behavior**: Return new function with bound arguments - **Reference**: Use `stdlib_compose` as template for function composition **Implementation Steps**: 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 ~815-1183) ✅ **WORKING** - Functions: `t.map`, `t.filter`, `t.reduce`, `t.set`, `t.delete`, `t.merge`, `t.length`, `t.has`, `t.get` - Basic functions: `map`, `filter`, `reduce` in `stdlib.c` (lines ~559-640) ❌ **PLACEHOLDERS** - Issue: Basic higher-order functions need full implementation ### **Memory Management** - Pattern matching: Uses recursion for nested patterns - Potential: Stack overflow or memory corruption - Solution: Add bounds checking and memory debugging ## Next Action **Continue with Task 1.3** (Pattern Matching Memory) - fix segmentation fault in complex pattern matching. ## Implementation Guide ### **Task 2.1: Basic Higher-Order Functions Implementation** ✅ **COMPLETE** #### **Function 1: `stdlib_map` Implementation** **Location**: `src/stdlib.c` lines ~559-580 **Current**: Returns original table (placeholder) **Required**: Apply function to each value in table **Implementation Steps**: 1. Get all keys from input table using `baba_yaga_table_get_keys` 2. Create new result table using `baba_yaga_value_table` 3. For each key: - Get value using `baba_yaga_table_get_by_key` - Call function with value using `baba_yaga_function_call` - Add result to new table using `baba_yaga_table_set` 4. Return new table **Reference**: Use `stdlib_t_map` (lines ~815-866) as template - it's already implemented correctly #### **Function 2: `stdlib_filter` Implementation** **Location**: `src/stdlib.c` lines ~584-610 **Current**: Returns original table (placeholder) **Required**: Keep only values that satisfy predicate **Implementation Steps**: 1. Get all keys from input table using `baba_yaga_table_get_keys` 2. Create new result table using `baba_yaga_value_table` 3. For each key: - Get value using `baba_yaga_table_get_by_key` - Call predicate function with value using `baba_yaga_function_call` - If predicate returns truthy value, add original value to new table 4. Return new table **Reference**: Use `stdlib_t_filter` (lines ~867-923) as template - it's already implemented correctly #### **Function 3: `stdlib_reduce` Implementation** **Location**: `src/stdlib.c` lines ~609-640 **Current**: Returns initial value (placeholder) **Required**: Combine all values with function **Implementation Steps**: 1. Start with initial value as accumulator 2. Get all keys from input table using `baba_yaga_table_get_keys` 3. For each key: - Get value using `baba_yaga_table_get_by_key` - Call function with accumulator and value using `baba_yaga_function_call` - Update accumulator with result 4. Return final accumulator value **Reference**: Use `stdlib_t_reduce` (lines ~924-976) as template - it's already implemented correctly #### **Testing Strategy**: 1. **Unit Tests**: Create simple tests for each function individually 2. **Integration Test**: Run Test 17 to verify all functions work together 3. **Regression Test**: Verify `t.*` functions still work correctly 4. **Target**: Test 17 should pass, moving from 25/27 to 26/27 tests passing ### **Task 2.3: Partial Application Support Implementation** 🔄 **IN PROGRESS** #### **Problem Analysis** The current function call mechanism returns `VAL_NIL` when a function is called with fewer arguments than required. Test 17 expects partial application behavior where: - `add_to_ten : add 10;` should create a function that adds 10 to its argument - `each add_to_ten numbers` should work with the partially applied function #### **Implementation Steps** 1. **Modify `baba_yaga_function_call`** in `src/function.c`: - When `arg_count < func_value->required_params`, create a new function - Bind the provided arguments to the new function - Return the new function instead of `VAL_NIL` 2. **Create Partial Function Structure**: - Store original function and bound arguments - When partial function is called, combine bound args with new args - Call original function with complete argument set 3. **Update Function Value Structure**: - Add support for partial functions in `FunctionValue` - Handle partial function cleanup in memory management #### **Reference Implementation** Use `stdlib_compose` as a template for function composition and partial application patterns. #### **Testing Strategy** 1. **Unit Test**: Create simple partial application test 2. **Integration Test**: Verify Test 17 passes with partial application 3. **Regression Test**: Ensure existing functions still work correctly