preprocessor-derive 0.1.2

Compile-time computation macro library — analyzes computable sub-expressions in code and evaluates parts that can be executed at compile time
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
//! Dynamic execution engine powered by cargo subprocess.
//!
//! Creates a temporary cargo project, injects the code, compiles and runs it,
//! then captures the output. This allows arbitrary code with external crates (like chrono)
//! to be evaluated at compile time.
//!
//! ## Architecture
//!
//! 1. **Dependency Detection**: Scans code for external crate paths
//! 2. **Dynamic Injection**: Auto-injects dependencies into Cargo.toml
//! 3. **Execution**: Creates temp project, runs `cargo run`, captures stdout
//! 4. **Result Parsing**: Parses text output back into typed `Value`s

use std::path::PathBuf;
use std::process::Command;
use syn::Expr;

use crate::evaluator::{EvalResult, Value};

/// Standard library crates that don't need dependency injection.
const STD_CRATES: &[&str] = &["std", "core", "alloc", "proc_macro"];

/// Common module/type names that should not be treated as crate names.
const COMMON_NAMES: &[&str] = &[
    // chrono types
    "Local",
    "Utc",
    "DateTime",
    "NaiveDateTime",
    "Duration",
    "TimeZone",
    // Result/Option variants
    "Some",
    "None",
    "Ok",
    "Err",
    // Common types
    "Vec",
    "String",
    "Box",
    "Option",
    "Result",
    // Collection types
    "HashMap",
    "HashSet",
    "BTreeMap",
    "BTreeSet",
    "LinkedList",
    "VecDeque",
    "BinaryHeap",
    // Hash types
    "DefaultHasher",
    "RandomState",
    // Path types
    "PathBuf",
    "Path",
    // Network types
    "Ipv4Addr",
    "Ipv6Addr",
    "SocketAddr",
    // Process types
    "Command",
    "Child",
    "Output",
    // Filesystem types
    "File",
    "OpenOptions",
    "Metadata",
    "DirBuilder",
    "ReadDir",
    "DirEntry",
    "FileType",
    "Permissions",
    // Time/Thread types
    "SystemTime",
    "Instant",
    "Thread",
    "JoinHandle",
    "Builder",
    "LocalKey",
    // std module names
    "mem",
    "size_of",
    "size_of_val",
    "align_of",
    "env",
    "net",
    "hash",
    "collections",
    "process",
    "path",
    "time",
    "fs",
    "io",
    "str",
    "slice",
    "array",
    "cmp",
    "fmt",
    "default",
    "clone",
    "iter",
    "ops",
];

/// Extracts external crate names from code by scanning:
/// 1. `use crate_name::...` statements
/// 2. `crate_name::...` path expressions
fn detect_external_crates(code: &str) -> Vec<String> {
    let mut crates = Vec::new();

    // Pattern 1: `use crate_name::` or `use ::crate_name::`
    let use_re = regex::Regex::new(r"use\s+(?:::)?([a-zA-Z_][a-zA-Z0-9_]*)::").unwrap();
    for cap in use_re.captures_iter(code) {
        if let Some(m) = cap.get(1) {
            let name = m.as_str();
            if !STD_CRATES.contains(&name) && !crates.contains(&name.to_string()) {
                crates.push(name.to_string());
            }
        }
    }

    // Pattern 2: `crate_name::` in paths (with optional spaces around ::)
    let path_re = regex::Regex::new(r"([a-zA-Z_][a-zA-Z0-9_]*)\s*::").unwrap();
    for cap in path_re.captures_iter(code) {
        if let Some(m) = cap.get(1) {
            let name = m.as_str();
            // 检查匹配前是否有 std::, core::, alloc:: 等前缀
            let start = m.start();
            if start > 0 {
                let before = &code[..start];
                if before.ends_with("std::")
                    || before.ends_with("core::")
                    || before.ends_with("alloc::")
                {
                    continue;
                }
            }
            if !STD_CRATES.contains(&name)
                && !COMMON_NAMES.contains(&name)
                && !crates.contains(&name.to_string())
            {
                crates.push(name.to_string());
            }
        }
    }

    crates
}

