about summary refs log tree commit diff stats
path: root/js/baba-yaga/scratch/docs/REIMPLEMENTATION_GUIDE.md
blob: 3e6f2e06c6e269d6ae63edcc8669029cc7d6dad8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# Baba Yaga Reimplementation Guide

This guide outlines how to reimplement the Baba Yaga functional language in a faster, compiled language. While the current JavaScript implementation serves as an excellent prototype, a native implementation could provide significant performance improvements and better integration capabilities.

## Language Recommendation: Rust

After analyzing the requirements, **Rust** emerges as the optimal choice because:

- **Memory safety** without garbage collection overhead
- **Native pattern matching** that directly maps to Baba Yaga's `when` expressions
- **Functional programming support** for closures and higher-order functions
- **Built-in `Result<T, E>`** type matching Baba Yaga's error handling
- **Zero-cost abstractions** for performance
- **Excellent tooling** and growing ecosystem

## Project Structure

```
baba-yaga-rust/
├── Cargo.toml
├── src/
│   ├── main.rs           # CLI entry point
│   ├── lib.rs            # Library exports
│   ├── lexer/
│   │   ├── mod.rs        # Lexer module
│   │   └── token.rs      # Token definitions
│   ├── parser/
│   │   ├── mod.rs        # Parser module
│   │   └── ast.rs        # AST node definitions
│   ├── interpreter/
│   │   ├── mod.rs        # Interpreter module
│   │   ├── value.rs      # Runtime value types
│   │   ├── scope.rs      # Scope management
│   │   └── builtins.rs   # Built-in functions
│   ├── error.rs          # Error types
│   └── repl.rs           # REPL implementation
├── tests/
│   ├── integration/
│   └── fixtures/
└── benches/              # Performance benchmarks
```

## Phase 1: Core Data Types and Error Handling

### 1.1 Define Core Types

**File: `src/error.rs`**
```rust
use std::fmt;

#[derive(Debug, Clone)]
pub enum BabaError {
    LexError(String),
    ParseError(String),
    RuntimeError(String),
    TypeError(String),
    UndefinedVariable(String),
    UndefinedProperty(String),
    DivisionByZero,
    IndexOutOfBounds(usize),
}

impl fmt::Display for BabaError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BabaError::LexError(msg) => write!(f, "Lexer error: {}", msg),
            BabaError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            BabaError::RuntimeError(msg) => write!(f, "Runtime error: {}", msg),
            BabaError::TypeError(msg) => write!(f, "Type error: {}", msg),
            BabaError::UndefinedVariable(name) => write!(f, "Undefined variable: {}", name),
            BabaError::UndefinedProperty(prop) => write!(f, "Undefined property: {}", prop),
            BabaError::DivisionByZero => write!(f, "Division by zero"),
            BabaError::IndexOutOfBounds(idx) => write!(f, "Index out of bounds: {}", idx),
        }
    }
}

impl std::error::Error for BabaError {}

pub type Result<T> = std::result::Result<T, BabaError>;
```

### 1.2 Runtime Value System

**File: `src/interpreter/value.rs`**
```rust
use std::collections::HashMap;
use std::rc::Rc;
use im::{Vector, HashMap as ImHashMap}; // Use persistent data structures

#[derive(Debug, Clone)]
pub enum Value {
    Number { value: f64, is_float: bool },
    String(String),
    Boolean(bool),
    List(Vector<Value>),
    Table(ImHashMap<String, Value>),
    Function(Function),
    NativeFunction(NativeFn),
    Result { variant: ResultVariant, value: Box<Value> },
    Unit,
}

#[derive(Debug, Clone)]
pub enum ResultVariant {
    Ok,
    Err,
}

#[derive(Debug, Clone)]
pub struct Function {
    pub params: Vec<String>,
    pub body: Rc<AstNode>,
    pub closure: Scope,
    pub return_type: Option<Type>,
}

pub type NativeFn = fn(&[Value]) -> crate::Result<Value>;
```

## Phase 2: Lexical Analysis

### 2.1 Token Definition

**File: `src/lexer/token.rs`**
```rust
#[derive(Debug, Clone, PartialEq)]
pub enum TokenType {
    // Literals
    Number { value: f64, is_float: bool },
    String(String),
    Identifier(String),
    
    // Keywords
    When, Is, Then, With, Rec, Ok, Err,
    True, False, Pi, Infinity,
    And, Or, Xor,
    
    // Operators
    Plus, Minus, Star, Slash, Percent,
    Equal, NotEqual, Greater, Less, GreaterEqual, LessEqual,
    Concat, // ..
    
    // Punctuation
    LeftParen, RightParen,
    LeftBrace, RightBrace,
    LeftBracket, RightBracket,
    Colon, Semicolon, Comma, Dot, Arrow,
    
    // Special
    Newline,
    Eof,
}

#[derive(Debug, Clone)]
pub struct Token {
    pub token_type: TokenType,
    pub line: usize,
    pub column: usize,
}
```

