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
//! AST to MIR lowering
use super::builder::MirBuilder;
use super::types::{
    BinOp, BlockId, Constant, Mutability, Operand, Place, Program, Rvalue, Type, UnOp,
};
use crate::frontend::ast::{
    BinaryOp as AstBinOp, Expr, ExprKind, Literal, Param, Type as AstType, UnaryOp as AstUnOp,
};
use anyhow::{anyhow, Result};
use std::collections::HashMap;
/// Context for lowering AST to MIR
pub struct LoweringContext {
    /// MIR builder
    builder: MirBuilder,
    /// Type environment for expressions
    #[allow(dead_code)]
    type_env: HashMap<String, Type>,
    /// Current block being built
    current_block: Option<BlockId>,
}
impl LoweringContext {
    /// Create a new lowering context
    #[must_use]
    /// # Examples
    ///
    /// ```
    /// use ruchy::middleend::mir::lower::new;
    ///
    /// let result = new(());
    /// assert_eq!(result, Ok(()));
    /// ```
    pub fn new() -> Self {
        Self {
            builder: MirBuilder::new(),
            type_env: HashMap::new(),
            current_block: None,
        }
    }
    /// Lower an expression to MIR
    ///
    /// # Errors
    ///
    /// Returns an error if the expression cannot be lowered to MIR
    /// # Errors
    ///
    /// Returns an error if the operation fails
    /// Lower a Ruchy expression to MIR
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::middleend::mir::lower::LoweringContext;
    /// use ruchy::frontend::ast::Expr;
    /// let mut ctx = LoweringContext::new();
    /// // ctx.lower_expr(&expr)?;
    /// ```
    pub fn lower_expr(&mut self, expr: &Expr) -> Result<Program> {
        match &expr.kind {
            ExprKind::Function {
                name,
                params,
                return_type,
                body,
                ..
            } => self.lower_function(name, params, return_type.as_ref(), body),
            _ => {
                // For non-function expressions, create a main function
                self.lower_main_expr(expr)
            }
        }
    }
    /// Lower a function expression
    fn lower_function(
        &mut self,
        name: &str,
        params: &[Param],
        return_type: Option<&AstType>,
        body: &Expr,
    ) -> Result<Program> {
        let func_name = name.to_string();
        let ret_ty = return_type.map_or(Type::Unit, Self::ast_to_mir_type);
        self.builder.start_function(func_name.clone(), ret_ty);
        // Add parameters
        for param in params {
            let ty = Self::ast_to_mir_type(&param.ty);
            self.builder.add_param(param.name(), ty);
        }
        // Create entry block
        let entry = self.builder.new_block();
        self.current_block = Some(entry);
        // Lower function body
        let result = self.lower_expr_to_operand(body)?;
        // Return the result
        self.builder.return_(entry, Some(result));
        let function = self
            .builder
            .finish_function()
            .ok_or_else(|| anyhow!("Failed to finish function"))?;
        let mut functions = HashMap::new();
        functions.insert(func_name.clone(), function);
        Ok(Program {
            functions,
            entry: func_name,
        })
    }
    /// Lower a main expression (wrap in main function)
    fn lower_main_expr(&mut self, expr: &Expr) -> Result<Program> {
        self.builder.start_function("main".to_string(), Type::Unit);
        let entry = self.builder.new_block();
        self.current_block = Some(entry);
        // Lower the expression
        let _result = self.lower_expr_to_operand(expr)?;
        // Main function returns unit
        self.builder
            .return_(entry, Some(Operand::Constant(Constant::Unit)));
        let function = self
            .builder
            .finish_function()
            .ok_or_else(|| anyhow!("Failed to finish main function"))?;
        let mut functions = HashMap::new();
        functions.insert("main".to_string(), function);
        Ok(Program {
            functions,
            entry: "main".to_string(),
        })
    }
    /// Lower an expression to an operand
    fn lower_expr_to_operand(&mut self, expr: &Expr) -> Result<Operand> {
        let block = self
            .current_block
            .ok_or_else(|| anyhow!("No current block"))?;
        match &expr.kind {
            ExprKind::Literal(lit) => Ok(Operand::Constant(Self::lower_literal(lit))),
            ExprKind::Identifier(name) => {
                if let Some(local) = self.builder.get_local(name) {
                    Ok(Operand::Copy(Place::Local(local)))
                } else {
                    Err(anyhow!("Unbound variable: {name}"))
                }
            }
            ExprKind::Binary { op, left, right } => {
                let left_op = self.lower_expr_to_operand(left)?;
                let right_op = self.lower_expr_to_operand(right)?;
                let mir_op = Self::lower_binary_op(*op);
                // Create a variable for the result
                let result_ty = Self::infer_binary_result_type(*op);
                let temp = self.builder.alloc_local(result_ty, false, None);
                self.builder
                    .binary_op(block, temp, mir_op, left_op, right_op);
                Ok(Operand::Move(Place::Local(temp)))
            }
            ExprKind::Unary { op, operand } => {
                let operand_mir = self.lower_expr_to_operand(operand)?;
                let mir_op = Self::lower_unary_op(*op);
                let result_ty = Self::infer_unary_result_type(*op);
                let temp = self.builder.alloc_local(result_ty, false, None);
                self.builder.unary_op(block, temp, mir_op, operand_mir);
                Ok(Operand::Move(Place::Local(temp)))
            }
            ExprKind::Let {
                name, value, body, ..
            } => {
                // Lower the value
                let value_op = self.lower_expr_to_operand(value)?;
                // Create a local for the binding
                let local_ty = Type::I32; // Default type inference
                let local = self
                    .builder
                    .alloc_local(local_ty, false, Some(name.clone()));
                // Assign the value
                self.builder
                    .assign(block, Place::Local(local), Rvalue::Use(value_op));
                // Lower the body with the binding in scope
                self.lower_expr_to_operand(body)
            }
            ExprKind::If {
                condition,
                then_branch,
                else_branch,
            } => {
                let cond_op = self.lower_expr_to_operand(condition)?;
                let then_block = self.builder.new_block();
                let else_block = self.builder.new_block();
                let merge_block = self.builder.new_block();
                // Branch based on condition
                self.builder.branch(block, cond_op, then_block, else_block);
                // Lower then branch
                self.current_block = Some(then_block);
                let then_result = self.lower_expr_to_operand(then_branch)?;
                self.builder.goto(then_block, merge_block);
                // Lower else branch
                self.current_block = Some(else_block);
                let _else_result = if let Some(else_expr) = else_branch {
                    self.lower_expr_to_operand(else_expr)?
                } else {
                    Operand::Constant(Constant::Unit)
                };
                self.builder.goto(else_block, merge_block);
                // Create a variable for the result
                let result_ty = Type::I32; // Type inference would determine this
                let result_temp = self.builder.alloc_local(result_ty, false, None);
                // In merge block, we'd need phi nodes, but for simplicity we'll just use the then result
                self.current_block = Some(merge_block);
                self.builder.assign(
                    merge_block,
                    Place::Local(result_temp),
                    Rvalue::Use(then_result),
                );
                Ok(Operand::Move(Place::Local(result_temp)))
            }
            ExprKind::Call { func, args } => {
                let func_op = self.lower_expr_to_operand(func)?;
                let mut arg_ops = Vec::new();
                for arg in args {
                    arg_ops.push(self.lower_expr_to_operand(arg)?);
                }
                // Create a variable for the result
                let result_ty = Type::I32; // Type inference would determine this
                let result_temp = self.builder.alloc_local(result_ty, false, None);
                // Create call terminator
                let next_block = self.builder.call(block, result_temp, func_op, arg_ops);
                self.current_block = Some(next_block);
                Ok(Operand::Move(Place::Local(result_temp)))
            }
            ExprKind::Block(exprs) => {
                if exprs.is_empty() {
                    Ok(Operand::Constant(Constant::Unit))
                } else {
                    // Lower all expressions, return the last one
                    let mut result = Operand::Constant(Constant::Unit);
                    for expr in exprs {
                        result = self.lower_expr_to_operand(expr)?;
                    }
                    Ok(result)
                }
            }
            _ => {
                // For unsupported expressions, return unit for now
                Ok(Operand::Constant(Constant::Unit))
            }
        }
    }
    /// Lower a literal to a constant
    fn lower_literal(lit: &Literal) -> Constant {
        match lit {
            Literal::Integer(i, _) => Constant::Int(i128::from(*i), Type::I32),
            Literal::Float(f) => Constant::Float(*f, Type::F64),
            Literal::String(s) => Constant::String(s.clone()),
            Literal::Bool(b) => Constant::Bool(*b),
            Literal::Char(c) => Constant::Char(*c),
            Literal::Byte(b) => Constant::Int(i128::from(*b), Type::U8),
            Literal::Unit => Constant::Unit,
            Literal::Null => Constant::Unit, // Null represented as Unit in MIR
            Literal::Atom(s) => Constant::Symbol(s.clone()),
        }
    }
    /// Lower binary operator
    fn lower_binary_op(op: AstBinOp) -> BinOp {
        match op {
            AstBinOp::Add => BinOp::Add,
            AstBinOp::Subtract => BinOp::Sub,
            AstBinOp::Multiply => BinOp::Mul,
            AstBinOp::Divide => BinOp::Div,
            AstBinOp::Modulo => BinOp::Rem,
            AstBinOp::Power => BinOp::Pow,
            AstBinOp::Equal => BinOp::Eq,
            AstBinOp::NotEqual => BinOp::Ne,
            AstBinOp::Less => BinOp::Lt,
            AstBinOp::LessEqual => BinOp::Le,
            AstBinOp::Greater => BinOp::Gt,
            AstBinOp::GreaterEqual => BinOp::Ge,
            AstBinOp::Gt => BinOp::Gt, // Alias for Greater
            AstBinOp::And => BinOp::And,
            AstBinOp::Or => BinOp::Or,
            AstBinOp::NullCoalesce => BinOp::NullCoalesce,
            AstBinOp::BitwiseAnd => BinOp::BitAnd,
            AstBinOp::BitwiseOr => BinOp::BitOr,
            AstBinOp::BitwiseXor => BinOp::BitXor,
            AstBinOp::LeftShift => BinOp::Shl,
            AstBinOp::RightShift => BinOp::Shr,
            AstBinOp::Send => BinOp::Send,
            AstBinOp::In => BinOp::In,
        }
    }
    /// Lower unary operator
    fn lower_unary_op(op: AstUnOp) -> UnOp {
        match op {
            AstUnOp::Negate => UnOp::Neg,
            AstUnOp::Not => UnOp::Not,
            AstUnOp::BitwiseNot => UnOp::BitNot,
            AstUnOp::Reference | AstUnOp::MutableReference => UnOp::Ref, // PARSER-085: Issue #71
            AstUnOp::Deref => UnOp::Deref,
        }
    }
    /// Convert AST Type to MIR Type
    #[allow(clippy::only_used_in_recursion)]
    fn ast_to_mir_type(ast_ty: &AstType) -> Type {
        use crate::frontend::ast::TypeKind;
        match &ast_ty.kind {
            TypeKind::Named(name) => match name.as_str() {
                "bool" => Type::Bool,
                "i8" => Type::I8,
                "i16" => Type::I16,
                "i32" => Type::I32,
                "i64" => Type::I64,
                "i128" => Type::I128,
                "u8" => Type::U8,
                "u16" => Type::U16,
                "u32" => Type::U32,
                "u64" => Type::U64,
                "u128" => Type::U128,
                "f32" => Type::F32,
                "f64" => Type::F64,
                "String" => Type::String,
                "()" => Type::Unit,
                _ => Type::UserType(name.clone()),
            },
            TypeKind::Generic { base, params } => {
                match base.as_str() {
                    "Vec" if params.len() == 1 => {
                        Type::Vec(Box::new(Self::ast_to_mir_type(&params[0])))
                    }
                    "Array" if params.len() == 1 => {
                        // For simplicity, treat arrays as vectors for now
                        Type::Vec(Box::new(Self::ast_to_mir_type(&params[0])))
                    }
                    _ => Type::UserType(base.clone()),
                }
            }
            TypeKind::Optional(inner) => {
                // For simplicity, treat optionals as user types for now
                Type::UserType(format!("Option<{:?}>", inner.kind))
            }
            TypeKind::Function { params, ret } => {
                let param_types = params.iter().map(Self::ast_to_mir_type).collect();
                Type::FnPtr(param_types, Box::new(Self::ast_to_mir_type(ret)))
            }
            TypeKind::List(inner) => Type::Vec(Box::new(Self::ast_to_mir_type(inner))),
            TypeKind::Array { elem_type, size: _ } => {
                // For MIR, treat arrays as vectors for now
                // The size information is preserved in the AST
                Type::Vec(Box::new(Self::ast_to_mir_type(elem_type)))
            }
            TypeKind::DataFrame { .. } => {
                // Map DataFrames to a user type for now
                Type::UserType("DataFrame".to_string())
            }
            TypeKind::Series { .. } => {
                // Map Series to a user type for now
                Type::UserType("Series".to_string())
            }
            TypeKind::Tuple(types) => {
                let mir_types: Vec<_> = types.iter().map(Self::ast_to_mir_type).collect();
                Type::Tuple(mir_types)
            }
            TypeKind::Reference { inner, .. } => {
                // For MIR, treat references as the inner type for now
                Self::ast_to_mir_type(inner)
            }
            // SPEC-001-H: Refined types - extract base type, ignore constraint
            // MIR operates on structural types, not refinements
            TypeKind::Refined { base, .. } => Self::ast_to_mir_type(base),
        }
    }
    /// Infer result type for binary operations
    fn infer_binary_result_type(op: AstBinOp) -> Type {
        match op {
            AstBinOp::Add
            | AstBinOp::Subtract
            | AstBinOp::Multiply
            | AstBinOp::Divide
            | AstBinOp::Modulo
            | AstBinOp::Power
            | AstBinOp::BitwiseAnd
            | AstBinOp::BitwiseOr
            | AstBinOp::BitwiseXor
            | AstBinOp::LeftShift
            | AstBinOp::RightShift => Type::I32,
            AstBinOp::Equal
            | AstBinOp::NotEqual
            | AstBinOp::Less
            | AstBinOp::LessEqual
            | AstBinOp::Greater
            | AstBinOp::GreaterEqual
            | AstBinOp::Gt
            | AstBinOp::And
            | AstBinOp::Or
            | AstBinOp::In => Type::Bool, // In returns boolean (membership test)
            AstBinOp::NullCoalesce => Type::I32, // For now, assume Int (could be improved)
            AstBinOp::Send => Type::Unit,        // Actor message passing returns unit
        }
    }
    /// Infer result type for unary operations
    fn infer_unary_result_type(op: AstUnOp) -> Type {
        match op {
            AstUnOp::Negate | AstUnOp::BitwiseNot => Type::I32,
            AstUnOp::Not => Type::Bool,
            AstUnOp::Reference => Type::Ref(Box::new(Type::I32), Mutability::Immutable), // & creates an immutable reference
            AstUnOp::MutableReference => Type::Ref(Box::new(Type::I32), Mutability::Mutable), // &mut creates a mutable reference (PARSER-085: Issue #71)
            AstUnOp::Deref => Type::I32, // Dereference returns the inner type
        }
    }
}
impl Default for LoweringContext {
    fn default() -> Self {
        Self::new()
    }
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::frontend::Parser;
    #[test]
    fn test_lower_literal() -> Result<()> {
        let mut parser = Parser::new("42");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        assert_eq!(program.entry, "main");
        assert!(program.functions.contains_key("main"));
        Ok(())
    }
    #[test]
    fn test_lower_binary_expr() -> Result<()> {
        let mut parser = Parser::new("1 + 2");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        let main_func = &program.functions["main"];
        assert!(!main_func.blocks.is_empty());
        Ok(())
    }
    #[test]
    fn test_lower_function() -> Result<()> {
        let mut parser = Parser::new("fun add(x: i32, y: i32) -> i32 { x + y }");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        assert!(program.functions.contains_key("add"));
        let func = &program.functions["add"];
        assert_eq!(func.params.len(), 2);
        Ok(())
    }
    #[test]
    fn test_lower_if_expr() -> Result<()> {
        let mut parser = Parser::new("if true { 1 } else { 2 }");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        let main_func = &program.functions["main"];
        // Should have multiple blocks for if/else
        assert!(main_func.blocks.len() > 1);
        Ok(())
    }

