ternlang-core 1.2.5

Compiler and VM for Ternlang — balanced ternary language with affirm/tend/reject trit semantics, @sparseskip codegen, and BET bytecode execution.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use crate::ast::*;

// ─── Errors ───────────────────────────────────────────────────────────────────

#[derive(Debug)]
pub enum SemanticError {
    TypeMismatch { expected: Type, found: Type },
    UndefinedVariable(String),
    UndefinedStruct(String),
    UndefinedField { struct_name: String, field: String },
    UndefinedFunction(String),
    ReturnTypeMismatch { function: String, expected: Type, found: Type },
    ArgCountMismatch { function: String, expected: usize, found: usize },
    ArgTypeMismatch { function: String, param_index: usize, expected: Type, found: Type },
    /// `?` used on an expression that doesn't return trit
    PropagateOnNonTrit { found: Type },
    NonExhaustiveMatch(String),
}

impl std::fmt::Display for SemanticError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::TypeMismatch { expected, found } =>
                write!(f, "[TYPE-001] Type mismatch: expected {expected:?}, found {found:?}. A trit is not an int. An int is not a trit. They don't coerce.\n           → details: stdlib/errors/TYPE-001.tern  |  ternlang errors TYPE-001"),
            Self::UndefinedVariable(n) =>
                write!(f, "[SCOPE-001] '{n}' is undefined — hold state. Declare before use, or check for a typo.\n            → details: stdlib/errors/SCOPE-001.tern  |  ternlang errors SCOPE-001"),
            Self::UndefinedStruct(n) =>
                write!(f, "[STRUCT-001] Struct '{n}' doesn't exist. A ghost type — the type system can't find it anywhere.\n             → details: stdlib/errors/STRUCT-001.tern  |  ternlang errors STRUCT-001"),
            Self::UndefinedField { struct_name, field } =>
                write!(f, "[STRUCT-002] Struct '{struct_name}' has no field '{field}'. Check the definition — maybe it was renamed.\n             → details: stdlib/errors/STRUCT-002.tern  |  ternlang errors STRUCT-002"),
            Self::UndefinedFunction(n) =>
                write!(f, "[FN-001] '{n}' was called but never defined. Declare it above the call site, or check for a typo.\n          → details: stdlib/errors/FN-001.tern  |  ternlang errors FN-001"),
            Self::ReturnTypeMismatch { function, expected, found } =>
                write!(f, "[FN-002] '{function}' promised to return {expected:?} but returned {found:?}. Ternary contracts are strict — all paths must match.\n          → details: stdlib/errors/FN-002.tern  |  ternlang errors FN-002"),
            Self::ArgCountMismatch { function, expected, found } =>
                write!(f, "[FN-003] '{function}' expects {expected} arg(s), got {found}. Arity is not optional — not even in hold state.\n          → details: stdlib/errors/FN-003.tern  |  ternlang errors FN-003"),
            Self::ArgTypeMismatch { function, param_index, expected, found } =>
                write!(f, "[FN-004] '{function}' arg {param_index}: expected {expected:?}, found {found:?}. Types travel with their values — they don't change at the border.\n          → details: stdlib/errors/FN-004.tern  |  ternlang errors FN-004"),
            Self::PropagateOnNonTrit { found } =>
                write!(f, "[PROP-001] '?' used on a {found:?} expression. Only trit-returning functions carry the three-valued signal. The third state requires a trit.\n            → details: stdlib/errors/PROP-001.tern  |  ternlang errors PROP-001"),
            Self::NonExhaustiveMatch(msg) =>
                write!(f, "Non-exhaustive match: {msg}"),
        }
    }
}

// ─── Full function signature ──────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct FunctionSig {
    /// Parameter types in declaration order. None = variadic / unknown (built-ins with flexible arity).
    pub params: Option<Vec<Type>>,
    pub return_type: Type,
}

impl FunctionSig {
    fn exact(params: Vec<Type>, return_type: Type) -> Self {
        Self { params: Some(params), return_type }
    }
    fn variadic(return_type: Type) -> Self {
        Self { params: None, return_type }
    }
}

// ─── Analyzer ────────────────────────────────────────────────────────────────

pub struct SemanticAnalyzer {
    scopes:           Vec<std::collections::HashMap<String, Type>>,
    struct_defs:      std::collections::HashMap<String, Vec<(String, Type)>>,
    func_signatures:  std::collections::HashMap<String, FunctionSig>,
    /// Set while type-checking a function body so Return stmts can be validated.
    current_fn_name:       Option<String>,
    current_fn_return:     Option<Type>,
}

