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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Runtime execution and REPL support
//!
//! This module provides the interactive REPL, runtime execution environment,
//! and actor system with supervision trees for the Ruchy language.
//!
//! # Core Components
//!
//! ## REPL (Read-Eval-Print Loop)
//! Interactive development environment with:
//! - Line editing and history
//! - Tab completion
//! - Magic commands
//! - Session recording and replay
//!
//! ## Actor System
//! Concurrent programming model featuring:
//! - Message passing between actors
//! - Supervision trees for fault tolerance
//! - Actor observability and debugging
//! - Deadlock detection
//!
//! ## Runtime Execution
//! - Expression evaluation engine
//! - Pattern matching implementation
//! - Binary operations with proper semantics
//! - Memory-safe arena allocation
//!
//! # Examples
//!
//! ```no_run
//! use ruchy::runtime::{Repl, ReplConfig};
//!
//! // Start an interactive REPL session
//! let config = ReplConfig::default();
//! let mut repl = Repl::new_with_config(config).expect("Repl creation should succeed");
//! repl.run().expect("REPL execution should succeed");
//! ```
//!
//! ```
//! use ruchy::runtime::{ActorSystem, Message, MessageValue};
//!
//! // Create an actor system
//! let mut system = ActorSystem::new();
//!
//! // Spawn an echo actor
//! let echo_ref = system.spawn_echo_actor("echo".to_string()).expect("Spawning actor should succeed");
//!
//! // Send a message
//! let msg = Message::new(MessageValue::String("Hello".to_string()));
//! // Note: In real usage, you would handle the Result properly
//! ```
//!
//! # Features
//!
//! - **Interactive Development**: Full-featured REPL with completion
//! - **Concurrent Programming**: Actor model with supervision
//! - **Debugging Tools**: Observatory for system introspection
//! - **Memory Safety**: Arena allocation without unsafe code
//! - **Educational Tools**: Assessment and grading systems
pub mod actor;
pub mod actor_concurrent;
pub mod actor_runtime;
#[cfg(not(target_arch = "wasm32"))]
pub mod async_runtime;
pub mod audit; // Audit logging with entrenar integration
pub mod bytecode; // OPT-001: Bytecode VM Foundation
pub mod cache;
#[cfg(not(target_arch = "wasm32"))]
pub mod completion;
pub mod grammar_coverage;
pub mod interpreter;
pub mod interpreter_control_flow; // Control flow: loops, match, assignments
pub mod interpreter_dataframe; // DataFrame operations and methods
pub mod interpreter_functions; // Function definitions, lambdas, and calls
pub mod interpreter_index; // Index access, field access, qualified names
#[cfg(test)]
pub mod interpreter_inline_tests; // Extracted tests from interpreter.rs
pub mod interpreter_methods; // Method dispatch for all value types (re-export)
pub mod interpreter_methods_actor; // Actor/struct/object methods
pub mod interpreter_methods_dispatch; // Core method dispatch logic
#[cfg(not(target_arch = "wasm32"))]
pub mod interpreter_methods_html; // HTML document/element methods
pub mod interpreter_methods_instance; // Mutable object instance methods
pub mod interpreter_methods_string; // String/array method dispatch
pub mod interpreter_types; // EXTREME TDD Round 52: InterpreterError, CallFrame extracted
pub mod interpreter_types_actor; // Actor definition and instantiation
pub mod interpreter_types_class; // Class definition, instantiation, methods
pub mod interpreter_types_enum; // Enum and impl block definitions
pub mod interpreter_types_impl; // Class/struct/enum/actor definitions (re-export)
pub mod interpreter_types_module; // Module expression evaluation
pub mod interpreter_types_struct; // Struct definition and instantiation
pub mod module_loader; // PARSER-060: Module resolution and import system
pub mod object_helpers; // EXTREME TDD: RefCell-based mutable objects
pub mod value; // EXTREME TDD Round 52: Value type extracted from interpreter.rs
pub mod value_utils;
// Decomposed interpreter modules
pub mod builtin_init; // EXTREME TDD: Builtin functions initialization
pub mod builtins;
pub mod compilation; // EXTREME TDD: Direct-threaded interpreter compilation
pub mod eval_actor; // EXTREME TDD: Actor operations extracted for 100% coverage
pub mod eval_array;
pub mod eval_builtin;
pub mod eval_control_flow_new;
pub mod eval_data_structures;
pub mod eval_dataframe;
pub mod eval_dataframe_ops;
pub mod eval_display;
pub mod eval_expr;
pub mod eval_func;
pub mod eval_function;
pub mod eval_html_methods; // HTTP-002-D: HTML parsing method support
pub mod eval_index; // EXTREME TDD: Index operations extracted for 100% coverage
pub mod eval_json; // EXTREME TDD: JSON operations extracted for 100% coverage
pub mod eval_literal;
pub mod eval_loops;
pub mod eval_method;
pub mod eval_method_dispatch;
pub mod eval_operations;
pub mod eval_pattern;
pub mod eval_pattern_match;
pub mod eval_string;
pub mod eval_string_interpolation;
pub mod eval_string_methods;
pub mod eval_try_catch; // EXTREME TDD: Try/catch error handling
pub mod gc;
pub mod gc_impl; // EXTREME TDD: Full GC implementation with tests
pub mod type_feedback; // EXTREME TDD: JIT type feedback system (extracted from interpreter.rs)
pub mod validation;
pub mod value_format; // EXTREME TDD: Value formatting utilities (extracted from interpreter.rs)
                      // pub mod interpreter_modules;  // Temporarily disabled - compilation errors
