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
+2 -16
View File
@@ -141,22 +141,8 @@ impl TypeChecker {
}
}
_ => {
// Fallback: Wrap in implicit lambda
let virtual_lambda = BoundNode {
identity: node.identity.clone(),
kind: BoundKind::Lambda {
params: Rc::new(Node {
identity: node.identity.clone(),
kind: BoundKind::Tuple { elements: vec![] },
ty: (),
}),
upvalues: vec![],
body: Rc::new(node.clone()),
positional_count: Some(0),
},
ty: (),
};
self.check(&virtual_lambda, &[], diag)
let mut root_ctx = TypeContext::new(0, vec![], &self.global_types, None);
self.check_node(node, &mut root_ctx, diag)
}
}
}
+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),
+14 -8
View File
@@ -447,22 +447,28 @@ impl VM {
}
let lambda_val = self.eval_internal(obs, lambda)?;
let lambda_obj = if let Value::Object(obj) = lambda_val {
obj
} else {
return Err("Pipe lambda must be a function/closure".to_string());
};
// Create the persistent execution closure for the PipeStream
let mut pipe_vm = crate::ast::vm::VM::new(self.globals.clone());
let my_closure = lambda_obj.clone();
let executor: Box<crate::ast::types::PipeFn> =
let executor: Box<crate::ast::types::PipeFn> = match lambda_val {
Value::Object(obj) => {
let my_closure = obj.clone();
Box::new(move |args: &[Value]| -> Value {
match pipe_vm.run_with_args(my_closure.clone(), args) {
Ok(res) => res,
Err(e) => panic!("Pipeline lambda execution failed: {}", e),
}
});
})
}
Value::Function(func) => {
let my_func = func.clone();
Box::new(move |args: &[Value]| -> Value {
(my_func.func)(args)
})
}
_ => return Err("Pipe lambda must be a function/closure".to_string()),
};
// Delegate to the RTL Factory for specialized buffer instantiation
let node =