impl SemanticAnalyzer {
    pub fn new() -> Self {
        let mut sigs: std::collections::HashMap<String, FunctionSig> = std::collections::HashMap::new();

        // ── std::trit built-ins ────────────────────────────────────────────
        sigs.insert("consensus".into(), FunctionSig::exact(vec![Type::Trit, Type::Trit], Type::Trit));
        sigs.insert("invert".into(),    FunctionSig::exact(vec![Type::Trit],             Type::Trit));
        sigs.insert("length".into(),    FunctionSig::variadic(Type::Int));
        sigs.insert("truth".into(),     FunctionSig::exact(vec![],                       Type::Trit));
        sigs.insert("hold".into(),      FunctionSig::exact(vec![],                       Type::Trit));
        sigs.insert("conflict".into(),  FunctionSig::exact(vec![],                       Type::Trit));
        sigs.insert("mul".into(),       FunctionSig::exact(vec![Type::Trit, Type::Trit], Type::Trit));

        // ── std::tensor ────────────────────────────────────────────────────
        sigs.insert("matmul".into(),   FunctionSig::variadic(Type::TritTensor { dims: vec![0, 0] }));
        sigs.insert("sparsity".into(), FunctionSig::variadic(Type::Int));
        sigs.insert("shape".into(),    FunctionSig::variadic(Type::Int));
        sigs.insert("zeros".into(),    FunctionSig::variadic(Type::TritTensor { dims: vec![0, 0] }));

        // ── std::io ────────────────────────────────────────────────────────
        sigs.insert("print".into(),    FunctionSig::variadic(Type::Trit));
        sigs.insert("println".into(),  FunctionSig::variadic(Type::Trit));

        // ── std::math ──────────────────────────────────────────────────────
        sigs.insert("abs".into(),      FunctionSig::exact(vec![Type::Int],  Type::Int));
        sigs.insert("min".into(),      FunctionSig::exact(vec![Type::Int, Type::Int], Type::Int));
        sigs.insert("max".into(),      FunctionSig::exact(vec![Type::Int, Type::Int], Type::Int));

        // ── ml::quantize ───────────────────────────────────────────────────
        sigs.insert("quantize".into(), FunctionSig::variadic(Type::TritTensor { dims: vec![0, 0] }));
        sigs.insert("threshold".into(),FunctionSig::variadic(Type::Float));

        // ── ml::inference ──────────────────────────────────────────────────
        sigs.insert("forward".into(),  FunctionSig::variadic(Type::TritTensor { dims: vec![0, 0] }));
        sigs.insert("argmax".into(),   FunctionSig::variadic(Type::Int));

        // ── type coercion ──────────────────────────────────────────────────
        sigs.insert("cast".into(),     FunctionSig::variadic(Type::Trit));

        Self {
            scopes: vec![std::collections::HashMap::new()],
            struct_defs: std::collections::HashMap::new(),
            func_signatures: sigs,
            current_fn_name: None,
            current_fn_return: None,
        }
    }

    // ── Registration ─────────────────────────────────────────────────────────

    pub fn register_structs(&mut self, structs: &[StructDef]) {
        for s in structs {
            self.struct_defs.insert(s.name.clone(), s.fields.clone());
        }
    }

    pub fn register_functions(&mut self, functions: &[Function]) {
        for f in functions {
            let params = f.params.iter().map(|(_, ty)| ty.clone()).collect();
            self.func_signatures.insert(
                f.name.clone(),
                FunctionSig::exact(params, f.return_type.clone()),
            );
        }
    }

    pub fn register_agents(&mut self, agents: &[AgentDef]) {
        for agent in agents {
            for method in &agent.methods {
                let params = method.params.iter().map(|(_, ty)| ty.clone()).collect();
                let sig = FunctionSig::exact(params, method.return_type.clone());
                self.func_signatures.insert(method.name.clone(), sig.clone());
                self.func_signatures.insert(
                    format!("{}::{}", agent.name, method.name),
                    sig,
                );
            }
        }
    }

    // ── Entry points ─────────────────────────────────────────────────────────

    pub fn check_program(&mut self, program: &Program) -> Result<(), SemanticError> {
        self.register_structs(&program.structs);
        self.register_functions(&program.functions);
        self.register_agents(&program.agents);
        for agent in &program.agents {
            for method in &agent.methods {
                self.check_function(method)?;
            }
        }
        for func in &program.functions {
            self.check_function(func)?;
        }
        Ok(())
    }

