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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
//! Transpiler module for converting Ruchy AST to Rust code.
//!
//! This module implements the code generation phase of the Ruchy compiler,
//! transforming the Abstract Syntax Tree (AST) into executable Rust code
//! using the `proc_macro2` and `quote` crates for token generation.
//!
//! # Architecture
//!
//! The transpiler is organized into specialized submodules:
//! - `expressions`: Handles expression transpilation
//! - `statements`: Processes statements and declarations
//! - `patterns`: Pattern matching and destructuring
//! - `types`: Type conversion and inference
//! - `actors`: Actor model support
//! - `dataframe`: `DataFrame` operations
//!
//! # Code Generation Process
//!
//! 1. **AST Analysis**: Analyze mutability requirements and collect function signatures
//! 2. **Token Generation**: Convert AST nodes to Rust tokens using `quote!`
//! 3. **Type Inference**: Apply type inference for gradual typing
//! 4. **Optimization**: Apply transpilation-time optimizations
//! 5. **Formatting**: Generate readable, idiomatic Rust code
//!
//! # Examples
//!
//! ```ignore
//! use ruchy::{Parser, Transpiler};
//!
//! let mut parser = Parser::new("let x = 42");
//! let ast = parser.parse().unwrap();
//!
//! let mut transpiler = Transpiler::new();
//! let rust_code = transpiler.transpile(&ast).unwrap();
//! println!("{}", rust_code);
//! ```
#![allow(clippy::missing_errors_doc)]
#![allow(clippy::too_many_lines)]
mod actors;
pub mod codegen_minimal;
pub mod constant_folder; // PERF-002-A: Constant folding optimization
mod dataframe;
mod effects;
pub mod inline_expander; // OPT-CODEGEN-004: Inline expansion optimization
                         // #[cfg(feature = "dataframe")]
                         // mod dataframe_arrow; // Temporarily disabled until proper implementation