pub mod lazy;
pub mod pattern_matching;
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub mod repl; // New EXTREME Quality REPL
              // pub mod repl_legacy; // Old REPL (backup) - temporarily disabled for integration
              // pub mod repl_modules;  // Temporarily disabled - compilation errors
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub mod assessment;
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub mod deterministic;
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub mod magic;
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub mod repl_recording;
pub mod replay;
pub mod replay_converter;
// pub mod arena;  // Disabled - uses unsafe code
pub mod inspect;
pub mod safe_arena;
pub mod transaction;
// pub mod resource_eval;  // Temporarily disabled - causes duplicate impl
// Export the unified REPL
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub use repl::{Repl, ReplConfig};
// pub use repl_legacy::{ReplConfig, ReplState as LegacyReplState}; // Temporarily disabled
// Export interpreter components
pub use interpreter::Interpreter;
// Export interpreter types from dedicated module (EXTREME TDD Round 52)
pub use interpreter_types::{CallFrame, InterpreterError, InterpreterResult};
// Export Value types from dedicated module (EXTREME TDD Round 52)
pub use value::{DataFrameColumn, Value};
// Export actor system components
pub use actor::{
    ActorBehavior, ActorContext, ActorId, ActorRef, ActorSystem, EchoActor, Message, MessageValue,
    SupervisorActor, SupervisorDirective,
};
// Export concurrent actor system
pub use actor_concurrent::{
    ActorState as ConcurrentActorState, ConcurrentActor, ConcurrentActorSystem, Envelope,
    SupervisionStrategy, SystemMessage, CONCURRENT_ACTOR_SYSTEM,
};
// Export assessment components
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub use assessment::{
    Assignment, AssignmentSetup, ExpectedBehavior, GradeReport, GradingEngine, GradingRubric,
    PlagiarismDetector, SecureSandbox, Task, TaskGrade, TestCase,
};
// Export magic commands
#[cfg(all(not(target_arch = "wasm32"), feature = "repl"))]
pub use magic::{MagicCommand, MagicRegistry, MagicResult, ProfileData, UnicodeExpander};
// Export inspection protocol
pub use inspect::{CompositeForm, DisplayForm, Inspect, InspectStyle, Inspector, OpaqueHandle};
// Export resource-bounded evaluation
pub use safe_arena::{SafeArena as Arena, TransactionalArena};
pub use transaction::{
    SavePoint, TransactionEvent, TransactionId, TransactionLog, TransactionMetadata,
    TransactionalState, Version, VersionedValue, MVCC,
};
// pub use resource_eval::{
//     CheckpointHandle, ResourceLimits, Sandbox,
// };
// Export replay-to-test converter
pub use replay_converter::{ConversionConfig, GeneratedTest, ReplayConverter, TestCategory};
// Export audit logging (entrenar integration)
pub use audit::{
    hash_chain_collector, ring_collector, stream_collector, EvalType, HashChainAuditCollector,
    ReplAuditCollector, ReplInputMode, ReplPath, RingAuditCollector, StreamAuditCollector,
    StreamFormat,
};