/// 根据代码中使用的类型/模块,自动生成 std use 语句。
/// 这确保动态引擎生成的 main.rs 中包含必要的 std 导入。
fn generate_std_imports(code: &str) -> String {
    let mut imports = Vec::new();

    // 集合类型
    if code.contains("HashMap::") || code.contains("HashMap<") {
        imports.push("use std::collections::HashMap;".to_string());
    }
    if code.contains("HashSet::") || code.contains("HashSet<") {
        imports.push("use std::collections::HashSet;".to_string());
    }
    if code.contains("BTreeMap::") || code.contains("BTreeMap<") {
        imports.push("use std::collections::BTreeMap;".to_string());
    }
    if code.contains("BTreeSet::") || code.contains("BTreeSet<") {
        imports.push("use std::collections::BTreeSet;".to_string());
    }
    if code.contains("VecDeque::") || code.contains("VecDeque<") {
        imports.push("use std::collections::VecDeque;".to_string());
    }
    if code.contains("BinaryHeap::") || code.contains("BinaryHeap<") {
        imports.push("use std::collections::BinaryHeap;".to_string());
    }
    if code.contains("LinkedList::") || code.contains("LinkedList<") {
        imports.push("use std::collections::LinkedList;".to_string());
    }

    // Hash 相关
    if code.contains("DefaultHasher::") || code.contains("hash::") {
        imports.push("use std::hash::{Hash, Hasher, DefaultHasher};".to_string());
    }
    if code.contains("RandomState") {
        imports.push("use std::collections::hash_map::RandomState;".to_string());
    }

    // 路径相关
    if code.contains("PathBuf::") || code.contains("PathBuf") {
        imports.push("use std::path::PathBuf;".to_string());
    }
    if code.contains("Path::") || (code.contains("Path") && !code.contains("PathBuf")) {
        imports.push("use std::path::Path;".to_string());
    }

    // 网络相关
    if code.contains("Ipv4Addr::") || code.contains("Ipv4Addr") {
        imports.push("use std::net::Ipv4Addr;".to_string());
    }
    if code.contains("Ipv6Addr::") || code.contains("Ipv6Addr") {
        imports.push("use std::net::Ipv6Addr;".to_string());
    }
    if code.contains("SocketAddr::") || code.contains("SocketAddr") {
        imports.push("use std::net::SocketAddr;".to_string());
    }

    // 进程相关
    if code.contains("Command::") && code.contains("new()") {
        imports.push("use std::process::Command;".to_string());
    }
    if code.contains("Child") {
        imports.push("use std::process::Child;".to_string());
    }
    if code.contains("Output") {
        imports.push("use std::process::Output;".to_string());
    }

    // 文件系统相关
    if code.contains("File::") || (code.contains("File") && code.contains("open")) {
        imports.push("use std::fs::File;".to_string());
    }
    if code.contains("OpenOptions::") {
        imports.push("use std::fs::OpenOptions;".to_string());
    }
    if code.contains("Metadata") && code.contains("metadata") {
        imports.push("use std::fs::Metadata;".to_string());
    }
    if code.contains("DirBuilder::") {
        imports.push("use std::fs::DirBuilder;".to_string());
    }
    if code.contains("read_dir") || code.contains("ReadDir") {
        imports.push("use std::fs::{read_dir, ReadDir};".to_string());
    }
    if code.contains("DirEntry") {
        imports.push("use std::fs::DirEntry;".to_string());
    }
    if code.contains("FileType") {
        imports.push("use std::fs::FileType;".to_string());
    }
    if code.contains("Permissions") {
        imports.push("use std::fs::Permissions;".to_string());
    }

    // 时间/线程相关
    if code.contains("SystemTime::") || code.contains("SystemTime") {
        imports.push("use std::time::SystemTime;".to_string());
    }
    if code.contains("Instant::") || code.contains("Instant") {
        imports.push("use std::time::Instant;".to_string());
    }
    if code.contains("Duration::") || code.contains("Duration") {
        imports.push("use std::time::Duration;".to_string());
    }
    if code.contains("thread::") || code.contains("Thread") {
        imports.push("use std::thread;".to_string());
    }
    if code.contains("JoinHandle") {
        imports.push("use std::thread::JoinHandle;".to_string());
    }

    // mem 相关
    if code.contains("size_of::") || code.contains("size_of::<") {
        imports.push("use std::mem::size_of;".to_string());
    }
    if code.contains("size_of_val::") {
        imports.push("use std::mem::size_of_val;".to_string());
    }
    if code.contains("align_of::") {
        imports.push("use std::mem::align_of;".to_string());
    }

    // env 相关
    if code.contains("env::") {
        imports.push("use std::env;".to_string());
    }

    // cmp 相关
    if code.contains("cmp::") || code.contains("Ordering::") {
        imports.push("use std::cmp::Ordering;".to_string());
    }

    // default 相关
    if code.contains("Default::default()") && !code.contains("use std::default::Default;") {
        imports.push("use std::default::Default;".to_string());
    }

    // clone 相关
    if code.contains(".clone()") && !code.contains("use std::clone::Clone;") {
        imports.push("use std::clone::Clone;".to_string());
    }

    // iter 相关
    if code.contains(".iter()") || code.contains(".into_iter()") {
        imports.push("use std::iter::Iterator;".to_string());
    }

    // ops 相关
    if code.contains("ops::") || code.contains("Range::") {
        imports.push("use std::ops;".to_string());
    }

    // io 相关
    if code.contains("io::") || code.contains("Read") || code.contains("Write") {
        imports.push("use std::io;".to_string());
    }

    imports.join("\n")
}