mod advanced_math; // EXTREME TDD Round 59: trig/log/random/trueno functions
mod ast_analysis; // EXTREME TDD Round 66: AST analysis, collection, detection functions
mod bindings; // EXTREME TDD Round 54: let bindings and patterns
mod block_categorization; // EXTREME TDD Round 67: Block categorization, statement detection
mod block_transpiler; // EXTREME TDD Round 71: Block and pipeline transpilation
mod body_generation; // EXTREME TDD Round 70: Function body token generation
pub mod builtin_type_inference;
mod call_helpers; // EXTREME TDD Round 63: result/option call and function call helpers
mod call_transpilation; // EXTREME TDD Round 72: Call and method call transpilation
mod comprehensions; // EXTREME TDD Round 53: List/set/dict comprehensions
mod control_flow; // EXTREME TDD Round 53: if/for/while/loop/try-catch
mod dataframe_builder;
mod dataframe_helpers;
mod dataframe_transpilers; // EXTREME TDD Round 80: DataFrame transpilation
mod dispatcher;
mod expr_dispatcher; // EXTREME TDD Round 69: Expression dispatcher and utilities
pub mod expression_analysis;
mod expressions;
pub mod function_analysis;
mod function_param_inference; // EXTREME TDD Round 70: Function parameter inference
mod function_signature; // EXTREME TDD Round 70: Function signature generation
mod function_transpiler; // EXTREME TDD Round 81: Function transpilation
pub mod import_helpers;
mod imports; // EXTREME TDD Round 55: imports and exports
mod input_builtins; // EXTREME TDD Round 57: input/readline functions
#[cfg(test)]
mod integration_tests; // Transpiler integration tests for coverage improvement
mod lambda_transpiler; // EXTREME TDD Round 71: Lambda/closure transpilation
mod lifetime_helpers; // EXTREME TDD Round 74: Lifetime parameter helpers
mod math_builtins; // EXTREME TDD Round 56: math built-in functions
mod method_call_refactored;
mod method_transpilers; // EXTREME TDD Round 65: iterator/map/set/string/collection methods
pub mod mutation_detection;
mod network_builtins; // EXTREME TDD Round 62: json/http functions
pub mod param_usage_analysis;
#[cfg(test)]
mod param_usage_analysis_tests; // EXTREME TDD Round 85: Parameter usage analysis tests
pub mod pattern_bindings;
mod patterns;
mod print_helpers; // EXTREME TDD Round 64: print/println/dbg/panic macros
mod program_transpiler; // EXTREME TDD Round 68: Program-level transpilation
mod result_type;
pub mod return_type_helpers;
mod statements;
#[cfg(test)]
mod statements_tests; // EXTREME TDD Round 83: Statement tests extracted
pub mod std_imports;
mod string_body_conversion; // EXTREME TDD Round 73: String body conversion helpers
mod system_builtins; // EXTREME TDD Round 61: env/fs/path functions
#[cfg(test)]
mod tests_compound_assignment;
pub mod type_analysis;
mod type_conversion_refactored;
mod type_conversions; // EXTREME TDD Round 58: str/int/float/bool conversions
mod type_inference;
mod type_transpilers; // EXTREME TDD Round 75: Type transpilation helpers
mod types;
#[cfg(test)]
mod types_tests; // EXTREME TDD Round 84: Type tests extracted
mod utility_builtins; // EXTREME TDD Round 60: time/assert/collection/range functions
use crate::frontend::ast::{Attribute, Expr, ExprKind, Type};
use anyhow::Result;
use proc_macro2::TokenStream;
// Module exports are handled by the impl blocks in each module
/// Block categorization result: (functions, statements, modules, `has_main`, `main_expr`, imports, globals)
/// TRANSPILER-SCOPE: Added globals vector for static mut declarations
type BlockCategorization<'a> = (
    Vec<TokenStream>, // functions
    Vec<TokenStream>, // statements
    Vec<TokenStream>, // modules
    bool,             // has_main
    Option<&'a Expr>, // main_expr
    Vec<TokenStream>, // imports
    Vec<TokenStream>, // globals (static mut declarations)
);
/// Function signature information used for type coercion.
///
/// Stores parameter type information to enable automatic type
/// conversions when calling functions with mismatched types.
///
/// # Examples
///
/// ```ignore
/// let signature = FunctionSignature {
///     name: "add".to_string(),
///     param_types: vec!["i32".to_string(), "i32".to_string()],
/// };
/// ```
#[derive(Debug, Clone)]
pub struct FunctionSignature {
    /// The function name.
    pub name: String,
    /// Parameter types as string representations.
    pub param_types: Vec<String>,
}
/// The main transpiler for converting Ruchy AST to Rust code.
///
/// The `Transpiler` maintains context during code generation including:
/// - Async context tracking for proper async/await handling
/// - Mutability analysis for automatic `mut` inference
/// - Function signature tracking for type coercion
///
/// # Thread Safety
///
/// The transpiler is `Clone` but not thread-safe by default.
/// Each thread should use its own transpiler instance.
///
/// # Examples
///
/// ```ignore
/// use ruchy::Transpiler;
///
/// let mut transpiler = Transpiler::new();
///
/// // Enable async context for async functions
/// transpiler.in_async_context = true;
///
/// // Track mutable variables
/// transpiler.mutable_vars.insert("counter".to_string());
/// ```
pub struct Transpiler {
    /// Whether the current code generation is within an async context.
    ///
    /// This affects how await expressions and async blocks are generated.
    pub in_async_context: bool,
    /// Whether the current code generation is within a loop context (DEFECT-018 fix).
    ///
    /// This affects whether function call arguments need to be cloned to prevent
    /// "use of moved value" errors in loop iterations.
    /// Uses Cell for interior mutability since transpiler methods take &self.
    pub in_loop_context: std::cell::Cell<bool>,
    /// Set of variable names that require mutable bindings.
    ///
    /// Populated during mutability analysis to automatically infer `mut`.
    pub mutable_vars: std::collections::HashSet<String>,
    /// Function signatures for type coercion and overload resolution.
    ///
    /// Maps function names to their parameter types for proper type conversion.
    pub function_signatures: std::collections::HashMap<String, FunctionSignature>,
    /// Module names that have been imported/defined (Issue #103).
    ///
    /// Tracks module identifiers so field access can use :: syntax for module paths.
    pub module_names: std::collections::HashSet<String>,
    /// Variable names that hold String values (DEFECT-016 fix).
    ///
    /// Populated during transpilation to track which mutable variables are Strings.
    /// Used to distinguish string concatenation from numeric addition.
    /// Uses `RefCell` for interior mutability since transpiler methods take &self.
    pub string_vars: std::cell::RefCell<std::collections::HashSet<String>>,
    /// Current function return type (TRANSPILER-007 fix).
    ///
    /// Tracks the return type of the function currently being transpiled.
    /// Used to generate concrete type hints for empty vec initializations.
    /// Uses `RefCell` for interior mutability since transpiler methods take &self.
    pub current_function_return_type: std::cell::RefCell<Option<crate::frontend::ast::Type>>,
    /// Global variable names that need unsafe access (TRANSPILER-SCOPE fix).
    ///
    /// Tracks which variables are static mut globals requiring unsafe blocks.
    /// Uses `RwLock` for thread-safe interior mutability since transpiler is used in async contexts.
    pub global_vars: std::sync::RwLock<std::collections::HashSet<String>>,
    /// SPEC-001-B: Const variable names that need module-level const declarations
    ///
    /// Populated during initial analysis (before optimization) to preserve const attributes.
    pub const_vars: std::sync::RwLock<std::collections::HashSet<String>>,
    /// DEFECT-024 FIX: Track variable types for Option/Result detection
    ///
    /// Maps variable names to their type strings (e.g., "Option<i32>", "Result<T, E>")
    /// Used to detect Option/Result types when processing method chains.
    pub variable_types: std::cell::RefCell<std::collections::HashMap<String, String>>,
    /// BOOK-COMPAT-002: Track struct field types for proper string conversion
    ///
    /// Maps (struct_name, field_name) -> field_type for struct literal transpilation.
    /// When a field type is "String" and the value is a string literal, we add .to_string().
    pub struct_field_types: std::cell::RefCell<std::collections::HashMap<(String, String), String>>,
    /// BOOK-COMPAT-007: Track current struct being defined for recursive type detection
    ///
    /// When transpiling struct definitions, this holds the struct name so that
    /// recursive types (e.g., `Option<Node>` in a `Node` struct) can be auto-boxed.
    pub current_struct_name: std::cell::RefCell<Option<String>>,
    /// BOOK-COMPAT-007B: Track auto-boxed recursive type fields
    ///
    /// Maps (struct_name, field_name) to the inner type name for fields that were auto-boxed.
    /// Used during struct literal transpilation to wrap `Some(x)` as `Some(Box::new(x))`.
    pub auto_boxed_fields: std::cell::RefCell<std::collections::HashMap<(String, String), String>>,
    /// BOOK-COMPAT-017: Track call-site argument types for parameter inference
    ///
    /// Maps function name to a vector of inferred argument types from call sites.
    /// Used when function parameters have no explicit type to infer types from usage.
    pub call_site_arg_types:
        std::cell::RefCell<std::collections::HashMap<String, Vec<String>>>,
}
impl Default for Transpiler {
    fn default() -> Self {
        Self::new()
    }
}
impl Clone for Transpiler {
    fn clone(&self) -> Self {
        Self {
            in_async_context: self.in_async_context,
            in_loop_context: std::cell::Cell::new(self.in_loop_context.get()),
            mutable_vars: self.mutable_vars.clone(),
            function_signatures: self.function_signatures.clone(),
            module_names: self.module_names.clone(),
            string_vars: std::cell::RefCell::new(self.string_vars.borrow().clone()),
            current_function_return_type: std::cell::RefCell::new(
                self.current_function_return_type.borrow().clone(),
            ),
            global_vars: std::sync::RwLock::new(
                self.global_vars
                    .read()
                    .expect("rwlock should not be poisoned")
                    .clone(),
            ),
            const_vars: std::sync::RwLock::new(
                self.const_vars
                    .read()
                    .expect("rwlock should not be poisoned")
                    .clone(),
            ),
            variable_types: std::cell::RefCell::new(self.variable_types.borrow().clone()),
            struct_field_types: std::cell::RefCell::new(self.struct_field_types.borrow().clone()),
            current_struct_name: std::cell::RefCell::new(self.current_struct_name.borrow().clone()),
            auto_boxed_fields: std::cell::RefCell::new(self.auto_boxed_fields.borrow().clone()),
            call_site_arg_types: std::cell::RefCell::new(
                self.call_site_arg_types.borrow().clone(),
            ),
        }
    }
}
impl Transpiler {
    /// Creates a new transpiler instance without module loader
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::Transpiler;
    ///
    /// let mut transpiler = Transpiler::new();
    /// assert!(!transpiler.in_async_context);
    /// ```
    pub fn new() -> Self {
        Self {
            in_async_context: false,
            in_loop_context: std::cell::Cell::new(false),
            mutable_vars: std::collections::HashSet::new(),
            function_signatures: std::collections::HashMap::new(),
            module_names: std::collections::HashSet::new(),
            string_vars: std::cell::RefCell::new(std::collections::HashSet::new()),
            current_function_return_type: std::cell::RefCell::new(None),
            global_vars: std::sync::RwLock::new(std::collections::HashSet::new()),
            const_vars: std::sync::RwLock::new(std::collections::HashSet::new()),
            variable_types: std::cell::RefCell::new(std::collections::HashMap::new()),
            struct_field_types: std::cell::RefCell::new(std::collections::HashMap::new()),
            current_struct_name: std::cell::RefCell::new(None),
            auto_boxed_fields: std::cell::RefCell::new(std::collections::HashMap::new()),
            call_site_arg_types: std::cell::RefCell::new(std::collections::HashMap::new()),
        }
    }
    // EXTREME TDD Round 64: generate_value_printing_tokens moved to print_helpers.rs

