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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! MIR type definitions
use std::collections::HashMap;
use std::fmt;
/// A MIR program consists of functions
#[derive(Debug, Clone)]
pub struct Program {
    /// Global functions in the program
    pub functions: HashMap<String, Function>,
    /// Entry point function name
    pub entry: String,
}
/// A function in MIR representation
#[derive(Debug, Clone)]
pub struct Function {
    /// Function name
    pub name: String,
    /// Parameters (as local variables)
    pub params: Vec<Local>,
    /// Return type
    pub return_ty: Type,
    /// Local variables (including parameters)
    pub locals: Vec<LocalDecl>,
    /// Basic blocks making up the function body
    pub blocks: Vec<BasicBlock>,
    /// Entry block index
    pub entry_block: BlockId,
}
/// A basic block is a sequence of statements with a single entry and exit
#[derive(Debug, Clone)]
pub struct BasicBlock {
    /// Block identifier
    pub id: BlockId,
    /// Statements in this block (no control flow)
    pub statements: Vec<Statement>,
    /// Terminator - how control leaves this block
    pub terminator: Terminator,
}
/// Block identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct BlockId(pub usize);
/// Local variable identifier
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Local(pub usize);
/// Local variable declaration
#[derive(Debug, Clone)]
pub struct LocalDecl {
    /// Variable identifier
    pub id: Local,
    /// Variable type
    pub ty: Type,
    /// Is this mutable?
    pub mutable: bool,
    /// Optional name for debugging
    pub name: Option<String>,
}
/// MIR types (simplified from AST types)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Type {
    /// Unit type
    Unit,
    /// Boolean
    Bool,
    /// Signed integers
    I8,
    I16,
    I32,
    I64,
    I128,
    /// Unsigned integers
    U8,
    U16,
    U32,
    U64,
    U128,
    /// Floating point
    F32,
    F64,
    /// String
    String,
    /// Reference (borrowed)
    Ref(Box<Type>, Mutability),
    /// Array with known size
    Array(Box<Type>, usize),
    /// Dynamic vector
    Vec(Box<Type>),
    /// Tuple
    Tuple(Vec<Type>),
    /// Function pointer
    FnPtr(Vec<Type>, Box<Type>),
    /// User-defined type
    UserType(String),
}
/// Mutability of references
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Mutability {
    Immutable,
    Mutable,
}
/// A statement that doesn't affect control flow
#[derive(Debug, Clone)]
pub enum Statement {
    /// Assign an rvalue to a place
    Assign(Place, Rvalue),
    /// Mark a local as live (for storage)
    StorageLive(Local),
    /// Mark a local as dead (storage can be reclaimed)
    StorageDead(Local),
    /// No operation
    Nop,
}
/// A place where a value can be stored
#[derive(Debug, Clone, PartialEq)]
pub enum Place {
    /// Local variable
    Local(Local),
    /// Field projection (e.g., struct.field)
    Field(Box<Place>, FieldIdx),
    /// Array/slice index
    Index(Box<Place>, Box<Place>),
    /// Dereference
    Deref(Box<Place>),
}
/// Field index in a struct/tuple
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FieldIdx(pub usize);
/// Right-hand side of an assignment
#[derive(Debug, Clone)]
pub enum Rvalue {
    /// Use a value from a place
    Use(Operand),
    /// Binary operation
    BinaryOp(BinOp, Operand, Operand),
    /// Unary operation
    UnaryOp(UnOp, Operand),
    /// Create a reference
    Ref(Mutability, Place),
    /// Create an aggregate (struct, tuple, array)
    Aggregate(AggregateKind, Vec<Operand>),
    /// Function call
    Call(Operand, Vec<Operand>),
    /// Cast between types
    Cast(CastKind, Operand, Type),
}
/// An operand (value that can be used)
#[derive(Debug, Clone)]
pub enum Operand {
    /// Copy value from a place
    Copy(Place),
    /// Move value from a place
    Move(Place),
    /// Constant value
    Constant(Constant),
}
/// Constant values
#[derive(Debug, Clone)]
pub enum Constant {
    /// Unit value
    Unit,
    /// Boolean
    Bool(bool),
    /// Integer
    Int(i128, Type),
    /// Unsigned integer
    Uint(u128, Type),
    /// Float
    Float(f64, Type),
    /// String literal
    String(String),
    /// Character literal
    Char(char),
    /// Symbol/Atom literal
    Symbol(String),
}
/// Binary operations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BinOp {
    // Arithmetic
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    Pow,
    // Bitwise
    BitAnd,
    BitOr,
    BitXor,
    Shl,
    Shr,
    // Comparison
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
    // Logical (short-circuiting is handled by control flow)
    And,
    Or,
    NullCoalesce,
    // Actor operations
    Send,
    // Containment check
    In,
}
/// Unary operations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnOp {
    /// Negation (arithmetic)
    Neg,
    /// Logical not
    Not,
    /// Bitwise not
    BitNot,
    /// Reference (borrow)
    Ref,
    /// Dereference
    Deref,
}
/// Aggregate kinds
#[derive(Debug, Clone)]
pub enum AggregateKind {
    /// Tuple
    Tuple,
    /// Array
    Array(Type),
    /// Struct
    Struct(String),
}
/// Cast kinds
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CastKind {
    /// Numeric cast (int to int, float to float, etc.)
    Numeric,
    /// Pointer to pointer
    Pointer,
    /// Unsizing (e.g., array to slice)
    Unsize,
}
/// How control flow leaves a basic block
#[derive(Debug, Clone)]
pub enum Terminator {
    /// Unconditional jump
    Goto(BlockId),
    /// Conditional branch
    If {
        condition: Operand,
        then_block: BlockId,
        else_block: BlockId,
    },
    /// Switch/match on a value
    Switch {
        discriminant: Operand,
        targets: Vec<(Constant, BlockId)>,
        default: Option<BlockId>,
    },
    /// Return from function
    Return(Option<Operand>),
    /// Call a function and continue
    Call {
        func: Operand,
        args: Vec<Operand>,
        destination: Option<(Place, BlockId)>,
    },
    /// Unreachable code (for exhaustiveness)
    Unreachable,
}
// Display implementations for debugging
impl fmt::Display for BlockId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "bb{}", self.0)
    }
}
impl fmt::Display for Local {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "__{}", self.0)
    }
}
impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Type::Unit => write!(f, "()"),
            Type::Bool => write!(f, "bool"),
            Type::I8 => write!(f, "i8"),
            Type::I16 => write!(f, "i16"),
            Type::I32 => write!(f, "i32"),
            Type::I64 => write!(f, "i64"),
            Type::I128 => write!(f, "i128"),
            Type::U8 => write!(f, "u8"),
            Type::U16 => write!(f, "u16"),
            Type::U32 => write!(f, "u32"),
            Type::U64 => write!(f, "u64"),
            Type::U128 => write!(f, "u128"),
            Type::F32 => write!(f, "f32"),
            Type::F64 => write!(f, "f64"),
            Type::String => write!(f, "String"),
            Type::Ref(ty, Mutability::Immutable) => write!(f, "&{ty}"),
            Type::Ref(ty, Mutability::Mutable) => write!(f, "&mut {ty}"),
            Type::Array(ty, size) => write!(f, "[{ty}; {size}]"),
            Type::Vec(ty) => write!(f, "Vec<{ty}>"),
            Type::Tuple(tys) => {
                write!(f, "(")?;
                for (i, ty) in tys.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{ty}")?;
                }
                write!(f, ")")
            }
            Type::FnPtr(params, ret) => {
                write!(f, "fn(")?;
                for (i, param) in params.iter().enumerate() {
                    if i > 0 {
                        write!(f, ", ")?;
                    }
                    write!(f, "{param}")?;
                }
                write!(f, ") -> {ret}")
            }
            Type::UserType(name) => write!(f, "{name}"),
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_program_creation() {
        let program = Program {
            functions: HashMap::new(),
            entry: "main".to_string(),
        };
        assert_eq!(program.entry, "main");
        assert_eq!(program.functions.len(), 0);
    }
    #[test]
    fn test_block_id() {
        let id1 = BlockId(0);
        let id2 = BlockId(1);
        let id3 = BlockId(0);
        assert_eq!(id1, id3);
        assert_ne!(id1, id2);
        assert_eq!(format!("{id1:?}"), "BlockId(0)");
    }
    #[test]
    fn test_local_variable() {
        let local1 = Local(0);
        let local2 = Local(1);
        let local3 = Local(0);
        assert_eq!(local1, local3);
        assert_ne!(local1, local2);
        assert_eq!(format!("{local1:?}"), "Local(0)");
    }
    #[test]
    fn test_type_variants() {
        let types = vec![
            Type::Unit,
            Type::Bool,
            Type::I32,
            Type::I64,
            Type::F32,
            Type::F64,
            Type::String,
            Type::Array(Box::new(Type::I32), 10),
            Type::Vec(Box::new(Type::F64)),
            Type::Tuple(vec![Type::I32, Type::Bool]),
        ];
        for ty in types {
            assert!(!format!("{ty:?}").is_empty());
        }
    }
    #[test]
    fn test_type_equality() {
        assert_eq!(Type::I32, Type::I32);
        assert_ne!(Type::I32, Type::I64);
        assert_eq!(
            Type::Array(Box::new(Type::U8), 5),
            Type::Array(Box::new(Type::U8), 5)
        );
        assert_ne!(
            Type::Array(Box::new(Type::U8), 5),
            Type::Array(Box::new(Type::U8), 10)
        );
    }

    #[test]
    fn test_function_creation() {
        let func = Function {
            name: "test_func".to_string(),
            params: vec![Local(0), Local(1)],
            return_ty: Type::I32,
            locals: vec![],
            blocks: vec![],
            entry_block: BlockId(0),
        };
        assert_eq!(func.name, "test_func");
        assert_eq!(func.params.len(), 2);
        assert_eq!(func.return_ty, Type::I32);
    }

    #[test]
    fn test_basic_block() {
        let block = BasicBlock {
            id: BlockId(0),
            statements: vec![],
            terminator: Terminator::Return(None),
        };
        assert_eq!(block.id, BlockId(0));
        assert!(block.statements.is_empty());
        assert!(matches!(block.terminator, Terminator::Return(None)));
    }

    #[test]
    fn test_local_decl() {
        let decl = LocalDecl {
            id: Local(5),
            ty: Type::String,
            mutable: true,
            name: Some("my_var".to_string()),
        };
        assert_eq!(decl.id, Local(5));
        assert_eq!(decl.ty, Type::String);
        assert!(decl.mutable);
        assert_eq!(decl.name, Some("my_var".to_string()));
    }

    #[test]
    fn test_operand_variants() {
        let operands = vec![
            Operand::Copy(Place::Local(Local(0))),
            Operand::Move(Place::Local(Local(1))),
            Operand::Constant(Constant::Int(42, Type::I64)),
        ];
        for op in operands {
            assert!(!format!("{op:?}").is_empty());
        }
    }

    #[test]
    fn test_place_projection() {
        let place = Place::Local(Local(0));
        assert!(matches!(place, Place::Local(_)));

        let field_place = Place::Field(Box::new(Place::Local(Local(0))), FieldIdx(1));
        assert!(matches!(field_place, Place::Field(_, _)));
    }

    #[test]
    fn test_constant_variants() {
        let constants = vec![
            Constant::Unit,
            Constant::Bool(true),
            Constant::Int(123, Type::I64),
            Constant::Float(3.15, Type::F64),
            Constant::String("hello".to_string()),
        ];
        for c in constants {
            assert!(!format!("{c:?}").is_empty());
        }
    }

    #[test]
    fn test_rvalue_variants() {
        let rvalues = vec![
            Rvalue::Use(Operand::Constant(Constant::Int(42, Type::I64))),
            Rvalue::BinaryOp(
                BinOp::Add,
                Operand::Constant(Constant::Int(1, Type::I64)),
                Operand::Constant(Constant::Int(2, Type::I64)),
            ),
            Rvalue::UnaryOp(UnOp::Neg, Operand::Constant(Constant::Int(5, Type::I64))),
            Rvalue::Ref(Mutability::Immutable, Place::Local(Local(0))),
        ];
        for rv in rvalues {
            assert!(!format!("{rv:?}").is_empty());
        }
    }

    #[test]
    fn test_binary_ops() {
        let ops = vec![
            BinOp::Add,
            BinOp::Sub,
            BinOp::Mul,
            BinOp::Div,
            BinOp::Eq,
            BinOp::Ne,
            BinOp::Lt,
            BinOp::Gt,
            BinOp::And,
            BinOp::Or,
            BinOp::BitAnd,
            BinOp::BitOr,
        ];
        for op in ops {
            assert!(!format!("{op:?}").is_empty());
        }
    }

    #[test]
    fn test_unary_ops() {
        let ops = vec![UnOp::Neg, UnOp::Not, UnOp::BitNot, UnOp::Ref];
        for op in ops {
            assert!(!format!("{op:?}").is_empty());
        }
    }

    #[test]
    fn test_aggregate_kind() {
        let kinds = vec![
            AggregateKind::Tuple,
            AggregateKind::Array(Type::I32),
            AggregateKind::Struct("MyStruct".to_string()),
        ];
        for kind in kinds {
            assert!(!format!("{kind:?}").is_empty());
        }
    }

    #[test]
    fn test_cast_kind() {
        assert_eq!(CastKind::Numeric, CastKind::Numeric);
        assert_ne!(CastKind::Numeric, CastKind::Pointer);
        assert_ne!(CastKind::Pointer, CastKind::Unsize);
    }

    #[test]
    fn test_terminator_variants() {
        let terminators = vec![
            Terminator::Goto(BlockId(1)),
            Terminator::If {
                condition: Operand::Constant(Constant::Bool(true)),
                then_block: BlockId(2),
                else_block: BlockId(3),
            },
            Terminator::Return(Some(Operand::Constant(Constant::Int(0, Type::I64)))),
            Terminator::Unreachable,
        ];
        for term in terminators {
            assert!(!format!("{term:?}").is_empty());
        }
    }

    #[test]
    fn test_mutability() {
        assert_eq!(Mutability::Immutable, Mutability::Immutable);
        assert_eq!(Mutability::Mutable, Mutability::Mutable);
        assert_ne!(Mutability::Immutable, Mutability::Mutable);
    }

    #[test]
    fn test_statement_assign() {
        let stmt = Statement::Assign(
            Place::Local(Local(0)),
            Rvalue::Use(Operand::Constant(Constant::Int(42, Type::I64))),
        );
        assert!(matches!(stmt, Statement::Assign(_, _)));
    }

    #[test]
    fn test_display_block_id() {
        let id = BlockId(42);
        assert_eq!(format!("{id}"), "bb42");
    }

    #[test]
    fn test_display_local() {
        let local = Local(7);
        assert_eq!(format!("{local}"), "__7");
    }

    #[test]
    fn test_display_types() {
        assert_eq!(format!("{}", Type::Unit), "()");
        assert_eq!(format!("{}", Type::Bool), "bool");
        assert_eq!(format!("{}", Type::I32), "i32");
        assert_eq!(format!("{}", Type::F64), "f64");
        assert_eq!(format!("{}", Type::String), "String");
        assert_eq!(format!("{}", Type::Vec(Box::new(Type::I32))), "Vec<i32>");
        assert_eq!(
            format!("{}", Type::Tuple(vec![Type::I32, Type::Bool])),
            "(i32, bool)"
        );
    }

    #[test]
    fn test_ref_type_display() {
        let immut_ref = Type::Ref(Box::new(Type::I32), Mutability::Immutable);
        assert_eq!(format!("{immut_ref}"), "&i32");

        let mut_ref = Type::Ref(Box::new(Type::String), Mutability::Mutable);
        assert_eq!(format!("{mut_ref}"), "&mut String");
    }

    #[test]
    fn test_array_type_display() {
        let arr = Type::Array(Box::new(Type::U8), 256);
        assert_eq!(format!("{arr}"), "[u8; 256]");
    }

    #[test]
    fn test_function_pointer_display() {
        let fn_ptr = Type::FnPtr(vec![Type::I32, Type::Bool], Box::new(Type::String));
        assert_eq!(format!("{fn_ptr}"), "fn(i32, bool) -> String");
    }

    #[test]
    fn test_user_type_display() {
        let user_type = Type::UserType("MyCustomType".to_string());
        assert_eq!(format!("{user_type}"), "MyCustomType");
    }

    #[test]
    fn test_switch_terminator() {
        let switch = Terminator::Switch {
            discriminant: Operand::Copy(Place::Local(Local(0))),
            targets: vec![
                (Constant::Int(0, Type::I64), BlockId(1)),
                (Constant::Int(1, Type::I64), BlockId(2)),
            ],
            default: Some(BlockId(3)),
        };
        if let Terminator::Switch {
            targets, default, ..
        } = switch
        {
            assert_eq!(targets.len(), 2);
            assert_eq!(default, Some(BlockId(3)));
        } else {
            panic!("Expected Switch terminator");
        }
    }

    #[test]
    fn test_call_terminator() {
        let call = Terminator::Call {
            func: Operand::Constant(Constant::String("my_func".to_string())),
            args: vec![
                Operand::Constant(Constant::Int(1, Type::I64)),
                Operand::Constant(Constant::Bool(true)),
            ],
            destination: Some((Place::Local(Local(0)), BlockId(1))),
        };
        if let Terminator::Call {
            args, destination, ..
        } = call
        {
            assert_eq!(args.len(), 2);
            assert!(destination.is_some());
        } else {
            panic!("Expected Call terminator");
        }
    }

    #[test]
    fn test_place_field() {
        let base = Place::Local(Local(0));
        let field = Place::Field(Box::new(base.clone()), FieldIdx(2));
        if let Place::Field(p, idx) = field {
            assert_eq!(*p, base);
            assert_eq!(idx, FieldIdx(2));
        } else {
            panic!("Expected Field place");
        }
    }

    #[test]
    fn test_place_index() {
        let base = Place::Local(Local(0));
        let index = Place::Index(Box::new(base.clone()), Box::new(Place::Local(Local(1))));
        if let Place::Index(p, _) = index {
            assert_eq!(*p, base);
        } else {
            panic!("Expected Index place");
        }
    }

    #[test]
    fn test_rvalue_aggregate() {
        let agg = Rvalue::Aggregate(
            AggregateKind::Tuple,
            vec![
                Operand::Constant(Constant::Int(1, Type::I64)),
                Operand::Constant(Constant::Bool(false)),
            ],
        );
        if let Rvalue::Aggregate(_, operands) = agg {
            assert_eq!(operands.len(), 2);
        } else {
            panic!("Expected Aggregate rvalue");
        }
    }

    #[test]
    fn test_rvalue_cast() {
        let cast = Rvalue::Cast(
            CastKind::Numeric,
            Operand::Constant(Constant::Int(42, Type::I64)),
            Type::F64,
        );
        if let Rvalue::Cast(kind, _, ty) = cast {
            assert_eq!(kind, CastKind::Numeric);
            assert_eq!(ty, Type::F64);
        } else {
            panic!("Expected Cast rvalue");
        }
    }
}