    #[test]
    fn test_lowering_context_default() {
        let ctx = LoweringContext::default();
        assert!(ctx.current_block.is_none());
    }

    #[test]
    fn test_lower_literal_integer() {
        let lit = Literal::Integer(42, None);
        let constant = LoweringContext::lower_literal(&lit);
        assert!(matches!(constant, Constant::Int(42, Type::I32)));
    }

    #[test]
    fn test_lower_literal_float() {
        let lit = Literal::Float(3.14);
        let constant = LoweringContext::lower_literal(&lit);
        if let Constant::Float(f, Type::F64) = constant {
            assert!((f - 3.14).abs() < f64::EPSILON);
        } else {
            panic!("Expected Float constant");
        }
    }

    #[test]
    fn test_lower_literal_string() {
        let lit = Literal::String("hello".to_string());
        let constant = LoweringContext::lower_literal(&lit);
        assert!(matches!(constant, Constant::String(s) if s == "hello"));
    }

    #[test]
    fn test_lower_literal_bool() {
        let lit = Literal::Bool(true);
        let constant = LoweringContext::lower_literal(&lit);
        assert!(matches!(constant, Constant::Bool(true)));
    }

    #[test]
    fn test_lower_literal_char() {
        let lit = Literal::Char('x');
        let constant = LoweringContext::lower_literal(&lit);
        assert!(matches!(constant, Constant::Char('x')));
    }