/// Extracts free variable names from an expression AST.
fn extract_free_variables(expr: &Expr) -> Vec<String> {
    let mut vars = Vec::new();
    collect_vars(expr, &mut vars);
    vars.sort();
    vars.dedup();
    vars
}

fn collect_vars(expr: &Expr, vars: &mut Vec<String>) {
    match expr {
        Expr::Path(path_expr) => {
            let path = &path_expr.path;
            if path.segments.len() == 1 && path.leading_colon.is_none() {
                let ident = &path.segments[0].ident;
                let name = ident.to_string();
                if !is_keyword(&name) && !vars.contains(&name) {
                    vars.push(name);
                }
            }
        }
        Expr::Binary(bin) => {
            collect_vars(&bin.left, vars);
            collect_vars(&bin.right, vars);
        }
        Expr::Unary(unary) => {
            collect_vars(&unary.expr, vars);
        }
        Expr::Paren(paren) => {
            collect_vars(&paren.expr, vars);
        }
        Expr::Group(group) => {
            collect_vars(&group.expr, vars);
        }
        Expr::Call(call) => {
            collect_vars(&call.func, vars);
            for arg in &call.args {
                collect_vars(arg, vars);
            }
        }
        Expr::MethodCall(method) => {
            collect_vars(&method.receiver, vars);
            for arg in &method.args {
                collect_vars(arg, vars);
            }
        }
        Expr::Block(block) => {
            for stmt in &block.block.stmts {
                match stmt {
                    syn::Stmt::Local(local) => {
                        if let Some(init) = &local.init {
                            collect_vars(&init.expr, vars);
                        }
                    }
                    syn::Stmt::Expr(e, _) => {
                        collect_vars(e, vars);
                    }
                    _ => {}
                }
            }
        }
        Expr::If(if_expr) => {
            collect_vars(&if_expr.cond, vars);
            collect_vars_block(&if_expr.then_branch, vars);
            if let Some((_, else_branch)) = &if_expr.else_branch {
                collect_vars(else_branch, vars);
            }
        }
        Expr::Match(match_expr) => {
            collect_vars(&match_expr.expr, vars);
            for arm in &match_expr.arms {
                collect_vars(&arm.body, vars);
                if let Some((_, guard)) = &arm.guard {
                    collect_vars(guard, vars);
                }
            }
        }
        Expr::Loop(loop_expr) => {
            collect_vars_block(&loop_expr.body, vars);
        }
        Expr::While(while_expr) => {
            collect_vars(&while_expr.cond, vars);
            collect_vars_block(&while_expr.body, vars);
        }
        Expr::ForLoop(for_expr) => {
            collect_vars(&for_expr.expr, vars);
            collect_vars_block(&for_expr.body, vars);
        }
        Expr::Closure(closure) => {
            collect_vars(&closure.body, vars);
        }
        Expr::Reference(ref_expr) => {
            collect_vars(&ref_expr.expr, vars);
        }
        Expr::Cast(cast) => {
            collect_vars(&cast.expr, vars);
        }
        Expr::Tuple(tuple) => {
            for elem in &tuple.elems {
                collect_vars(elem, vars);
            }
        }
        Expr::Array(array) => {
            for elem in &array.elems {
                collect_vars(elem, vars);
            }
        }
        Expr::Range(range) => {
            if let Some(start) = &range.start {
                collect_vars(start, vars);
            }
            if let Some(end) = &range.end {
                collect_vars(end, vars);
            }
        }
        Expr::Index(index) => {
            collect_vars(&index.expr, vars);
            collect_vars(&index.index, vars);
        }
        Expr::Field(field) => {
            collect_vars(&field.base, vars);
        }
        _ => {}
    }
}

