Introduce closure AST node
The `Closure` struct has been moved from `ast/vm.rs` to a new module `ast/closure.rs`. This improves the organization of the AST nodes. The `Closure` struct represents a compiled function body along with its captured upvalues, which is a key feature for Myc Script.
This commit is contained in:
+2
-49
@@ -1,58 +1,11 @@
|
||||
use crate::ast::nodes::{Address, AnalyzedNode, ExecNode, IdentifierBinding, NodeKind, StackOffset};
|
||||
use crate::ast::closure::Closure;
|
||||
use crate::ast::nodes::{Address, ExecNode, IdentifierBinding, NodeKind, StackOffset};
|
||||
use crate::ast::rtl::series::{RecordSeries, SeriesView};
|
||||
use crate::ast::rtl::streams::{build_map_stream, build_pipeline_node, StreamNode};
|
||||
use crate::ast::types::{Object, PipeFn, Value};
|
||||
use std::any::Any;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Closure {
|
||||
/// The executable parameter pattern.
|
||||
pub parameter_node: Rc<ExecNode>,
|
||||
/// The analyzed body (before TCO).
|
||||
pub function_node: Rc<AnalyzedNode>,
|
||||
/// The executable node (after TCO).
|
||||
pub exec_node: Rc<ExecNode>,
|
||||
pub upvalues: Vec<Rc<RefCell<Value>>>,
|
||||
pub positional_count: Option<u32>,
|
||||
/// The number of stack slots required for this closure (calculated late).
|
||||
pub stack_size: u32,
|
||||
}
|
||||
|
||||
impl Closure {
|
||||
#[inline]
|
||||
pub fn new(
|
||||
params: Rc<ExecNode>,
|
||||
body: Rc<AnalyzedNode>,
|
||||
exec: Rc<ExecNode>,
|
||||
upvalues: Vec<Rc<RefCell<Value>>>,
|
||||
positional_count: Option<u32>,
|
||||
stack_size: u32,
|
||||
) -> Self {
|
||||
Self {
|
||||
parameter_node: params,
|
||||
function_node: body,
|
||||
exec_node: exec,
|
||||
upvalues,
|
||||
positional_count,
|
||||
stack_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Object for Closure {
|
||||
fn type_name(&self) -> &'static str {
|
||||
"closure"
|
||||
}
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn into_rc_any(self: Rc<Self>) -> Rc<dyn Any> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CallFrame {
|
||||
stack_base: usize,
|
||||
|
||||
Reference in New Issue
Block a user