    fn check_function(&mut self, func: &Function) -> Result<(), SemanticError> {
        // Track return type context for this function body.
        let prev_name   = self.current_fn_name.take();
        let prev_return = self.current_fn_return.take();
        self.current_fn_name   = Some(func.name.clone());
        self.current_fn_return = Some(func.return_type.clone());

        self.scopes.push(std::collections::HashMap::new());
        for (name, ty) in &func.params {
            self.scopes.last_mut().unwrap().insert(name.clone(), ty.clone());
        }
        for stmt in &func.body {
            self.check_stmt(stmt)?;
        }
        self.scopes.pop();

        // Restore outer context (handles nested definitions if ever needed).
        self.current_fn_name   = prev_name;
        self.current_fn_return = prev_return;
        Ok(())
    }

    // ── Statement checking ───────────────────────────────────────────────────

    pub fn check_stmt(&mut self, stmt: &Stmt) -> Result<(), SemanticError> {
        match stmt {
            Stmt::Let { name, ty, value } => {
                let val_ty = self.infer_expr_type(value)?;
                let type_ok = val_ty == *ty
                    || matches!(value, Expr::Cast { .. })
                    || matches!(value, Expr::StructLiteral { .. }) // Struct literals checked in infer_expr_type
                    || (*ty == Type::Int && val_ty == Type::Trit)
                    || (*ty == Type::Trit && val_ty == Type::Int)
                    || (matches!(ty, Type::Named(_)) && val_ty == Type::Trit)
                    || (matches!(ty, Type::TritTensor { .. }) && matches!(val_ty, Type::TritTensor { .. }))
                    || (*ty == Type::AgentRef && val_ty == Type::AgentRef);
                if !type_ok {
                    return Err(SemanticError::TypeMismatch { expected: ty.clone(), found: val_ty });
                }
                self.scopes.last_mut().unwrap().insert(name.clone(), ty.clone());
                Ok(())
            }

            Stmt::Return(expr) => {
                let found = self.infer_expr_type(expr)?;
                if let (Some(fn_name), Some(expected)) = (&self.current_fn_name, &self.current_fn_return) {
                    // Allow TritTensor shape flexibility and AgentRef, cast
                    let ok = found == *expected
                        || matches!(expr, Expr::Cast { .. })
                        || matches!(expr, Expr::StructLiteral { .. })
                        || (*expected == Type::Int && found == Type::Trit)
                        || (*expected == Type::Trit && found == Type::Int)
                        || (matches!(expected, Type::TritTensor { .. }) && matches!(found, Type::TritTensor { .. }))
                        || (matches!(expected, Type::Named(_)) && found == Type::Trit);
                    if !ok {
                        return Err(SemanticError::ReturnTypeMismatch {
                            function: fn_name.clone(),
                            expected: expected.clone(),
                            found,
                        });
                    }
                }
                Ok(())
            }

            Stmt::IfTernary { condition, on_pos, on_zero, on_neg } => {
                let cond_ty = self.infer_expr_type(condition)?;
                if cond_ty != Type::Trit {
                    return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: cond_ty });
                }
                self.check_stmt(on_pos)?;
                self.check_stmt(on_zero)?;
                self.check_stmt(on_neg)?;
                Ok(())
            }

