Refactor: Simplify type checking context

The type checker's fallback logic for non-lambda nodes was unnecessarily
complex. This commit simplifies it by directly checking the node within
a new, basic type context.

Additionally, the environment now explicitly wraps non-lambda AST nodes
in a lambda before type checking, ensuring consistent handling. This
aligns the type checking process for all top-level expressions.
This commit is contained in:
Michael Schimmel
2026-03-06 23:19:52 +01:00
parent 84ef3f9aed
commit f797ac37bf
3 changed files with 59 additions and 31 deletions
+38 -2
View File
@@ -428,7 +428,25 @@ impl Environment {
LambdaCollector::collect(&bound_ast, &mut self.function_registry.borrow_mut());
let checker = TypeChecker::new(self.global_types.clone());
let typed_ast = checker.check(&bound_ast, &[], &mut diagnostics);
let wrapped_ast = if let crate::ast::compiler::bound_nodes::BoundKind::Lambda { .. } = bound_ast.kind {
bound_ast
} else {
crate::ast::compiler::bound_nodes::BoundNode {
identity: bound_ast.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Lambda {
params: std::rc::Rc::new(crate::ast::nodes::Node {
identity: bound_ast.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Tuple { elements: vec![] },
ty: (),
}),
upvalues: vec![],
body: std::rc::Rc::new(bound_ast),
positional_count: Some(0),
},
ty: (),
}
};
let typed_ast = checker.check(&wrapped_ast, &[], &mut diagnostics);
if diagnostics.has_errors() {
return Err(diagnostics
@@ -556,7 +574,25 @@ impl Environment {
LambdaCollector::collect(&bound_ast, &mut self.function_registry.borrow_mut());
let checker = TypeChecker::new(self.global_types.clone());
let typed_ast = checker.check(&bound_ast, &[], &mut diagnostics);
let wrapped_ast = if let crate::ast::compiler::bound_nodes::BoundKind::Lambda { .. } = bound_ast.kind {
bound_ast
} else {
crate::ast::compiler::bound_nodes::BoundNode {
identity: bound_ast.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Lambda {
params: std::rc::Rc::new(crate::ast::nodes::Node {
identity: bound_ast.identity.clone(),
kind: crate::ast::compiler::bound_nodes::BoundKind::Tuple { elements: vec![] },
ty: (),
}),
upvalues: vec![],
body: std::rc::Rc::new(bound_ast),
positional_count: Some(0),
},
ty: (),
}
};
let typed_ast = checker.check(&wrapped_ast, &[], &mut diagnostics);
CompilationResult {
ast: Some(typed_ast),