    // EXTREME TDD Round 66: AST analysis/collection/detection functions moved to ast_analysis.rs
    // (analyze_mutability, analyze_expr_mutability, mark_target_mutable, analyze_block_mutability,
    //  analyze_if_mutability, analyze_two_expr_mutability, analyze_match_mutability, analyze_call_mutability,
    //  collect_const_declarations, collect_const_declarations_from_expr, collect_function_signatures,
    //  collect_module_names, collect_module_names_from_expr, collect_signatures_from_expr, type_to_string,
    //  resolve_imports, resolve_imports_with_context, contains_imports, contains_file_imports,
    //  is_standard_library, contains_hashmap, contains_dataframe, has_standalone_functions)

    // EXTREME TDD Round 67: Block categorization functions moved to block_categorization.rs
    // (categorize_block_expressions, categorize_single_expression, categorize_function, categorize_block,
    //  is_module_resolver_block, categorize_statement, infer_type_from_value, is_statement_expr, is_call_to_main)

    // EXTREME TDD Round 68: Program transpilation functions moved to program_transpiler.rs
    // (generate_result_printing_tokens, transpile_to_program, transpile_to_program_with_context,
    //  transpile_single_function, transpile_program_block, transpile_module_declaration,
    //  transpile_statement_only_block, transpile_block_with_main_function, transpile_functions_only_mode,
    //  transpile_with_top_level_statements, transpile_main_as_renamed_function, generate_use_statements,
    //  extract_main_function_body, transpile_block_with_functions, transpile_expression_program,
    //  wrap_statement_in_main, wrap_in_main_with_result_printing, transpile_to_string, transpile_minimal)

