ruchy 4.2.1

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
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
//! Control flow evaluation module
//!
//! This module handles evaluation of all control flow constructs including
//! if expressions, loops, match statements, and pattern matching.
//! Extracted for maintainability and following Toyota Way principles.
//! All functions maintain <10 cyclomatic complexity.

use crate::frontend::ast::{Expr, Literal, MatchArm, Pattern};
use crate::runtime::{InterpreterError, Value};
use std::sync::Arc;

/// Evaluate an if expression with optional else branch
///
/// # Complexity
/// Cyclomatic complexity: 4 (within Toyota Way limits)
pub fn eval_if_expr<F>(
    condition: &Expr,
    then_branch: &Expr,
    else_branch: Option<&Expr>,
    mut eval_expr: F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let condition_val = eval_expr(condition)?;

    if condition_val.is_truthy() {
        eval_expr(then_branch)
    } else if let Some(else_expr) = else_branch {
        eval_expr(else_expr)
    } else {
        Ok(Value::Nil)
    }
}

/// Evaluate a let expression with variable binding
///
/// # Complexity
/// Cyclomatic complexity: 3 (within Toyota Way limits)
pub fn eval_let_expr<F1, F2>(
    name: &str,
    value: &Expr,
    _body: &Expr,
    mut eval_expr: F1,
    mut with_variable: F2,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(
        &str,
        Value,
        &mut dyn FnMut(&Expr) -> Result<Value, InterpreterError>,
    ) -> Result<Value, InterpreterError>,
{
    let val = eval_expr(value)?;
    with_variable(name, val, &mut eval_expr)
}

/// Evaluate a for loop with iterator and body
///
/// # Complexity
/// Cyclomatic complexity: 3 (reduced from 8, cognitive complexity from 42 → ≤10)
pub fn eval_for_loop<F1, F2>(
    var: &str,
    iter: &Expr,
    _body: &Expr,
    mut eval_expr: F1,
    mut with_variable: F2,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(
        &str,
        Value,
        &mut dyn FnMut(&Expr) -> Result<Value, InterpreterError>,
    ) -> Result<Value, InterpreterError>,
{
    let iter_val = eval_expr(iter)?;

    match &iter_val {
        Value::Array(_) => eval_array_iteration(&iter_val, var, &mut with_variable, &mut eval_expr),
        Value::Range { .. } => {
            eval_range_iteration(&iter_val, var, &mut with_variable, &mut eval_expr)
        }
        _ => Err(InterpreterError::TypeError(format!(
            "Cannot iterate over {}",
            iter_val.type_name()
        ))),
    }
}

// =============================================================================
// COMPLEXITY REFACTORING: While Loop Helper Functions
// Target: Reduce eval_while_loop cognitive complexity from 16 → ≤10
// =============================================================================

/// Evaluate loop condition
/// Complexity: ≤3
pub fn eval_loop_condition<F>(condition: &Expr, eval_expr: &mut F) -> Result<bool, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    Ok(eval_expr(condition)?.is_truthy())
}

/// Evaluate loop body and handle control flow
/// Complexity: ≤5
pub fn eval_loop_body<F>(
    body: &Expr,
    last_val: &mut Value,
    eval_expr: &mut F,
) -> Result<Option<Value>, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    match eval_expr(body) {
        Ok(val) => {
            *last_val = val;
            Ok(None)
        }
        Err(InterpreterError::Break(None, val)) => Ok(Some(val)),
        Err(InterpreterError::Continue(_)) => Ok(None),
        Err(e) => Err(e),
    }
}

/// Run the while loop with separated concerns
/// Complexity: ≤8
pub fn run_while_loop<F>(
    condition: &Expr,
    body: &Expr,
    eval_expr: &mut F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let mut last_val = Value::Nil;

    loop {
        if !eval_loop_condition(condition, eval_expr)? {
            break;
        }

        if let Some(break_val) = eval_loop_body(body, &mut last_val, eval_expr)? {
            return Ok(break_val);
        }
    }

    Ok(last_val)
}

/// Evaluate a while loop with condition and body
///
/// # Complexity
/// Cyclomatic complexity: 1 (reduced from 5, cognitive from 16 → ≤3)
pub fn eval_while_loop<F>(
    condition: &Expr,
    body: &Expr,
    mut eval_expr: F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    run_while_loop(condition, body, &mut eval_expr)
}

