onion-frontend 0.3.4

Compilation frontend for the Onion programming language - lexer, parser, and IR generator
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
//! AST 语义分析模块:提供变量定义检查、类型推断、作用域管理等功能。
//!
//! 本模块实现了 Onion 语言 AST 的语义分析,包括变量定义检查、类型推断、
//! 作用域管理、以及自动捕获变量分析。主要用于编译期的静态检查和优化。
//!
//! # 主要类型
//! - `AssumedType`:推断的变量类型枚举
//! - `Variable`:变量信息,包含类型推断
//! - `VariableFrame` 和 `VariableContext`:作用域和变量上下文管理
//! - `ASTAnalysisDiagnostic`:分析过程中的诊断信息
//!
//! # 核心功能
//! - 变量定义和使用检查
//! - 作用域管理(Frame 和 Context)
//! - 类型推断(基于 AST 节点类型)
//! - 自动捕获变量分析和重构
//! - 断点词法上下文分析
//!
//! # 用法示例
//! ```ignore
//! let mut diagnostics = DiagnosticCollector::new();
//! let context = analyze_ast(&ast, &mut diagnostics, &None)?;
//! let (captured_vars, rebuilt_ast) = auto_capture_and_rebuild(&ast);
//! ```

use std::collections::{HashMap, HashSet};

use crate::{
    diagnostics::{Diagnostic, SourceLocation, collector::DiagnosticCollector},
    parser::{
        Source,
        ast::{ASTNodeOperation, ASTNodeType},
    },
};

use onion_vm::types::lambda::vm_instructions::ir_translator::PRE_ALLOCATED_VARIABLE_STRINGS;

use super::ast::ASTNode;

/// 推断的变量类型枚举。
///
/// 用于在语义分析过程中推断变量的可能类型,帮助进行类型检查和优化。
#[derive(Debug, Clone, PartialEq)]
pub enum AssumedType {
    /// 未知类型。
    Unknown,
    /// Lambda 函数类型。
    Lambda,
    /// 字符串类型。
    String,
    /// 数值类型。
    Number,
    /// 布尔类型。
    Boolean,
    /// Base64 编码的字节类型。
    Base64,
    /// 空值类型。
    Null,
    /// 未定义类型。
    Undefined,
    /// 范围类型。
    Range,
    /// 元组类型。
    Tuple,
    /// 集合类型。
    Set,
    /// 键值对类型。
    KeyVal,
}

/// 变量信息结构。
///
/// 存储变量的推断类型信息,用于语义分析和类型检查。
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Variable {
    pub assumed_type: AssumedType,
}

/// 变量作用域帧。
///
/// 表示单个作用域内的变量定义,支持变量的定义和查找。
#[derive(Debug, Clone)]
pub struct VariableFrame {
    pub variables: HashMap<String, Variable>,
}

impl VariableFrame {
    /// 定义或更新变量。
    ///
    /// # 参数
    /// - `name`:变量名。
    /// - `var`:变量信息。
    ///
    /// # 返回
    /// 成功时返回 `Ok(())`,失败时返回错误信息。
    pub fn define_variable(&mut self, name: String, var: Variable) -> Result<(), String> {
        if let Some(existing_var) = self.variables.get_mut(&name) {
            existing_var.assumed_type = var.assumed_type;
        } else {
            self.variables.insert(name, var);
        }
        Ok(())
    }
}

/// 变量上下文管理器。
///
/// 管理多层嵌套的作用域,支持上下文和帧的推入/弹出操作。
/// 用于跟踪不同作用域中的变量定义和可见性。
#[derive(Debug, Clone)]
pub struct VariableContext {
    contexts: Vec<Vec<VariableFrame>>,
}

impl VariableContext {
    /// 获取所有上下文的引用。
    pub fn all_contexts(&self) -> &Vec<Vec<VariableFrame>> {
        &self.contexts
    }
}

/// AST 分析诊断信息类型。
///
/// 封装语义分析过程中可能出现的错误和警告信息。
#[derive(Debug, Clone)]
pub enum ASTAnalysisDiagnostic {
    /// 未定义变量错误。
    UndefinedVariable(Option<SourceLocation>, String),
    /// 详细错误信息。
    DetailedError(Option<SourceLocation>, String),
}