fn collect_vars_block(block: &syn::Block, vars: &mut Vec<String>) {
    for stmt in &block.stmts {
        match stmt {
            syn::Stmt::Local(local) => {
                if let Some(init) = &local.init {
                    collect_vars(&init.expr, vars);
                }
            }
            syn::Stmt::Expr(e, _) => {
                collect_vars(e, vars);
            }
            _ => {}
        }
    }
}

fn is_keyword(name: &str) -> bool {
    matches!(
        name,
        "true"
            | "false"
            | "self"
            | "Self"
            | "super"
            | "crate"
            | "Some"
            | "None"
            | "Ok"
            | "Err"
            | "Vec"
            | "String"
            | "Box"
            | "Option"
            | "Result"
            | "i8"
            | "i16"
            | "i32"
            | "i64"
            | "i128"
            | "isize"
            | "u8"
            | "u16"
            | "u32"
            | "u64"
            | "u128"
            | "usize"
            | "f32"
            | "f64"
            | "bool"
            | "char"
            | "str"
            | "if"
            | "else"
            | "match"
            | "for"
            | "while"
            | "loop"
            | "fn"
            | "let"
            | "mut"
            | "pub"
            | "use"
            | "mod"
            | "struct"
            | "enum"
            | "impl"
            | "trait"
            | "type"
            | "return"
            | "break"
            | "continue"
            | "move"
            | "ref"
            | "async"
            | "await"
            | "where"
            | "const"
            | "static"
            | "in"
            | "as"
            | "dyn"
            | "unsafe"
            | "extern"
    )
}

/// Dynamic execution engine powered by cargo subprocess.
pub struct DynamicEngine {}

impl DynamicEngine {
    pub fn new() -> Result<Self, String> {
        // Verify that cargo is available
        let output = Command::new("cargo")
            .arg("--version")
            .output()
            .map_err(|e| format!("cargo not available: {}", e))?;
        if !output.status.success() {
            return Err("cargo --version failed".to_string());
        }
        Ok(Self {})
    }

    /// Executes code through a temporary cargo project and returns the raw text output.
    fn execute_code(&mut self, code: &str) -> Result<String, String> {
        // Detect and collect dependencies
        let external_crates = detect_external_crates(code);

        // Create a unique temp directory based on code hash to enable caching
        let code_hash = format!("{:x}", md5::compute(code));
        let temp_dir = std::env::temp_dir().join(format!("preprocessor_cargo_{}", code_hash));

        // Always recreate the project to ensure dependencies are correct
        if temp_dir.exists() {
            std::fs::remove_dir_all(&temp_dir)
                .map_err(|e| format!("failed to remove old temp dir: {}", e))?;
        }

        let src_dir = temp_dir.join("src");
        std::fs::create_dir_all(&src_dir)
            .map_err(|e| format!("failed to create temp dir: {}", e))?;

        // Write Cargo.toml with detected dependencies
        write_cargo_toml(&temp_dir, &external_crates, code)?;

        // Write src/main.rs with the code
        write_main_rs(&temp_dir, code)?;

        // Run `cargo run` and capture output
        let output = Command::new("cargo")
            .arg("run")
            .arg("--quiet")
            .current_dir(&temp_dir)
            .output()
            .map_err(|e| format!("failed to run cargo: {}", e))?;

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();

        if !output.status.success() {
            return Err(format!("compilation failed:\n{}", stderr));
        }

        Ok(stdout)
    }