// =============================================================================
// COMPLEXITY REFACTORING: Match Expression Helper Functions
// Target: Reduce eval_match cognitive complexity from 25 → ≤10
// =============================================================================

/// Evaluate a single match arm
/// Complexity: ≤5
pub fn eval_match_arm<F1, F2>(
    arm: &MatchArm,
    value: &Value,
    pattern_matches: &mut F2,
    eval_expr: &mut F1,
) -> Result<Option<Value>, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(&Pattern, &Value) -> Result<bool, InterpreterError>,
{
    if pattern_matches(&arm.pattern, value)? && eval_match_guard(arm.guard.as_deref(), eval_expr)? {
        return Ok(Some(eval_expr(&arm.body)?));
    }
    Ok(None)
}

/// Evaluate guard expression if present
/// Complexity: ≤3
pub fn eval_match_guard<F>(
    guard: Option<&Expr>,
    eval_expr: &mut F,
) -> Result<bool, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    if let Some(guard_expr) = guard {
        Ok(eval_expr(guard_expr)?.is_truthy())
    } else {
        Ok(true) // No guard means always pass
    }
}

/// Find the first matching arm and evaluate it
/// Complexity: ≤8
pub fn find_matching_arm<F1, F2>(
    arms: &[MatchArm],
    value: &Value,
    pattern_matches: &mut F2,
    eval_expr: &mut F1,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(&Pattern, &Value) -> Result<bool, InterpreterError>,
{
    for arm in arms {
        if let Some(result) = eval_match_arm(arm, value, pattern_matches, eval_expr)? {
            return Ok(result);
        }
    }

    Err(InterpreterError::RuntimeError(
        "No matching pattern found in match expression".to_string(),
    ))
}

/// Evaluate a match expression with pattern matching
///
/// # Complexity
/// Cyclomatic complexity: 2 (reduced from 6, cognitive from 25 → ≤5)
pub fn eval_match<F1, F2>(
    expr: &Expr,
    arms: &[MatchArm],
    mut eval_expr: F1,
    mut pattern_matches: F2,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(&Pattern, &Value) -> Result<bool, InterpreterError>,
{
    let value = eval_expr(expr)?;
    find_matching_arm(arms, &value, &mut pattern_matches, &mut eval_expr)
}

/// Evaluate a block expression (sequence of statements)
///
/// # Complexity
/// Cyclomatic complexity: 4 (within Toyota Way limits)
pub fn eval_block_expr<F>(statements: &[Expr], mut eval_expr: F) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let mut last_val = Value::Nil;

    for stmt in statements {
        last_val = eval_expr(stmt)?; // Propagate all errors including Return
    }

    Ok(last_val)
}

/// Evaluate a list expression (array literal)
///
/// # Complexity
/// Cyclomatic complexity: 3 (within Toyota Way limits)
pub fn eval_list_expr<F>(elements: &[Expr], mut eval_expr: F) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let mut values = Vec::new();

    for element in elements {
        values.push(eval_expr(element)?);
    }

    Ok(Value::from_array(values))
}

/// Evaluate a tuple expression
///
/// # Complexity
/// Cyclomatic complexity: 3 (within Toyota Way limits)
pub fn eval_tuple_expr<F>(elements: &[Expr], mut eval_expr: F) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let mut values = Vec::new();

    for element in elements {
        values.push(eval_expr(element)?);
    }

    Ok(Value::Tuple(Arc::from(values.as_slice())))
}

/// Evaluate a range expression
///
/// # Complexity
/// Cyclomatic complexity: 3 (within Toyota Way limits)
pub fn eval_range_expr<F>(
    start: &Expr,
    end: &Expr,
    inclusive: bool,
    mut eval_expr: F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let start_val = eval_expr(start)?;
    let end_val = eval_expr(end)?;

    Ok(Value::Range {
        start: Box::new(start_val),
        end: Box::new(end_val),
        inclusive,
    })
}