### 2.2 Lexer Implementation

**File: `src/lexer/mod.rs`**
Use a character-by-character state machine approach:

```rust
pub struct Lexer {
    input: Vec<char>,
    position: usize,
    line: usize,
    column: usize,
}

impl Lexer {
    pub fn new(input: String) -> Self {
        Self {
            input: input.chars().collect(),
            position: 0,
            line: 1,
            column: 1,
        }
    }
    
    pub fn tokenize(&mut self) -> crate::Result<Vec<Token>> {
        let mut tokens = Vec::new();
        
        while !self.is_at_end() {
            self.skip_whitespace();
            if self.is_at_end() { break; }
            
            tokens.push(self.next_token()?);
        }
        
        tokens.push(Token {
            token_type: TokenType::Eof,
            line: self.line,
            column: self.column,
        });
        
        Ok(tokens)
    }
    
    fn next_token(&mut self) -> crate::Result<Token> {
        // Implementation details...
    }
}
```

## Phase 3: Abstract Syntax Tree

### 3.1 AST Node Definition

**File: `src/parser/ast.rs`**
```rust
#[derive(Debug, Clone)]
pub enum AstNode {
    // Literals
    Number { value: f64, is_float: bool },
    String(String),
    Boolean(bool),
    List(Vec<AstNode>),
    Table(Vec<(String, AstNode)>),
    
    // Identifiers and access
    Identifier(String),
    MemberAccess { object: Box<AstNode>, property: Box<AstNode> },
    
    // Functions
    Function { params: Vec<String>, body: Box<AstNode> },
    FunctionCall { callee: Box<AstNode>, args: Vec<AstNode> },
    
    // Control flow
    When { 
        discriminants: Vec<AstNode>,
        cases: Vec<WhenCase>,
    },
    
    // Declarations
    VariableDeclaration { name: String, value: Box<AstNode> },
    FunctionDeclaration { 
        name: String, 
        params: Vec<String>, 
        body: Box<AstNode>,
        return_type: Option<Type>,
    },
    
    // Local bindings
    WithHeader {
        entries: Vec<WithEntry>,
        body: Box<AstNode>,
        recursive: bool,
    },
    
    // Expressions
    BinaryOp { left: Box<AstNode>, op: BinaryOperator, right: Box<AstNode> },
    UnaryOp { op: UnaryOperator, operand: Box<AstNode> },
    
    // Result types
    Result { variant: ResultVariant, value: Box<AstNode> },
    
    // Program structure
    Program(Vec<AstNode>),
}

#[derive(Debug, Clone)]
pub struct WhenCase {
    pub patterns: Vec<Pattern>,
    pub body: Box<AstNode>,
}

#[derive(Debug, Clone)]
pub enum Pattern {
    Literal(AstNode),
    Wildcard,
    Type(String),
    Result { variant: ResultVariant, binding: String },
    List(Vec<Pattern>),
    Table(Vec<(String, Pattern)>),
}
```

## Phase 4: Parser Implementation

### 4.1 Recursive Descent Parser

**File: `src/parser/mod.rs`**
```rust
pub struct Parser {
    tokens: Vec<Token>,
    current: usize,
}

impl Parser {
    pub fn new(tokens: Vec<Token>) -> Self {
        Self { tokens, current: 0 }
    }
    
    pub fn parse(&mut self) -> crate::Result<AstNode> {
        let mut statements = Vec::new();
        
        while !self.is_at_end() {
            statements.push(self.statement()?);
        }
        
        Ok(AstNode::Program(statements))
    }
    
    fn statement(&mut self) -> crate::Result<AstNode> {
        match self.peek().token_type {
            TokenType::Identifier(_) => {
                if self.peek_ahead(1).token_type == TokenType::Colon {
                    self.declaration()
                } else {
                    self.expression()
                }
            }
            _ => self.expression(),
        }
    }
    
    // Implement precedence climbing for expressions
    fn expression(&mut self) -> crate::Result<AstNode> {
        self.expression_with_precedence(0)
    }
    
    fn expression_with_precedence(&mut self, min_precedence: u8) -> crate::Result<AstNode> {
        // Implementation using precedence climbing algorithm
    }
}
```

## Phase 5: Interpreter Core

### 5.1 Scope Management