    /// Evaluates a `syn::Expr` through dynamic cargo execution.
    pub fn evaluate(&mut self, expr: &Expr) -> EvalResult {
        // Check for free variables first
        let free_vars = extract_free_variables(expr);
        if !free_vars.is_empty() {
            return EvalResult::PassThrough;
        }

        // Convert expression to executable code
        let code = expr_to_code(expr);

        // Execute through cargo
        match self.execute_code(&code) {
            Ok(output) => match parse_output(&output) {
                Some(value) => EvalResult::Value(value),
                None => {
                    let trimmed = output.trim();
                    if trimmed == "()" || trimmed.is_empty() {
                        EvalResult::Value(Value::Unit)
                    } else {
                        EvalResult::PassThrough
                    }
                }
            },
            Err(e) => {
                eprintln!("[preprocessor:dynamic] Execution failed: {}", e);
                EvalResult::PassThrough
            }
        }
    }

    /// Evaluates a block of statements through dynamic cargo execution.
    #[allow(dead_code)]
    pub fn evaluate_block(&mut self, block: &syn::Block) -> EvalResult {
        let code = block_to_code(block);

        match self.execute_code(&code) {
            Ok(output) => match parse_output(&output) {
                Some(value) => EvalResult::Value(value),
                None => EvalResult::PassThrough,
            },
            Err(e) => {
                eprintln!("[preprocessor:dynamic] Block execution failed: {}", e);
                EvalResult::PassThrough
            }
        }
    }
}

impl Default for DynamicEngine {
    fn default() -> Self {
        Self::new().expect("Failed to initialize dynamic engine")
    }
}

// ============================================================================
// Temp project helpers
// ============================================================================

fn write_cargo_toml(dir: &PathBuf, external_crates: &[String], code: &str) -> Result<(), String> {
    let mut toml = String::from(
        r#"[package]
name = "preprocessor_eval"
version = "0.1.0"
edition = "2021"

[dependencies]
"#,
    );

    for crate_name in external_crates {
        toml.push_str(&format!("{} = \"*\"\n", crate_name));
    }

    // 坡底逻辑:如果代码中明显使用了 chrono 但未被检测到,则强制添加
    if (code.contains("Local") || code.contains("chrono::"))
        && !external_crates.iter().any(|c| c == "chrono")
    {
        toml.push_str("chrono = \"*\"\n");
    }

    // 检测异步代码并自动添加 tokio 依赖
    let is_async = code.contains(".await") || code.contains("async ");
    if is_async && !external_crates.iter().any(|c| c == "tokio") {
        toml.push_str("tokio = { version = \"*\", features = [\"full\"] }\n");
    }

    // 检测是否使用了 reqwest,确保添加完整特性
    if code.contains("reqwest::") && !external_crates.iter().any(|c| c == "reqwest") {
        toml.push_str("reqwest = { version = \"*\", features = [\"blocking\"] }\n");
    }

    let cargo_toml_path = dir.join("Cargo.toml");
    std::fs::write(&cargo_toml_path, toml)
        .map_err(|e| format!("failed to write Cargo.toml: {}", e))?;
    Ok(())
}