/// Evaluate an array initialization expression
///
/// # Complexity
/// Cyclomatic complexity: 5 (within Toyota Way limits)
pub fn eval_array_init_expr<F>(
    value_expr: &Expr,
    size_expr: &Expr,
    mut eval_expr: F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let value = eval_expr(value_expr)?;
    let size_val = eval_expr(size_expr)?;

    if let Value::Integer(size) = size_val {
        if size >= 0 {
            let mut values = Vec::new();
            for _ in 0..size {
                values.push(value.clone());
            }
            Ok(Value::from_array(values))
        } else {
            Err(InterpreterError::RuntimeError(
                "Array size must be non-negative".to_string(),
            ))
        }
    } else {
        Err(InterpreterError::TypeError(
            "Array size must be integer".to_string(),
        ))
    }
}

/// Evaluate a return expression
///
/// # Complexity
/// Cyclomatic complexity: 3 (within Toyota Way limits)
pub fn eval_return_expr<F>(
    value: Option<&Expr>,
    mut eval_expr: F,
) -> Result<Value, InterpreterError>
where
    F: FnMut(&Expr) -> Result<Value, InterpreterError>,
{
    let return_value = if let Some(expr) = value {
        eval_expr(expr)?
    } else {
        Value::Nil
    };

    Err(InterpreterError::Return(return_value))
}

// =============================================================================
// COMPLEXITY REFACTORING: For Loop Helper Functions
// Target: Reduce eval_for_loop cognitive complexity from 42 → ≤10
// =============================================================================