// EXTREME TDD Round 86: Comprehensive tests extracted to separate files
#[cfg(test)]
mod eval_builtin_tests;
#[cfg(test)]
mod interpreter_tests;

#[cfg(all(test, feature = "repl"))]
mod tests {
    use super::*;
    use std::sync::Arc;

    // Sprint 4: Comprehensive runtime tests for coverage improvement
    // Requires `repl` feature since all tests use Repl

    #[test]
    fn test_repl_creation_and_basic_eval() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("1 + 1")
                .expect("operation should succeed in test"),
            "2"
        );
        assert_eq!(
            repl.eval("2 * 3")
                .expect("operation should succeed in test"),
            "6"
        );
        assert_eq!(
            repl.eval("10 - 5")
                .expect("operation should succeed in test"),
            "5"
        );
    }

    #[test]
    fn test_repl_variable_binding() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("let x = 42")
                .expect("operation should succeed in test"),
            "42"
        );
        assert_eq!(
            repl.eval("x").expect("operation should succeed in test"),
            "42"
        );
        assert_eq!(
            repl.eval("let y = x + 8")
                .expect("operation should succeed in test"),
            "50"
        );
        assert_eq!(
            repl.eval("y").expect("operation should succeed in test"),
            "50"
        );
    }

    #[test]
    fn test_repl_function_definition() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("fn add(a, b) { a + b }")
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("add(3, 4)")
                .expect("operation should succeed in test"),
            "7"
        );
        assert_eq!(
            repl.eval("add(10, 20)")
                .expect("operation should succeed in test"),
            "30"
        );
    }

    #[test]
    fn test_repl_if_expression() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("if true { 1 } else { 2 }")
                .expect("operation should succeed in test"),
            "1"
        );
        assert_eq!(
            repl.eval("if false { 1 } else { 2 }")
                .expect("operation should succeed in test"),
            "2"
        );
        assert_eq!(
            repl.eval("if 5 > 3 { \"yes\" } else { \"no\" }")
                .expect("operation should succeed in test"),
            "\"yes\""
        );
    }

    #[test]
    fn test_repl_list_operations() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("[1, 2, 3]")
                .expect("operation should succeed in test"),
            "[1, 2, 3]"
        );
        assert_eq!(
            repl.eval("[]").expect("operation should succeed in test"),
            "[]"
        );
        assert_eq!(
            repl.eval("[1] + [2, 3]")
                .expect("operation should succeed in test"),
            "[1, 2, 3]"
        );
    }

    #[test]
    fn test_repl_for_loop() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("let mut sum = 0")
            .expect("operation should succeed in test");
        repl.eval("for i in 1..=5 { sum = sum + i }")
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("sum").expect("operation should succeed in test"),
            "15"
        );
    }

    #[test]
    fn test_repl_while_loop() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("let mut n = 0")
            .expect("operation should succeed in test");
        repl.eval("while n < 5 { n = n + 1 }")
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("n").expect("operation should succeed in test"),
            "5"
        );
    }

    #[test]
    fn test_repl_match_expression() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        let code = r#"
            match 2 {
                1 => "one",
                2 => "two",
                _ => "other"
            }
        "#;
        assert_eq!(
            repl.eval(code).expect("operation should succeed in test"),
            "\"two\""
        );
    }

    #[test]
    fn test_repl_lambda() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("let double = |x| x * 2")
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("double(21)")
                .expect("operation should succeed in test"),
            "42"
        );
    }

    #[test]
    fn test_repl_string_operations() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("\"hello\" + \" world\"")
                .expect("operation should succeed in test"),
            "\"hello world\""
        );
        assert_eq!(
            repl.eval("\"test\"")
                .expect("operation should succeed in test"),
            "\"test\""
        );
    }

    #[test]
    fn test_repl_boolean_operations() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("true && true")
                .expect("operation should succeed in test"),
            "true"
        );
        assert_eq!(
            repl.eval("true || false")
                .expect("operation should succeed in test"),
            "true"
        );
        assert_eq!(
            repl.eval("!true")
                .expect("operation should succeed in test"),
            "false"
        );
    }

    #[test]
    fn test_repl_comparison_operators() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("5 > 3")
                .expect("operation should succeed in test"),
            "true"
        );
        assert_eq!(
            repl.eval("3 < 5")
                .expect("operation should succeed in test"),
            "true"
        );
        assert_eq!(
            repl.eval("5 == 5")
                .expect("operation should succeed in test"),
            "true"
        );
        assert_eq!(
            repl.eval("5 != 3")
                .expect("operation should succeed in test"),
            "true"
        );
    }

    #[test]
    fn test_repl_float_arithmetic() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert_eq!(
            repl.eval("3.5 + 1.5")
                .expect("operation should succeed in test"),
            "5.0"
        );
        assert_eq!(
            repl.eval("10.0 - 2.5")
                .expect("operation should succeed in test"),
            "7.5"
        );
        assert_eq!(
            repl.eval("2.5 * 2.0")
                .expect("operation should succeed in test"),
            "5.0"
        );
    }

    #[test]
    fn test_repl_error_handling() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        assert!(repl.eval("undefined_var").is_err());
        assert!(repl.eval("1 / 0").is_err());
        // Should recover after error
        assert_eq!(
            repl.eval("2 + 2")
                .expect("operation should succeed in test"),
            "4"
        );
    }

    #[test]
    fn test_repl_memory_tracking() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        let initial = repl.memory_used();
        assert_eq!(initial, 0);

        repl.eval("let x = [1, 2, 3, 4, 5]")
            .expect("operation should succeed in test");
        assert!(repl.memory_used() >= initial);

        let pressure = repl.memory_pressure();
        assert!((0.0..=1.0).contains(&pressure));
    }

    #[test]
    fn test_repl_checkpoint_restore() {
        use crate::runtime::replay::DeterministicRepl;

        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("let x = 10")
            .expect("operation should succeed in test");

        let checkpoint = DeterministicRepl::checkpoint(&repl);
        repl.eval("let x = 20")
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("x").expect("operation should succeed in test"),
            "20"
        );

        DeterministicRepl::restore(&mut repl, &checkpoint)
            .expect("operation should succeed in test");
        assert_eq!(
            repl.eval("x").expect("operation should succeed in test"),
            "10"
        );
    }

    #[test]
    fn test_repl_bindings_management() {
        let mut repl = Repl::new(std::env::temp_dir()).expect("Repl::new should succeed in test");
        repl.eval("let a = 1")
            .expect("operation should succeed in test");
        repl.eval("let b = 2")
            .expect("operation should succeed in test");

        let bindings = repl.get_bindings();
        assert!(bindings.contains_key("a"));
        assert!(bindings.contains_key("b"));

        repl.clear_bindings();
        assert!(repl.get_bindings().is_empty());
    }

    #[test]
    fn test_value_types() {
        assert_eq!(Value::Integer(42).to_string(), "42");
        assert_eq!(Value::Float(3.15).to_string(), "3.15");
        assert_eq!(Value::Bool(true).to_string(), "true");
        assert_eq!(
            Value::from_string("hello".to_string()).to_string(),
            "\"hello\""
        );
        assert_eq!(Value::Nil.to_string(), "nil");
    }

    #[test]
    fn test_value_list() {
        let list = Value::Array(Arc::from(
            vec![Value::Integer(1), Value::Integer(2), Value::Integer(3)].as_slice(),
        ));
        assert_eq!(list.to_string(), "[1, 2, 3]");

        let empty = Value::from_array(vec![]);
        assert_eq!(empty.to_string(), "[]");
    }

    #[test]
    fn test_value_tuple() {
        let tuple = Value::Tuple(Arc::from(
            vec![Value::Integer(1), Value::from_string("test".to_string())].as_slice(),
        ));
        assert_eq!(tuple.to_string(), "(1, \"test\")");
    }
}