XDL Array Features Implementation
Summary
Complete implementation of advanced array features for the XDL interpreter, including negative indexing, multi-dimensional arrays (matrices), array element assignment, and array arithmetic operations.
Implemented Features
1. Array Literals
- Basic array syntax:
arr = [1, 2, 3, 4, 5] - Empty arrays:
empty = [] - Nested arrays (matrices):
matrix = [[1, 2], [3, 4]]
2. Array Indexing
- Positive indexing:
arr[0],arr[2] - Negative indexing:
arr[-1](last element),arr[-2](second to last) - Multi-dimensional indexing:
matrix[0, 1],matrix[1, 1] - Row access:
matrix[0]returns entire first row
3. Array Slicing
- Range slicing:
arr[0:3],arr[1:4] - Open-ended slicing:
arr[2:](from index to end) - Step slicing:
arr[0:10:2](every 2nd element)
4. Array Arithmetic
- Array-array operations:
arr1 + arr2,arr1 - arr2,arr1 * arr2,arr1 / arr2 - Array-scalar operations:
arr * 2,arr + 10,arr mod 7 - Scalar-array operations:
2 * arr,10 + arr - Modulo operator:
10 mod 3,arr mod 7(works with scalars and arrays)
5. Array Element Assignment
- Single element:
arr[0] = 100 - Negative indices:
arr[-1] = 999 - Multi-dimensional:
grid[0, 0] = 1,matrix[i, j] = value - In loops: Fibonacci, matrix building, array reversal
Architecture Changes
Added NestedArray Type
- Added
XdlValue::NestedArray(Vec<XdlValue>)to support matrices - Automatic detection: arrays of arrays become NestedArray
- Proper string representation for nested structures
Unified Execution Path
- Removed: Duplicate
Executorstruct inexecutor.rs - Kept: Single
Interpreterstruct inlib.rsas the canonical implementation - All array operations go through one code path for consistency
Core Implementation Files
xdl-core/src/types.rs: Added NestedArray variant to XdlValuexdl-interpreter/src/lib.rs: Main interpreter with array assignment logicxdl-interpreter/src/evaluator.rs: Array evaluation and indexingxdl-parser/: Array parsing (already supported)
Test Coverage
Basic Tests (examples/test_arrays.xdl)
- Array literals and empty arrays
- Positive and negative indexing
- Array slicing
- Array arithmetic
- Nested arrays (matrices)
- Simple element assignment
Advanced Tests (examples/test_advanced_arrays.xdl)
- Comprehensive negative index testing
- Multi-dimensional indexing with all combinations
- Array element assignment in various contexts
- Combined operations (negative indices in calculations)
- Array element swapping
- Matrix diagonal operations
- Building arrays with assignments (Fibonacci, multiplication tables)
- Array reversal algorithms
- Boundary and edge cases
Updated Tests
control_flow_tests.xdl: Uses new array features- All examples verified to work with current capabilities
Usage Examples
; Basic array operations
arr = [1, 2, 3, 4, 5]
print, arr[0] ; 1
print, arr[-1] ; 5
arr[2] = 99 ; [1, 2, 99, 4, 5]
; Matrix operations
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print, matrix[1, 1] ; 5 (center element)
matrix[0, 0] = 100 ; Modify top-left
print, matrix[0] ; [100, 2, 3] (first row)
; Array building with loops
fib = [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
for i = 2, 9
fib[i] = fib[i-1] + fib[i-2]
endfor
; fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
; Matrix building
mult_table = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
for i = 0, 2
for j = 0, 2
mult_table[i, j] = (i + 1) * (j + 1)
endfor
endfor
Known Limitations
- Array slicing with negative indices: Currently
arr[-3:-1]returns empty array (needs refinement) - Array creation functions: Functions like
fltarr(),intarr()mentioned in old tests may not be fully implemented - Range assignment:
arr[0:3] = valuenot yet supported - System constants: Some math constants like
!pimay not be available
Future Enhancements
- Implement missing slice edge cases with negative indices
- Add array creation functions (fltarr, intarr, etc.)
- Support range assignment operations
- Add array broadcasting for mismatched dimensions
- Implement array methods (min, max, sum, etc.)
- Add support for more complex indexing patterns
Testing
Run all array tests:
cargo build --release
./target/release/xdl examples/test_arrays.xdl
./target/release/xdl examples/test_advanced_arrays.xdl
Compliance
- Code formatted with
cargo fmt --allbefore commit - All builds pass without warnings
- Tests verify functionality end-to-end