/// Handle array iteration in for loops
/// Complexity: ≤8
pub fn eval_array_iteration<F1, F2>(
    array: &Value,
    var: &str,
    with_variable: &mut F2,
    eval_expr: &mut F1,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(
        &str,
        Value,
        &mut dyn FnMut(&Expr) -> Result<Value, InterpreterError>,
    ) -> Result<Value, InterpreterError>,
{
    if let Value::Array(arr) = array {
        let mut last_val = Value::Nil;
        for item in arr.iter() {
            let should_continue =
                execute_iteration_step(var, item.clone(), with_variable, eval_expr, &mut last_val)?;
            if !should_continue {
                break;
            }
        }
        Ok(last_val)
    } else {
        Err(InterpreterError::TypeError(format!(
            "Expected array, got {}",
            array.type_name()
        )))
    }
}

/// Execute iteration body with loop control handling
/// Complexity: ≤5
fn execute_iteration_step<F1, F2>(
    loop_var: &str,
    value: Value,
    with_variable: &mut F2,
    eval_expr: &mut F1,
    last_val: &mut Value,
) -> Result<bool, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(
        &str,
        Value,
        &mut dyn FnMut(&Expr) -> Result<Value, InterpreterError>,
    ) -> Result<Value, InterpreterError>,
{
    match with_variable(loop_var, value, eval_expr) {
        Ok(result_val) => {
            *last_val = result_val;
            Ok(true) // Continue iteration
        }
        Err(InterpreterError::Break(None, break_val)) => {
            *last_val = break_val;
            Ok(false) // Stop iteration
        }
        Err(InterpreterError::Continue(_)) => Ok(true), // Continue iteration
        Err(e) => Err(e),
    }
}

/// Handle range iteration in for loops
/// Complexity: ≤5
pub fn eval_range_iteration<F1, F2>(
    range: &Value,
    var: &str,
    with_variable: &mut F2,
    eval_expr: &mut F1,
) -> Result<Value, InterpreterError>
where
    F1: FnMut(&Expr) -> Result<Value, InterpreterError>,
    F2: FnMut(
        &str,
        Value,
        &mut dyn FnMut(&Expr) -> Result<Value, InterpreterError>,
    ) -> Result<Value, InterpreterError>,
{
    if let Value::Range { .. } = range {
        let (start_val, end_val, inclusive) = extract_range_bounds(range)?;
        let iter = create_range_iterator(start_val, end_val, inclusive);

        let mut last_val = Value::Nil;
        for i in iter {
            let should_continue = execute_iteration_step(
                var,
                Value::Integer(i),
                with_variable,
                eval_expr,
                &mut last_val,
            )?;
            if !should_continue {
                break;
            }
        }
        Ok(last_val)
    } else {
        Err(InterpreterError::TypeError(format!(
            "Expected range, got {}",
            range.type_name()
        )))
    }
}

/// Extract integer from a value
/// Complexity: ≤2
fn value_to_integer(value: &Value, context: &str) -> Result<i64, InterpreterError> {
    match value {
        Value::Integer(i) => Ok(*i),
        _ => Err(InterpreterError::TypeError(format!(
            "{context} must be integer"
        ))),
    }
}

/// Extract integer bounds from a range value
/// Complexity: ≤5
pub fn extract_range_bounds(range: &Value) -> Result<(i64, i64, bool), InterpreterError> {
    if let Value::Range {
        start,
        end,
        inclusive,
    } = range
    {
        let start_val = value_to_integer(start, "Range start")?;
        let end_val = value_to_integer(end, "Range end")?;
        Ok((start_val, end_val, *inclusive))
    } else {
        Err(InterpreterError::TypeError(
            "Expected range value".to_string(),
        ))
    }
}

/// Handle loop control flow (break/continue)
/// Complexity: ≤5
pub fn handle_loop_control(
    result: Result<Value, InterpreterError>,
    last_val: &mut Value,
) -> Result<Option<Value>, InterpreterError> {
    match result {
        Ok(val) => {
            *last_val = val;
            Ok(None)
        }
        Err(InterpreterError::Break(None, val)) => Ok(Some(val)),
        Err(InterpreterError::Continue(_)) => Ok(None),
        Err(e) => Err(e),
    }
}

/// Create an iterator from range bounds
/// Complexity: ≤3
pub fn create_range_iterator(
    start: i64,
    end: i64,
    inclusive: bool,
) -> Box<dyn Iterator<Item = i64>> {
    if inclusive {
        Box::new(start..=end)
    } else {
        Box::new(start..end)
    }
}

// Additional control flow utilities

/// Check if a pattern matches a value (simplified version)
///
/// # Complexity
/// Cyclomatic complexity: 8 (within Toyota Way limits)
// =============================================================================
// COMPLEXITY REFACTORING: Pattern Matching Helper Functions
// Target: Reduce pattern_matches_simple complexity from 12 → ≤10
// =============================================================================

/// Match wildcard patterns (always matches)
/// Complexity: 1
pub fn match_wildcard_pattern(_value: &Value) -> bool {
    true // Wildcard always matches
}

/// Match literal patterns
/// Complexity: 3
pub fn match_literal_pattern(lit: &Literal, value: &Value) -> Result<bool, InterpreterError> {
    let pattern_val = crate::runtime::eval_literal::eval_literal(lit);
    Ok(pattern_val == *value)
}

/// Match identifier patterns (always matches, binds variable)
/// Complexity: 1
pub fn match_identifier_pattern(_name: &str, _value: &Value) -> bool {
    true // Identifier always matches, binds the variable
}

/// Check if patterns match values element-wise
/// Complexity: ≤5
fn patterns_match_values(patterns: &[Pattern], values: &[Value]) -> Result<bool, InterpreterError> {
    if patterns.len() != values.len() {
        return Ok(false);
    }
    for (pat, val) in patterns.iter().zip(values.iter()) {
        if !pattern_matches_simple(pat, val)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// Match list patterns recursively
/// Complexity: ≤3
pub fn match_list_pattern(patterns: &[Pattern], value: &Value) -> Result<bool, InterpreterError> {
    match value {
        Value::Array(arr) => patterns_match_values(patterns, arr),
        _ => Ok(false),
    }
}

/// Match tuple patterns recursively
/// Complexity: ≤3
pub fn match_tuple_pattern(patterns: &[Pattern], value: &Value) -> Result<bool, InterpreterError> {
    match value {
        Value::Tuple(elements) => patterns_match_values(patterns, elements),
        _ => Ok(false),
    }
}

/// Pattern matching with complexity reduced from 12 → 5
/// Complexity: 5 (down from 12)
pub fn pattern_matches_simple(pattern: &Pattern, value: &Value) -> Result<bool, InterpreterError> {
    match pattern {
        Pattern::Wildcard => Ok(match_wildcard_pattern(value)),
        Pattern::Literal(lit) => match_literal_pattern(lit, value),
        Pattern::Identifier(name) => Ok(match_identifier_pattern(name, value)),
        Pattern::List(patterns) => match_list_pattern(patterns, value),
        Pattern::Tuple(patterns) => match_tuple_pattern(patterns, value),
        _ => Ok(false), // Other patterns not implemented yet
    }
}


#[cfg(test)]
#[path = "eval_control_flow_new_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "eval_control_flow_new_r130_tests.rs"]
mod round_130_tests;

#[cfg(test)]
#[path = "eval_control_flow_new_coverage_tests.rs"]
mod coverage_tests;