fn write_main_rs(dir: &PathBuf, code: &str) -> Result<(), String> {
    let main_rs_path = dir.join("src").join("main.rs");

    // 尝试从原始代码中提取 use 语句
    let mut use_statements = String::new();
    let mut body_code = String::new();

    for line in code.lines() {
        if line.trim().starts_with("use ") {
            use_statements.push_str(line);
            use_statements.push('\n');
        } else {
            body_code.push_str(line);
            body_code.push('\n');
        }
    }

    // 自动生成 std 库 use 语句
    let std_imports = generate_std_imports(&body_code);
    if !std_imports.is_empty() {
        use_statements.insert_str(0, &std_imports);
        use_statements.push('\n');
    }

    // 检测是否使用了 chrono 相关的类型,如果有则自动注入依赖
    let needs_chrono =
        body_code.contains("chrono::") || body_code.contains("Local") || body_code.contains("Utc");

    if needs_chrono && !use_statements.contains("chrono") {
        use_statements.insert_str(0, "use chrono::{Local, Utc, TimeZone, NaiveDateTime};\n");
    }

    // 检测是否是异步代码
    let is_async = body_code.contains(".await") || body_code.contains("async ");
    
    // 检测是否使用了 ? 运算符
    let uses_try = body_code.contains('?');
    
    let content = if is_async {
        // 异步代码处理
        if uses_try {
            // 使用 ? 运算符时,需要返回 Result 类型
            format!(
                r#"{}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {{
    let result = (async {{
        let _result = {{
            {}
        }};
        Ok::<_, Box<dyn std::error::Error>>(_result)
    }}).await?;
    println!("{{:?}}", result);
    Ok(())
}}
"#,
                use_statements, body_code
            )
        } else {
            // 不使用 ? 运算符的异步代码
            format!(
                r#"{}
#[tokio::main]
async fn main() {{
    let result = async {{
        {}
    }}.await;
    println!("{{:?}}", result);
}}
"#,
                use_statements, body_code
            )
        }
    } else {
        // 同步代码处理
        format!(
            r#"{}
fn main() {{
    let result = {{
        {}
    }};
    println!("{{:?}}", result);
}}
"#,
            use_statements, body_code
        )
    };
    
    std::fs::write(&main_rs_path, content)
        .map_err(|e| format!("failed to write main.rs: {}", e))?;
    Ok(())
}

// ============================================================================
// Output parsing
// ============================================================================

fn parse_output(output: &str) -> Option<Value> {
    let text = output.trim();
    if text.is_empty() {
        return Some(Value::Unit);
    }

    parse_value(text)
}

fn parse_value(text: &str) -> Option<Value> {
    if text.is_empty() {
        return Some(Value::Unit);
    }

    if text == "()" {
        return Some(Value::Unit);
    }

    if text == "true" {
        return Some(Value::Bool(true));
    }
    if text == "false" {
        return Some(Value::Bool(false));
    }

    // Char: 'x'
    if text.starts_with('\'') && text.ends_with('\'') && text.len() >= 3 {
        if let Ok(c) = text[1..text.len() - 1].parse::<char>() {
            return Some(Value::Char(c));
        }
    }

    // String: "hello"
    if text.starts_with('"') && text.ends_with('"') && text.len() >= 2 {
        let inner = &text[1..text.len() - 1];
        let unescaped = inner
            .replace("\\n", "\n")
            .replace("\\t", "\t")
            .replace("\\\"", "\"")
            .replace("\\\\", "\\");
        return Some(Value::Str(unescaped));
    }

    // Float special cases
    match text {
        "inf" | "infinity" | "f64::INFINITY" => return Some(Value::Float(f64::INFINITY)),
        "-inf" | "-infinity" | "f64::NEG_INFINITY" => return Some(Value::Float(f64::NEG_INFINITY)),
        "NaN" | "f64::NAN" | "nan" => return Some(Value::Float(f64::NAN)),
        _ => {}
    }

    // Float
    if text.contains('.') || text.contains('e') || text.contains('E') {
        if let Ok(f) = text.parse::<f64>() {
            return Some(Value::Float(f));
        }
    }

    // Integer
    if let Ok(n) = text.parse::<i64>() {
        return Some(Value::Int(n));
    }

    if let Ok(n) = text.parse::<u64>() {
        if n <= i64::MAX as u64 {
            return Some(Value::Int(n as i64));
        }
    }

    // Tuple
    if text.starts_with('(') && text.ends_with(')') {
        if let Some(vals) = parse_tuple(text) {
            return Some(Value::Tuple(vals));
        }
    }

    // Array/Vec
    if text.starts_with('[') && text.ends_with(']') {
        if let Some(vals) = parse_array(text) {
            return Some(Value::Array(vals));
        }
    }

    None
}

fn parse_tuple(text: &str) -> Option<Vec<Value>> {
    let inner = &text[1..text.len() - 1];
    if inner.trim().is_empty() {
        return Some(vec![]);
    }

    let elements = split_top_level(inner, ',');
    let mut values = Vec::new();
    for elem in elements {
        values.push(parse_value(elem.trim())?);
    }
    Some(values)
}

