Refactor: Create typed series factory function
Introduces `create_typed_series` to centralize series creation logic. This function dispatches on `StaticType` to select the appropriate `SeriesStorage` implementation, improving code reuse and maintainability within the series RTL. The `SeriesHook` compiler hook is updated to use this new factory function. Additionally, new tests have been added to `rtl/streams.rs` and `tests/pipeline.rs` to specifically cover the behavior of `pipe-buffered`, focusing on its fill gate mechanism and multi-input handling.
This commit is contained in:
+26
-42
@@ -9,7 +9,24 @@ use crate::ast::compiler::call_hooks::{InferenceAccess, RtlCompilerHook};
|
||||
use crate::ast::diagnostics::Diagnostics;
|
||||
use crate::ast::environment::Environment;
|
||||
use crate::ast::nodes::{IdentifierBinding, Node, NodeKind, TypedNode, TypedPhase};
|
||||
use crate::ast::types::{NativeFunction, NodeIdentity, Purity, Signature, SourceLocation, StaticType, Value};
|
||||
use crate::ast::types::{NativeFunction, NodeIdentity, Purity, SeriesStorage, Signature, SourceLocation, StaticType, Value};
|
||||
|
||||
/// Creates a type-specialized series with the given lookback depth.
|
||||
/// Dispatches on `StaticType` to allocate the optimal storage backend:
|
||||
/// - `Float` → `ScalarSeries<f64>`
|
||||
/// - `Int`/`DateTime` → `ScalarSeries<i64>`
|
||||
/// - `Bool` → `ScalarSeries<bool>`
|
||||
/// - `Record` → `RecordSeries` (SoA layout)
|
||||
/// - Anything else → `ValueSeries` (boxed fallback)
|
||||
pub fn create_typed_series(element_type: &StaticType, lookback: usize) -> Rc<dyn SeriesStorage> {
|
||||
match element_type {
|
||||
StaticType::Float => Rc::new(ScalarSeries::<f64>::new("FloatSeries", lookback)),
|
||||
StaticType::Int | StaticType::DateTime => Rc::new(ScalarSeries::<i64>::new("IntSeries", lookback)),
|
||||
StaticType::Bool => Rc::new(ScalarSeries::<bool>::new("BoolSeries", lookback)),
|
||||
StaticType::Record(layout) => Rc::new(RecordSeries::new(Arc::clone(layout), lookback)),
|
||||
_ => Rc::new(ValueSeries::new(lookback)),
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 4. Compiler Hooks (registered via RTL bootstrap, no name coupling)
|
||||
@@ -55,47 +72,14 @@ impl RtlCompilerHook for SeriesHook {
|
||||
|
||||
// Build a factory Value::Function whose closure already knows the exact
|
||||
// storage type — no keyword encoding, no runtime dispatch.
|
||||
let factory_value: Value = match inner.as_ref() {
|
||||
StaticType::Float => Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(|args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(Rc::new(ScalarSeries::<f64>::new("FloatSeries", n)))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
})),
|
||||
StaticType::Int | StaticType::DateTime => Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(|args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(Rc::new(ScalarSeries::<i64>::new("IntSeries", n)))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
})),
|
||||
StaticType::Bool => Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(|args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(Rc::new(ScalarSeries::<bool>::new("BoolSeries", n)))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
})),
|
||||
StaticType::Text => Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(|args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(Rc::new(ValueSeries::new(n)))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
})),
|
||||
StaticType::Record(layout) => {
|
||||
let layout = Arc::clone(layout);
|
||||
Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(move |args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(Rc::new(RecordSeries::new(Arc::clone(&layout), n)))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
}))
|
||||
}
|
||||
_ => return None,
|
||||
};
|
||||
let elem_ty = inner.as_ref().clone();
|
||||
let factory_value: Value = Value::Function(Rc::new(NativeFunction {
|
||||
func: Rc::new(move |args: &[Value]| {
|
||||
let n = args[0].as_int().unwrap_or(0) as usize;
|
||||
Value::Series(create_typed_series(&elem_ty, n))
|
||||
}),
|
||||
purity: Purity::Impure,
|
||||
}));
|
||||
|
||||
let factory_node = Rc::new(Node {
|
||||
kind: NodeKind::Constant(factory_value),
|
||||
|
||||
Reference in New Issue
Block a user