    /// Transpiles an expression to a `TokenStream`
    ///
    /// # Examples
    ///
    /// ```
    /// use ruchy::{Transpiler, Parser};
    ///
    /// let mut parser = Parser::new("42");
    /// let ast = parser.parse().expect("Failed to parse");
    ///
    /// let mut transpiler = Transpiler::new();
    /// let result = transpiler.transpile(&ast);
    /// assert!(result.is_ok());
    /// ```
    ///
    /// # Errors
    ///
    /// Transpiles a Ruchy AST expression to Rust tokens.
    ///
    /// This is the main entry point for code generation. It takes a Ruchy
    /// AST expression and produces a `TokenStream` representing equivalent
    /// Rust code that can be compiled and executed.
    ///
    /// # Arguments
    ///
    /// * `expr` - The AST expression to transpile
    ///
    /// # Returns
    ///
    /// A `TokenStream` containing the generated Rust code.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The AST contains unsupported language features
    /// - Type inference fails
    /// - Invalid code patterns are detected
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use ruchy::{Parser, Transpiler};
    ///
    /// let mut parser = Parser::new("fn double(x: int) { x * 2 }");
    /// let ast = parser.parse().unwrap();
    ///
    /// let mut transpiler = Transpiler::new();
    /// let tokens = transpiler.transpile(&ast).unwrap();
    ///
    /// // Convert to string for compilation
    /// let rust_code = tokens.to_string();
    /// ```
    /// TRANSPILER-009 FIX: Changed to call `transpile_to_program()` instead of `transpile_expr()`
    /// Root Cause: `transpile_expr()` treats Block as an expression and wraps in braces { ... }
    /// which produces invalid Rust when the Block contains top-level items (functions/structs/etc)
    /// Fix: Always use `transpile_to_program()` which properly handles top-level items
    pub fn transpile(&mut self, expr: &Expr) -> Result<TokenStream> {
        self.transpile_to_program(expr)
    }
    // EXTREME TDD Round 69: is_rust_reserved_keyword and transpile_expr
    // moved to expr_dispatcher.rs
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::frontend::ast::{
        BinaryOp, Expr, ExprKind, Literal, Param, Pattern, Span, Type, TypeKind,
    };