impl Diagnostic for ASTAnalysisDiagnostic {
    fn severity(&self) -> crate::diagnostics::ReportSeverity {
        match self {
            ASTAnalysisDiagnostic::UndefinedVariable(_, _) => {
                crate::diagnostics::ReportSeverity::Error
            }
            ASTAnalysisDiagnostic::DetailedError(_, _) => crate::diagnostics::ReportSeverity::Error,
        }
    }

    fn title(&self) -> String {
        "AST Analysis Error".into()
    }

    fn message(&self) -> String {
        match self {
            ASTAnalysisDiagnostic::UndefinedVariable(_, var_name) => {
                format!("Variable '{}' was used before it was defined.", var_name)
            }
            ASTAnalysisDiagnostic::DetailedError(_, msg) => msg.clone(),
        }
    }

    fn location(&self) -> Option<SourceLocation> {
        match self {
            ASTAnalysisDiagnostic::UndefinedVariable(loc, _) => loc.clone(),
            ASTAnalysisDiagnostic::DetailedError(loc, _) => loc.clone(),
        }
    }

    fn help(&self) -> Option<String> {
        match self {
            ASTAnalysisDiagnostic::UndefinedVariable(_, var_name) => Some(format!(
                "Define the variable '{}' before using it. If the variable is dynamic, use \"@required '{}'\" to ensure it is defined at runtime.",
                var_name, var_name
            )),
            ASTAnalysisDiagnostic::DetailedError(_, _) => None,
        }
    }

    fn copy(&self) -> Box<dyn Diagnostic> {
        Box::new(self.clone())
    }
}

/// 分析输出结果。
///
/// 包含断点处的变量上下文信息,用于调试支持。
#[derive(Debug)]
pub struct AnalysisOutput {
    pub context_at_break: Option<VariableContext>,
}

impl VariableContext {
    /// 创建新的变量上下文。
    pub fn new() -> Self {
        VariableContext {
            contexts: vec![vec![VariableFrame {
                variables: HashMap::new(),
            }]],
        }
    }

    /// 获取当前上下文的可变引用。
    fn current_context_mut(&mut self) -> &mut Vec<VariableFrame> {
        self.contexts
            .last_mut()
            .expect("Context stack should never be empty")
    }

    /// 获取当前上下文的引用。
    fn current_context(&self) -> &Vec<VariableFrame> {
        self.contexts
            .last()
            .expect("Context stack should never be empty")
    }

    /// 在当前作用域定义变量。
    pub fn define_variable(&mut self, name: String, var: Variable) {
        self.current_context_mut()
            .last_mut()
            .expect("Current context should always have at least one frame")
            .define_variable(name, var)
            .unwrap_or_else(|e| {
                panic!("Failed to define variable: {}", e);
            });
    }

    /// 查找变量(搜索所有上下文和帧)。
    pub fn get_variable(&self, name: &str) -> Option<&Variable> {
        for frames in self.contexts.iter().rev() {
            for frame in frames.iter().rev() {
                if let Some(var) = frame.variables.get(name) {
                    return Some(var);
                }
            }
        }
        None
    }

    /// 在当前上下文中查找变量。
    pub fn get_variable_current_context(&self, name: &str) -> Option<&Variable> {
        for frame in self.current_context().iter().rev() {
            if let Some(var) = frame.variables.get(name) {
                return Some(var);
            }
        }
        None
    }

    /// 推入新的作用域帧。
    pub fn push_frame(&mut self) {
        self.current_context_mut().push(VariableFrame {
            variables: HashMap::new(),
        });
    }

    /// 弹出作用域帧。
    pub fn pop_frame(&mut self) -> Result<(), String> {
        let current_context = self.current_context_mut();
        if current_context.len() > 1 {
            current_context.pop();
            Ok(())
        } else {
            Err("Cannot pop the initial frame of a context".to_string())
        }
    }

    /// 推入新的上下文。
    pub fn push_context(&mut self) {
        self.contexts.push(vec![VariableFrame {
            variables: HashMap::new(),
        }]);
    }

    /// 弹出上下文。
    pub fn pop_context(&mut self) -> Result<(), String> {
        if self.contexts.len() > 1 {
            self.contexts.pop();
            Ok(())
        } else {
            Err("Cannot pop the global context".to_string())
        }
    }
}