**File: `src/interpreter/scope.rs`**
```rust
use std::collections::HashMap;
use std::rc::Rc;
use crate::interpreter::value::Value;

#[derive(Debug, Clone)]
pub struct Scope {
    bindings: HashMap<String, Value>,
    parent: Option<Rc<Scope>>,
}

impl Scope {
    pub fn new() -> Self {
        Self {
            bindings: HashMap::new(),
            parent: None,
        }
    }
    
    pub fn with_parent(parent: Rc<Scope>) -> Self {
        Self {
            bindings: HashMap::new(),
            parent: Some(parent),
        }
    }
    
    pub fn get(&self, name: &str) -> Option<Value> {
        self.bindings.get(name).cloned()
            .or_else(|| self.parent.as_ref().and_then(|p| p.get(name)))
    }
    
    pub fn set(&mut self, name: String, value: Value) {
        self.bindings.insert(name, value);
    }
}
```

### 5.2 Interpreter Implementation

**File: `src/interpreter/mod.rs`**
```rust
use std::rc::Rc;
use crate::parser::ast::AstNode;
use crate::interpreter::value::Value;
use crate::interpreter::scope::Scope;

pub struct Interpreter {
    global_scope: Rc<Scope>,
}

impl Interpreter {
    pub fn new() -> Self {
        let mut global_scope = Scope::new();
        Self::register_builtins(&mut global_scope);
        
        Self {
            global_scope: Rc::new(global_scope),
        }
    }
    
    pub fn eval(&self, ast: &AstNode) -> crate::Result<Value> {
        self.eval_with_scope(ast, self.global_scope.clone())
    }
    
    fn eval_with_scope(&self, ast: &AstNode, scope: Rc<Scope>) -> crate::Result<Value> {
        match ast {
            AstNode::Number { value, is_float } => {
                Ok(Value::Number { value: *value, is_float: *is_float })
            }
            
            AstNode::String(s) => Ok(Value::String(s.clone())),
            
            AstNode::Boolean(b) => Ok(Value::Boolean(*b)),
            
            AstNode::Identifier(name) => {
                scope.get(name)
                    .ok_or_else(|| BabaError::UndefinedVariable(name.clone()))
            }
            
            AstNode::When { discriminants, cases } => {
                self.eval_when(discriminants, cases, scope)
            }
            
            AstNode::FunctionCall { callee, args } => {
                self.eval_function_call(callee, args, scope)
            }
            
            // ... other cases
            _ => todo!("Implement remaining AST node evaluation"),
        }
    }
}
```

## Phase 6: Built-in Functions

### 6.1 Built-in Registry

**File: `src/interpreter/builtins.rs`**
```rust
use crate::interpreter::value::{Value, NativeFn};
use crate::interpreter::scope::Scope;
use im::Vector;

impl Interpreter {
    fn register_builtins(scope: &mut Scope) {
        // Math functions
        scope.set("math".to_string(), create_math_namespace());
        
        // String functions  
        scope.set("str".to_string(), create_str_namespace());
        
        // List functions
        scope.set("map".to_string(), Value::NativeFunction(builtin_map));
        scope.set("filter".to_string(), Value::NativeFunction(builtin_filter));
        scope.set("reduce".to_string(), Value::NativeFunction(builtin_reduce));
        scope.set("append".to_string(), Value::NativeFunction(builtin_append));
        
        // IO functions
        scope.set("io".to_string(), create_io_namespace());
    }
}

fn builtin_map(args: &[Value]) -> crate::Result<Value> {
    if args.len() != 2 {
        return Err(BabaError::RuntimeError("map expects 2 arguments".to_string()));
    }
    
    let func = &args[0];
    let list = &args[1];
    
    match (func, list) {
        (Value::Function(f), Value::List(items)) => {
            let mut result = Vector::new();
            for item in items {
                // Apply function to each item
                let mapped = apply_function(f, &[item.clone()])?;
                result.push_back(mapped);
            }
            Ok(Value::List(result))
        }
        _ => Err(BabaError::TypeError("Invalid arguments to map".to_string())),
    }
}
```

## Phase 7: Performance Optimizations

### 7.1 Benchmark Setup

**File: `benches/interpreter.rs`**
```rust
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use baba_yaga_rust::*;

fn benchmark_fibonacci(c: &mut Criterion) {
    let code = r#"
        fibonacci : n ->
          when n is
            0 then 0
            1 then 1
            _ then (fibonacci (n - 1)) + (fibonacci (n - 2));
        result : fibonacci 10;
    "#;
    
    c.bench_function("fibonacci", |b| {
        b.iter(|| {
            let mut lexer = Lexer::new(black_box(code.to_string()));
            let tokens = lexer.tokenize().unwrap();
            let mut parser = Parser::new(tokens);
            let ast = parser.parse().unwrap();
            let interpreter = Interpreter::new();
            interpreter.eval(&ast).unwrap()
        })
    });
}

criterion_group!(benches, benchmark_fibonacci);
criterion_main!(benches);
```

