Skip to main content

compiler/
lib.rs

1pub mod infer;
2mod symbol;
3use dynamic::{Dynamic, Type};
4use indexmap::IndexMap;
5use parser::{BinaryOp, Expr, ExprKind, Parser, Pattern, PatternKind, Span, Stmt, StmtKind};
6use smol_str::SmolStr;
7use std::{
8    collections::{BTreeMap, BTreeSet},
9    path::{Path, PathBuf},
10    sync::Arc,
11};
12pub use symbol::{Symbol, SymbolTable, eval_const_int_type, substitute_type};
13
14#[derive(Clone)]
15enum FnInferRet {
16    Pending(Option<Type>),
17    Done(Type),
18}
19
20#[derive(Clone)]
21pub enum ListElemState {
22    Unknown,
23    Known(Type),
24    Mixed,
25}
26
27#[derive(Clone)]
28pub struct Compiler {
29    pub symbols: SymbolTable,
30    pub frames: Vec<usize>,
31    pub tys: Vec<Type>,
32    /// 编译期常量表:键是稳定的 SmolStr 名字(通常来自字面量文本或字段名),
33    /// 值是 Dynamic。`ExprKind::Const(idx)` 中的 idx 是 IndexMap 中的位置,
34    /// 在单次编译中稳定;热重载场景下同一名字会拿到同一 idx,跨编译不保证。
35    pub consts: IndexMap<SmolStr, Dynamic>,
36    names: Vec<SmolStr>,
37    list_elem_states: Vec<Option<ListElemState>>,
38    arg_counts: Vec<usize>,
39    fns: BTreeMap<u32, Vec<(Vec<Type>, Vec<Type>, FnInferRet)>>,
40    local_type_hints: BTreeMap<u32, Vec<(Vec<Type>, Vec<Type>, Vec<Option<Type>>)>>,
41    infer_stack: Vec<(u32, Vec<Type>, Vec<Type>)>,
42    importing_paths: BTreeSet<PathBuf>,
43    source_files: BTreeMap<SmolStr, SourceFile>,
44}
45
46#[derive(Clone)]
47pub struct SourceFile {
48    pub path: Option<PathBuf>,
49    pub code: Arc<Vec<u8>>,
50}
51
52fn impl_target_name(target: &Type) -> anyhow::Result<SmolStr> {
53    match target {
54        Type::Ident { name, .. } => Ok(name.clone()),
55        _ => anyhow::bail!("impl 目标类型暂不支持: {:?}", target),
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::{Compiler, Symbol};
62    use dynamic::Type;
63
64    #[test]
65    fn inferred_function_return_type_is_written_back_to_symbol() -> anyhow::Result<()> {
66        let mut compiler = Compiler::new();
67        compiler.import_code(
68            "compiler_infer_return",
69            br#"
70            pub fn is_alive() {
71                true
72            }
73
74            pub fn can_act() {
75                is_alive() && true && is_alive()
76            }
77            "#
78            .to_vec(),
79        )?;
80
81        let is_alive = compiler.symbols.get_id("compiler_infer_return::is_alive")?;
82        assert_eq!(compiler.infer_fn(is_alive, &[])?, Type::Bool);
83
84        let (_, symbol) = compiler.symbols.get_symbol(is_alive)?;
85        let Symbol::Fn { ty: Type::Fn { ret, .. }, .. } = symbol else {
86            panic!("is_alive should be a function symbol");
87        };
88        assert_eq!(ret.as_ref(), &Type::Bool);
89
90        let can_act = compiler.symbols.get_id("compiler_infer_return::can_act")?;
91        assert_eq!(compiler.infer_fn(can_act, &[])?, Type::Bool);
92        Ok(())
93    }
94
95    #[test]
96    fn top_level_const_composite_resolves_const_idents() -> anyhow::Result<()> {
97        let mut compiler = Compiler::new();
98        compiler.import_code(
99            "compiler_const_table",
100            br#"
101            pub const GEM_ATK = "atk";
102            pub const GEM_DEF = "def";
103            pub const GEM_TABLE = [
104                { key: GEM_ATK, score: 3i32 },
105                { key: GEM_DEF, score: 1i32 },
106            ];
107            "#
108            .to_vec(),
109        )?;
110
111        let table = compiler.symbols.get_id("compiler_const_table::GEM_TABLE")?;
112        let (_, symbol) = compiler.symbols.get_symbol(table)?;
113        let Symbol::Const { value, .. } = symbol else {
114            panic!("GEM_TABLE should be a const symbol");
115        };
116
117        let first = value.get_idx(0).expect("first table row");
118        assert_eq!(first.get_dynamic("key").expect("key").as_str(), "atk");
119        assert_eq!(first.get_dynamic("score").expect("score").as_int(), Some(3));
120        Ok(())
121    }
122
123    #[test]
124    fn const_unary_neg_handles_min_integer_literal() -> anyhow::Result<()> {
125        let mut compiler = Compiler::new();
126        compiler.import_code(
127            "compiler_const_min_int",
128            br#"
129            pub const MIN_I32: i32 = -2147483648i32;
130            "#
131            .to_vec(),
132        )?;
133
134        let id = compiler.symbols.get_id("compiler_const_min_int::MIN_I32")?;
135        let (_, symbol) = compiler.symbols.get_symbol(id)?;
136        let Symbol::Const { value, .. } = symbol else {
137            panic!("MIN_I32 should be a const symbol");
138        };
139        assert_eq!(value.as_int(), Some(i32::MIN as i64));
140        Ok(())
141    }
142
143    #[test]
144    fn return_check_resolves_function_args_before_body_compile() -> anyhow::Result<()> {
145        let mut compiler = Compiler::new();
146        compiler.import_code(
147            "compiler_return_check_args",
148            br#"
149            pub fn no_value_return(flag: bool) {
150                if flag {
151                    return;
152                }
153            }
154
155            pub fn tail_if(flag: bool) {
156                if flag {
157                    1
158                } else {
159                    2
160                }
161            }
162
163            pub fn loop_index(low: i64, high: i64) {
164                let total = 0i64;
165                for i in low..high {
166                    total += i;
167                }
168                total
169            }
170
171            pub fn closure_capture() {
172                let base = 10i32;
173                let add_base = |value: i32| {
174                    value + base
175                };
176                add_base(1i32)
177            }
178
179            pub fn destructured_names() {
180                let (left, right) = (3i32, 4i32);
181                let [first, second] = [5i32, 6i32];
182                let _ = first;
183                left + right + second
184            }
185            "#
186            .to_vec(),
187        )?;
188
189        let no_value_return = compiler.symbols.get_id("compiler_return_check_args::no_value_return")?;
190        assert_eq!(compiler.infer_fn(no_value_return, &[Type::Bool])?, Type::Void);
191
192        let tail_if = compiler.symbols.get_id("compiler_return_check_args::tail_if")?;
193        // 无后缀整数字面量默认 I64
194        assert_eq!(compiler.infer_fn(tail_if, &[Type::Bool])?, Type::I64);
195
196        let loop_index = compiler.symbols.get_id("compiler_return_check_args::loop_index")?;
197        assert_eq!(compiler.infer_fn(loop_index, &[Type::I64, Type::I64])?, Type::I64);
198
199        Ok(())
200    }
201
202    #[test]
203    fn return_check_infers_raw_assoc_calls_before_body_compile() -> anyhow::Result<()> {
204        let mut compiler = Compiler::new();
205        compiler.import_code(
206            "compiler_return_check_assoc",
207            br#"
208            pub struct Box<N> {
209                data: [u32; N],
210            }
211
212            impl Box<N> {
213                pub fn make() {
214                    Box<N>{ data: [0u32; N] }
215                }
216
217                pub fn ok(self: Box<N>) {
218                    true
219                }
220            }
221
222            pub fn main() {
223                let item = Box<2>::make();
224                if item.ok() {
225                    1i32
226                } else {
227                    0i32
228                }
229            }
230            "#
231            .to_vec(),
232        )?;
233
234        let main_id = compiler.symbols.get_id("compiler_return_check_assoc::main")?;
235        assert_eq!(compiler.infer_fn(main_id, &[])?, Type::I32);
236        Ok(())
237    }
238
239    #[test]
240    fn forward_function_call_in_bool_condition_infers_callee_first() -> anyhow::Result<()> {
241        let mut compiler = Compiler::new();
242        compiler.import_code(
243            "compiler_forward_bool",
244            br#"
245            pub fn can_start() {
246                if is_ready() {
247                    return true;
248                }
249                false
250            }
251
252            pub fn is_ready() {
253                true
254            }
255            "#
256            .to_vec(),
257        )?;
258
259        let can_start = compiler.symbols.get_id("compiler_forward_bool::can_start")?;
260        assert_eq!(compiler.infer_fn(can_start, &[])?, Type::Bool);
261
262        let is_ready = compiler.symbols.get_id("compiler_forward_bool::is_ready")?;
263        assert_eq!(compiler.infer_fn(is_ready, &[])?, Type::Bool);
264        Ok(())
265    }
266
267    #[test]
268    fn inferred_return_cache_keeps_pending_separate_from_any() -> anyhow::Result<()> {
269        let mut compiler = Compiler::new();
270        compiler.import_code(
271            "compiler_pending_any",
272            br#"
273            pub fn dynamic_value(value) {
274                value
275            }
276
277            pub fn bool_value() {
278                true
279            }
280            "#
281            .to_vec(),
282        )?;
283
284        let dynamic_value = compiler.symbols.get_id("compiler_pending_any::dynamic_value")?;
285        assert_eq!(compiler.infer_fn(dynamic_value, &[Type::Any])?, Type::Any);
286
287        let bool_value = compiler.symbols.get_id("compiler_pending_any::bool_value")?;
288        assert_eq!(compiler.infer_fn(bool_value, &[])?, Type::Bool);
289        Ok(())
290    }
291
292    #[test]
293    fn recursive_function_uses_inferred_return_seed() -> anyhow::Result<()> {
294        let mut compiler = Compiler::new();
295        compiler.import_code(
296            "compiler_recursive_return",
297            br#"
298            pub fn factorial(n: i64) {
299                if n <= 1 {
300                    return 1;
301                }
302                n * factorial(n - 1)
303            }
304
305            pub fn factorial_reversed(n: i64) {
306                if n > 1 {
307                    return n * factorial_reversed(n - 1);
308                }
309                1
310            }
311            "#
312            .to_vec(),
313        )?;
314
315        let factorial = compiler.symbols.get_id("compiler_recursive_return::factorial")?;
316        assert_eq!(compiler.infer_fn(factorial, &[Type::I64])?, Type::I64);
317
318        let factorial_reversed = compiler.symbols.get_id("compiler_recursive_return::factorial_reversed")?;
319        assert_eq!(compiler.infer_fn(factorial_reversed, &[Type::I64])?, Type::I64);
320        Ok(())
321    }
322
323    #[test]
324    fn generic_function_infers_type_param_from_arg() -> anyhow::Result<()> {
325        let mut compiler = Compiler::new();
326        compiler.import_code(
327            "compiler_generic_identity",
328            br#"
329            pub fn identity<T>(value: T) {
330                value
331            }
332            "#
333            .to_vec(),
334        )?;
335
336        let identity = compiler.symbols.get_id("compiler_generic_identity::identity")?;
337        assert_eq!(compiler.infer_fn(identity, &[Type::I64])?, Type::I64);
338        assert_eq!(compiler.infer_fn(identity, &[Type::Bool])?, Type::Bool);
339        Ok(())
340    }
341
342    #[test]
343    fn generic_function_uses_explicit_const_param() -> anyhow::Result<()> {
344        let mut compiler = Compiler::new();
345        compiler.import_code(
346            "compiler_generic_const",
347            br#"
348            pub fn value<N>() {
349                N
350            }
351            "#
352            .to_vec(),
353        )?;
354
355        let value = compiler.symbols.get_id("compiler_generic_const::value")?;
356        assert_eq!(compiler.infer_fn_with_params(value, &[], &[Type::ConstInt(7)])?, Type::I32);
357        Ok(())
358    }
359
360    #[test]
361    fn generic_function_infers_const_param_from_array_len() -> anyhow::Result<()> {
362        let mut compiler = Compiler::new();
363        compiler.import_code(
364            "compiler_generic_array_len",
365            br#"
366            pub fn len<N>(items: [i32; N]) {
367                N
368            }
369            "#
370            .to_vec(),
371        )?;
372
373        let len = compiler.symbols.get_id("compiler_generic_array_len::len")?;
374        assert_eq!(compiler.infer_fn(len, &[Type::Array(std::rc::Rc::new(Type::I32), 3)])?, Type::I32);
375        Ok(())
376    }
377
378    #[test]
379    fn generic_function_reports_uninferred_param() -> anyhow::Result<()> {
380        let mut compiler = Compiler::new();
381        compiler.import_code(
382            "compiler_generic_uninferred",
383            br#"
384            pub fn value<T>() {
385                1i32
386            }
387            "#
388            .to_vec(),
389        )?;
390
391        let value = compiler.symbols.get_id("compiler_generic_uninferred::value")?;
392        let err = compiler.infer_fn(value, &[]).expect_err("generic parameter should not be inferred");
393        assert!(format!("{err:#}").contains("无法从实参类型推断函数范型参数"));
394        Ok(())
395    }
396
397    #[test]
398    fn assignment_target_type_keeps_dynamic_index_sum_static() -> anyhow::Result<()> {
399        let mut compiler = Compiler::new();
400        compiler.import_code(
401            "compiler_dynamic_index_sum",
402            br#"
403            pub fn sum_list(n: i64) {
404                let l = [];
405                for i in 0..n {
406                    l.push(i);
407                }
408                let sum = 0i64;
409                for i in 0..n {
410                    sum = sum + l[i];
411                }
412                sum
413            }
414            "#
415            .to_vec(),
416        )?;
417
418        let sum_list = compiler.symbols.get_id("compiler_dynamic_index_sum::sum_list")?;
419        assert_eq!(compiler.infer_fn(sum_list, &[Type::I64])?, Type::I64);
420        Ok(())
421    }
422
423    #[test]
424    fn list_literal_infers_element_type() -> anyhow::Result<()> {
425        let mut compiler = Compiler::new();
426        compiler.import_code(
427            "compiler_list_elem_type",
428            br#"
429            pub fn pushed_empty() {
430                let items = [];
431                items.push(1i64);
432                items[0]
433            }
434
435            pub fn ints() {
436                [1i64, 2i64]
437            }
438
439            pub fn mixed_then_int() {
440                let items = [];
441                items.push(1i64);
442                items.push("aaa");
443                items.push(2i64);
444                items[0]
445            }
446
447            "#
448            .to_vec(),
449        )?;
450
451        let pushed_empty = compiler.symbols.get_id("compiler_list_elem_type::pushed_empty")?;
452        assert_eq!(compiler.infer_fn(pushed_empty, &[])?, Type::I64);
453        let hints = compiler.inferred_local_type_hints(pushed_empty, &[], &[]);
454        assert_eq!(hints.first().cloned().flatten(), Some(Type::List(std::rc::Rc::new(Type::I64))));
455
456        let ints = compiler.symbols.get_id("compiler_list_elem_type::ints")?;
457        assert_eq!(compiler.infer_fn(ints, &[])?, Type::Any);
458
459        let mixed_then_int = compiler.symbols.get_id("compiler_list_elem_type::mixed_then_int")?;
460        assert_eq!(compiler.infer_fn(mixed_then_int, &[])?, Type::Any);
461        let hints = compiler.inferred_local_type_hints(mixed_then_int, &[], &[]);
462        assert_eq!(hints.first().cloned().flatten(), None);
463        Ok(())
464    }
465
466    #[test]
467    fn return_map_and_struct_is_type_error() -> anyhow::Result<()> {
468        let mut compiler = Compiler::new();
469        let err = match compiler.import_code(
470            "compiler_return_map_struct",
471            br#"
472            struct S {
473                hp: i32,
474            }
475
476            pub fn make_s_or_error(flag: i32) {
477                if flag == 0 {
478                    return { error: "bad" };
479                }
480                S{hp: 123}
481            }
482            "#
483            .to_vec(),
484        ) {
485            Ok(_) => panic!("expected mismatched return types to fail"),
486            Err(err) => err,
487        };
488
489        assert!(format!("{err:#}").contains("返回类型不一致"));
490        Ok(())
491    }
492
493    #[test]
494    fn unknown_function_call_strict_reports_error() -> anyhow::Result<()> {
495        let mut compiler = Compiler::new();
496        let err = match compiler.import_code(
497            "compiler_unknown_fn",
498            br#"
499            pub fn call_typo() {
500                prnt("hi")
501            }
502            "#
503            .to_vec(),
504        ) {
505            Ok(_) => panic!("expected unknown function to fail"),
506            Err(err) => err,
507        };
508
509        let msg = format!("{err:#}");
510        assert!(msg.contains("未注册函数"), "got: {msg}");
511        Ok(())
512    }
513}
514
515fn has_unresolved_generic_param(ty: &Type) -> bool {
516    match ty {
517        Type::Ident { name, params } => {
518            if params.is_empty() {
519                name.chars().next().map(|ch| ch.is_ascii_uppercase()).unwrap_or(false)
520            } else {
521                params.iter().any(has_unresolved_generic_param)
522            }
523        }
524        Type::Struct { params, fields } => params.iter().any(has_unresolved_generic_param) || fields.iter().any(|(_, ty)| has_unresolved_generic_param(ty)),
525        Type::Tuple(items) => items.iter().any(has_unresolved_generic_param),
526        Type::List(elem) | Type::Vec(elem, _) | Type::Array(elem, _) => has_unresolved_generic_param(elem),
527        Type::ArrayParam(elem, len) => has_unresolved_generic_param(elem) || has_unresolved_generic_param(len),
528        Type::Fn { tys, ret } => tys.iter().any(has_unresolved_generic_param) || has_unresolved_generic_param(ret),
529        Type::Symbol { params, .. } => params.iter().any(has_unresolved_generic_param),
530        Type::ConstBinary { left, right, .. } => has_unresolved_generic_param(left) || has_unresolved_generic_param(right),
531        _ => false,
532    }
533}
534
535fn is_top_level_import_expr(expr: &Expr) -> bool {
536    matches!(
537        &expr.kind,
538        ExprKind::Call { obj, .. } if matches!(&obj.kind, ExprKind::Ident(name) if name.as_str() == "import")
539    )
540}
541
542fn string_value(expr: &Expr) -> Option<&str> {
543    if let ExprKind::Value(Dynamic::String(value)) = &expr.kind { Some(value.as_str()) } else { None }
544}
545
546fn import_decl(stmt: &Stmt) -> Option<(SmolStr, SmolStr)> {
547    let StmtKind::Expr(expr, _) = &stmt.kind else {
548        return None;
549    };
550    let ExprKind::Call { obj, params } = &expr.kind else {
551        return None;
552    };
553    let ExprKind::Ident(name) = &obj.kind else {
554        return None;
555    };
556    if name.as_str() != "import" {
557        return None;
558    }
559
560    match params.as_slice() {
561        [module, path] => Some((string_value(module)?.into(), string_value(path)?.into())),
562        [module] => match &module.kind {
563            ExprKind::Value(Dynamic::String(value)) => Some((value.clone(), format!("{value}.zs").into())),
564            ExprKind::Ident(value) => Some((value.clone(), format!("{value}.zs").into())),
565            _ => None,
566        },
567        _ => None,
568    }
569}
570
571fn generic_arg_for_name<'a>(name: &str, params: &'a [Type], args: &'a [Type]) -> Option<&'a Type> {
572    params.iter().position(|param| matches!(param, Type::Ident { name: param_name, params } if params.is_empty() && param_name == name)).and_then(|idx| args.get(idx))
573}
574
575pub fn infer_generic_args_from_types(generic_params: &[Type], decl_tys: &[Type], arg_tys: &[Type]) -> Vec<Type> {
576    if generic_params.is_empty() {
577        return Vec::new();
578    }
579    let mut inferred = vec![None; generic_params.len()];
580    for (decl, actual) in decl_tys.iter().zip(arg_tys.iter()) {
581        infer_generic_arg_from_type(generic_params, decl, actual, &mut inferred);
582    }
583    if inferred.iter().all(|item| item.is_some()) {
584        return inferred.into_iter().map(Option::unwrap).collect();
585    }
586    if let Some(Type::Struct { params, .. }) = arg_tys.iter().find(|ty| matches!(ty, Type::Struct { params, .. } if params.len() == generic_params.len())) {
587        return params.clone();
588    }
589    for (decl, actual) in decl_tys.iter().zip(arg_tys.iter()) {
590        if let (Type::Ident { params: decl_params, .. }, Type::Ident { params: actual_params, .. }) = (decl, actual)
591            && decl_params.len() == actual_params.len()
592            && decl_params.iter().any(|param| generic_params.contains(param))
593        {
594            return actual_params.clone();
595        }
596    }
597    Vec::new()
598}
599
600pub fn resolve_generic_args_from_types(generic_params: &[Type], decl_tys: &[Type], arg_tys: &[Type], explicit_args: &[Type]) -> Result<Vec<Type>> {
601    if generic_params.is_empty() {
602        if explicit_args.is_empty() {
603            return Ok(Vec::new());
604        }
605        return Err(anyhow!("函数不接受范型参数,但传入了 {}", explicit_args.len()));
606    }
607    if !explicit_args.is_empty() {
608        if explicit_args.len() == generic_params.len() {
609            return Ok(explicit_args.to_vec());
610        }
611        return Err(anyhow!("函数范型参数数量不匹配,期望 {} 个,实际 {} 个", generic_params.len(), explicit_args.len()));
612    }
613
614    let inferred = infer_generic_args_from_types(generic_params, decl_tys, arg_tys);
615    if inferred.len() == generic_params.len() {
616        Ok(inferred)
617    } else if generic_params.len() == 1
618        && let Some(Type::List(elem) | Type::Vec(elem, _) | Type::Array(elem, _)) = arg_tys.first()
619    {
620        Ok(vec![elem.as_ref().clone()])
621    } else {
622        Err(anyhow!("无法从实参类型推断函数范型参数 {:?}", generic_params))
623    }
624}
625
626fn infer_generic_arg_from_type(generic_params: &[Type], decl: &Type, actual: &Type, inferred: &mut [Option<Type>]) {
627    if let Some(idx) = generic_params.iter().position(|param| param == decl) {
628        inferred[idx] = Some(actual.clone());
629        return;
630    }
631
632    match (decl, actual) {
633        (Type::List(decl_elem), Type::List(actual_elem)) => {
634            infer_generic_arg_from_type(generic_params, decl_elem, actual_elem, inferred);
635        }
636        (Type::Vec(decl_elem, decl_len), Type::Vec(actual_elem, actual_len)) | (Type::Array(decl_elem, decl_len), Type::Array(actual_elem, actual_len)) => {
637            infer_generic_arg_from_type(generic_params, decl_elem, actual_elem, inferred);
638            infer_generic_arg_from_type(generic_params, &Type::ConstInt(*decl_len as i64), &Type::ConstInt(*actual_len as i64), inferred);
639        }
640        (Type::ArrayParam(decl_elem, decl_len), Type::Array(actual_elem, actual_len)) => {
641            infer_generic_arg_from_type(generic_params, decl_elem, actual_elem, inferred);
642            infer_generic_arg_from_type(generic_params, decl_len, &Type::ConstInt(*actual_len as i64), inferred);
643        }
644        (Type::Ident { params: decl_params, .. }, Type::Ident { params: actual_params, .. })
645        | (Type::Ident { params: decl_params, .. }, Type::Symbol { params: actual_params, .. })
646        | (Type::Symbol { params: decl_params, .. }, Type::Symbol { params: actual_params, .. })
647        | (Type::Symbol { params: decl_params, .. }, Type::Ident { params: actual_params, .. })
648        | (Type::Struct { params: decl_params, .. }, Type::Struct { params: actual_params, .. }) => {
649            for (decl, actual) in decl_params.iter().zip(actual_params.iter()) {
650                infer_generic_arg_from_type(generic_params, decl, actual, inferred);
651            }
652        }
653        _ => {}
654    }
655}
656
657fn substitute_pattern(pattern: &Pattern, params: &[Type], args: &[Type]) -> Pattern {
658    let kind = match &pattern.kind {
659        PatternKind::Ident { name, ty } => PatternKind::Ident { name: name.clone(), ty: substitute_type(ty, params, args) },
660        PatternKind::Var { idx, ty } => PatternKind::Var { idx: *idx, ty: substitute_type(ty, params, args) },
661        PatternKind::Tuple(items) => PatternKind::Tuple(items.iter().map(|item| substitute_pattern(item, params, args)).collect()),
662        PatternKind::List { elems, has_rest } => PatternKind::List { elems: elems.iter().map(|item| substitute_pattern(item, params, args)).collect(), has_rest: *has_rest },
663        other => other.clone(),
664    };
665    Pattern { kind, span: pattern.span }
666}
667
668fn substitute_expr(expr: &Expr, params: &[Type], args: &[Type]) -> Expr {
669    let kind = match &expr.kind {
670        ExprKind::Ident(name) => match generic_arg_for_name(name, params, args) {
671            Some(Type::ConstInt(value)) => ExprKind::Value(Dynamic::I32(*value as i32)),
672            Some(ty) => eval_const_int_type(ty).map(|value| ExprKind::Value(Dynamic::I32(value as i32))).unwrap_or_else(|| expr.kind.clone()),
673            _ => expr.kind.clone(),
674        },
675        ExprKind::Typed { value, ty } => ExprKind::Typed { value: Box::new(substitute_expr(value, params, args)), ty: substitute_type(ty, params, args) },
676        ExprKind::Unary { op, value } => ExprKind::Unary { op: op.clone(), value: Box::new(substitute_expr(value, params, args)) },
677        ExprKind::Binary { left, op, right } => ExprKind::Binary { left: Box::new(substitute_expr(left, params, args)), op: op.clone(), right: Box::new(substitute_expr(right, params, args)) },
678        ExprKind::Generic { obj, params: nested } => ExprKind::Generic { obj: Box::new(substitute_expr(obj, params, args)), params: nested.iter().map(|param| substitute_type(param, params, args)).collect() },
679        ExprKind::Assoc { ty, name } => ExprKind::Assoc { ty: substitute_type(ty, params, args), name: name.clone() },
680        ExprKind::TypedMethod { obj, ty, name } => ExprKind::TypedMethod { obj: Box::new(substitute_expr(obj, params, args)), ty: substitute_type(ty, params, args), name: name.clone() },
681        ExprKind::AssocId { id, params: nested } => ExprKind::AssocId { id: *id, params: nested.iter().map(|param| substitute_type(param, params, args)).collect() },
682        ExprKind::Tuple(items) => ExprKind::Tuple(items.iter().map(|item| substitute_expr(item, params, args)).collect()),
683        ExprKind::List(items) => ExprKind::List(items.iter().map(|item| substitute_expr(item, params, args)).collect()),
684        ExprKind::Repeat { value, len } => ExprKind::Repeat { value: Box::new(substitute_expr(value, params, args)), len: substitute_type(len, params, args) },
685        ExprKind::Dict(items) => ExprKind::Dict(items.iter().map(|(name, value)| (name.clone(), substitute_expr(value, params, args))).collect()),
686        ExprKind::Range { start, stop, inclusive } => ExprKind::Range { start: Box::new(substitute_expr(start, params, args)), stop: Box::new(substitute_expr(stop, params, args)), inclusive: *inclusive },
687        ExprKind::Call { obj, params: call_params } => ExprKind::Call { obj: Box::new(substitute_expr(obj, params, args)), params: call_params.iter().map(|param| substitute_expr(param, params, args)).collect() },
688        ExprKind::Stmt(stmt) => ExprKind::Stmt(Box::new(substitute_stmt(stmt, params, args))),
689        ExprKind::Closure { args: closure_args, body } => {
690            ExprKind::Closure { args: closure_args.iter().map(|(name, ty)| (name.clone(), substitute_type(ty, params, args))).collect(), body: Box::new(substitute_stmt(body, params, args)) }
691        }
692        _ => expr.kind.clone(),
693    };
694    Expr::new(kind, expr.span)
695}
696
697pub fn substitute_stmt(stmt: &Stmt, params: &[Type], args: &[Type]) -> Stmt {
698    let kind = match &stmt.kind {
699        StmtKind::Let { pat, value } => StmtKind::Let { pat: substitute_pattern(pat, params, args), value: Box::new(substitute_stmt(value, params, args)) },
700        StmtKind::Expr(expr, close) => StmtKind::Expr(substitute_expr(expr, params, args), *close),
701        StmtKind::Block(stmts) => StmtKind::Block(stmts.iter().map(|stmt| substitute_stmt(stmt, params, args)).collect()),
702        StmtKind::Return(expr) => StmtKind::Return(expr.as_ref().map(|expr| substitute_expr(expr, params, args))),
703        StmtKind::While { cond, body } => StmtKind::While { cond: substitute_expr(cond, params, args), body: Box::new(substitute_stmt(body, params, args)) },
704        StmtKind::Loop(body) => StmtKind::Loop(Box::new(substitute_stmt(body, params, args))),
705        StmtKind::For { pat, range, body } => StmtKind::For { pat: substitute_pattern(pat, params, args), range: substitute_expr(range, params, args), body: Box::new(substitute_stmt(body, params, args)) },
706        StmtKind::Fn { name, generic_params, args: fn_args, body, is_pub } => StmtKind::Fn {
707            name: name.clone(),
708            generic_params: generic_params.iter().map(|param| substitute_type(param, params, args)).collect(),
709            args: fn_args.iter().map(|(name, ty)| (name.clone(), substitute_type(ty, params, args))).collect(),
710            body: Box::new(substitute_stmt(body, params, args)),
711            is_pub: *is_pub,
712        },
713        StmtKind::Struct { name, def, is_pub } => StmtKind::Struct { name: name.clone(), def: substitute_type(def, params, args), is_pub: *is_pub },
714        StmtKind::Impl { target, body } => StmtKind::Impl { target: substitute_type(target, params, args), body: Box::new(substitute_stmt(body, params, args)) },
715        StmtKind::If { cond, then_body, else_body } => StmtKind::If {
716            cond: substitute_expr(cond, params, args),
717            then_body: Box::new(substitute_stmt(then_body, params, args)),
718            else_body: else_body.as_ref().map(|body| Box::new(substitute_stmt(body, params, args))),
719        },
720        StmtKind::Static { name, ty, value, is_pub } => {
721            StmtKind::Static { name: name.clone(), ty: substitute_type(ty, params, args), value: value.as_ref().map(|value| substitute_expr(value, params, args)), is_pub: *is_pub }
722        }
723        StmtKind::Const { name, ty, value, is_pub } => StmtKind::Const { name: name.clone(), ty: substitute_type(ty, params, args), value: substitute_expr(value, params, args), is_pub: *is_pub },
724        other => other.clone(),
725    };
726    Stmt::new(kind, stmt.span)
727}
728
729#[derive(Debug, Clone, Default)]
730pub struct Capture {
731    pub names: Vec<(SmolStr, Type)>,
732    pub vars: Vec<usize>,
733}
734
735impl Capture {
736    pub fn new(names: Vec<(SmolStr, Type)>) -> Self {
737        Self { names, vars: Vec::new() }
738    }
739
740    pub fn get(&mut self, name: &str) -> Option<usize> {
741        if let Some(idx) = self.names.iter().position(|n| n.0 == name) {
742            if let Some(pos) = self.vars.iter().position(|v| *v == idx) {
743                Some(pos)
744            } else {
745                self.vars.push(idx);
746                Some(self.vars.len() - 1)
747            }
748        } else {
749            None
750        }
751    }
752
753    pub fn get_type(&self, idx: u32) -> Option<Type> {
754        self.names.get(idx as usize).map(|(_, ty)| ty.clone())
755    }
756}
757
758use anyhow::{Context, Result, anyhow};
759use thiserror::Error;
760
761#[derive(Debug, Error)]
762#[error("{message}")]
763pub struct SpannedCompilerError {
764    pub message: String,
765    pub span: Span,
766}
767
768#[derive(Debug, Clone)]
769pub struct CompilerDiagnostic {
770    pub message: String,
771    pub span: Span,
772}
773
774impl Compiler {
775    pub fn clear(&mut self) {
776        self.frames.clear();
777        self.names.clear();
778        self.tys.clear();
779        self.list_elem_states.clear();
780        self.arg_counts.clear();
781    }
782
783    pub fn take_local_state(&mut self) -> (Vec<usize>, Vec<SmolStr>, Vec<Type>, Vec<Option<ListElemState>>, Vec<usize>) {
784        (std::mem::take(&mut self.frames), std::mem::take(&mut self.names), std::mem::take(&mut self.tys), std::mem::take(&mut self.list_elem_states), std::mem::take(&mut self.arg_counts))
785    }
786
787    pub fn restore_local_state(&mut self, state: (Vec<usize>, Vec<SmolStr>, Vec<Type>, Vec<Option<ListElemState>>, Vec<usize>)) {
788        self.frames = state.0;
789        self.names = state.1;
790        self.tys = state.2;
791        self.list_elem_states = state.3;
792        self.arg_counts = state.4;
793    }
794
795    pub fn get_value(&self, expr: &Expr) -> Option<Dynamic> {
796        match &expr.kind {
797            ExprKind::Value(v) => Some(v.clone()),
798            ExprKind::Const(idx) => self.consts.get_index(*idx).map(|(_, v)| v.clone()),
799            _ => None,
800        }
801    }
802
803    pub fn get_const(&mut self, value: Dynamic) -> usize {
804        let key: SmolStr = if value.is_str() {
805            format!("str:{}", value.as_str()).into()
806        } else if value.is_null() {
807            "null".into()
808        } else {
809            format!("{value:?}").into()
810        };
811        if let Some((idx, _, _)) = self.consts.get_full(&key) {
812            return idx;
813        }
814        self.consts.insert_full(key, value).0
815    }
816
817    fn normalize_self_assign(left: Expr, op: BinaryOp, right: Expr, span: Span, arg_count: usize) -> Expr {
818        if let Some(idx) = left.var()
819            && (idx as usize) < arg_count
820        {
821            let base_op = match op {
822                BinaryOp::AddAssign => Some(BinaryOp::Add),
823                BinaryOp::SubAssign => Some(BinaryOp::Sub),
824                BinaryOp::MulAssign => Some(BinaryOp::Mul),
825                BinaryOp::DivAssign => Some(BinaryOp::Div),
826                BinaryOp::ModAssign => Some(BinaryOp::Mod),
827                BinaryOp::ShlAssign => Some(BinaryOp::Shl),
828                BinaryOp::ShrAssign => Some(BinaryOp::Shr),
829                BinaryOp::BitAndAssign => Some(BinaryOp::BitAnd),
830                BinaryOp::BitOrAssign => Some(BinaryOp::BitOr),
831                BinaryOp::BitXorAssign => Some(BinaryOp::BitXor),
832                _ => None,
833            };
834            if let Some(op) = base_op {
835                let right = Expr::new(ExprKind::Binary { left: Box::new(left.clone()), op, right: Box::new(right) }, span);
836                return Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Assign, right: Box::new(right) }, span);
837            }
838        }
839        if op == BinaryOp::Assign
840            && let Some(idx) = left.var()
841            && idx as usize >= arg_count
842            && let ExprKind::Binary { left: rhs_left, op: rhs_op, right: rhs_right } = &right.kind
843            && rhs_left.var() == Some(idx)
844        {
845            let compound_op = match rhs_op {
846                BinaryOp::Add => Some(BinaryOp::AddAssign),
847                BinaryOp::Sub => Some(BinaryOp::SubAssign),
848                BinaryOp::Mul => Some(BinaryOp::MulAssign),
849                BinaryOp::Div => Some(BinaryOp::DivAssign),
850                BinaryOp::Mod => Some(BinaryOp::ModAssign),
851                BinaryOp::Shl => Some(BinaryOp::ShlAssign),
852                BinaryOp::Shr => Some(BinaryOp::ShrAssign),
853                BinaryOp::BitAnd => Some(BinaryOp::BitAndAssign),
854                BinaryOp::BitOr => Some(BinaryOp::BitOrAssign),
855                BinaryOp::BitXor => Some(BinaryOp::BitXorAssign),
856                _ => None,
857            };
858            if let Some(op) = compound_op {
859                return Expr::new(ExprKind::Binary { left: Box::new(left), op, right: Box::new((**rhs_right).clone()) }, span);
860            }
861        }
862        Expr::new(ExprKind::Binary { left: Box::new(left), op, right: Box::new(right) }, span)
863    }
864
865    pub fn top(&self) -> usize {
866        self.frames.last().copied().unwrap_or(0)
867    }
868
869    fn add_name(&mut self, name: SmolStr) -> u32 {
870        self.names.push(name);
871        (self.names.len() - self.top() - 1) as u32
872    }
873
874    fn list_elem_state_for_ty(ty: &Type) -> Option<ListElemState> {
875        match ty {
876            Type::List(elem) if elem.is_any() => Some(ListElemState::Unknown),
877            Type::List(elem) => Some(ListElemState::Known(elem.as_ref().clone())),
878            _ => None,
879        }
880    }
881
882    pub(crate) fn list_elem_state(&self, idx: u32) -> Option<ListElemState> {
883        self.list_elem_states.get(self.top() + idx as usize).cloned().flatten()
884    }
885
886    pub(crate) fn set_list_elem_state(&mut self, idx: u32, state: Option<ListElemState>) {
887        let pos = idx as usize + self.top();
888        if self.list_elem_states.len() <= pos {
889            self.list_elem_states.resize(pos + 1, None);
890        }
891        self.list_elem_states[pos] = state;
892    }
893
894    fn add_ty(&mut self, ty: Type) -> u32 {
895        self.list_elem_states.push(Self::list_elem_state_for_ty(&ty));
896        self.tys.push(ty);
897        (self.tys.len() - self.top() - 1) as u32
898    }
899
900    fn set_ty(&mut self, idx: u32, ty: Type) {
901        let pos = idx as usize + self.top();
902        if self.list_elem_states.len() <= pos {
903            self.list_elem_states.resize(pos + 1, None);
904        }
905        self.list_elem_states[pos] = Self::list_elem_state_for_ty(&ty);
906        if pos < self.tys.len() {
907            self.tys[pos] = ty;
908        } else if pos == self.tys.len() {
909            self.tys.push(ty);
910        } else {
911            self.tys.resize(pos + 1, Type::Any);
912            self.tys[pos] = ty;
913        }
914    }
915
916    pub fn add_symbol(&mut self, name: &str, s: Symbol) -> u32 {
917        self.symbols.add(name.into(), s)
918    }
919
920    pub fn new() -> Self {
921        let symbols = SymbolTable::default();
922        Self {
923            symbols,
924            tys: Vec::new(),
925            names: Vec::new(),
926            consts: IndexMap::with_capacity(10240),
927            frames: Vec::new(),
928            list_elem_states: Vec::new(),
929            arg_counts: Vec::new(),
930            fns: BTreeMap::new(),
931            local_type_hints: BTreeMap::new(),
932            infer_stack: Vec::new(),
933            importing_paths: BTreeSet::new(),
934            source_files: BTreeMap::new(),
935        }
936    }
937
938    fn byte_to_line_col(src: &[u8], pos: usize) -> (usize, usize) {
939        let mut line = 1;
940        let mut col = 1;
941        for &b in src.iter().take(pos.min(src.len())) {
942            if b == b'\n' {
943                line += 1;
944                col = 1;
945            } else {
946                col += 1;
947            }
948        }
949        (line, col)
950    }
951
952    fn line_snippet(code: &[u8], span: Span) -> String {
953        let pos = span.start.min(code.len());
954        let line_start = code[..pos].iter().rposition(|&b| b == b'\n').map(|idx| idx + 1).unwrap_or(0);
955        let line_end = code[pos..].iter().position(|&b| b == b'\n').map(|idx| pos + idx).unwrap_or(code.len());
956        String::from_utf8_lossy(&code[line_start..line_end]).into_owned()
957    }
958
959    fn semantic_error(span: Span, message: impl Into<String>) -> anyhow::Error {
960        SpannedCompilerError { message: message.into(), span }.into()
961    }
962
963    fn format_compile_error(code: &[u8], err: anyhow::Error) -> anyhow::Error {
964        if let Some(err) = err.downcast_ref::<SpannedCompilerError>() {
965            return Self::format_span_error(code, err.span, &err.message);
966        }
967        if let Some(err) = err.downcast_ref::<parser::ParserErr>() {
968            return Self::format_span_error(code, err.span(), err.message());
969        }
970        if let Some(err) = err.downcast_ref::<parser::SpannedParseError>() {
971            let pos = err.pos.min(code.len());
972            let (line, col) = Self::byte_to_line_col(code, pos);
973            let snippet = Self::line_snippet(code, Span::new(pos, pos));
974            return anyhow!("解析错误:第 {line} 行,第 {col} 列(字节偏移 {pos}):{}\n{}", err.err, snippet);
975        }
976        err
977    }
978
979    fn format_span_error(code: &[u8], span: Span, message: &str) -> anyhow::Error {
980        let pos = span.start.min(code.len());
981        let (line, col) = Self::byte_to_line_col(code, pos);
982        let snippet = Self::line_snippet(code, span);
983        anyhow!("语义错误:第 {line} 行,第 {col} 列(字节偏移 {pos}):{}\n{}", message, snippet)
984    }
985
986    pub fn format_source_span(&self, fn_name: &str, span: Span, message: &str) -> String {
987        let module = fn_name.split_once("::").map(|(module, _)| module).unwrap_or(fn_name);
988        let Some(source) = self.source_files.get(module) else {
989            return format!("{fn_name}: 字节偏移 {}:{message}", span.start);
990        };
991        let code = source.code.as_ref();
992        let pos = span.start.min(code.len());
993        let (line, col) = Self::byte_to_line_col(code, pos);
994        let snippet = Self::line_snippet(code, span);
995        let location = source.path.as_ref().map(|path| path.display().to_string()).unwrap_or_else(|| module.to_string());
996        format!("{location}:{line}:{col}: {message}\n{snippet}")
997    }
998
999    pub fn parse_code(code: Vec<u8>) -> Result<Vec<Stmt>> {
1000        let mut p = Parser::new(code.clone());
1001        let mut stmts = Vec::new();
1002        loop {
1003            match p.stmt(false) {
1004                Ok(stmt) => stmts.push(stmt),
1005                Err(e) => {
1006                    if p.is_eof() {
1007                        return Ok(stmts);
1008                    }
1009                    // 优先用 SpannedParseError / ParserErr 拿精确 pos;
1010                    // 取不到时(老接口 Err 链)fallback 到当前位置。
1011                    let pos = e.downcast_ref::<parser::SpannedParseError>().map(|s| s.pos).or_else(|| e.downcast_ref::<parser::ParserErr>().map(|s| s.span().start)).unwrap_or_else(|| p.current_pos());
1012                    let (line, col) = Self::byte_to_line_col(&code, pos);
1013                    return Err(anyhow!("解析错误:第 {line} 行,第 {col} 列(字节偏移 {pos}):{e:#}\n{}", p.error_stmt()));
1014                }
1015            }
1016        }
1017    }
1018
1019    pub fn import_code(&mut self, name: &str, code: Vec<u8>) -> Result<Vec<u32>> {
1020        self.import_code_with_source(name, code, None, None)
1021    }
1022
1023    pub fn import_code_from_path(&mut self, name: &str, code: Vec<u8>, path: impl AsRef<Path>) -> Result<Vec<u32>> {
1024        let path = path.as_ref();
1025        self.import_code_with_source(name, code, path.parent(), Some(path))
1026    }
1027
1028    pub fn import_file(&mut self, name: &str, path: impl AsRef<Path>) -> Result<Vec<u32>> {
1029        let path = path.as_ref();
1030        let canonical = std::fs::canonicalize(path).with_context(|| format!("failed to resolve import path {}", path.display()))?;
1031        if !self.importing_paths.insert(canonical.clone()) {
1032            return Ok(Vec::new());
1033        }
1034        let code = std::fs::read(&canonical).with_context(|| format!("failed to read import path {}", canonical.display()))?;
1035        let result = self.import_code_from_path(name, code, &canonical);
1036        self.importing_paths.remove(&canonical);
1037        result
1038    }
1039
1040    fn import_code_with_source(&mut self, name: &str, code: Vec<u8>, base_dir: Option<&Path>, source_path: Option<&Path>) -> Result<Vec<u32>> {
1041        self.source_files.insert(name.into(), SourceFile { path: source_path.map(Path::to_path_buf), code: Arc::new(code.clone()) });
1042        let stmts = Self::parse_code(code.clone())?;
1043        log::debug!("func->{}", name);
1044        for s in stmts.iter() {
1045            log::debug!("{}", s);
1046        }
1047        self.resolve_imports(&stmts, base_dir).map_err(|err| Self::format_compile_error(&code, err))?;
1048        self.clear();
1049        self.compile(name.into(), stmts).map_err(|err| Self::format_compile_error(&code, err))
1050    }
1051
1052    pub fn resolve_imports(&mut self, stmts: &[Stmt], base_dir: Option<&Path>) -> Result<()> {
1053        for stmt in stmts {
1054            let Some((module, path)) = import_decl(stmt) else {
1055                continue;
1056            };
1057            if !self.symbols.symbol(module.as_str()).is_empty() {
1058                continue;
1059            }
1060            let path = Path::new(path.as_str());
1061            let resolved = if path.is_absolute() {
1062                path.to_path_buf()
1063            } else if let Some(base_dir) = base_dir {
1064                base_dir.join(path)
1065            } else {
1066                std::env::current_dir()?.join(path)
1067            };
1068            self.import_file(module.as_str(), &resolved).with_context(|| format!("failed to import {module} from {}", resolved.display()))?;
1069        }
1070        Ok(())
1071    }
1072
1073    pub fn check_code(name: &str, code: Vec<u8>) -> Vec<CompilerDiagnostic> {
1074        let mut parser = Parser::new(code.clone());
1075        let mut stmts = Vec::new();
1076        loop {
1077            match parser.stmt(false) {
1078                Ok(stmt) => stmts.push(stmt),
1079                Err(err) => {
1080                    if parser.is_eof() {
1081                        break;
1082                    }
1083                    return vec![CompilerDiagnostic { message: format!("解析错误:{err:#}"), span: Span::empty(parser.current_pos()) }];
1084                }
1085            }
1086        }
1087
1088        let mut compiler = Self::new();
1089        compiler.clear();
1090        match compiler.compile(name.into(), stmts) {
1091            Ok(_) => Vec::new(),
1092            Err(err) => {
1093                if let Some(err) = err.downcast_ref::<SpannedCompilerError>() {
1094                    vec![CompilerDiagnostic { message: err.message.clone(), span: err.span }]
1095                } else {
1096                    vec![CompilerDiagnostic { message: format!("{err:#}"), span: Span::default() }]
1097                }
1098            }
1099        }
1100    }
1101
1102    pub fn get_field(&self, ty: &Type, name: &str) -> Result<(usize, Type)> {
1103        self.symbols.get_field(ty, name)
1104    }
1105
1106    pub fn get_ident(&mut self, ident: &str, span: Span) -> Result<Expr> {
1107        for idx in (self.top()..self.names.len()).rev() {
1108            if self.names[idx].eq(ident) {
1109                return Ok(Expr::new(ExprKind::Var((idx - self.top()) as u32), span));
1110            }
1111        }
1112        let id = self.symbols.get_id(ident).map_err(|_| Self::semantic_error(span, format!("未找到标识符 {}", ident)))?;
1113        let s = self.symbols.get_symbol(id).map(|(_, v)| v.clone()).unwrap();
1114        if let Symbol::Const { value, ty, .. } = s {
1115            let c = self.get_const(value);
1116            return Ok(Expr::new(ExprKind::Typed { value: Box::new(Expr::new(ExprKind::Const(c), span)), ty }, span));
1117        } else if let Symbol::Static { value, ty, .. } = s
1118            && let Some(v) = value
1119        {
1120            let c = self.get_const(v);
1121            return Ok(Expr::new(ExprKind::Typed { value: Box::new(Expr::new(ExprKind::Const(c), span)), ty }, span));
1122        }
1123        Ok(Expr::new(ExprKind::Id(id, None), span))
1124    }
1125
1126    fn field_access_expr(&mut self, left: Expr, idx: usize, ty: Type, key: &str, span: Span) -> Expr {
1127        if let Type::Symbol { id, .. } = ty {
1128            Expr::new(ExprKind::Id(id, Some(Box::new(left))), span)
1129        } else if ty.is_bool() && idx == usize::MAX {
1130            Expr::new(ExprKind::Value(Dynamic::Bool(false)), span)
1131        } else if ty.is_any() && idx == usize::MAX {
1132            let right = Expr::new(ExprKind::Const(self.get_const(Dynamic::String(key.into()))), span);
1133            Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(right) }, span)
1134        } else {
1135            Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(Expr::new(ExprKind::Value(Dynamic::U32(idx as u32)), span)) }, span)
1136        }
1137    }
1138
1139    fn literal_field_access_expr(&mut self, left: Expr, key: &str, span: Span) -> Expr {
1140        let right = Expr::new(ExprKind::Const(self.get_const(Dynamic::String(key.into()))), span);
1141        Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(right) }, span)
1142    }
1143
1144    fn type_field_access_expr(&mut self, left: Expr, key: &str, span: Span, prefer_dynamic_field: bool) -> Option<Expr> {
1145        let ty = self.infer_expr(&left).ok()?;
1146        if prefer_dynamic_field && ty.is_any() {
1147            return Some(self.literal_field_access_expr(left, key, span));
1148        }
1149        let (idx, field_ty) = self.get_field(&ty, key).ok()?;
1150        Some(self.field_access_expr(left, idx, field_ty, key, span))
1151    }
1152
1153    fn global_method_access_expr(&self, left: Expr, method: &str, span: Span) -> Result<Option<Expr>> {
1154        let Ok(id) = self.symbols.get_id(method) else {
1155            return Ok(None);
1156        };
1157        if self.symbols.get_symbol(id)?.1.is_fn() { Ok(Some(Expr::new(ExprKind::Id(id, Some(Box::new(left))), span))) } else { Ok(None) }
1158    }
1159
1160    fn method_call_obj_expr(&mut self, obj: &Expr, stmts: &mut Vec<Stmt>, cap: &mut Capture) -> Result<Option<Expr>> {
1161        if let ExprKind::TypedMethod { obj: left, ty, name } = &obj.kind {
1162            let left = self.eval(left, stmts, cap)?;
1163            let base_name = match ty {
1164                Type::Ident { name, .. } => name.clone(),
1165                Type::Symbol { id, .. } => self.symbols.get_symbol(*id)?.0.clone(),
1166                _ => return Err(Self::semantic_error(obj.span, format!("方法调用类型提示必须是类型: {:?}", ty))),
1167            };
1168            let method = format!("{}::{}", base_name, name);
1169            let id = self.symbols.get_id(&method).map_err(|_| Self::semantic_error(obj.span, format!("未找到类型方法 {}", method)))?;
1170            return Ok(Some(Expr::new(ExprKind::Id(id, Some(Box::new(left))), obj.span)));
1171        }
1172
1173        let ExprKind::Binary { left, op: BinaryOp::Idx, right } = &obj.kind else {
1174            return Ok(None);
1175        };
1176        let Some(method) = self.get_value(right).and_then(|v| if v.is_str() { Some(v.as_str().to_string()) } else { None }) else {
1177            return Ok(None);
1178        };
1179        let left = self.eval(left, stmts, cap)?;
1180        if let Some(field) = self.type_field_access_expr(left.clone(), &method, obj.span, false) {
1181            return Ok(Some(field));
1182        }
1183        if let Some(method_fn) = self.global_method_access_expr(left.clone(), &method, obj.span)? {
1184            return Ok(Some(method_fn));
1185        }
1186        Ok(Some(self.literal_field_access_expr(left, &method, obj.span)))
1187    }
1188
1189    pub fn compile_fn(&mut self, args: &[SmolStr], tys: &mut Vec<Type>, body: Stmt, cap: &mut Capture) -> Result<Vec<Stmt>> {
1190        let top = self.tys.len();
1191        self.frames.push(top);
1192        self.arg_counts.push(args.len());
1193        let result = (|| -> Result<Vec<Stmt>> {
1194            for (arg, ty) in args.iter().zip(tys.iter_mut()) {
1195                *ty = self.symbols.get_type(ty)?;
1196                self.add_name(arg.clone());
1197                self.add_ty(ty.clone());
1198            }
1199            if cap.names.is_empty() && tys.iter().all(|ty| !ty.is_any()) {
1200                let saved_state = (self.frames.clone(), self.names.clone(), self.tys.clone(), self.list_elem_states.clone(), self.arg_counts.clone());
1201                let result = self.check_return_type(&body);
1202                self.restore_local_state(saved_state);
1203                result?;
1204            }
1205            let mut compiled = Vec::new();
1206            self.compile_stmt(body, &mut compiled, cap)?;
1207            if !compiled.last_mut().map(|stmt| stmt.last_return()).unwrap_or(false) {
1208                compiled.push(Stmt::new(StmtKind::Return(None), Span::default()));
1209            }
1210            Ok(compiled)
1211        })();
1212        if let Some(top) = self.frames.pop() {
1213            self.tys.truncate(top);
1214            self.names.truncate(top);
1215            self.list_elem_states.truncate(top);
1216        }
1217        self.arg_counts.pop();
1218        result
1219    }
1220
1221    pub fn compile(&mut self, mod_name: SmolStr, stmts: Vec<Stmt>) -> Result<Vec<u32>> {
1222        self.symbols.add_module(mod_name.clone());
1223        for stmt in stmts {
1224            match stmt.kind {
1225                StmtKind::Struct { name, def, is_pub } => {
1226                    self.symbols.add(name, Symbol::Struct(def, is_pub));
1227                }
1228                StmtKind::Static { name, ty, value, is_pub } => {
1229                    self.symbols.add(name, Symbol::Static { value: value.and_then(|v| v.value().ok()), ty, is_pub });
1230                }
1231                StmtKind::Const { name, ty, value, is_pub } => {
1232                    let value = self.const_expr_value(&value)?;
1233                    let ty = if ty.is_any() { value.get_type() } else { ty };
1234                    self.symbols.add(name, Symbol::Const { value, ty, is_pub });
1235                }
1236                StmtKind::Fn { name, generic_params, args, body, is_pub } => {
1237                    let (ty, args) = Type::from_args(args);
1238                    self.symbols.add(name, Symbol::Fn { ty, args, generic_params, cap: Capture::default(), body: Arc::new(*body), is_pub });
1239                }
1240                StmtKind::Impl { target, body } => {
1241                    let name = impl_target_name(&target)?;
1242                    let def_id = match self.symbols.get_id(&name) {
1243                        Ok(id) => id,
1244                        Err(_) if name.as_str() == "Vec" => self.symbols.add(name.clone(), Symbol::Struct(Type::Struct { params: Vec::new(), fields: Vec::new() }, true)),
1245                        Err(err) => return Err(err),
1246                    };
1247                    if let StmtKind::Block(fns) = body.kind {
1248                        for f in fns {
1249                            if let StmtKind::Fn { name: fn_name, generic_params: fn_generic_params, args, body, is_pub } = f.kind {
1250                                let (ty, args) = Type::from_args(args);
1251                                let mut generic_params = if has_unresolved_generic_param(&target) {
1252                                    match &target {
1253                                        Type::Ident { params, .. } => params.clone(),
1254                                        _ => Vec::new(),
1255                                    }
1256                                } else {
1257                                    Vec::new()
1258                                };
1259                                for param in fn_generic_params {
1260                                    if !generic_params.contains(&param) {
1261                                        generic_params.push(param);
1262                                    }
1263                                }
1264                                let fn_id = self.symbols.add(SmolStr::from(format!("{}::{}", name, fn_name)), Symbol::Fn { ty, args, generic_params, cap: Capture::default(), body: Arc::new(*body), is_pub });
1265                                if let Symbol::Struct(ty, _) = &mut self.symbols.symbols[def_id as usize] {
1266                                    ty.add_field(fn_name.into(), Type::Symbol { id: fn_id, params: Vec::new() })?;
1267                                }
1268                            } else {
1269                                log::debug!("impl 包含非函数语句 {:?}", f);
1270                            }
1271                        }
1272                    }
1273                }
1274                StmtKind::Expr(expr, _) if is_top_level_import_expr(&expr) => {}
1275                _ => {
1276                    log::debug!("未知的顶层语句 {:?}", stmt);
1277                }
1278            }
1279        }
1280        let mut fn_ids = Vec::new();
1281        for (name, id) in self.symbols.symbol(&mod_name) {
1282            log::debug!("compile symbol {:?}[{}]", name, id);
1283            if let Some((_, Symbol::Fn { ty, generic_params, .. })) = self.symbols.get_symbol(id).ok() {
1284                let resolved_ty = self.symbols.get_type(ty).unwrap_or_else(|_| ty.clone());
1285                if has_unresolved_generic_param(&resolved_ty) || !generic_params.is_empty() {
1286                    continue;
1287                }
1288            }
1289            if let Some(s) = self.symbols.get_symbol(id).ok().map(|(_, symbol)| symbol.clone()) {
1290                if let Symbol::Fn { ty, args, generic_params, mut cap, body, is_pub } = s {
1291                    if let Type::Fn { mut tys, ret } = ty {
1292                        let compiled = self.compile_fn(&args, &mut tys, body.as_ref().clone(), &mut cap)?;
1293                        for s in compiled.iter() {
1294                            log::debug!("{}", s);
1295                        }
1296                        self.symbols.symbols[id as usize] = Symbol::Fn { ty: Type::Fn { tys, ret }, args, generic_params, cap, body: Arc::new(Stmt::new(StmtKind::Block(compiled), Span::default())), is_pub };
1297                        fn_ids.push(id);
1298                    }
1299                }
1300            }
1301        }
1302        self.symbols.pop_module();
1303        Ok(fn_ids)
1304    }
1305
1306    fn pat_to_var(&mut self, pat: Pattern, expr_ty: Type) -> Result<Pattern> {
1307        match pat.kind {
1308            PatternKind::Var { idx, ty } => Ok(Pattern { kind: PatternKind::Var { idx, ty }, span: pat.span }),
1309            PatternKind::Ident { name, ty } => {
1310                let ty = self.symbols.get_type(&ty)?;
1311                let ty = if ty.is_any() { expr_ty } else { ty };
1312                self.add_ty(ty.clone());
1313                Ok(Pattern { kind: PatternKind::Var { idx: self.add_name(name), ty }, span: pat.span })
1314            }
1315            PatternKind::Tuple(pats) => {
1316                if let Type::Tuple(tys) = &expr_ty {
1317                    let pats: Vec<Pattern> = pats.into_iter().zip(tys).filter_map(|p| self.pat_to_var(p.0, p.1.clone()).ok()).collect();
1318                    if pats.len() == tys.len() { Ok(Pattern { kind: PatternKind::Tuple(pats), span: pat.span }) } else { Err(Self::semantic_error(pat.span, format!("模式与元组类型不匹配: {:?}", expr_ty))) }
1319                } else {
1320                    let pats = pats.into_iter().filter_map(|p| self.pat_to_var(p, Type::Any).ok()).collect();
1321                    Ok(Pattern { kind: PatternKind::Tuple(pats), span: pat.span })
1322                }
1323            }
1324            PatternKind::List { elems, has_rest } => {
1325                if expr_ty.is_any() {
1326                    let elems: Vec<Pattern> = elems.into_iter().filter_map(|p| self.pat_to_var(p, Type::Any).ok()).collect();
1327                    Ok(Pattern { kind: PatternKind::List { elems, has_rest }, span: pat.span })
1328                } else if let Type::List(elem_ty) | Type::Array(elem_ty, _) | Type::Vec(elem_ty, _) = &expr_ty {
1329                    let elems: Vec<Pattern> = elems.into_iter().filter_map(|p| self.pat_to_var(p, elem_ty.as_ref().clone()).ok()).collect();
1330                    Ok(Pattern { kind: PatternKind::List { elems, has_rest }, span: pat.span })
1331                } else {
1332                    Err(Self::semantic_error(pat.span, format!("列表模式 {:?} 与类型 {:?} 不匹配", elems, expr_ty)))
1333                }
1334            }
1335            PatternKind::Wildcard => {
1336                self.add_ty(expr_ty.clone());
1337                Ok(Pattern { kind: PatternKind::Var { idx: self.add_name(SmolStr::new_static("")), ty: expr_ty }, span: pat.span })
1338            }
1339            _ => panic!("未知的模式 {:?}", pat),
1340        }
1341    }
1342
1343    fn infer_range_type(&self, range: &Expr) -> Type {
1344        if let ExprKind::Range { start, stop, .. } = &range.kind {
1345            let start_ty = start.get_type();
1346            let stop_ty = stop.get_type();
1347            if start_ty.is_any() {
1348                stop_ty
1349            } else if stop_ty.is_any() {
1350                start_ty
1351            } else if start_ty == Type::I32 && stop_ty.is_uint() {
1352                stop_ty
1353            } else if stop_ty == Type::I32 && start_ty.is_uint() {
1354                start_ty
1355            } else {
1356                start_ty + stop_ty
1357            }
1358        } else {
1359            range.get_type()
1360        }
1361    }
1362
1363    fn dyn_init(&mut self, expr: Expr, stmts: &mut Vec<Stmt>, items: Vec<(Expr, Expr)>, ty: Type) -> Expr {
1364        self.add_name("".into());
1365        let temp = self.add_ty(ty);
1366        let span = expr.span;
1367        stmts.push(Stmt::new(StmtKind::Expr(Expr::new(ExprKind::Binary { left: Box::new(Expr::new(ExprKind::Var(temp), span)), op: BinaryOp::Assign, right: Box::new(expr) }, span), true), span));
1368        for (idx, item) in items {
1369            let item_span = idx.span.merge(item.span);
1370            let left = Expr::new(ExprKind::Binary { left: Box::new(Expr::new(ExprKind::Var(temp), item_span)), op: BinaryOp::Idx, right: Box::new(idx) }, item_span);
1371            stmts.push(Stmt::new(StmtKind::Expr(Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Assign, right: Box::new(item) }, item_span), false), item_span));
1372        }
1373        Expr::new(ExprKind::Var(temp), span)
1374    }
1375
1376    fn is_spawn_closure_call(obj: &Expr, params: &[Expr]) -> bool {
1377        params.len() == 2 && matches!(&obj.kind, ExprKind::Ident(name) if name.as_str() == "spawn") && matches!(&params[0].kind, ExprKind::Closure { .. })
1378    }
1379
1380    fn eval_spawn_arg_pack(&mut self, expr: &Expr, stmts: &mut Vec<Stmt>, cap: &mut Capture) -> Result<Expr> {
1381        match &expr.kind {
1382            ExprKind::Tuple(items) | ExprKind::List(items) => Ok(Expr::new(ExprKind::Tuple(items.iter().map(|item| self.eval(item, stmts, cap)).collect::<Result<Vec<_>>>()?), expr.span)),
1383            _ => Err(Self::semantic_error(expr.span, "spawn closure args must be tuple")),
1384        }
1385    }
1386
1387    fn is_multi_assign_target(expr: &Expr) -> bool {
1388        matches!(expr.kind, ExprKind::Tuple(_) | ExprKind::List(_))
1389    }
1390
1391    fn push_assign(stmts: &mut Vec<Stmt>, left: Expr, right: Expr, span: Span) {
1392        stmts.push(Stmt::new(StmtKind::Expr(Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Assign, right: Box::new(right) }, span), true), span));
1393    }
1394
1395    fn temp_var(&mut self, ty: Type, span: Span) -> Expr {
1396        self.add_name("".into());
1397        let idx = self.add_ty(ty);
1398        Expr::new(ExprKind::Var(idx), span)
1399    }
1400
1401    fn typed_expr(value: Expr, ty: &Type) -> Expr {
1402        if ty.is_any() {
1403            value
1404        } else {
1405            let span = value.span;
1406            Expr::new(ExprKind::Typed { value: Box::new(value), ty: ty.clone() }, span)
1407        }
1408    }
1409
1410    fn lower_multi_assign(&mut self, left: &Expr, right: &Expr, stmts: &mut Vec<Stmt>, cap: &mut Capture, span: Span) -> Result<Expr> {
1411        let left_items = match &left.kind {
1412            ExprKind::Tuple(items) | ExprKind::List(items) => items,
1413            _ => return Err(Self::semantic_error(left.span, "多重赋值左侧必须是 tuple 或 list")),
1414        };
1415        if left_items.is_empty() {
1416            return Err(Self::semantic_error(left.span, "多重赋值左侧不能为空"));
1417        }
1418
1419        let mut temps = Vec::with_capacity(left_items.len());
1420        if let ExprKind::Tuple(right_items) | ExprKind::List(right_items) = &right.kind {
1421            if left_items.len() != right_items.len() {
1422                return Err(Self::semantic_error(span, format!("多重赋值数量不匹配: 左侧 {} 个,右侧 {} 个", left_items.len(), right_items.len())));
1423            }
1424            for item in right_items {
1425                let value = self.eval(item, stmts, cap)?;
1426                let ty = self.infer_expr(&value)?;
1427                let temp = self.temp_var(ty.clone(), item.span);
1428                Self::push_assign(stmts, temp.clone(), Self::typed_expr(value, &ty), item.span);
1429                temps.push((temp, ty));
1430            }
1431        } else {
1432            let value = self.eval(right, stmts, cap)?;
1433            let ty = self.infer_expr(&value)?;
1434            let source = self.temp_var(ty.clone(), right.span);
1435            Self::push_assign(stmts, source.clone(), Self::typed_expr(value, &ty), right.span);
1436            for idx in 0..left_items.len() {
1437                let item_span = left_items[idx].span;
1438                let item = Expr::new(ExprKind::Binary { left: Box::new(source.clone()), op: BinaryOp::Idx, right: Box::new(Expr::new(ExprKind::Value((idx as u32).into()), item_span)) }, item_span);
1439                let value = self.eval(&item, stmts, cap)?;
1440                let ty = self.infer_expr(&value)?;
1441                let temp = self.temp_var(ty.clone(), item_span);
1442                Self::push_assign(stmts, temp.clone(), Self::typed_expr(value, &ty), item_span);
1443                temps.push((temp, ty));
1444            }
1445        }
1446
1447        for (target, (temp, ty)) in left_items.iter().zip(temps.iter()) {
1448            let target = self.eval(target, stmts, cap)?;
1449            let assign_span = target.span.merge(temp.span);
1450            Self::push_assign(stmts, target, Self::typed_expr(temp.clone(), ty), assign_span);
1451        }
1452
1453        Ok(temps.last().map(|(temp, ty)| Self::typed_expr(temp.clone(), ty)).unwrap_or_else(|| Expr::new(ExprKind::Value(Dynamic::Null), span)))
1454    }
1455
1456    fn static_composite_literal(&self, expr: &Expr) -> Result<Option<Dynamic>> {
1457        match &expr.kind {
1458            ExprKind::List(items) | ExprKind::Tuple(items) => {
1459                let mut values = Vec::with_capacity(items.len());
1460                for item in items {
1461                    let Some(value) = self.static_literal_value(item)? else {
1462                        return Ok(None);
1463                    };
1464                    values.push(value);
1465                }
1466                Ok(Some(Dynamic::list(values)))
1467            }
1468            ExprKind::Dict(items) => {
1469                let mut values = BTreeMap::new();
1470                for (key, item) in items {
1471                    let Some(value) = self.static_literal_value(item)? else {
1472                        return Ok(None);
1473                    };
1474                    values.insert(key.clone(), value);
1475                }
1476                Ok(Some(Dynamic::map(values)))
1477            }
1478            _ => Ok(None),
1479        }
1480    }
1481
1482    fn static_literal_value(&self, expr: &Expr) -> Result<Option<Dynamic>> {
1483        match &expr.kind {
1484            ExprKind::Value(value) => Ok(Some(value.clone())),
1485            ExprKind::Const(idx) => Ok(self.consts.get_index(*idx).map(|(_, v)| v.clone())),
1486            ExprKind::Typed { value, ty } if ty.is_native() => Ok(self.static_literal_value(value)?.map(|value| ty.force(value)).transpose()?),
1487            _ => self.static_composite_literal(expr),
1488        }
1489    }
1490
1491    fn const_expr_value(&self, expr: &Expr) -> Result<Dynamic> {
1492        match &expr.kind {
1493            ExprKind::Value(value) => Ok(value.clone()),
1494            ExprKind::Const(idx) => self.consts.get_index(*idx).map(|(_, v)| v.clone()).ok_or_else(|| Self::semantic_error(expr.span, format!("常量索引 {} 不存在", idx))),
1495            ExprKind::Ident(ident) => {
1496                let id = self.symbols.get_id(ident).map_err(|_| Self::semantic_error(expr.span, format!("未找到常量 {}", ident)))?;
1497                match self.symbols.get_symbol(id).map(|(_, symbol)| symbol) {
1498                    Ok(Symbol::Const { value, .. }) => Ok(value.clone()),
1499                    Ok(Symbol::Static { value: Some(value), .. }) => Ok(value.clone()),
1500                    _ => Err(Self::semantic_error(expr.span, format!("{} 不是可用于 const 的静态值", ident))),
1501                }
1502            }
1503            ExprKind::Typed { value, ty } if ty.is_native() => Ok(ty.force(self.const_expr_value(value)?)?),
1504            ExprKind::Typed { value, .. } => self.const_expr_value(value),
1505            ExprKind::List(items) | ExprKind::Tuple(items) => {
1506                let values = items.iter().map(|item| self.const_expr_value(item)).collect::<Result<Vec<_>>>()?;
1507                Ok(Dynamic::list(values))
1508            }
1509            ExprKind::Dict(items) => {
1510                let mut values = BTreeMap::new();
1511                for (key, item) in items {
1512                    values.insert(key.clone(), self.const_expr_value(item)?);
1513                }
1514                Ok(Dynamic::map(values))
1515            }
1516            ExprKind::Unary { op, value } => {
1517                let value = self.const_expr_value(value)?;
1518                match op {
1519                    parser::UnaryOp::Neg => Ok(-value),
1520                    parser::UnaryOp::Not => Ok(!value),
1521                    parser::UnaryOp::Unknow => Err(Self::semantic_error(expr.span, "const 一元表达式无法在编译期求值")),
1522                }
1523            }
1524            ExprKind::Binary { left, op, right } => {
1525                let left = Expr::new(ExprKind::Value(self.const_expr_value(left)?), left.span);
1526                let right = Expr::new(ExprKind::Value(self.const_expr_value(right)?), right.span);
1527                Expr::new(ExprKind::Binary { left: Box::new(left), op: op.clone(), right: Box::new(right) }, expr.span).compact().ok_or_else(|| Self::semantic_error(expr.span, "const 二元表达式无法在编译期求值"))
1528            }
1529            _ => Err(Self::semantic_error(expr.span, "const 只能使用字面量、已声明常量和静态 composite literal")),
1530        }
1531    }
1532
1533    fn eval_stmt_expr(&mut self, stmt: &Stmt, stmts: &mut Vec<Stmt>, cap: &mut Capture, span: Span) -> Result<Expr> {
1534        self.compile_stmt(stmt.clone(), stmts, cap)?;
1535        let expr_ty = if let Some(stmt) = stmts.last() { if let StmtKind::Expr(expr, _) = &stmt.kind { self.infer_expr(expr)? } else { self.infer_stmt(stmt)? } } else { Type::Any };
1536        self.add_name("".into());
1537        let temp = self.add_ty(expr_ty.clone());
1538        let pat = Pattern { kind: PatternKind::Var { idx: temp, ty: expr_ty }, span };
1539        stmts.last_mut().ok_or_else(|| Self::semantic_error(span, "没有生成可求值语句表达式")).and_then(|stmt| stmt.bind_pattern(pat))?;
1540        Ok(Expr::new(ExprKind::Var(temp), span))
1541    }
1542
1543    fn eval(&mut self, expr: &Expr, stmts: &mut Vec<Stmt>, cap: &mut Capture) -> Result<Expr> {
1544        match &expr.kind {
1545            ExprKind::Stmt(stmt) => self.eval_stmt_expr(stmt, stmts, cap, expr.span),
1546            ExprKind::Closure { args, body } => {
1547                let (mut names, mut tys): (Vec<SmolStr>, Vec<Type>) = args.clone().into_iter().unzip();
1548                let top = self.top();
1549                let mut cap_vars: Vec<(SmolStr, Type)> = self.names[top..].iter().zip(self.tys[top..].iter()).map(|(n, ty)| (n.clone(), ty.clone())).collect();
1550                let parent_cap_start = cap_vars.len();
1551                cap_vars.extend(cap.names.iter().cloned());
1552                let mut local_cap = Capture::new(cap_vars);
1553                let _ = self.compile_fn(names.as_slice(), &mut tys.clone(), *body.clone(), &mut local_cap)?;
1554                for cap_idx in local_cap.vars.iter() {
1555                    if *cap_idx >= parent_cap_start {
1556                        let _ = cap.get(&local_cap.names[*cap_idx].0);
1557                    }
1558                    names.push(local_cap.names[*cap_idx].0.clone());
1559                    tys.push(local_cap.names[*cap_idx].1.clone());
1560                }
1561                let mut compiled = self.compile_fn(names.as_slice(), &mut tys.clone(), *body.clone(), &mut Capture::default())?;
1562                let (ty, args) = Type::from_args(args.clone());
1563                let body_stmt = if compiled.len() == 1 { compiled.pop().unwrap() } else { Stmt::new(StmtKind::Block(compiled), expr.span) };
1564                let name = SmolStr::from(format!("__closure_{}_{}", expr.span.start, expr.span.end));
1565                let fn_id = self.symbols.add(name, Symbol::Fn { ty, args, generic_params: Vec::new(), cap: local_cap, body: Arc::new(body_stmt), is_pub: false });
1566                Ok(Expr::new(ExprKind::Id(fn_id, None), expr.span))
1567            }
1568            ExprKind::Value(v) => {
1569                if v.is_native() {
1570                    Ok(Expr::new(ExprKind::Value(v.clone()), expr.span))
1571                } else {
1572                    Ok(Expr::new(ExprKind::Const(self.get_const(v.clone())), expr.span))
1573                }
1574            }
1575            ExprKind::Typed { value, ty } => {
1576                let ty = self.symbols.get_type(ty)?;
1577                if let Type::Struct { fields, .. } = &ty
1578                    && let ExprKind::Dict(dict) = &value.kind
1579                {
1580                    let mut items = Vec::new();
1581                    for field in fields {
1582                        if let Some((_, v)) = dict.iter().find(|(name, _)| name == &field.0) {
1583                            items.push(self.eval(v, stmts, cap)?);
1584                        }
1585                    }
1586                    Ok(Expr::new(ExprKind::Typed { value: Box::new(Expr::new(ExprKind::List(items), expr.span)), ty }, expr.span))
1587                } else if let Type::Struct { .. } = &ty
1588                    && let ExprKind::List(list) = &value.kind
1589                {
1590                    let items = list.iter().map(|item| self.eval(item, stmts, cap)).collect::<Result<Vec<_>>>()?;
1591                    Ok(Expr::new(ExprKind::Typed { value: Box::new(Expr::new(ExprKind::List(items), expr.span)), ty }, expr.span))
1592                } else if let Type::Array(_, _) = &ty
1593                    && let ExprKind::List(list) = &value.kind
1594                {
1595                    let items = list.iter().map(|item| self.eval(item, stmts, cap)).collect::<Result<Vec<_>>>()?;
1596                    Ok(Expr::new(ExprKind::Typed { value: Box::new(Expr::new(ExprKind::List(items), expr.span)), ty }, expr.span))
1597                } else if value.is_value() {
1598                    let value = value.clone().value()?;
1599                    // 字符串字面量被类型注解为 string 时,当前 VM 没有原生 String 通路,
1600                    // 退化为 Dynamic::String(Any) 行为更稳。这里不再 log warn,因为这条
1601                    // 路径是设计选择,不是 bug。
1602                    if ty.is_str() && value.is_str() { Ok(Expr::new(ExprKind::Const(self.get_const(value)), expr.span)) } else { Ok(Expr::new(ExprKind::Value(ty.force(value)?), expr.span)) }
1603                } else {
1604                    Ok(Expr::new(ExprKind::Typed { value: Box::new(self.eval(value, stmts, cap)?), ty }, expr.span))
1605                }
1606            }
1607            ExprKind::Ident(ident) => {
1608                // 局部变量 → 捕获变量 → 全局符号
1609                for idx in (self.top()..self.names.len()).rev() {
1610                    if self.names[idx].eq(ident) {
1611                        return Ok(Expr::new(ExprKind::Var((idx - self.top()) as u32), expr.span));
1612                    }
1613                }
1614                if let Some(idx) = cap.get(ident) {
1615                    return Ok(Expr::new(ExprKind::Capture(idx as u32), expr.span));
1616                }
1617                self.get_ident(ident, expr.span)
1618            }
1619            ExprKind::Generic { obj, params } => {
1620                let obj = self.eval(obj, stmts, cap)?;
1621                let params = params.iter().map(|param| self.symbols.get_type(param).unwrap_or_else(|_| param.clone())).collect();
1622                match obj.kind {
1623                    ExprKind::Id(id, None) | ExprKind::AssocId { id, .. } => Ok(Expr::new(ExprKind::AssocId { id, params }, expr.span)),
1624                    _ => Err(Self::semantic_error(expr.span, format!("范型参数只能用于函数或关联函数调用: {:?}", obj))),
1625                }
1626            }
1627            ExprKind::Assoc { ty, name } => {
1628                let base_name = match ty {
1629                    Type::Ident { name, .. } => name.clone(),
1630                    Type::Symbol { id, .. } => self.symbols.get_symbol(*id)?.0.clone(),
1631                    _ => return Err(Self::semantic_error(expr.span, format!("关联函数目标必须是类型: {:?}", ty))),
1632                };
1633                let id = self.symbols.get_id(&format!("{}::{}", base_name, name)).map_err(|_| Self::semantic_error(expr.span, format!("未找到关联函数 {}::{}", base_name, name)))?;
1634                let params = match ty {
1635                    Type::Ident { params, .. } | Type::Symbol { params, .. } => params.iter().map(|param| self.symbols.get_type(param).unwrap_or_else(|_| param.clone())).collect(),
1636                    _ => Vec::new(),
1637                };
1638                Ok(Expr::new(ExprKind::AssocId { id, params }, expr.span))
1639            }
1640            ExprKind::Unary { op, value } => {
1641                let value = Expr::new(ExprKind::Unary { op: op.clone(), value: Box::new(self.eval(value, stmts, cap)?) }, expr.span);
1642                if let Some(v) = value.compact() { Ok(Expr::new(ExprKind::Value(v), expr.span)) } else { Ok(value) }
1643            }
1644            ExprKind::Binary { left, op, right } => {
1645                if *op == BinaryOp::Assign && Self::is_multi_assign_target(left) {
1646                    return self.lower_multi_assign(left, right, stmts, cap, expr.span);
1647                }
1648                let left = self.eval(left, stmts, cap)?;
1649                if *op == BinaryOp::Idx {
1650                    if let Some(key) = self.get_value(right).and_then(|v| if v.is_str() { Some(v.as_str().to_string()) } else { None }) {
1651                        if let Some(field) = self.type_field_access_expr(left.clone(), &key, expr.span, true) {
1652                            return Ok(field);
1653                        }
1654                        return Ok(self.literal_field_access_expr(left, &key, expr.span));
1655                    } else if let Ok(ident) = right.ident() {
1656                        if let Ok(found) = self.get_ident(ident, right.span) {
1657                            return Ok(if let Some(id) = found.id() {
1658                                Expr::new(ExprKind::Id(id, Some(Box::new(left))), expr.span)
1659                            } else {
1660                                Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(found) }, expr.span)
1661                            });
1662                        }
1663                        if let Ok(ty) = self.infer_expr(&left)
1664                            && let Ok((idx, ty)) = self.get_field(&ty, ident)
1665                        {
1666                            return Ok(if let Type::Symbol { id, .. } = ty {
1667                                Expr::new(ExprKind::Id(id, Some(Box::new(left))), expr.span)
1668                            } else if ty.is_bool() && idx == usize::MAX {
1669                                Expr::new(ExprKind::Value(Dynamic::Bool(false)), expr.span)
1670                            } else if ty.is_any() && idx == usize::MAX {
1671                                let right = Expr::new(ExprKind::Const(self.get_const(Dynamic::String(ident.into()))), expr.span);
1672                                Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(right) }, expr.span)
1673                            } else {
1674                                Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(Expr::new(ExprKind::Value(Dynamic::U32(idx as u32)), expr.span)) }, expr.span)
1675                            });
1676                        } else {
1677                            let right = Expr::new(ExprKind::Const(self.get_const(Dynamic::String(ident.into()))), expr.span);
1678                            return Ok(Expr::new(ExprKind::Binary { left: Box::new(left), op: BinaryOp::Idx, right: Box::new(right) }, expr.span));
1679                        }
1680                    }
1681                }
1682                let right = self.eval(right, stmts, cap)?;
1683                let value = Self::normalize_self_assign(left, op.clone(), right, expr.span, self.arg_counts.last().copied().unwrap_or(0));
1684                if let Some(v) = value.compact() { Ok(Expr::new(ExprKind::Value(v), expr.span)) } else { Ok(value) }
1685            }
1686            ExprKind::Call { obj, params } => {
1687                let params: Vec<Expr> = if Self::is_spawn_closure_call(obj, params) {
1688                    vec![self.eval(&params[0], stmts, cap)?, self.eval_spawn_arg_pack(&params[1], stmts, cap)?]
1689                } else {
1690                    params.iter().map(|p| self.eval(p, stmts, cap)).collect::<Result<Vec<_>>>()?
1691                };
1692                let obj_result = if let Some(method_obj) = self.method_call_obj_expr(obj, stmts, cap)? { Ok(method_obj) } else { self.eval(obj, stmts, cap) };
1693                match obj_result {
1694                    Ok(obj) if obj.is_value() && params.is_empty() => Ok(obj),
1695                    Ok(obj) => Ok(Expr::new(ExprKind::Call { obj: Box::new(obj), params }, expr.span)),
1696                    Err(e) => {
1697                        // 严格模式:未注册函数 / 方法调用直接报错,不再用 Symbol::Null 兜底。
1698                        // 隐藏 typo 在生产代码里代价大于修复编译期的便利。
1699                        Err(Self::semantic_error(obj.span, format!("未注册函数 {:?}: {}", obj.kind, e)))
1700                    }
1701                }
1702            }
1703            ExprKind::Range { start, stop, inclusive } => {
1704                let start = Box::new(self.eval(start, stmts, cap)?);
1705                let stop = Box::new(self.eval(stop, stmts, cap)?);
1706                Ok(Expr::new(ExprKind::Range { start, stop, inclusive: *inclusive }, expr.span))
1707            }
1708            ExprKind::List(list) | ExprKind::Tuple(list) => {
1709                if let Some(value) = self.static_composite_literal(expr)? {
1710                    let idx = self.get_const(value);
1711                    return Ok(Expr::new(ExprKind::Const(idx), expr.span));
1712                }
1713                let mut v = Vec::new();
1714                let mut items = Vec::new();
1715                for (idx, item) in list.iter().enumerate() {
1716                    if item.is_value() {
1717                        v.push(item.clone().value().unwrap());
1718                    } else {
1719                        items.push((Expr::new(ExprKind::Value((idx as u32).into()), item.span), self.eval(item, stmts, cap)?));
1720                        v.push(Dynamic::Null);
1721                    }
1722                }
1723                let list = Expr::new(ExprKind::Const(self.get_const(Dynamic::list(v))), expr.span);
1724                Ok(self.dyn_init(list, stmts, items, Type::Any))
1725            }
1726            ExprKind::Repeat { value, len } => {
1727                let len = self.symbols.get_type(len)?;
1728                let Type::ConstInt(len) = len else {
1729                    return Err(Self::semantic_error(expr.span, format!("重复数组长度必须是编译期整数: {:?}", len)));
1730                };
1731                if len < 0 {
1732                    return Err(Self::semantic_error(expr.span, "重复数组长度不能为负数"));
1733                }
1734                Ok(Expr::new(ExprKind::Repeat { value: Box::new(self.eval(value, stmts, cap)?), len: Type::ConstInt(len) }, expr.span))
1735            }
1736            ExprKind::Dict(dict) => {
1737                if let Some(value) = self.static_composite_literal(expr)? {
1738                    let idx = self.get_const(value);
1739                    return Ok(Expr::new(ExprKind::Const(idx), expr.span));
1740                }
1741                let mut dyn_kv = Vec::new();
1742                let mut m = BTreeMap::new();
1743                for (k, v) in dict {
1744                    if v.is_value() {
1745                        m.insert(k.clone(), v.clone().value()?);
1746                    } else {
1747                        let key = Expr::new(ExprKind::Const(self.get_const(Dynamic::String(k.clone()))), v.span);
1748                        dyn_kv.push((key, self.eval(v, stmts, cap)?));
1749                        m.insert(k.clone(), Dynamic::Null);
1750                    }
1751                }
1752                let dict = Expr::new(ExprKind::Const(self.get_const(Dynamic::map(m))), expr.span);
1753                Ok(self.dyn_init(dict, stmts, dyn_kv, Type::Any))
1754            }
1755            ExprKind::Id(_, _) | ExprKind::AssocId { .. } => Ok(expr.clone()),
1756            _ => Ok(expr.clone()),
1757        }
1758    }
1759
1760    fn get_stmt(&mut self, stmt: Stmt, cap: &mut Capture) -> Result<Stmt> {
1761        let span = stmt.span;
1762        let mut stmts = Vec::new();
1763        self.compile_stmt(stmt, &mut stmts, cap)?;
1764        Ok(Stmt::new(StmtKind::Block(stmts), span))
1765    }
1766
1767    fn compile_stmt(&mut self, stmt: Stmt, compiled: &mut Vec<Stmt>, cap: &mut Capture) -> Result<()> {
1768        let stmt_span = stmt.span;
1769        match stmt.kind {
1770            StmtKind::Let { pat, value } => {
1771                let value = *value;
1772                let annotated_ty = if let PatternKind::Ident { ty, .. } = &pat.kind {
1773                    let ty = self.symbols.get_type(ty)?;
1774                    if ty.is_any() { None } else { Some(ty) }
1775                } else {
1776                    None
1777                };
1778                let pattern_expr_ty = if matches!(pat.kind, PatternKind::List { .. } | PatternKind::Tuple(_)) { if let StmtKind::Expr(expr, _) = &value.kind { Some(self.infer_expr(expr)?) } else { None } } else { None };
1779                if let Some(ty) = annotated_ty {
1780                    if let StmtKind::Expr(expr, close) = value.kind {
1781                        let span = expr.span;
1782                        let typed = Expr::new(ExprKind::Typed { value: Box::new(expr), ty }, span);
1783                        self.compile_stmt(Stmt::new(StmtKind::Expr(typed, close), value.span), compiled, cap)?;
1784                    } else {
1785                        self.compile_stmt(value, compiled, cap)?;
1786                    }
1787                } else {
1788                    self.compile_stmt(value, compiled, cap)?;
1789                }
1790                let expr_ty = if let Some(ty) = pattern_expr_ty {
1791                    ty
1792                } else if let Some(stmt) = compiled.last() {
1793                    if let StmtKind::Expr(expr, _) = &stmt.kind { self.infer_expr(expr)? } else { self.infer_stmt(stmt)? }
1794                } else {
1795                    Type::Any
1796                };
1797                let pat = self.pat_to_var(pat, expr_ty)?;
1798                compiled.last_mut().ok_or_else(|| Self::semantic_error(stmt_span, "没有生成可绑定模式的编译语句")).and_then(|stmt| stmt.bind_pattern(pat))?;
1799            }
1800            StmtKind::Expr(expr, close) => {
1801                if let ExprKind::Binary { left, op: BinaryOp::Assign, right } = &expr.kind
1802                    && Self::is_multi_assign_target(left)
1803                {
1804                    self.lower_multi_assign(left, right, compiled, cap, stmt_span)?;
1805                    return Ok(());
1806                }
1807                let e = self.eval(&expr, compiled, cap)?;
1808                compiled.push(Stmt::new(StmtKind::Expr(e, close), stmt_span));
1809            }
1810            StmtKind::Block(stmts) => {
1811                let mut block = Vec::new();
1812                for stmt in stmts {
1813                    self.compile_stmt(stmt, &mut block, cap)?;
1814                }
1815                compiled.push(Stmt::new(StmtKind::Block(block), stmt_span));
1816            }
1817            StmtKind::Fn { name, generic_params, args, body, is_pub } => {
1818                let (ty, args) = Type::from_args(args);
1819                if let Type::Fn { mut tys, ret } = ty {
1820                    let mut fn_cap = Capture::default();
1821                    let compiled_body = self.compile_fn(&args, &mut tys, *body, &mut fn_cap)?;
1822                    self.symbols.add(name, Symbol::Fn { ty: Type::Fn { tys, ret }, args, generic_params, cap: fn_cap, body: Arc::new(Stmt::new(StmtKind::Block(compiled_body), stmt_span)), is_pub });
1823                } else {
1824                    panic!("nested functions are not supported here")
1825                }
1826            }
1827            StmtKind::Return(expr) => {
1828                let expr = expr.and_then(|e| self.eval(&e, compiled, cap).ok());
1829                compiled.push(Stmt::new(StmtKind::Return(expr), stmt_span));
1830            }
1831            StmtKind::If { cond, then_body, else_body } => {
1832                let cond = self.eval(&cond, compiled, cap)?;
1833                if let Some(cond_value) = cond.compact()
1834                    && let Some(cond_bool) = cond_value.as_bool()
1835                {
1836                    if cond_bool {
1837                        self.compile_stmt(*then_body, compiled, cap)?;
1838                    } else if let Some(body) = else_body {
1839                        self.compile_stmt(*body, compiled, cap)?;
1840                    }
1841                } else {
1842                    let then_body = Box::new(self.get_stmt(*then_body, cap)?);
1843                    let else_body = if let Some(body) = else_body { Some(Box::new(self.get_stmt(*body, cap)?)) } else { None };
1844                    compiled.push(Stmt::new(StmtKind::If { cond, then_body, else_body }, stmt_span));
1845                }
1846            }
1847            StmtKind::Loop(body) => {
1848                compiled.push(Stmt::new(StmtKind::Loop(Box::new(self.get_stmt(*body, cap)?)), stmt_span));
1849            }
1850            StmtKind::While { cond, body } => {
1851                let cond = self.eval(&cond, compiled, cap)?;
1852                compiled.push(Stmt::new(StmtKind::While { cond, body: Box::new(self.get_stmt(*body, cap)?) }, stmt_span));
1853            }
1854            StmtKind::For { pat, range, body } => {
1855                let range = self.eval(&range, compiled, cap)?;
1856                let range_ty = self.infer_range_type(&range);
1857                let pat = self.pat_to_var(pat, range_ty)?;
1858                compiled.push(Stmt::new(StmtKind::For { pat, range, body: Box::new(self.get_stmt(*body, cap)?) }, stmt_span));
1859            }
1860            stmt_kind => {
1861                compiled.push(Stmt::new(stmt_kind, stmt_span));
1862            }
1863        }
1864        Ok(())
1865    }
1866}