// --- 主要分析逻辑 ---

/// 检查子节点数量是否精确匹配的辅助宏。
macro_rules! check_children_exact {
    ($diagnostics:expr, $node:expr, $expected:expr, $name:expr) => {
        if $node.children.len() != $expected {
            $diagnostics.report(ASTAnalysisDiagnostic::DetailedError(
                $node.source_location.clone(),
                format!(
                    "'{}' node expects exactly {} child(ren), but found {}",
                    $name,
                    $expected,
                    $node.children.len()
                ),
            ));
            return AssumedType::Unknown;
        }
    };
}

/// 检查子节点数量是否在范围内的辅助宏。
macro_rules! check_children_range {
    ($diagnostics:expr, $node:expr, $range:expr, $name:expr) => {
        let range_clone = $range.clone();
        if !range_clone.contains(&$node.children.len()) {
            $diagnostics.report(ASTAnalysisDiagnostic::DetailedError(
                $node.source_location.clone(),
                format!(
                    "'{}' node expects between {} and {} children, but found {}",
                    $name,
                    $range.start(),
                    $range.end(),
                    $node.children.len()
                ),
            ));
            return AssumedType::Unknown;
        }
    };
}

/// 分析 AST 的主入口函数。
///
/// # 参数
/// - `ast`:要分析的 AST 根节点。
/// - `diagnostics_collector`:诊断信息收集器。
/// - `break_at_position`:可选的断点位置,用于调试支持。
///
/// # 返回
/// 成功时返回断点处的变量上下文,失败时返回 `Err(())`。
pub fn analyze_ast(
    ast: &ASTNode,
    diagnostics_collector: &mut DiagnosticCollector,
    break_at_position: &Option<(Source, usize)>,
) -> Result<Option<VariableContext>, ()> {
    let mut context = VariableContext::new();
    context.push_context();
    for builtin in PRE_ALLOCATED_VARIABLE_STRINGS {
        context.define_variable(
            builtin.to_string(),
            Variable {
                assumed_type: AssumedType::Unknown,
            },
        );
    }

    let mut context_at_break: Option<VariableContext> = None;

    analyze_node(
        ast,
        &mut context,
        diagnostics_collector,
        false,
        break_at_position,
        &mut context_at_break,
    );

    Ok(context_at_break)
}

/// 检查是否在断点位置,并保存上下文。
fn check_postorder_break(
    node: &ASTNode,
    break_at_position: &Option<(Source, usize)>,
    context: &VariableContext,
    context_at_break: &mut Option<VariableContext>,
) {
    if break_at_position.is_none() || context_at_break.is_some() {
        return;
    }
    if let Some((source, break_pos)) = break_at_position {
        if let Some(location) = &node.source_location {
            if location.source.ne(source) {
                return;
            }
            let (start_char, end_char) = location.span;
            let source_code = location.source.content_str();
            let start_byte = source_code
                .char_indices()
                .nth(start_char)
                .map(|(b, _)| b)
                .unwrap_or(source_code.len());
            let end_byte = source_code
                .char_indices()
                .nth(end_char)
                .map(|(b, _)| b)
                .unwrap_or(source_code.len());
            if *break_pos >= start_byte && *break_pos <= end_byte {
                *context_at_break = Some(context.clone());
            }
        }
    }
}

