ruchy 4.1.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
//! Print Macro Helper Transpilation
//!
//! This module handles transpilation of print-related macros:
//! - println/print with string interpolation
//! - dbg and panic macros
//! - Multiple argument formatting
//! - Value printing with type-aware formatting
//!
//! **EXTREME TDD Round 64**: Extracted from statements.rs and mod.rs for modularization.

use super::Transpiler;
use crate::frontend::ast::{Expr, ExprKind, Literal};
use anyhow::Result;
use proc_macro2::TokenStream;
use quote::quote;

impl Transpiler {
    /// Transpiles println/print with string interpolation directly
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::{Transpiler, Parser};
    ///
    /// let mut transpiler = Transpiler::new();
    /// let mut parser = Parser::new(r#"println("Hello {name}")"#);
    /// let ast = parser.parse().expect("Failed to parse");
    /// let result = transpiler.transpile(&ast).expect("transpile should succeed in test").to_string();
    /// assert!(result.contains("println"));
    /// ```
    /// Complexity: 6 (within Toyota Way limits)
    pub fn transpile_print_with_interpolation(
        &self,
        func_name: &str,
        parts: &[crate::frontend::ast::StringPart],
    ) -> Result<TokenStream> {
        if parts.is_empty() {
            let func_tokens = proc_macro2::Ident::new(func_name, proc_macro2::Span::call_site());
            return Ok(quote! { #func_tokens!("") });
        }
        let mut format_string = String::new();
        let mut args = Vec::new();
        for part in parts {
            match part {
                crate::frontend::ast::StringPart::Text(s) => {
                    // Escape any format specifiers in literal parts
                    format_string.push_str(&s.replace('{', "{{").replace('}', "}}"));
                }
                crate::frontend::ast::StringPart::Expr(expr) => {
                    format_string.push_str("{}");
                    let expr_tokens = self.transpile_expr(expr)?;
                    args.push(expr_tokens);
                }
                crate::frontend::ast::StringPart::ExprWithFormat { expr, format_spec } => {
                    // Include the format specifier in the format string
                    format_string.push('{');
                    format_string.push_str(format_spec);
                    format_string.push('}');
                    let expr_tokens = self.transpile_expr(expr)?;
                    args.push(expr_tokens);
                }
            }
        }
        let func_tokens = proc_macro2::Ident::new(func_name, proc_macro2::Span::call_site());
        Ok(quote! {
            #func_tokens!(#format_string #(, #args)*)
        })
    }

    /// Try to transpile print/println/dbg/panic macros
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::{Transpiler, Parser};
    ///
    /// let mut transpiler = Transpiler::new();
    /// let mut parser = Parser::new("println(42)");
    /// let ast = parser.parse().expect("Failed to parse");
    /// let result = transpiler.transpile(&ast).expect("transpile should succeed in test").to_string();
    /// assert!(result.contains("println"));
    /// ```
    /// Complexity: 7 (within Toyota Way limits)
    pub fn try_transpile_print_macro(
        &self,
        func_tokens: &TokenStream,
        base_name: &str,
        args: &[Expr],
    ) -> Result<Option<TokenStream>> {
        if !(base_name == "println"
            || base_name == "print"
            || base_name == "dbg"
            || base_name == "panic")
        {
            return Ok(None);
        }
        // Handle single argument with string interpolation
        if (base_name == "println" || base_name == "print") && args.len() == 1 {
            if let ExprKind::StringInterpolation { parts } = &args[0].kind {
                return Ok(Some(
                    self.transpile_print_with_interpolation(base_name, parts)?,
                ));
            }
            // For single non-string arguments, add smart format string
            if !matches!(&args[0].kind, ExprKind::Literal(Literal::String(_))) {
                let arg_tokens = self.transpile_expr(&args[0])?;
                // DEFECT-DICT-DETERMINISM FIX: Use Debug format with BTreeMap (deterministic)
                // BTreeMap Debug format is sorted, so {:?} is safe and deterministic
                let format_str = "{:?}";
                return Ok(Some(quote! { #func_tokens!(#format_str, #arg_tokens) }));
            }
        }
        // Handle multiple arguments
        if args.len() > 1 {
            return self.transpile_print_multiple_args(func_tokens, args);
        }
        // Single string literal or simple case
        // RUCHYRUCHY-001: Escape braces in string literals used as format strings
        if args.len() == 1 {
            if let ExprKind::Literal(Literal::String(s)) = &args[0].kind {
                // For println!/print!, string literals are format strings, so escape braces
                let escaped = s.replace('{', "{{").replace('}', "}}");
                return Ok(Some(quote! { #func_tokens!(#escaped) }));
            }
        }
        let arg_tokens: Result<Vec<_>> = args.iter().map(|a| self.transpile_expr(a)).collect();
        let arg_tokens = arg_tokens?;
        Ok(Some(quote! { #func_tokens!(#(#arg_tokens),*) }))
    }

    /// Handle multiple arguments for print macros
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::{Transpiler, Parser};
    ///
    /// let mut transpiler = Transpiler::new();
    /// let mut parser = Parser::new(r#"println("Hello", "World")"#);
    /// let ast = parser.parse().expect("Failed to parse");
    /// let result = transpiler.transpile(&ast).expect("transpile should succeed in test").to_string();
    /// assert!(result.contains("println"));
    /// ```
    /// Complexity: 9 (within Toyota Way limits)
    pub fn transpile_print_multiple_args(
        &self,
        func_tokens: &TokenStream,
        args: &[Expr],
    ) -> Result<Option<TokenStream>> {
        // FIXED: Don't treat first string argument as format string
        // Instead, treat all arguments as values to print with spaces
        if args.is_empty() {
            return Ok(Some(quote! { #func_tokens!() }));
        }
        let all_args: Result<Vec<_>> = args.iter().map(|a| self.transpile_expr(a)).collect();
        let all_args = all_args?;
        if args.len() == 1 {
            // Single argument - check if it's a string-like expression
            match &args[0].kind {
                ExprKind::Literal(Literal::String(_)) | ExprKind::StringInterpolation { .. } => {
                    // String literal or interpolation - use Display format
                    Ok(Some(quote! { #func_tokens!("{}", #(#all_args)*) }))
                }
                ExprKind::Identifier(_) => {
                    // For identifiers, we can't know the type at compile time
                    // Use a runtime check to decide format
                    let arg = &all_args[0];
                    let printing_logic = self
                        .generate_value_printing_tokens(quote! { #arg }, quote! { #func_tokens });
                    Ok(Some(printing_logic))
                }
                _ => {
                    // DEFECT-DICT-DETERMINISM FIX: Debug format is OK with BTreeMap (sorted)
                    Ok(Some(quote! { #func_tokens!("{:?}", #(#all_args)*) }))
                }
            }
        } else {
            // Multiple arguments - check if first is format string
            if let ExprKind::Literal(Literal::String(format_str)) = &args[0].kind {
                if format_str.contains("{}") {
                    // First argument is a format string, rest are values
                    let format_arg = &all_args[0];
                    let value_args = &all_args[1..];
                    Ok(Some(
                        quote! { #func_tokens!(#format_arg, #(#value_args),*) },
                    ))
                } else {
                    // First argument is regular string, treat all as separate values
                    let format_parts: Vec<_> = args
                        .iter()
                        .map(|arg| match &arg.kind {
                            ExprKind::Literal(Literal::String(_)) => "{}",
                            _ => "{:?}",
                        })
                        .collect();
                    let format_str = format_parts.join(" ");
                    Ok(Some(quote! { #func_tokens!(#format_str, #(#all_args),*) }))
                }
            } else {
                // No format string, treat all as separate values
                let format_parts: Vec<_> = args
                    .iter()
                    .map(|arg| match &arg.kind {
                        ExprKind::Literal(Literal::String(_)) => "{}",
                        _ => "{:?}",
                    })
                    .collect();
                let format_str = format_parts.join(" ");
                Ok(Some(quote! { #func_tokens!(#format_str, #(#all_args),*) }))
            }
        }
    }

    /// Centralized value printing logic for functions like println
    ///
    /// Generates code that checks type at runtime to avoid quotes around strings.
    ///
    /// # Examples
    ///
    /// ```ignore
    /// let tokens = transpiler.generate_value_printing_tokens(
    ///     quote! { my_value },
    ///     quote! { println }
    /// );
    /// ```
    /// Complexity: 3 (within Toyota Way limits)
    pub fn generate_value_printing_tokens(
        &self,
        value_expr: TokenStream,
        func_tokens: TokenStream,
    ) -> TokenStream {
        quote! {
            {
                use std::any::Any;
                let value = #value_expr;
                // Special handling for String and &str types to avoid quotes
                if let Some(s) = (&value as &dyn Any).downcast_ref::<String>() {
                    #func_tokens!("{}", s)
                } else if let Some(s) = (&value as &dyn Any).downcast_ref::<&str>() {
                    #func_tokens!("{}", s)
                } else {
                    #func_tokens!("{:?}", value)
                }
            }
        }
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frontend::ast::{Literal, Span, StringPart};

    fn make_expr(kind: ExprKind) -> Expr {
        Expr {
            kind,
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn string_expr(s: &str) -> Expr {
        make_expr(ExprKind::Literal(Literal::String(s.to_string())))
    }

    fn int_expr(n: i64) -> Expr {
        make_expr(ExprKind::Literal(Literal::Integer(n, None)))
    }

    fn ident_expr(name: &str) -> Expr {
        make_expr(ExprKind::Identifier(name.to_string()))
    }

    fn interpolation_expr(parts: Vec<StringPart>) -> Expr {
        make_expr(ExprKind::StringInterpolation { parts })
    }

    // ========================================================================
    // transpile_print_with_interpolation tests
    // ========================================================================

    #[test]
    fn test_print_interpolation_empty() {
        let transpiler = Transpiler::new();
        let parts: Vec<StringPart> = vec![];
        let result = transpiler.transpile_print_with_interpolation("println", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("println"));
    }

    #[test]
    fn test_print_interpolation_text_only() {
        let transpiler = Transpiler::new();
        let parts = vec![StringPart::Text("Hello, World!".to_string())];
        let result = transpiler.transpile_print_with_interpolation("println", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("println"));
        assert!(tokens_str.contains("Hello, World!"));
    }

    #[test]
    fn test_print_interpolation_expr() {
        let transpiler = Transpiler::new();
        let parts = vec![
            StringPart::Text("Value: ".to_string()),
            StringPart::Expr(Box::new(int_expr(42))),
        ];
        let result = transpiler.transpile_print_with_interpolation("println", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("println"));
        assert!(tokens_str.contains("Value:"));
        assert!(tokens_str.contains("42"));
    }

    #[test]
    fn test_print_interpolation_with_format_spec() {
        let transpiler = Transpiler::new();
        let parts = vec![StringPart::ExprWithFormat {
            expr: Box::new(int_expr(42)),
            format_spec: ":>10".to_string(),
        }];
        let result = transpiler.transpile_print_with_interpolation("print", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("print"));
    }

    #[test]
    fn test_print_interpolation_escapes_braces() {
        let transpiler = Transpiler::new();
        let parts = vec![StringPart::Text("Use {braces}".to_string())];
        let result = transpiler.transpile_print_with_interpolation("println", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        // Braces should be escaped
        assert!(tokens_str.contains("{{braces}}"));
    }

    #[test]
    fn test_print_interpolation_multiple_parts() {
        let transpiler = Transpiler::new();
        let parts = vec![
            StringPart::Text("Hello, ".to_string()),
            StringPart::Expr(Box::new(ident_expr("name"))),
            StringPart::Text("! You have ".to_string()),
            StringPart::Expr(Box::new(int_expr(5))),
            StringPart::Text(" messages.".to_string()),
        ];
        let result = transpiler.transpile_print_with_interpolation("println", &parts);
        assert!(result.is_ok());
        let tokens_str = result.unwrap().to_string();
        assert!(tokens_str.contains("println"));
        assert!(tokens_str.contains("name"));
    }

    // ========================================================================
    // try_transpile_print_macro tests
    // ========================================================================

    #[test]
    fn test_print_macro_println() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Hello")];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "println", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("println"));
    }

    #[test]
    fn test_print_macro_println_escapes_braces() {
        // RUCHYRUCHY-001: Test brace escaping for ruchyruchy bootstrap compatibility
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Delimiters: {, }")];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "println", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        // Braces should be escaped to {{ and }}
        assert!(
            tokens_str.contains("{{") && tokens_str.contains("}}"),
            "Expected escaped braces, got: {}",
            tokens_str
        );
    }

    #[test]
    fn test_print_macro_print() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { print };
        let args = vec![string_expr("Hello")];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "print", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_macro_dbg() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { dbg };
        let args = vec![int_expr(42)];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "dbg", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_macro_panic() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { panic };
        let args = vec![string_expr("error!")];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "panic", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_macro_unknown() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { foo };
        let args = vec![int_expr(42)];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "foo", &args);
        assert!(result.is_ok());
        assert!(result.unwrap().is_none());
    }

    #[test]
    fn test_print_macro_with_interpolation() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![interpolation_expr(vec![
            StringPart::Text("Hello, ".to_string()),
            StringPart::Expr(Box::new(ident_expr("name"))),
        ])];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "println", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_macro_single_int() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![int_expr(42)];
        let result = transpiler.try_transpile_print_macro(&func_tokens, "println", &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        // Should use {:?} format for non-string
        assert!(tokens_str.contains("{:?}"));
    }

    // ========================================================================
    // transpile_print_multiple_args tests
    // ========================================================================

    #[test]
    fn test_print_multiple_args_empty() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args: Vec<Expr> = vec![];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_multiple_args_single_string() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Hello")];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("{}"));
    }

    #[test]
    fn test_print_multiple_args_single_ident() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![ident_expr("value")];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_multiple_args_single_int() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![int_expr(42)];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        assert!(tokens_str.contains("{:?}"));
    }

    #[test]
    fn test_print_multiple_args_format_string() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Value: {}"), int_expr(42)];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_multiple_args_no_format_string() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Hello"), string_expr("World")];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_multiple_args_mixed_types() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![string_expr("Count:"), int_expr(42), ident_expr("name")];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
    }

    #[test]
    fn test_print_multiple_args_no_format_all_ints() {
        let transpiler = Transpiler::new();
        let func_tokens = quote! { println };
        let args = vec![int_expr(1), int_expr(2), int_expr(3)];
        let result = transpiler.transpile_print_multiple_args(&func_tokens, &args);
        assert!(result.is_ok());
        let tokens = result.unwrap();
        assert!(tokens.is_some());
        let tokens_str = tokens.unwrap().to_string();
        // Should have {:?} format parts
        assert!(tokens_str.contains("{:?}"));
    }

    // ========================================================================
    // generate_value_printing_tokens tests
    // ========================================================================

    #[test]
    fn test_generate_value_printing_tokens() {
        let transpiler = Transpiler::new();
        let value_expr = quote! { my_value };
        let func_tokens = quote! { println };
        let result = transpiler.generate_value_printing_tokens(value_expr, func_tokens);
        let result_str = result.to_string();
        assert!(result_str.contains("Any"));
        assert!(result_str.contains("downcast_ref"));
        assert!(result_str.contains("String"));
    }

    #[test]
    fn test_generate_value_printing_tokens_different_func() {
        let transpiler = Transpiler::new();
        let value_expr = quote! { x };
        let func_tokens = quote! { print };
        let result = transpiler.generate_value_printing_tokens(value_expr, func_tokens);
        let result_str = result.to_string();
        assert!(result_str.contains("print"));
    }

    #[test]
    fn test_generate_value_printing_tokens_complex_expr() {
        let transpiler = Transpiler::new();
        let value_expr = quote! { some_struct.field };
        let func_tokens = quote! { println };
        let result = transpiler.generate_value_printing_tokens(value_expr, func_tokens);
        let result_str = result.to_string();
        assert!(result_str.contains("some_struct"));
    }
}