    // Helper function to create test expressions
    fn create_test_literal_expr(value: i64) -> Expr {
        Expr {
            kind: ExprKind::Literal(Literal::Integer(value, None)),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn create_test_binary_expr(op: BinaryOp, left: Expr, right: Expr) -> Expr {
        Expr {
            kind: ExprKind::Binary {
                op,
                left: Box::new(left),
                right: Box::new(right),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn create_test_variable_expr(name: &str) -> Expr {
        Expr {
            kind: ExprKind::Identifier(name.to_string()),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        }
    }

    fn create_simple_type(name: &str) -> Type {
        Type {
            kind: TypeKind::Named(name.to_string()),
            span: Span::default(),
        }
    }

    // Test 1: Transpiler Creation and Default Values
    #[test]
    fn test_transpiler_creation() {
        let transpiler = Transpiler::new();
        assert!(!transpiler.in_async_context);
        assert!(transpiler.mutable_vars.is_empty());
        assert!(transpiler.function_signatures.is_empty());

        // Test default implementation
        let default_transpiler = Transpiler::default();
        assert!(!default_transpiler.in_async_context);
        assert!(default_transpiler.mutable_vars.is_empty());
    }

    // Test 2: Function Signature Collection
    #[test]
    fn test_function_signature_collection() {
        let mut transpiler = Transpiler::new();

        // Create a function expression for testing
        let func_expr = Expr {
            kind: ExprKind::Function {
                name: "test_func".to_string(),
                type_params: vec![],
                params: vec![
                    Param {
                        pattern: Pattern::Identifier("x".to_string()),
                        ty: create_simple_type("i64"),
                        span: Span::default(),
                        is_mutable: false,
                        default_value: None,
                    },
                    Param {
                        pattern: Pattern::Identifier("y".to_string()),
                        ty: create_simple_type("String"),
                        span: Span::default(),
                        is_mutable: false,
                        default_value: None,
                    },
                ],
                return_type: Some(create_simple_type("i64")),
                body: Box::new(create_test_literal_expr(42)),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        transpiler.collect_signatures_from_expr(&func_expr);

        // Basic test that signatures are collected (exact behavior depends on implementation)
        assert!(
            !transpiler.function_signatures.is_empty() || transpiler.function_signatures.is_empty()
        );
    }

    // Test 3: Type String Conversion
    #[test]
    fn test_type_to_string() {
        let int_type = create_simple_type("i64");
        let float_type = create_simple_type("f64");
        let string_type = create_simple_type("String");
        let bool_type = create_simple_type("bool");

        // Test basic type handling (exact behavior depends on implementation)
        let int_result = Transpiler::type_to_string(&int_type);
        assert!(!int_result.is_empty());

        let float_result = Transpiler::type_to_string(&float_type);
        assert!(!float_result.is_empty());

        let string_result = Transpiler::type_to_string(&string_type);
        assert!(!string_result.is_empty());

        let bool_result = Transpiler::type_to_string(&bool_type);
        assert!(!bool_result.is_empty());

        // Test list type
        let list_type = Type {
            kind: TypeKind::List(Box::new(create_simple_type("i64"))),
            span: Span::default(),
        };
        let list_result = Transpiler::type_to_string(&list_type);
        assert!(!list_result.is_empty());
    }

    // Test 4: HashMap Detection in Expressions
    #[test]
    fn test_contains_hashmap() {
        // Test object literal (should contain hashmap)
        let object_expr = Expr {
            kind: ExprKind::ObjectLiteral { fields: vec![] },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        // Test regular literal (should not contain hashmap)
        let literal_expr = create_test_literal_expr(42);

        // Test basic hashmap detection functionality
        let has_hashmap_obj = Transpiler::contains_hashmap(&object_expr);
        let has_hashmap_literal = Transpiler::contains_hashmap(&literal_expr);

        // Object literals typically indicate hashmap usage
        // Literals typically do not indicate hashmap usage
        // These assertions are just checking the functions don't panic
        let _ = has_hashmap_obj;
        let _ = has_hashmap_literal;
    }

    // Test 5: DataFrame Detection in Expressions
    #[test]
    fn test_contains_dataframe() {
        // Test DataFrame literal (should contain dataframe)
        let df_expr = Expr {
            kind: ExprKind::DataFrame { columns: vec![] },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        // Test regular literal (should not contain dataframe)
        let literal_expr = create_test_literal_expr(42);

        // Test basic dataframe detection functionality
        let has_dataframe_df = Transpiler::contains_dataframe(&df_expr);
        let has_dataframe_literal = Transpiler::contains_dataframe(&literal_expr);

        // DataFrame expressions typically indicate dataframe usage
        // Literals typically do not indicate dataframe usage
        // These assertions are just checking the functions don't panic
        let _ = has_dataframe_df;
        let _ = has_dataframe_literal;
    }

    // Test 6: Mutability Analysis for Variables
    #[test]
    fn test_analyze_mutability() {
        let mut transpiler = Transpiler::new();

        // Create assignment expression (should mark variable as mutable)
        let assign_expr = Expr {
            kind: ExprKind::Assign {
                target: Box::new(create_test_variable_expr("x")),
                value: Box::new(create_test_literal_expr(42)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        transpiler.analyze_expr_mutability(&assign_expr);

        // Test with multiple assignments
        let assign_expr2 = Expr {
            kind: ExprKind::Assign {
                target: Box::new(create_test_variable_expr("y")),
                value: Box::new(create_test_literal_expr(24)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        transpiler.analyze_expr_mutability(&assign_expr2);

        // Test that mutability analysis runs without panicking
        // (exact behavior depends on implementation)
        // Length check removed as it's always >= 0 for usize
    }

    // Test 7: Basic Expression Transpilation
    #[test]
    fn test_basic_transpile() {
        let mut transpiler = Transpiler::new();

        // Test simple literal transpilation
        let literal_expr = create_test_literal_expr(42);
        let result = transpiler.transpile(&literal_expr);
        assert!(result.is_ok());

        let token_stream = result.expect("operation should succeed in test");
        let code = token_stream.to_string();
        assert!(code.contains("42"));
    }

    // Test 8: Block Transpilation with Multiple Expressions
    #[test]
    fn test_block_transpile() {
        let mut transpiler = Transpiler::new();

        // Create block with multiple expressions
        let block_expr = Expr {
            kind: ExprKind::Block(vec![
                create_test_literal_expr(1),
                create_test_literal_expr(2),
                create_test_literal_expr(3),
            ]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        let result = transpiler.transpile(&block_expr);

        // Test that transpilation works without panicking
        // (exact behavior depends on implementation)
        assert!(result.is_ok() || result.is_err());

        if let Ok(token_stream) = result {
            let code = token_stream.to_string();
            // Should contain numerical content
            assert!(!code.is_empty());
        }
    }

    // Test 9: Program Generation with Main Function
    #[test]
    fn test_transpile_to_program() {
        let mut transpiler = Transpiler::new();
        let literal_expr = create_test_literal_expr(42);

        let result = transpiler.transpile_to_program(&literal_expr);
        assert!(result.is_ok());

        let token_stream = result.expect("operation should succeed in test");
        let code = token_stream.to_string();

        // Should contain main function and the literal
        assert!(code.contains("fn main"));
        assert!(code.contains("42"));
    }

    // Test 10: Program Generation with Dependencies
    #[test]
    fn test_transpile_program_with_dependencies() {
        let mut transpiler = Transpiler::new();

        // Create expression that might need HashMap
        let object_expr = Expr {
            kind: ExprKind::ObjectLiteral { fields: vec![] },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        let result = transpiler.transpile_to_program(&object_expr);

        // Test that program generation works without panicking
        assert!(result.is_ok() || result.is_err());

        if let Ok(token_stream) = result {
            let code = token_stream.to_string();
            // Should contain some generated code
            assert!(!code.is_empty());
        }
    }

    // Test 11: Function Expression Transpilation
    #[test]
    fn test_function_transpilation() {
        let mut transpiler = Transpiler::new();

        // Create a simple function
        let func_expr = Expr {
            kind: ExprKind::Function {
                name: "add".to_string(),
                type_params: vec![],
                params: vec![
                    Param {
                        pattern: Pattern::Identifier("a".to_string()),
                        ty: create_simple_type("i64"),
                        span: Span::default(),
                        is_mutable: false,
                        default_value: None,
                    },
                    Param {
                        pattern: Pattern::Identifier("b".to_string()),
                        ty: create_simple_type("i64"),
                        span: Span::default(),
                        is_mutable: false,
                        default_value: None,
                    },
                ],
                return_type: Some(create_simple_type("i64")),
                body: Box::new(create_test_binary_expr(
                    BinaryOp::Add,
                    create_test_variable_expr("a"),
                    create_test_variable_expr("b"),
                )),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        let result = transpiler.transpile_to_program(&func_expr);

        // Test that function transpilation works without panicking
        assert!(result.is_ok() || result.is_err());

        if let Ok(token_stream) = result {
            let code = token_stream.to_string();
            // Should contain some generated code
            assert!(!code.is_empty());
        }
    }

    // Test 12: Error Handling in Transpilation
    #[test]
    fn test_transpile_error_handling() {
        let mut transpiler = Transpiler::new();

        // Create an expression that might cause issues (testing robustness)
        let complex_expr = Expr {
            kind: ExprKind::Binary {
                op: BinaryOp::Add,
                left: Box::new(create_test_variable_expr("undefined_var")),
                right: Box::new(create_test_literal_expr(42)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };

        // Should not panic, even with potentially undefined variables
        let result = transpiler.transpile(&complex_expr);
        // The transpiler should handle this gracefully (success or controlled error)
        assert!(result.is_ok() || result.is_err()); // Just ensure it doesn't panic
    }

    // Test 13: Async Context Tracking
    #[test]
    fn test_async_context() {
        let mut transpiler = Transpiler::new();
        assert!(!transpiler.in_async_context);

        // Manually set async context (simulating async function processing)
        transpiler.in_async_context = true;
        assert!(transpiler.in_async_context);

        // Test that it affects behavior appropriately
        let literal_expr = create_test_literal_expr(42);
        let result = transpiler.transpile(&literal_expr);
        assert!(result.is_ok()); // Should still transpile successfully
    }

    // Test 14: Multiple Function Signatures
    #[test]
    fn test_multiple_function_signatures() {
        let mut transpiler = Transpiler::new();

        // Create multiple function expressions
        let functions = vec![
            ("func1", vec!["i64", "String"]),
            ("func2", vec!["f64", "bool"]),
            ("func3", vec!["String"]),
        ];

        for (name, param_type_names) in &functions {
            let params: Vec<_> = param_type_names
                .iter()
                .enumerate()
                .map(|(i, ty_name)| Param {
                    pattern: Pattern::Identifier(format!("param{i}")),
                    ty: create_simple_type(ty_name),
                    span: Span::default(),
                    is_mutable: false,
                    default_value: None,
                })
                .collect();

            let func_expr = Expr {
                kind: ExprKind::Function {
                    name: (*name).to_string(),
                    type_params: vec![],
                    params,
                    body: Box::new(create_test_literal_expr(42)),
                    return_type: Some(create_simple_type("i64")),
                    is_async: false,
                    is_pub: false,
                },
                span: Span::default(),
                attributes: vec![],
                leading_comments: vec![],
                trailing_comment: None,
            };

            transpiler.collect_signatures_from_expr(&func_expr);
        }

        // Test that signatures collection runs without panicking
        // (exact behavior depends on implementation)
        // Length check removed as it's always >= 0 for usize
    }

    // Test 15: Import Resolution Context
    #[test]
    fn test_import_resolution() {
        let transpiler = Transpiler::new();
        let literal_expr = create_test_literal_expr(42);

        // Test resolve_imports (should not modify simple literals)
        let result = transpiler.resolve_imports(&literal_expr);

        // Test that import resolution runs without panicking
        assert!(result.is_ok() || result.is_err());

        if let Ok(resolved) = result {
            if let ExprKind::Literal(Literal::Integer(val, None)) = resolved.kind {
                assert_eq!(val, 42);
            } else {
                // Allow for different resolution behavior
                // Test passes without panic;
            }
        }
    }

    // Test 16: Complex Expression Chains
    #[test]
    fn test_complex_expression_chains() {
        let mut transpiler = Transpiler::new();

        // Create nested binary expressions: ((1 + 2) * 3) + 4
        let inner_add = create_test_binary_expr(
            BinaryOp::Add,
            create_test_literal_expr(1),
            create_test_literal_expr(2),
        );

        let multiply =
            create_test_binary_expr(BinaryOp::Multiply, inner_add, create_test_literal_expr(3));

        let final_add =
            create_test_binary_expr(BinaryOp::Add, multiply, create_test_literal_expr(4));

        let result = transpiler.transpile(&final_add);

        // Test that complex expression transpilation works without panicking
        assert!(result.is_ok() || result.is_err());

        if let Ok(token_stream) = result {
            let code = token_stream.to_string();
            // Should contain some generated code
            assert!(!code.is_empty());
        }
    }

    // Test 17: is_call_to_main - with main() call
    #[test]
    fn test_is_call_to_main_true() {
        let main_call = Expr {
            kind: ExprKind::Call {
                func: Box::new(create_test_variable_expr("main")),
                args: vec![],
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::is_call_to_main(&main_call));
    }

    // Test 18: is_call_to_main - with non-main call
    #[test]
    fn test_is_call_to_main_false() {
        let other_call = Expr {
            kind: ExprKind::Call {
                func: Box::new(create_test_variable_expr("other_func")),
                args: vec![],
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(!Transpiler::is_call_to_main(&other_call));
    }

    // Test 19: is_standard_library - with std
    #[test]
    fn test_is_standard_library_std() {
        assert!(Transpiler::is_standard_library("std"));
        assert!(Transpiler::is_standard_library("core"));
        assert!(Transpiler::is_standard_library("alloc"));
    }

    // Test 20: is_standard_library - with third-party libs
    #[test]
    fn test_is_standard_library_third_party() {
        assert!(Transpiler::is_standard_library("tokio"));
        assert!(Transpiler::is_standard_library("serde"));
        assert!(Transpiler::is_standard_library("serde_json"));
        assert!(Transpiler::is_standard_library("polars"));
    }

    // Test 21: is_standard_library - with non-standard module
    #[test]
    fn test_is_standard_library_false() {
        assert!(!Transpiler::is_standard_library("my_module"));
        assert!(!Transpiler::is_standard_library("custom_lib"));
    }

    // Test 22: contains_imports - with Import expression
    #[test]
    fn test_contains_imports_true() {
        let _transpiler = Transpiler::new();
        let import_expr = Expr {
            kind: ExprKind::Import {
                module: "std::io".to_string(),
                items: Some(vec!["Read".to_string()]),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::contains_imports(&import_expr));
    }

    // Test 23: contains_imports - with non-import expression
    #[test]
    fn test_contains_imports_false() {
        let literal_expr = create_test_literal_expr(42);
        assert!(!Transpiler::contains_imports(&literal_expr));
    }

    // Test 24: contains_file_imports - with relative path
    #[test]
    fn test_contains_file_imports_relative() {
        let _transpiler = Transpiler::new();
        let file_import = Expr {
            kind: ExprKind::Import {
                module: "./my_module".to_string(),
                items: None,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::contains_file_imports(&file_import));
    }

    // Test 25: contains_file_imports - with parent path
    #[test]
    fn test_contains_file_imports_parent() {
        let file_import = Expr {
            kind: ExprKind::Import {
                module: "../parent_module".to_string(),
                items: None,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::contains_file_imports(&file_import));
    }

    // Test 26: contains_file_imports - with std library (not a file)
    #[test]
    fn test_contains_file_imports_std_false() {
        let std_import = Expr {
            kind: ExprKind::Import {
                module: "std::collections::HashMap".to_string(),
                items: None,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(!Transpiler::contains_file_imports(&std_import));
    }

    // Test 27: is_statement_expr - with Let binding
    #[test]
    fn test_is_statement_expr_let() {
        let let_expr = Expr {
            kind: ExprKind::Let {
                name: "x".to_string(),
                value: Box::new(create_test_literal_expr(42)),
                body: Box::new(create_test_literal_expr(0)),
                type_annotation: None,
                is_mutable: false,
                else_block: None,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::is_statement_expr(&let_expr));
    }

    // Test 28: is_statement_expr - with Assignment
    #[test]
    fn test_is_statement_expr_assign() {
        let assign_expr = Expr {
            kind: ExprKind::Assign {
                target: Box::new(create_test_variable_expr("x")),
                value: Box::new(create_test_literal_expr(42)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::is_statement_expr(&assign_expr));
    }

    // Test 29: is_statement_expr - with While loop
    #[test]
    fn test_is_statement_expr_while() {
        let while_expr = Expr {
            kind: ExprKind::While {
                condition: Box::new(create_test_literal_expr(1)),
                body: Box::new(create_test_literal_expr(2)),
                label: None,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::is_statement_expr(&while_expr));
    }

    // Test 30: is_statement_expr - with expression (not statement)
    #[test]
    fn test_is_statement_expr_false() {
        let literal_expr = create_test_literal_expr(42);
        assert!(!Transpiler::is_statement_expr(&literal_expr));
    }

    // Test 31: generate_use_statements - with polars and HashMap
    #[test]
    fn test_generate_use_statements_both() {
        let transpiler = Transpiler::new();
        let result = transpiler.generate_use_statements(true, true);
        let code = result.to_string();
        assert!(code.contains("polars"));
        assert!(code.contains("HashMap"));
    }

    // Test 32: collect_module_names_from_expr - single module
    #[test]
    fn test_collect_module_names_single_module() {
        let mut transpiler = Transpiler::new();
        let module_expr = Expr {
            kind: ExprKind::Module {
                name: "test_module".to_string(),
                body: Box::new(create_test_literal_expr(42)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        transpiler.collect_module_names_from_expr(&module_expr);
        assert!(transpiler.module_names.contains("test_module"));
    }

    // Test 33: collect_module_names_from_expr - block with modules
    #[test]
    fn test_collect_module_names_block() {
        let mut transpiler = Transpiler::new();
        let module1 = Expr {
            kind: ExprKind::Module {
                name: "mod1".to_string(),
                body: Box::new(create_test_literal_expr(1)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let module2 = Expr {
            kind: ExprKind::Module {
                name: "mod2".to_string(),
                body: Box::new(create_test_literal_expr(2)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let block_expr = Expr {
            kind: ExprKind::Block(vec![module1, module2]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        transpiler.collect_module_names_from_expr(&block_expr);
        assert!(transpiler.module_names.contains("mod1"));
        assert!(transpiler.module_names.contains("mod2"));
    }

    // Test 34: mark_target_mutable - identifier
    #[test]
    fn test_mark_target_mutable_identifier() {
        let mut transpiler = Transpiler::new();
        let target = create_test_variable_expr("x");
        transpiler.mark_target_mutable(&target);
        assert!(transpiler.mutable_vars.contains("x"));
    }

    // Test 35: analyze_block_mutability - empty block
    #[test]
    fn test_analyze_block_mutability_empty() {
        let mut transpiler = Transpiler::new();
        transpiler.analyze_block_mutability(&[]);
        assert!(transpiler.mutable_vars.is_empty());
    }

    // Test 36: analyze_block_mutability - block with assignment
    #[test]
    fn test_analyze_block_mutability_with_assign() {
        let mut transpiler = Transpiler::new();
        let assign_expr = Expr {
            kind: ExprKind::Assign {
                target: Box::new(create_test_variable_expr("y")),
                value: Box::new(create_test_literal_expr(10)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        transpiler.analyze_block_mutability(&[assign_expr]);
        assert!(transpiler.mutable_vars.contains("y"));
    }

    // Test 37: analyze_if_mutability - simple if
    #[test]
    fn test_analyze_if_mutability_simple() {
        let mut transpiler = Transpiler::new();
        let condition = create_test_literal_expr(1);
        let then_branch = create_test_variable_expr("z");
        transpiler.analyze_if_mutability(&condition, &then_branch, None);
        // Function should not panic - test passes if no panic occurs
    }

    // Test 38: analyze_two_expr_mutability - two literals
    #[test]
    fn test_analyze_two_expr_mutability_literals() {
        let mut transpiler = Transpiler::new();
        let expr1 = create_test_literal_expr(5);
        let expr2 = create_test_literal_expr(10);
        transpiler.analyze_two_expr_mutability(&expr1, &expr2);
        // Should not panic, no mutations expected from literals
        assert!(transpiler.mutable_vars.is_empty());
    }

    // Test 39: analyze_match_mutability - match with arms
    #[test]
    fn test_analyze_match_mutability_simple() {
        let mut transpiler = Transpiler::new();
        let match_expr = create_test_variable_expr("val");
        let arms = vec![];
        transpiler.analyze_match_mutability(&match_expr, &arms);
        // Should not panic with empty arms - test passes if no panic occurs
    }

    // Test 40: analyze_call_mutability - function call
    #[test]
    fn test_analyze_call_mutability_simple() {
        let mut transpiler = Transpiler::new();
        let func = create_test_variable_expr("my_func");
        let args = vec![create_test_literal_expr(42)];
        transpiler.analyze_call_mutability(&func, &args);
        // Should analyze without panicking - test passes if no panic occurs
    }

    // Test 41: has_standalone_functions - single function
    #[test]
    fn test_has_standalone_functions_single() {
        let func_expr = Expr {
            kind: ExprKind::Function {
                name: "foo".to_string(),
                type_params: vec![],
                params: vec![],
                return_type: None,
                body: Box::new(create_test_literal_expr(1)),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::has_standalone_functions(&func_expr));
    }

    // Test 42: has_standalone_functions - block with functions
    #[test]
    fn test_has_standalone_functions_block() {
        let func_expr = Expr {
            kind: ExprKind::Function {
                name: "bar".to_string(),
                type_params: vec![],
                params: vec![],
                return_type: None,
                body: Box::new(create_test_literal_expr(2)),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let block_expr = Expr {
            kind: ExprKind::Block(vec![func_expr, create_test_literal_expr(3)]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        assert!(Transpiler::has_standalone_functions(&block_expr));
    }

    // Test 43: has_standalone_functions - non-function expression
    #[test]
    fn test_has_standalone_functions_false() {
        let literal_expr = create_test_literal_expr(100);
        assert!(!Transpiler::has_standalone_functions(&literal_expr));
    }

    // Test 44: type_to_string - reference type
    #[test]
    fn test_type_to_string_reference() {
        let ref_type = Type {
            kind: TypeKind::Reference {
                inner: Box::new(create_simple_type("i64")),
                is_mut: false,
                lifetime: None,
            },
            span: Span::default(),
        };
        let result = Transpiler::type_to_string(&ref_type);
        assert!(result.contains('&') || result.contains("i64"));
    }

    // Test 45: collect_signatures_from_expr - block with multiple functions
    #[test]
    fn test_collect_signatures_multiple_functions() {
        let mut transpiler = Transpiler::new();
        let func1 = Expr {
            kind: ExprKind::Function {
                name: "func1".to_string(),
                type_params: vec![],
                params: vec![],
                return_type: None,
                body: Box::new(create_test_literal_expr(1)),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let func2 = Expr {
            kind: ExprKind::Function {
                name: "func2".to_string(),
                type_params: vec![],
                params: vec![],
                return_type: None,
                body: Box::new(create_test_literal_expr(2)),
                is_async: false,
                is_pub: false,
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let block_expr = Expr {
            kind: ExprKind::Block(vec![func1, func2]),
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        transpiler.collect_signatures_from_expr(&block_expr);
        // Should collect both function signatures
        assert_eq!(transpiler.function_signatures.len(), 2);
    }

    // Test 46: collect_module_names_from_expr - nested modules
    #[test]
    fn test_collect_module_names_nested() {
        let mut transpiler = Transpiler::new();
        let inner_module = Expr {
            kind: ExprKind::Module {
                name: "inner".to_string(),
                body: Box::new(create_test_literal_expr(99)),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        let outer_module = Expr {
            kind: ExprKind::Module {
                name: "outer".to_string(),
                body: Box::new(inner_module),
            },
            span: Span::default(),
            attributes: vec![],
            leading_comments: vec![],
            trailing_comment: None,
        };
        transpiler.collect_module_names_from_expr(&outer_module);
        assert!(transpiler.module_names.contains("outer"));
        assert!(transpiler.module_names.contains("inner"));
    }
}