/// 递归分析单个 AST 节点。
///
/// # 参数
/// - `node`:要分析的 AST 节点。
/// - `context`:当前变量上下文。
/// - `diagnostics`:诊断信息收集器。
/// - `dynamic`:是否在动态模式下分析。
/// - `break_at_position`:断点位置。
/// - `context_at_break`:断点处的上下文输出。
///
/// # 返回
/// 节点的推断类型。
#[stacksafe::stacksafe]
fn analyze_node(
    node: &ASTNode,
    context: &mut VariableContext,
    diagnostics: &mut DiagnosticCollector,
    dynamic: bool,
    break_at_position: &Option<(Source, usize)>,
    context_at_break: &mut Option<VariableContext>,
) -> AssumedType {
    if context_at_break.is_some() {
        return AssumedType::Unknown;
    }

    match &node.node_type {
        ASTNodeType::Let(var_name) => {
            check_children_exact!(diagnostics, node, 1, "Let");
            let assumed_type = analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Unknown;
            }
            context.define_variable(
                var_name.clone(),
                Variable {
                    assumed_type: assumed_type.clone(),
                },
            );
            check_postorder_break(node, break_at_position, context, context_at_break);
            assumed_type
        }
        ASTNodeType::Variable(var_name) => {
            check_children_exact!(diagnostics, node, 0, "Variable");
            if !dynamic && context.get_variable_current_context(var_name).is_none() {
                diagnostics.report(ASTAnalysisDiagnostic::UndefinedVariable(
                    node.source_location.clone(),
                    var_name.clone(),
                ));
            }
            check_postorder_break(node, break_at_position, context, context_at_break);
            context
                .get_variable(var_name)
                .map_or(AssumedType::Unknown, |v| v.assumed_type.clone())
        }
        ASTNodeType::Required(var_name) => {
            check_children_exact!(diagnostics, node, 0, "Required");
            context.define_variable(
                var_name.clone(),
                Variable {
                    assumed_type: AssumedType::Unknown,
                },
            );
            check_postorder_break(node, break_at_position, context, context_at_break);
            AssumedType::Unknown
        }
        ASTNodeType::Frame => {
            check_children_exact!(diagnostics, node, 1, "Frame");
            context.push_frame();
            let assumed_type = analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_none() {
                let _ = context.pop_frame();
            }
            check_postorder_break(node, break_at_position, context, context_at_break);
            assumed_type
        }
        ASTNodeType::LambdaDef(is_dynamic_gen, captured_vars) => {
            check_children_exact!(diagnostics, node, 2, "LambdaDef");
            let params = &node.children[0];
            let body = &node.children[1];
            let mut captured_vars = captured_vars.clone();
            if !dynamic {
                // for var_name in captured_vars.iter() {
                //     if context.get_variable_current_context(var_name).is_none() {
                //         diagnostics.report(ASTAnalysisDiagnostic::UndefinedVariable(
                //             node.source_location.clone(),
                //             var_name.clone(),
                //         ));
                //     }
                // }
                captured_vars.retain(|v| context.get_variable_current_context(v).is_some());
            }
            let params_def = analyze_tuple_params(
                params,
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Lambda;
            }

            if *is_dynamic_gen {
                analyze_node(
                    body,
                    context,
                    diagnostics,
                    true,
                    break_at_position,
                    context_at_break,
                );
            } else {
                let params_def = params_def.unwrap_or_default();
                context.push_context();
                for var in PRE_ALLOCATED_VARIABLE_STRINGS {
                    context.define_variable(
                        var.to_string(),
                        Variable {
                            assumed_type: AssumedType::Unknown,
                        },
                    );
                }
                let mut all_vars = captured_vars.clone();
                all_vars.extend(params_def);
                for var in all_vars {
                    context.define_variable(
                        var,
                        Variable {
                            assumed_type: AssumedType::Unknown,
                        },
                    );
                }
                analyze_node(
                    body,
                    context,
                    diagnostics,
                    false,
                    break_at_position,
                    context_at_break,
                );
                if context_at_break.is_none() {
                    let _ = context.pop_context();
                }
            }
            check_postorder_break(node, break_at_position, context, context_at_break);
            AssumedType::Lambda
        }
        ASTNodeType::Assign => {
            check_children_exact!(diagnostics, node, 2, "Assign");
            let assumed_type = analyze_node(
                &node.children[1],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Unknown;
            }
            analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            check_postorder_break(node, break_at_position, context, context_at_break);
            assumed_type
        }
        ASTNodeType::Pair => {
            check_children_exact!(diagnostics, node, 2, "Pair");
            analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::KeyVal;
            }
            analyze_node(
                &node.children[1],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            check_postorder_break(node, break_at_position, context, context_at_break);
            AssumedType::KeyVal
        }
        ASTNodeType::Operation(_) => {
            // This case was correctly identified as needing a nested match
            if let ASTNodeType::Operation(op) = &node.node_type {
                match op {
                    ASTNodeOperation::Not | ASTNodeOperation::Abs | ASTNodeOperation::Minus => {
                        check_children_exact!(diagnostics, node, 1, "Unary Operation 'not'")
                    }
                    _ => check_children_exact!(diagnostics, node, 2, "Binary Operation"),
                }
            }
            let mut last_type = AssumedType::Unknown;
            for child in &node.children {
                last_type = analyze_node(
                    child,
                    context,
                    diagnostics,
                    dynamic,
                    break_at_position,
                    context_at_break,
                );
                if context_at_break.is_some() {
                    return AssumedType::Unknown;
                }
            }
            check_postorder_break(node, break_at_position, context, context_at_break);
            last_type
        }
        ASTNodeType::Modifier(_) => {
            check_children_exact!(diagnostics, node, 1, "Modifier");
            analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            )
        }
        ASTNodeType::Return => {
            check_children_range!(diagnostics, node, 0..=1, "Return");
            if let Some(child) = node.children.first() {
                analyze_node(
                    child,
                    context,
                    diagnostics,
                    dynamic,
                    break_at_position,
                    context_at_break,
                )
            } else {
                AssumedType::Null
            }
        }
        ASTNodeType::If => {
            check_children_range!(diagnostics, node, 2..=3, "If");
            analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Unknown;
            }
            let then_type = analyze_node(
                &node.children[1],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Unknown;
            }
            if node.children.len() == 3 {
                let _else_type = analyze_node(
                    &node.children[2],
                    context,
                    diagnostics,
                    dynamic,
                    break_at_position,
                    context_at_break,
                );
            }
            then_type
        }
        ASTNodeType::While => {
            check_children_exact!(diagnostics, node, 2, "While");
            analyze_node(
                &node.children[0],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            if context_at_break.is_some() {
                return AssumedType::Unknown;
            }
            analyze_node(
                &node.children[1],
                context,
                diagnostics,
                dynamic,
                break_at_position,
                context_at_break,
            );
            AssumedType::Unknown
        }
        ASTNodeType::String(_) => {
            check_children_exact!(diagnostics, node, 0, "String Literal");
            AssumedType::String
        }
        ASTNodeType::Boolean(_) => {
            check_children_exact!(diagnostics, node, 0, "Boolean Literal");
            AssumedType::Boolean
        }
        ASTNodeType::Number(_) => {
            check_children_exact!(diagnostics, node, 0, "Number Literal");
            AssumedType::Number
        }
        ASTNodeType::Base64(_) => {
            check_children_exact!(diagnostics, node, 0, "Base64 Literal");
            AssumedType::Base64
        }
        ASTNodeType::Null => {
            check_children_exact!(diagnostics, node, 0, "Null");
            AssumedType::Null
        }
        ASTNodeType::Undefined => {
            check_children_exact!(diagnostics, node, 0, "Undefined");
            AssumedType::Undefined
        }
        ASTNodeType::Break => {
            check_children_exact!(diagnostics, node, 1, "Break");
            AssumedType::Unknown
        }
        ASTNodeType::Continue => {
            check_children_exact!(diagnostics, node, 1, "Continue");
            AssumedType::Unknown
        }
        node_type => {
            let exact_children = match node_type {
                ASTNodeType::Apply => 2, // Apply expects exactly two children (function and argument)
                ASTNodeType::Tuple => 0, // Tuple can have variable number of children
                ASTNodeType::AssumeTuple => 1, // AssumeTuple expects exactly one child
                ASTNodeType::GetAttr => 2, // GetAttr expects exactly two children (object and attribute name)
                ASTNodeType::Range => 2,   // Range expects exactly two children (start and end)
                ASTNodeType::In => 2,      // In expects exactly two children (value and collection)
                ASTNodeType::Namespace(_) => 1, // Namespace expects exactly one child (the namespace definition)
                ASTNodeType::LazySet => 2, // LazySet expects exactly two children (container and filter)
                ASTNodeType::Map => 2, // Map expects exactly two children (collection and function)
                ASTNodeType::Is => 2,  // Is expects exactly two children (value and type)
                ASTNodeType::Raise => 1, // Raise expects exactly one child (the error message)
                ASTNodeType::Dynamic => 1, // Dynamic expects exactly one child (the dynamic expression)
                ASTNodeType::Static => 1, // Static expects exactly one child (the static expression)
                ASTNodeType::Comptime => 1, // Comptime expects exactly one child (the expression to evaluate at compile time)
                _ => 0,                     // Default case for unhandled node types
            };
            if exact_children != 0 {
                check_children_exact!(diagnostics, node, exact_children, node_type.to_string());
            }
            // Default for variable-child nodes or unhandled ones
            let mut last_type = AssumedType::Unknown;
            for child in &node.children {
                last_type = analyze_node(
                    child,
                    context,
                    diagnostics,
                    dynamic,
                    break_at_position,
                    context_at_break,
                );
                if context_at_break.is_some() {
                    return AssumedType::Unknown;
                }
            }
            last_type
        }
    }
}

