ruchy 4.2.0

A systems scripting language that transpiles to idiomatic Rust with extreme quality engineering
Documentation
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
//! REPL Expression Evaluation Engine
//!
//! Handles evaluation of user input with proper error handling and multiline support.

use crate::runtime::interpreter::{Interpreter, Value};
use anyhow::Result;

/// Result of evaluating a line of input
#[derive(Debug, Clone)]
pub enum EvalResult {
    /// Successfully evaluated to a value
    Value(Value),
    /// Input incomplete, needs more input
    NeedMoreInput,
    /// Evaluation error with message
    Error(String),
}

/// Expression evaluator for the REPL
#[derive(Debug)]
pub struct Evaluator {
    /// Internal interpreter instance
    interpreter: Interpreter,
    /// Tracks if we're in multiline mode
    multiline_buffer: String,
}

impl Evaluator {
    /// Create a new evaluator (complexity: 2)
    pub fn new() -> Self {
        Self {
            interpreter: Interpreter::new(),
            multiline_buffer: String::new(),
        }
    }

    /// Evaluate a line of input with state synchronization (complexity: 9)
    pub fn evaluate_line(
        &mut self,
        line: &str,
        state: &mut crate::runtime::repl::state::ReplState,
    ) -> Result<EvalResult> {
        // Handle multiline continuation
        if self.multiline_buffer.is_empty() {
            self.multiline_buffer = line.to_string();
        } else {
            self.multiline_buffer.push('\n');
            self.multiline_buffer.push_str(line);
        }

        // Try to parse and evaluate the accumulated input
        use crate::frontend::parser::Parser;

        let mut parser = Parser::new(&self.multiline_buffer);
        match parser.parse() {
            Ok(expr) => {
                // [RUNTIME-083] Catch InterpreterError::Return and extract value (early return support)
                // This matches the pattern in interpreter.rs:2044-2046 for function calls
                let result = match self.interpreter.eval_expr(&expr) {
                    Err(crate::runtime::InterpreterError::Return(val)) => Ok(val),
                    other => other,
                };

                match result {
                    Ok(value) => {
                        self.multiline_buffer.clear();

                        // Synchronize interpreter bindings with REPL state
                        let interpreter_bindings = self.interpreter.get_current_bindings();
                        let state_bindings = state.get_bindings_mut();
                        for (name, val) in interpreter_bindings {
                            state_bindings.insert(name, val);
                        }

                        Ok(EvalResult::Value(value))
                    }
                    Err(e) => {
                        self.multiline_buffer.clear();
                        Ok(EvalResult::Error(e.to_string()))
                    }
                }
            }
            Err(e) => {
                // Check if this looks like an incomplete expression
                let error_str = e.to_string();
                if self.is_incomplete_error(&error_str) {
                    Ok(EvalResult::NeedMoreInput)
                } else {
                    self.multiline_buffer.clear();
                    Ok(EvalResult::Error(error_str))
                }
            }
        }
    }

    /// Check if we're in multiline mode (complexity: 1)
    pub fn is_multiline(&self) -> bool {
        !self.multiline_buffer.is_empty()
    }

    /// Clear interpreter variables for checkpoint restore (complexity: 2)
    pub fn clear_interpreter_variables(&mut self) {
        self.interpreter.clear_user_variables();
    }

    /// Set a variable in the interpreter (complexity: 1)
    pub fn set_variable(&mut self, name: String, value: Value) {
        self.interpreter.set_global_binding(name, value);
    }

    /// Check if error indicates incomplete input (complexity: 3)
    fn is_incomplete_error(&self, error_msg: &str) -> bool {
        error_msg.contains("unexpected end of input")
            || error_msg.contains("incomplete")
            || (error_msg.contains("Expected") && error_msg.contains("EOF"))
            || (error_msg.contains("expected") && error_msg.contains("EOF"))
            || error_msg.contains("found EOF")
    }
}

impl Default for Evaluator {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_evaluator_creation() {
        let evaluator = Evaluator::new();
        assert!(!evaluator.is_multiline());
    }