fn parse_array(text: &str) -> Option<Vec<Value>> {
    let inner = &text[1..text.len() - 1];
    if inner.trim().is_empty() {
        return Some(vec![]);
    }

    let elements = split_top_level(inner, ',');
    let mut values = Vec::new();
    for elem in elements {
        values.push(parse_value(elem.trim())?);
    }
    Some(values)
}

fn split_top_level(text: &str, delimiter: char) -> Vec<&str> {
    let mut parts = Vec::new();
    let mut depth_paren = 0;
    let mut depth_bracket = 0;
    let mut in_string = false;
    let mut in_char = false;
    let mut escape = false;
    let mut start = 0;

    let chars: Vec<char> = text.chars().collect();
    for i in 0..chars.len() {
        let c = chars[i];

        if escape {
            escape = false;
            continue;
        }

        match c {
            '\\' if in_string || in_char => {
                escape = true;
            }
            '"' if !in_char => {
                in_string = !in_string;
            }
            '\'' if !in_string => {
                in_char = !in_char;
            }
            '(' if !in_string && !in_char => {
                depth_paren += 1;
            }
            ')' if !in_string && !in_char => {
                depth_paren -= 1;
            }
            '[' if !in_string && !in_char => {
                depth_bracket += 1;
            }
            ']' if !in_string && !in_char => {
                depth_bracket -= 1;
            }
            c if c == delimiter
                && depth_paren == 0
                && depth_bracket == 0
                && !in_string
                && !in_char =>
            {
                parts.push(&text[start..i]);
                start = i + 1;
            }
            _ => {}
        }
    }

    parts.push(&text[start..]);
    parts
}

fn expr_to_code(expr: &Expr) -> String {
    quote::quote!(#expr).to_string()
}

#[allow(dead_code)]
fn block_to_code(block: &syn::Block) -> String {
    quote::quote!(#block).to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_external_crates() {
        let code = r#"use chrono::Utc;
use serde::Deserialize;
let now = Utc::now();"#;
        let crates = detect_external_crates(code);
        assert!(crates.contains(&"chrono".to_string()));
        assert!(crates.contains(&"serde".to_string()));
    }

    #[test]
    fn test_extract_free_variables_simple() {
        let expr: Expr = syn::parse_quote!(1 + 2);
        let vars = extract_free_variables(&expr);
        assert!(vars.is_empty());
    }

    #[test]
    fn test_extract_free_variables_with_vars() {
        let expr: Expr = syn::parse_quote!(x + 1);
        let vars = extract_free_variables(&expr);
        assert_eq!(vars, vec!["x"]);
    }

    #[test]
    fn test_parse_output_integer() {
        assert_eq!(parse_value("42"), Some(Value::Int(42)));
        assert_eq!(parse_value("-7"), Some(Value::Int(-7)));
    }

    #[test]
    fn test_parse_output_float() {
        assert_eq!(parse_value("3.14"), Some(Value::Float(3.14)));
        assert_eq!(parse_value("-2.5"), Some(Value::Float(-2.5)));
    }

    #[test]
    fn test_parse_output_bool() {
        assert_eq!(parse_value("true"), Some(Value::Bool(true)));
        assert_eq!(parse_value("false"), Some(Value::Bool(false)));
    }

    #[test]
    fn test_parse_output_string() {
        assert_eq!(
            parse_value("\"hello\""),
            Some(Value::Str("hello".to_string()))
        );
    }

    #[test]
    fn test_parse_output_tuple() {
        let result = parse_value("(1, 2, 3)");
        assert!(matches!(result, Some(Value::Tuple(_))));
        if let Some(Value::Tuple(vals)) = result {
            assert_eq!(vals.len(), 3);
        }
    }

    #[test]
    fn test_parse_output_array() {
        let result = parse_value("[1, 2, 3]");
        assert!(matches!(result, Some(Value::Array(_))));
        if let Some(Value::Array(vals)) = result {
            assert_eq!(vals.len(), 3);
        }
    }

    #[test]
    fn test_split_top_level() {
        let parts = split_top_level("1, (2, 3), 4", ',');
        assert_eq!(parts, vec!["1", " (2, 3)", " 4"]);
    }
}