            Stmt::Match { condition, arms } => {
                let cond_ty = self.infer_expr_type(condition)?;
                if cond_ty != Type::Trit && cond_ty != Type::Int && cond_ty != Type::Float {
                    return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: cond_ty });
                }
                
                if cond_ty == Type::Trit {
                    // Enforce exhaustiveness and value range for Trit match
                    let has_pos = arms.iter().any(|(p, _)| matches!(p, Pattern::Trit(1) | Pattern::Int(1)));
                    let has_wildcard = arms.iter().any(|(p, _)| matches!(p, Pattern::Wildcard));
                    if !has_wildcard {
                        let has_zero = arms.iter().any(|(p, _)| matches!(p, Pattern::Trit(0) | Pattern::Int(0)));
                        let has_neg = arms.iter().any(|(p, _)| matches!(p, Pattern::Trit(-1) | Pattern::Int(-1)));
                        if !has_pos || !has_zero || !has_neg {
                            return Err(SemanticError::NonExhaustiveMatch("Trit match must cover -1, 0, and 1 (or use _ wildcard)".into()));
                        }
                    }
                    for (pattern, _) in arms {
                        match pattern {
                            Pattern::Trit(v) => if *v < -1 || *v > 1 { return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: Type::Int }); }
                            Pattern::Int(v)  => if *v < -1 || *v > 1 { return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: Type::Int }); }
                            Pattern::Float(_) => return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: Type::Float }),
                            Pattern::Wildcard => {} // valid in any match
                        }
                    }
                }

                for (_pattern, arm_stmt) in arms {
                    self.check_stmt(arm_stmt)?;
                }
                Ok(())
            }

            Stmt::Block(stmts) => {
                self.scopes.push(std::collections::HashMap::new());
                for s in stmts { self.check_stmt(s)?; }
                self.scopes.pop();
                Ok(())
            }

            Stmt::Decorated { stmt, .. } => self.check_stmt(stmt),

            Stmt::Expr(expr) => { self.infer_expr_type(expr)?; Ok(()) }

            Stmt::ForIn { var, iter, body } => {
                self.infer_expr_type(iter)?;
                self.scopes.push(std::collections::HashMap::new());
                self.scopes.last_mut().unwrap().insert(var.clone(), Type::Trit);
                self.check_stmt(body)?;
                self.scopes.pop();
                Ok(())
            }

            Stmt::WhileTernary { condition, on_pos, on_zero, on_neg } => {
                let cond_ty = self.infer_expr_type(condition)?;
                if cond_ty != Type::Trit {
                    return Err(SemanticError::TypeMismatch { expected: Type::Trit, found: cond_ty });
                }
                self.check_stmt(on_pos)?;
                self.check_stmt(on_zero)?;
                self.check_stmt(on_neg)?;
                Ok(())
            }

            Stmt::Loop { body }   => self.check_stmt(body),
            Stmt::Break           => Ok(()),
            Stmt::Continue        => Ok(()),
            Stmt::Use { .. }      => Ok(()),
            Stmt::FromImport { .. } => Ok(()),

            Stmt::Send { target, message } => {
                self.infer_expr_type(target)?;
                self.infer_expr_type(message)?;
                Ok(())
            }

            Stmt::FieldSet { object, field, value } => {
                let obj_ty = self.lookup_var(object)?;
                if let Type::Named(struct_name) = obj_ty {
                    let field_ty = self.lookup_field(&struct_name, field)?;
                    let val_ty   = self.infer_expr_type(value)?;
                    if val_ty != field_ty {
                        return Err(SemanticError::TypeMismatch { expected: field_ty, found: val_ty });
                    }
                } else {
                    self.infer_expr_type(value)?;
                }
                Ok(())
            }

            Stmt::IndexSet { object, row, col, value } => {
                self.lookup_var(object)?;
                self.infer_expr_type(row)?;
                self.infer_expr_type(col)?;
                self.infer_expr_type(value)?;
                Ok(())
            }

            Stmt::Set { name, value } => {
                let var_ty = self.lookup_var(name)?;
                let val_ty = self.infer_expr_type(value)?;
                let ok = var_ty == val_ty
                    || matches!(value, Expr::Cast { .. })
                    || (var_ty == Type::Int && val_ty == Type::Trit)
                    || (var_ty == Type::Trit && val_ty == Type::Int);
                if !ok {
                    return Err(SemanticError::TypeMismatch { expected: var_ty, found: val_ty });
                }
                Ok(())
            }
        }
    }

    // ── Expression type inference ─────────────────────────────────────────────

    fn infer_expr_type(&self, expr: &Expr) -> Result<Type, SemanticError> {
        match expr {
            Expr::TritLiteral(_)   => Ok(Type::Trit),
            Expr::IntLiteral(_)    => Ok(Type::Int),
            Expr::FloatLiteral(_)  => Ok(Type::Float),
            Expr::StringLiteral(_) => Ok(Type::String),
            Expr::Ident(name)      => self.lookup_var(name),

            Expr::BinaryOp { op, lhs, rhs } => {
                let l = self.infer_expr_type(lhs)?;
                let r = self.infer_expr_type(rhs)?;
                match op {
                    BinOp::Less | BinOp::Greater | BinOp::LessEqual | BinOp::GreaterEqual | BinOp::Equal | BinOp::NotEqual | BinOp::And | BinOp::Or => {
                        Ok(Type::Trit)
                    }

                    _ => {
                        // Allow cross-type Numeric operations (Int vs Trit)
                        let is_numeric = |t: &Type| matches!(t, Type::Int | Type::Trit | Type::Float);
                        if is_numeric(&l) && is_numeric(&r) {
                            if l == Type::Float || r == Type::Float { return Ok(Type::Float); }
                            if l == Type::Int || r == Type::Int { return Ok(Type::Int); }
                            return Ok(Type::Trit);
                        }

                        if l != r {
                            return Err(SemanticError::TypeMismatch { expected: l, found: r });
                        }
                        Ok(l)
                    }
                }
            }

            Expr::UnaryOp { expr, .. } => self.infer_expr_type(expr),

            Expr::Call { callee, args } => {
                let sig = self.func_signatures.get(callee.as_str())
                    .ok_or_else(|| SemanticError::UndefinedFunction(callee.clone()))?
                    .clone();

                // Argument arity + type checking (only for exact signatures).
                if let Some(param_types) = &sig.params {
                    if args.len() != param_types.len() {
                        return Err(SemanticError::ArgCountMismatch {
                            function: callee.clone(),
                            expected: param_types.len(),
                            found:    args.len(),
                        });
                    }
                    for (i, (arg, expected_ty)) in args.iter().zip(param_types.iter()).enumerate() {
                        let found_ty = self.infer_expr_type(arg)?;
                        // Allow TritTensor shape flexibility and cast coercion.
                        let ok = found_ty == *expected_ty
                            || matches!(arg, Expr::Cast { .. })
                            || (expected_ty == &Type::Int && found_ty == Type::Trit)
                            || (expected_ty == &Type::Trit && found_ty == Type::Int)
                            || (matches!(expected_ty, Type::TritTensor { .. })
                                && matches!(found_ty, Type::TritTensor { .. }))
                            || (matches!(expected_ty, Type::Named(_)) && found_ty == Type::Trit);
                        if !ok {
                            return Err(SemanticError::ArgTypeMismatch {
                                function:    callee.clone(),
                                param_index: i,
                                expected:    expected_ty.clone(),
                                found:       found_ty,
                            });
                        }
                    }
                } else {
                    // Variadic — still infer arg types to catch undefined variables.
                    for arg in args { self.infer_expr_type(arg)?; }
                }

                Ok(sig.return_type)
            }

            Expr::Cast { ty, .. }     => Ok(ty.clone()),
            Expr::Spawn { .. }        => Ok(Type::AgentRef),
            Expr::Await { .. }        => Ok(Type::Trit),
            Expr::NodeId              => Ok(Type::String),

            Expr::Propagate { expr } => {
                let inner = self.infer_expr_type(expr)?;
                if inner != Type::Trit {
                    return Err(SemanticError::PropagateOnNonTrit { found: inner });
                }
                Ok(Type::Trit)
            }

            Expr::TritTensorLiteral(vals) => {
                Ok(Type::TritTensor { dims: vec![vals.len()] })
            }

            Expr::StructLiteral { name, fields } => {
                // Verify struct exists and fields match
                let def = self.struct_defs.get(name)
                    .ok_or_else(|| SemanticError::UndefinedStruct(name.clone()))?;
                
                if fields.len() != def.len() {
                    return Err(SemanticError::ArgCountMismatch { 
                        function: name.clone(), 
                        expected: def.len(), 
                        found: fields.len() 
                    });
                }

                for (f_name, f_val) in fields {
                    let expected_f_ty = def.iter()
                        .find(|(n, _)| n == f_name)
                        .ok_or_else(|| SemanticError::UndefinedField { 
                            struct_name: name.clone(), 
                            field: f_name.clone() 
                        })?
                        .1.clone();
                    let found_f_ty = self.infer_expr_type(f_val)?;
                    if found_f_ty != expected_f_ty {
                        return Err(SemanticError::TypeMismatch { 
                            expected: expected_f_ty, 
                            found: found_f_ty 
                        });
                    }
                }
                Ok(Type::Named(name.clone()))
            }

            Expr::FieldAccess { object, field } => {
                let obj_ty = self.infer_expr_type(object)?;
                if let Type::Named(struct_name) = obj_ty {
                    self.lookup_field(&struct_name, field)
                } else {
                    Ok(Type::Trit)
                }
            }

            Expr::Index { object, row, col } => {
                self.infer_expr_type(object)?;
                self.infer_expr_type(row)?;
                self.infer_expr_type(col)?;
                Ok(Type::Trit)
            }

            Expr::Slice { object, start, end, stride } => {
                self.infer_expr_type(object)?;
                self.infer_expr_type(start)?;
                self.infer_expr_type(end)?;
                self.infer_expr_type(stride)?;
                // Returns a 1D view (TensorView is treated as TensorRef in types)
                Ok(Type::TritTensor { dims: vec![0] })
            }
        }
    }

    // ── Scope helpers ─────────────────────────────────────────────────────────

    fn lookup_var(&self, name: &str) -> Result<Type, SemanticError> {
        for scope in self.scopes.iter().rev() {
            if let Some(ty) = scope.get(name) { return Ok(ty.clone()); }
        }
        Err(SemanticError::UndefinedVariable(name.to_string()))
    }

    fn lookup_field(&self, struct_name: &str, field: &str) -> Result<Type, SemanticError> {
        let fields = self.struct_defs.get(struct_name)
            .ok_or_else(|| SemanticError::UndefinedStruct(struct_name.to_string()))?;
        fields.iter()
            .find(|(f, _)| f == field)
            .map(|(_, ty)| ty.clone())
            .ok_or_else(|| SemanticError::UndefinedField {
                struct_name: struct_name.to_string(),
                field: field.to_string(),
            })
    }
}