    #[test]
    fn test_simple_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("2 + 2", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(4)) => {}
            result => panic!("Expected Integer(4), got {result:?}"),
        }
    }

    #[test]
    fn test_error_handling() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator
            .evaluate_line("undefined_variable", &mut state)
            .unwrap()
        {
            EvalResult::Error(_) => {}
            result => panic!("Expected Error, got {result:?}"),
        }
    }

    // === EXTREME TDD Round 15 tests ===

    #[test]
    fn test_evaluator_default() {
        let evaluator = Evaluator::default();
        assert!(!evaluator.is_multiline());
    }

    #[test]
    fn test_eval_result_debug() {
        let value_result = EvalResult::Value(Value::Integer(42));
        let debug_str = format!("{:?}", value_result);
        assert!(debug_str.contains("Value"));

        let need_more = EvalResult::NeedMoreInput;
        let debug_str = format!("{:?}", need_more);
        assert!(debug_str.contains("NeedMoreInput"));

        let error_result = EvalResult::Error("test error".to_string());
        let debug_str = format!("{:?}", error_result);
        assert!(debug_str.contains("Error"));
    }

    #[test]
    fn test_eval_result_clone() {
        let original = EvalResult::Value(Value::Integer(100));
        let cloned = original.clone();
        match cloned {
            EvalResult::Value(Value::Integer(100)) => {}
            _ => panic!("Clone should preserve value"),
        }
    }

    #[test]
    fn test_is_incomplete_error() {
        let evaluator = Evaluator::new();

        // These should be detected as incomplete
        assert!(evaluator.is_incomplete_error("unexpected end of input"));
        assert!(evaluator.is_incomplete_error("incomplete expression"));
        assert!(evaluator.is_incomplete_error("Expected } but found EOF"));
        assert!(evaluator.is_incomplete_error("expected ) at EOF"));
        assert!(evaluator.is_incomplete_error("found EOF unexpectedly"));

        // These should NOT be incomplete
        assert!(!evaluator.is_incomplete_error("undefined variable"));
        assert!(!evaluator.is_incomplete_error("type mismatch"));
    }

    #[test]
    fn test_string_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("\"hello\"", &mut state).unwrap() {
            EvalResult::Value(Value::String(s)) => {
                assert_eq!(&*s, "hello");
            }
            result => panic!("Expected String, got {result:?}"),
        }
    }

    #[test]
    fn test_let_binding_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        // Define a variable
        let result = evaluator.evaluate_line("let x = 10", &mut state).unwrap();
        match result {
            EvalResult::Value(_) => {}
            result => panic!("Expected Value for let binding, got {result:?}"),
        }
    }

    // === Additional tests for improved coverage ===

    #[test]
    fn test_evaluator_debug_trait() {
        let evaluator = Evaluator::new();
        let debug_str = format!("{:?}", evaluator);
        assert!(debug_str.contains("Evaluator"));
    }

    #[test]
    fn test_evaluator_is_multiline_false_initially() {
        let evaluator = Evaluator::new();
        assert!(!evaluator.is_multiline());
    }

    #[test]
    fn test_float_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("3.14", &mut state).unwrap() {
            EvalResult::Value(Value::Float(f)) => {
                assert!((f - 3.14).abs() < 0.0001);
            }
            result => panic!("Expected Float, got {result:?}"),
        }
    }

    #[test]
    fn test_boolean_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("true", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true), got {result:?}"),
        }

        match evaluator.evaluate_line("false", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(false)) => {}
            result => panic!("Expected Bool(false), got {result:?}"),
        }
    }

    #[test]
    fn test_nil_evaluation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("nil", &mut state).unwrap() {
            EvalResult::Value(Value::Nil) => {}
            result => panic!("Expected Nil, got {result:?}"),
        }
    }

    #[test]
    fn test_arithmetic_expressions() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        // Addition
        match evaluator.evaluate_line("10 + 5", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(15)) => {}
            result => panic!("Expected Integer(15), got {result:?}"),
        }

        // Subtraction
        match evaluator.evaluate_line("10 - 3", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(7)) => {}
            result => panic!("Expected Integer(7), got {result:?}"),
        }

        // Multiplication
        match evaluator.evaluate_line("4 * 5", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(20)) => {}
            result => panic!("Expected Integer(20), got {result:?}"),
        }

        // Division
        match evaluator.evaluate_line("20 / 4", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(5)) => {}
            result => panic!("Expected Integer(5), got {result:?}"),
        }
    }

    #[test]
    fn test_comparison_expressions() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("5 > 3", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true), got {result:?}"),
        }

        match evaluator.evaluate_line("3 < 5", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true), got {result:?}"),
        }

        match evaluator.evaluate_line("5 == 5", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true), got {result:?}"),
        }

        match evaluator.evaluate_line("5 != 3", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true), got {result:?}"),
        }
    }

    #[test]
    fn test_parenthesized_expressions() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("(2 + 3) * 4", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(20)) => {}
            result => panic!("Expected Integer(20), got {result:?}"),
        }
    }

    #[test]
    fn test_negative_numbers() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("-5", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(-5)) => {}
            result => panic!("Expected Integer(-5), got {result:?}"),
        }
    }

    #[test]
    fn test_set_variable() {
        let mut evaluator = Evaluator::new();
        evaluator.set_variable("test_var".to_string(), Value::Integer(42));
        // Verify the variable was set by evaluating it
        let mut state = crate::runtime::repl::state::ReplState::new();
        match evaluator.evaluate_line("test_var", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(42)) => {}
            result => panic!("Expected Integer(42), got {result:?}"),
        }
    }

    #[test]
    fn test_clear_interpreter_variables() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        // Define a variable
        evaluator
            .evaluate_line("let my_var = 100", &mut state)
            .unwrap();

        // Clear variables
        evaluator.clear_interpreter_variables();

        // Variable should no longer exist
        match evaluator.evaluate_line("my_var", &mut state).unwrap() {
            EvalResult::Error(_) => {} // Expected: variable undefined
            result => panic!("Expected Error (undefined variable), got {result:?}"),
        }
    }

    #[test]
    fn test_is_incomplete_error_patterns() {
        let evaluator = Evaluator::new();

        // Should detect as incomplete
        assert!(evaluator.is_incomplete_error("unexpected end of input"));
        assert!(evaluator.is_incomplete_error("expression is incomplete"));
        assert!(evaluator.is_incomplete_error("Expected } but found EOF"));
        assert!(evaluator.is_incomplete_error("expected ) at EOF"));
        assert!(evaluator.is_incomplete_error("found EOF while parsing"));

        // Should NOT detect as incomplete
        assert!(!evaluator.is_incomplete_error("syntax error"));
        assert!(!evaluator.is_incomplete_error("invalid token"));
        assert!(!evaluator.is_incomplete_error("type error: cannot add"));
    }

    #[test]
    fn test_logical_expressions() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("true && true", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true) for AND, got {result:?}"),
        }

        match evaluator
            .evaluate_line("true || false", &mut state)
            .unwrap()
        {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true) for OR, got {result:?}"),
        }

        match evaluator.evaluate_line("!false", &mut state).unwrap() {
            EvalResult::Value(Value::Bool(true)) => {}
            result => panic!("Expected Bool(true) for NOT, got {result:?}"),
        }
    }

    #[test]
    fn test_if_expression() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator
            .evaluate_line("if true { 1 } else { 2 }", &mut state)
            .unwrap()
        {
            EvalResult::Value(Value::Integer(1)) => {}
            result => panic!("Expected Integer(1), got {result:?}"),
        }

        match evaluator
            .evaluate_line("if false { 1 } else { 2 }", &mut state)
            .unwrap()
        {
            EvalResult::Value(Value::Integer(2)) => {}
            result => panic!("Expected Integer(2), got {result:?}"),
        }
    }

    #[test]
    fn test_state_synchronization() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        // Define a variable via evaluator
        evaluator
            .evaluate_line("let sync_var = 999", &mut state)
            .unwrap();

        // State should have the binding now
        assert!(state.get_bindings().contains_key("sync_var"));
    }

    #[test]
    fn test_string_concatenation() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator
            .evaluate_line("\"hello\" + \" \" + \"world\"", &mut state)
            .unwrap()
        {
            EvalResult::Value(Value::String(s)) => {
                assert_eq!(&*s, "hello world");
            }
            result => panic!("Expected String, got {result:?}"),
        }
    }

    #[test]
    fn test_modulo_operator() {
        let mut evaluator = Evaluator::new();
        let mut state = crate::runtime::repl::state::ReplState::new();

        match evaluator.evaluate_line("10 % 3", &mut state).unwrap() {
            EvalResult::Value(Value::Integer(1)) => {}
            result => panic!("Expected Integer(1), got {result:?}"),
        }
    }

    #[test]
    fn test_eval_result_clone_variants() {
        // Test clone for all EvalResult variants
        let value = EvalResult::Value(Value::String("test".into()));
        let cloned = value.clone();
        match cloned {
            EvalResult::Value(Value::String(s)) => assert_eq!(&*s, "test"),
            _ => panic!("Clone failed for Value variant"),
        }

        let need_more = EvalResult::NeedMoreInput;
        let cloned = need_more.clone();
        assert!(matches!(cloned, EvalResult::NeedMoreInput));

        let error = EvalResult::Error("error msg".to_string());
        let cloned = error.clone();
        match cloned {
            EvalResult::Error(msg) => assert_eq!(msg, "error msg"),
            _ => panic!("Clone failed for Error variant"),
        }
    }
}