/// 分析元组参数定义,提取参数名称。
///
/// # 参数
/// - `params`:参数定义节点。
/// - `context`:变量上下文。
/// - `diagnostics`:诊断收集器。
/// - `dynamic`:是否动态模式。
/// - `break_at_position`:断点位置。
/// - `context_at_break`:断点上下文输出。
///
/// # 返回
/// 参数名称集合。
#[stacksafe::stacksafe]
fn analyze_tuple_params(
    params: &ASTNode,
    context: &mut VariableContext,
    diagnostics: &mut DiagnosticCollector,
    dynamic: bool,
    break_at_position: &Option<(Source, usize)>,
    context_at_break: &mut Option<VariableContext>,
) -> Option<HashSet<String>> {
    if context_at_break.is_some() {
        return None;
    }
    analyze_node(
        params,
        context,
        diagnostics,
        dynamic,
        break_at_position,
        context_at_break,
    );
    if context_at_break.is_some() {
        return None;
    }

    let mut param_names = HashSet::new();

    fn inner(node: &ASTNode, param_names: &mut HashSet<String>) {
        match &node.node_type {
            ASTNodeType::String(name) => {
                param_names.insert(name.clone());
            }
            ASTNodeType::Tuple => {
                for child in &node.children {
                    inner(child, param_names);
                }
            }
            ASTNodeType::Pair => {
                if let Some(key_node) = node.children.first() {
                    if let ASTNodeType::String(key_name) = &key_node.node_type {
                        param_names.insert(key_name.clone());
                    }
                }
            }
            _ => {}
        }
    }

    inner(params, &mut param_names);

    Some(param_names)
}