// ─── Tests ───────────────────────────────────────────────────────────────────

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

    fn check(src: &str) -> Result<(), SemanticError> {
        let mut parser = Parser::new(src);
        let prog = parser.parse_program().expect("parse failed");
        let mut analyzer = SemanticAnalyzer::new();
        analyzer.check_program(&prog)
    }

    fn check_ok(src: &str) {
        assert!(check(src).is_ok(), "expected ok, got: {:?}", check(src));
    }

    fn check_err(src: &str) {
        assert!(check(src).is_err(), "expected error but check passed");
    }

    // ── Return type validation ────────────────────────────────────────────────

    #[test]
    fn test_return_correct_type() {
        check_ok("fn f() -> trit { return 1; }");
    }

    #[test]
    fn test_return_int_in_trit_fn() {
        // Now allowed via implicit coercion
        check_ok("fn f() -> trit { let x: int = 42; return x; }");
    }

    #[test]
    fn test_return_trit_in_trit_fn() {
        check_ok("fn decide(a: trit, b: trit) -> trit { return consensus(a, b); }");
    }

    // ── Argument count checking ───────────────────────────────────────────────

    #[test]
    fn test_call_correct_arity() {
        check_ok("fn f() -> trit { return consensus(1, -1); }");
    }

    #[test]
    fn test_call_too_few_args_caught() {
        check_err("fn f() -> trit { return consensus(1); }");
    }

    #[test]
    fn test_call_too_many_args_caught() {
        check_err("fn f() -> trit { return invert(1, 1); }");
    }

    // ── Argument type checking ────────────────────────────────────────────────

    #[test]
    fn test_call_int_arg_in_trit_fn() {
        // Now allowed via implicit coercion
        check_ok("fn f(a: trit) -> trit { return invert(a); } fn main() -> trit { let x: int = 42; return f(x); }");
    }

    #[test]
    fn test_call_correct_arg_type() {
        check_ok("fn f(a: trit) -> trit { return invert(a); }");
    }

    // ── Undefined function ────────────────────────────────────────────────────

    #[test]
    fn test_undefined_function_caught() {
        check_err("fn f() -> trit { return doesnt_exist(1); }");
    }

    // ── User-defined function forward references ──────────────────────────────

    #[test]
    fn test_user_fn_return_type_registered() {
        check_ok("fn helper(a: trit) -> trit { return invert(a); } fn main() -> trit { return helper(1); }");
    }

    #[test]
    fn test_user_fn_int_return_ok() {
        // Now allowed
        check_ok("fn helper(a: trit) -> trit { let x: int = 1; return x; }");
    }

    // ── Undefined variable ────────────────────────────────────────────────────

    #[test]
    fn test_undefined_variable_caught() {
        check_err("fn f() -> trit { return ghost_var; }");
    }

    #[test]
    fn test_defined_variable_ok() {
        check_ok("fn f() -> trit { let x: trit = 1; return x; }");
    }

    // ── Struct field types ────────────────────────────────────────────────────

    #[test]
    fn test_struct_field_access_ok() {
        check_ok("struct S { val: trit } fn f(s: S) -> trit { return s.val; }");
    }
}