    #[test]
    fn test_lower_literal_unit() {
        let lit = Literal::Unit;
        let constant = LoweringContext::lower_literal(&lit);
        assert!(matches!(constant, Constant::Unit));
    }

    #[test]
    fn test_lower_binary_op_add() {
        let op = LoweringContext::lower_binary_op(AstBinOp::Add);
        assert!(matches!(op, BinOp::Add));
    }

    #[test]
    fn test_lower_binary_op_subtract() {
        let op = LoweringContext::lower_binary_op(AstBinOp::Subtract);
        assert!(matches!(op, BinOp::Sub));
    }

    #[test]
    fn test_lower_binary_op_equal() {
        let op = LoweringContext::lower_binary_op(AstBinOp::Equal);
        assert!(matches!(op, BinOp::Eq));
    }

    #[test]
    fn test_lower_binary_op_and() {
        let op = LoweringContext::lower_binary_op(AstBinOp::And);
        assert!(matches!(op, BinOp::And));
    }

    #[test]
    fn test_lower_unary_op_negate() {
        let op = LoweringContext::lower_unary_op(AstUnOp::Negate);
        assert!(matches!(op, UnOp::Neg));
    }

    #[test]
    fn test_lower_unary_op_not() {
        let op = LoweringContext::lower_unary_op(AstUnOp::Not);
        assert!(matches!(op, UnOp::Not));
    }

