diff --git a/src/ast/compiler/type_checker.rs b/src/ast/compiler/type_checker.rs index a40764d..71a3d07 100644 --- a/src/ast/compiler/type_checker.rs +++ b/src/ast/compiler/type_checker.rs @@ -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) } } } diff --git a/src/ast/environment.rs b/src/ast/environment.rs index 90f4b76..ef7d6c0 100644 --- a/src/ast/environment.rs +++ b/src/ast/environment.rs @@ -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), diff --git a/src/ast/vm.rs b/src/ast/vm.rs index 457d011..70e556c 100644 --- a/src/ast/vm.rs +++ b/src/ast/vm.rs @@ -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 = - 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), - } - }); + + let executor: Box = 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 =