/// 自动捕获变量并重构 AST。
///
/// # 参数
/// - `node`:要分析的 AST 节点。
///
/// # 返回
/// (需要捕获的变量集合, 重构后的 AST)
pub fn auto_capture_and_rebuild(node: &ASTNode) -> (HashSet<String>, ASTNode) {
    let mut context = VariableContext::new();
    context.push_context();
    for var in PRE_ALLOCATED_VARIABLE_STRINGS {
        context.define_variable(
            var.to_string(),
            Variable {
                assumed_type: AssumedType::Unknown,
            },
        );
    }
    auto_capture(&mut context, node, false)
}

/// 自动捕获变量分析的递归实现。
///
/// # 参数
/// - `context`:变量上下文。
/// - `node`:当前 AST 节点。
/// - `dynamic`:是否动态模式。
///
/// # 返回
/// (需要捕获的变量集合, 重构后的 AST 节点)
#[stacksafe::stacksafe]
pub fn auto_capture(
    context: &mut VariableContext,
    node: &ASTNode,
    dynamic: bool,
) -> (HashSet<String>, ASTNode) {
    let empty_result = || (HashSet::new(), node.clone());

    match &node.node_type {
        ASTNodeType::Variable(var_name) => {
            if dynamic || context.get_variable_current_context(var_name).is_some() {
                return (HashSet::new(), node.clone());
            }
            let mut required_vars = HashSet::new();
            required_vars.insert(var_name.clone());
            (required_vars, node.clone())
        }

        ASTNodeType::Let(name) => {
            if node.children.len() != 1 {
                return empty_result();
            } // Robustness check
            let value_node = &node.children[0];

            let (value_req_vars, new_value_node) = auto_capture(context, value_node, dynamic);

            context.define_variable(
                name.clone(),
                Variable {
                    assumed_type: AssumedType::Unknown,
                },
            );

            let mut new_node = node.clone();
            new_node.children = vec![new_value_node];
            (value_req_vars, new_node)
        }

        ASTNodeType::Frame => {
            if node.children.len() != 1 {
                return empty_result();
            } // Robustness check
            let child = &node.children[0];

            context.push_frame();
            let (child_req_vars, new_child_node) = auto_capture(context, child, dynamic);
            context.pop_frame().unwrap();

            let mut new_node = node.clone();
            new_node.children = vec![new_child_node];
            (child_req_vars, new_node)
        }

        ASTNodeType::LambdaDef(is_dynamic_gen, captured_vars) => {
            if node.children.len() != 2 {
                return empty_result();
            } // Robustness check
            let params = &node.children[0];
            let body = &node.children[1];

            let (mut required_vars, new_params_node) =
                auto_capture(context, params, *is_dynamic_gen);

            if !dynamic {
                for v in captured_vars {
                    if context.get_variable_current_context(v).is_none() {
                        required_vars.insert(v.clone());
                    }
                }
            }

            if *is_dynamic_gen {
                let (body_req_vars, new_body_node) = auto_capture(context, body, true);
                required_vars.extend(body_req_vars);
                let mut new_node = node.clone();
                new_node.children = vec![new_params_node, new_body_node];
                return (required_vars, new_node);
            }

            context.push_context();
            for var in PRE_ALLOCATED_VARIABLE_STRINGS {
                context.define_variable(
                    var.to_string(),
                    Variable {
                        assumed_type: AssumedType::Unknown,
                    },
                );
            }

            for var in captured_vars {
                context.define_variable(
                    var.clone(),
                    Variable {
                        assumed_type: AssumedType::Unknown,
                    },
                );
            }

            fn define_params(context: &mut VariableContext, params: &ASTNode) {
                match &params.node_type {
                    ASTNodeType::String(var_name) => context.define_variable(
                        var_name.clone(),
                        Variable {
                            assumed_type: AssumedType::Unknown,
                        },
                    ),
                    ASTNodeType::Tuple => {
                        for param in &params.children {
                            define_params(context, param);
                        }
                    }
                    ASTNodeType::Pair => {
                        if let Some(key_node) = params.children.first() {
                            if let ASTNodeType::String(var_name) = &key_node.node_type {
                                context.define_variable(
                                    var_name.clone(),
                                    Variable {
                                        assumed_type: AssumedType::Unknown,
                                    },
                                )
                            }
                        }
                    }
                    _ => (),
                }
            }

            // Define parameters in the new context
            define_params(context, params);
            let (body_req_vars, new_body_node) = auto_capture(context, body, false);
            context.pop_context().unwrap();

            for var in &body_req_vars {
                if context.get_variable_current_context(var).is_none() {
                    required_vars.insert(var.clone());
                }
            }

            let mut final_captured = captured_vars.clone();
            final_captured.extend(body_req_vars);

            let rebuilt_ast_node = ASTNode {
                node_type: ASTNodeType::LambdaDef(*is_dynamic_gen, final_captured),
                source_location: node.source_location.clone(),
                children: vec![new_params_node, new_body_node],
            };
            (required_vars, rebuilt_ast_node)
        }

        ASTNodeType::Dynamic | ASTNodeType::Static => {
            let dynamic_flag = node.node_type == ASTNodeType::Dynamic;
            let mut required_vars = HashSet::new();
            let mut new_children = Vec::with_capacity(node.children.len());
            for child in &node.children {
                let (child_req_vars, new_child_node) = auto_capture(context, child, dynamic_flag);
                required_vars.extend(child_req_vars);
                new_children.push(new_child_node);
            }
            let mut new_node = node.clone();
            new_node.children = new_children;
            (required_vars, new_node)
        }
        _ => {
            let mut required_vars = HashSet::new();
            let mut new_children = Vec::with_capacity(node.children.len());
            for child in &node.children {
                let (child_req_vars, new_child_node) = auto_capture(context, child, dynamic);
                required_vars.extend(child_req_vars);
                new_children.push(new_child_node);
            }
            let mut new_node = node.clone();
            new_node.children = new_children;
            (required_vars, new_node)
        }
    }
}