### 7.2 Optimization Strategies

1. **AST Interning**: Use `Rc<AstNode>` to avoid cloning large AST subtrees
2. **Value Interning**: Intern common strings and small numbers
3. **Scope Optimization**: Use arena allocation for scopes
4. **Tail Call Detection**: Identify tail-recursive patterns for optimization
5. **Constant Folding**: Evaluate constant expressions at parse time

## Phase 8: Integration and CLI

### 8.1 Command Line Interface

**File: `src/main.rs`**
```rust
use clap::{App, Arg};
use std::fs;
use baba_yaga_rust::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let matches = App::new("Baba Yaga")
        .version("2.0.0")
        .about("A functional scripting language")
        .arg(Arg::with_name("file")
            .help("The input file to execute")
            .required(false)
            .index(1))
        .arg(Arg::with_name("debug")
            .short("d")
            .long("debug")
            .help("Enable debug output"))
        .get_matches();

    if let Some(filename) = matches.value_of("file") {
        let code = fs::read_to_string(filename)?;
        execute_code(&code)?;
    } else {
        start_repl()?;
    }

    Ok(())
}

fn execute_code(code: &str) -> crate::Result<()> {
    let mut lexer = Lexer::new(code.to_string());
    let tokens = lexer.tokenize()?;
    let mut parser = Parser::new(tokens);
    let ast = parser.parse()?;
    let interpreter = Interpreter::new();
    let result = interpreter.eval(&ast)?;
    
    if !matches!(result, Value::Unit) {
        println!("{:?}", result);
    }
    
    Ok(())
}
```

### 8.2 REPL Implementation

**File: `src/repl.rs`**
```rust
use rustyline::{Editor, Result as RLResult};
use crate::*;

pub fn start_repl() -> crate::Result<()> {
    let mut rl = Editor::<()>::new();
    let interpreter = Interpreter::new();
    
    println!("Baba Yaga REPL v2.0.0");
    println!("Type :help for commands, :quit to exit");
    
    loop {
        match rl.readline("baba> ") {
            Ok(line) => {
                rl.add_history_entry(line.as_str());
                
                if line.starts_with(':') {
                    handle_repl_command(&line)?;
                } else {
                    match execute_line(&interpreter, &line) {
                        Ok(value) => {
                            if !matches!(value, Value::Unit) {
                                println!("{:?}", value);
                            }
                        }
                        Err(e) => eprintln!("Error: {}", e),
                    }
                }
            }
            Err(_) => break,
        }
    }
    
    Ok(())
}
```

## Phase 9: Testing Strategy

### 9.1 Unit Tests
- Test each component in isolation
- Property-based testing for parser/lexer
- Comprehensive built-in function tests

### 9.2 Integration Tests
- Port existing JavaScript test cases
- Performance regression tests
- Memory usage tests

### 9.3 Compatibility Tests
- Ensure identical behavior to JavaScript version
- Cross-platform compatibility
- Host integration tests

## Phase 10: Deployment and Distribution

### 10.1 Build Configuration

**File: `Cargo.toml`**
```toml
[package]
name = "baba-yaga-rust"
version = "2.0.0"
edition = "2021"

[dependencies]
im = "15.1"           # Persistent data structures
clap = "3.0"          # CLI parsing
rustyline = "9.0"     # REPL readline
criterion = "0.4"     # Benchmarking

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
panic = "abort"

[[bin]]
name = "baba"
path = "src/main.rs"

[[bench]]
name = "interpreter"
harness = false
```

### 10.2 Cross-Compilation Targets
- Linux x86_64
- macOS (Intel + Apple Silicon)  
- Windows x86_64
- WebAssembly (for browser embedding)

## Expected Performance Improvements

Based on typical JavaScript to Rust ports:

- **Startup time**: 10-50x faster (no JIT warmup)
- **Execution speed**: 2-10x faster for compute-heavy workloads
- **Memory usage**: 2-5x less memory consumption
- **Binary size**: Much smaller self-contained executable
- **Predictable performance**: No garbage collection pauses

## Migration Path

1. **Phase 1-3**: Core infrastructure (2-3 weeks)
2. **Phase 4-5**: Parser and basic interpreter (2-3 weeks)  
3. **Phase 6**: Built-in functions (1-2 weeks)
4. **Phase 7-8**: Optimization and CLI (1-2 weeks)
5. **Phase 9-10**: Testing and deployment (1-2 weeks)

**Total estimated time**: 7-12 weeks for a complete reimplementation

This approach provides a systematic path to a high-performance native Baba Yaga implementation while maintaining full compatibility with the existing JavaScript version.