rl_lang/checker/structs.rs
1//! Core data structures for the type checker.
2//!
3use std::{
4 collections::{HashMap, HashSet},
5 path::PathBuf,
6};
7
8use crate::{
9 ast::{Ast, statements::TypeAnnotation},
10 interpreter::native::Module,
11 utils::{errors::Error, source::SourceFile, span::Span},
12};
13
14/// The stateful type checker, threaded through the entire AST walk.
15pub struct TypeChecker {
16 /// Stack of scopes, each mapping variable/function names to their [`ScopeItem`].
17 pub scopes: Vec<HashMap<String, ScopeItem>>,
18 /// Source file attached for Ariadne error rendering; `None` in LSP-less contexts.
19 pub source_file: Option<SourceFile>,
20 /// The stdlib module tree, used to resolve stdlib call paths.
21 pub root_module: Module,
22 /// All type errors accumulated during the check pass.
23 pub errors: Vec<Error>,
24 /// Stack of expected return types, pushed/popped on function and lambda entry/exit.
25 pub return_type_stack: Vec<TypeAnnotation>,
26 /// Nesting depth of loops - used to validate `break` and `continue`.
27 pub loop_depth: u32,
28 /// Flat set of all stdlib function names for fast single-name lookup.
29 pub stdlib_fn_names: std::collections::HashSet<String>,
30 /// `(span, markdown)` pairs collected at every declaration and usage site,
31 /// consumed by the LSP hover provider.
32 pub hovers: Vec<(Span, String)>,
33 pub base_dir: Option<PathBuf>,
34 pub importing: Vec<PathBuf>,
35 pub imported: HashSet<PathBuf>,
36 pub ast_arena: Ast,
37 /// Maps `record` type names to their declared `(field name, field type)` list.
38 pub records: HashMap<String, Vec<(String, TypeAnnotation)>>,
39 /// Maps `tag` (enum) type names to their declared variant name list.
40 pub tags: HashMap<String, Vec<String>>,
41}
42
43/// A single entry in a type checker scope.
44pub struct ScopeItem {
45 /// The static type of this variable or function.
46 pub type_annotation: CheckType,
47 /// Whether this binding is immutable (`CONST`).
48 pub is_const: bool,
49}
50
51/// The type of a value as seen by the static checker.
52#[derive(Debug, Clone, PartialEq)]
53pub enum CheckType {
54 /// A fully resolved type (e.g. `int`, `arr[string]`).
55 Known(TypeAnnotation),
56 /// A function type with known parameter and return types.
57 Function {
58 params: Vec<TypeAnnotation>,
59 return_type: TypeAnnotation,
60 },
61 /// Type could not be determined statically (stdlib calls, unresolved names).
62 /// Propagates silently to avoid cascading false errors.
63 Unknown,
64}