    #[test]
    fn test_infer_binary_result_type_arithmetic() {
        let ty = LoweringContext::infer_binary_result_type(AstBinOp::Add);
        assert!(matches!(ty, Type::I32));
    }

    #[test]
    fn test_infer_binary_result_type_comparison() {
        let ty = LoweringContext::infer_binary_result_type(AstBinOp::Equal);
        assert!(matches!(ty, Type::Bool));
    }

    #[test]
    fn test_infer_unary_result_type_negate() {
        let ty = LoweringContext::infer_unary_result_type(AstUnOp::Negate);
        assert!(matches!(ty, Type::I32));
    }

    #[test]
    fn test_infer_unary_result_type_not() {
        let ty = LoweringContext::infer_unary_result_type(AstUnOp::Not);
        assert!(matches!(ty, Type::Bool));
    }

    #[test]
    fn test_lower_let_expr() -> Result<()> {
        let mut parser = Parser::new("let x = 5 in x");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        assert!(program.functions.contains_key("main"));
        Ok(())
    }

    #[test]
    fn test_lower_block_expr() -> Result<()> {
        let mut parser = Parser::new("{ 1; 2; 3 }");
        let ast = parser.parse()?;
        let mut ctx = LoweringContext::new();
        let program = ctx.lower_expr(&ast)?;
        assert!(program.functions.contains_key("main"));
        Ok(())
    }
}
#[cfg(test)]
mod property_tests_lower {
    use proptest::proptest;

    proptest! {
        /// Property: Function never panics on any input
        #[test]
        fn test_new_never_panics(input: String) {
            // Limit input size to avoid timeout
            let _input = if input.len() > 100 { &input[..100] } else { &input[..] };
            // Function should not panic on any input
            let _ = std::panic::catch_unwind(|| {
                // Call function with various inputs
                // This is a template - adjust based on actual function signature
            });
        }
    }
}