mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-15 00:18:06 -05:00
wip generators
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
|
||||
|
||||
pub static EVERY: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(EveryState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct EveryState {}
|
||||
|
||||
impl ArrayMappingState for EveryState {
|
||||
@@ -20,4 +20,8 @@ impl ArrayMappingState for EveryState {
|
||||
fn finish(&mut self) -> Val {
|
||||
Val::Bool(true)
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub static FILTER: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FilterState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct FilterState {
|
||||
filter_results: Vec<Val>,
|
||||
}
|
||||
@@ -27,4 +27,8 @@ impl ArrayMappingState for FilterState {
|
||||
std::mem::swap(&mut self.filter_results, &mut filter_results);
|
||||
filter_results.to_val()
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
|
||||
|
||||
pub static FIND: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FindState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct FindState {}
|
||||
|
||||
impl ArrayMappingState for FindState {
|
||||
@@ -20,4 +20,8 @@ impl ArrayMappingState for FindState {
|
||||
fn finish(&mut self) -> Val {
|
||||
Val::Undefined
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
|
||||
|
||||
pub static FIND_INDEX: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FindIndexState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct FindIndexState {}
|
||||
|
||||
impl ArrayMappingState for FindIndexState {
|
||||
@@ -20,4 +20,8 @@ impl ArrayMappingState for FindIndexState {
|
||||
fn finish(&mut self) -> Val {
|
||||
Val::Number(-1f64)
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub static FLAT_MAP: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FlatMapState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct FlatMapState {
|
||||
flat_map_results: Vec<Val>,
|
||||
}
|
||||
@@ -32,4 +32,8 @@ impl ArrayMappingState for FlatMapState {
|
||||
std::mem::swap(&mut self.flat_map_results, &mut flat_map_results);
|
||||
flat_map_results.to_val()
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ pub static MAP: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(MapState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct MapState {
|
||||
map_results: Vec<Val>,
|
||||
}
|
||||
@@ -24,4 +24,8 @@ impl ArrayMappingState for MapState {
|
||||
std::mem::swap(&mut self.map_results, &mut map_results);
|
||||
map_results.to_val()
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,24 @@ use std::rc::Rc;
|
||||
|
||||
use crate::builtins::type_error_builtin::ToTypeError;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::stack_frame::FrameStepResult;
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, StackFrameTrait};
|
||||
use crate::stack_frame::{FrameStepResult, StackFrame};
|
||||
use crate::vs_array::VsArray;
|
||||
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
|
||||
|
||||
pub trait ArrayMappingState {
|
||||
fn process(&mut self, i: usize, element: &Val, mapped: Val) -> Option<Val>;
|
||||
fn finish(&mut self) -> Val;
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState>;
|
||||
}
|
||||
|
||||
impl Clone for Box<dyn ArrayMappingState> {
|
||||
fn clone(&self) -> Self {
|
||||
self.clone_to_array_mapping_state()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ArrayMappingFrame {
|
||||
state: Box<dyn ArrayMappingState>,
|
||||
early_exit: Option<Result<Val, Val>>,
|
||||
@@ -141,4 +149,8 @@ impl StackFrameTrait for ArrayMappingFrame {
|
||||
fn catch_exception(&mut self, _exception: Val) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
fn clone_to_stack_frame(&self) -> StackFrame {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::rc::Rc;
|
||||
use crate::builtins::type_error_builtin::ToTypeError;
|
||||
use crate::native_frame_function::NativeFrameFunction;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrameTrait};
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrame, StackFrameTrait};
|
||||
use crate::vs_array::VsArray;
|
||||
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
|
||||
|
||||
@@ -19,6 +19,7 @@ pub static REDUCE: NativeFrameFunction = NativeFrameFunction {
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ReduceFrame {
|
||||
this: Option<Rc<VsArray>>,
|
||||
array_i: usize,
|
||||
@@ -113,4 +114,8 @@ impl StackFrameTrait for ReduceFrame {
|
||||
fn catch_exception(&mut self, _exception: Val) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
fn clone_to_stack_frame(&self) -> StackFrame {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::rc::Rc;
|
||||
use crate::builtins::type_error_builtin::ToTypeError;
|
||||
use crate::native_frame_function::NativeFrameFunction;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrameTrait};
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrame, StackFrameTrait};
|
||||
use crate::vs_array::VsArray;
|
||||
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
|
||||
|
||||
@@ -19,6 +19,7 @@ pub static REDUCE_RIGHT: NativeFrameFunction = NativeFrameFunction {
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct ReduceRightFrame {
|
||||
this: Option<Rc<VsArray>>,
|
||||
array_i: usize,
|
||||
@@ -124,6 +125,10 @@ impl StackFrameTrait for ReduceRightFrame {
|
||||
}
|
||||
|
||||
fn catch_exception(&mut self, _exception: Val) -> bool {
|
||||
return false;
|
||||
false
|
||||
}
|
||||
|
||||
fn clone_to_stack_frame(&self) -> StackFrame {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::super::native_frame_function::NativeFrameFunction;
|
||||
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
|
||||
use super::super::vs_value::{Val, ValTrait};
|
||||
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
|
||||
|
||||
pub static SOME: NativeFrameFunction = NativeFrameFunction {
|
||||
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(SomeState::default()))),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Clone)]
|
||||
struct SomeState {}
|
||||
|
||||
impl ArrayMappingState for SomeState {
|
||||
@@ -20,4 +20,8 @@ impl ArrayMappingState for SomeState {
|
||||
fn finish(&mut self) -> Val {
|
||||
Val::Bool(false)
|
||||
}
|
||||
|
||||
fn clone_to_array_mapping_state(&self) -> Box<dyn ArrayMappingState> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ use std::rc::Rc;
|
||||
use crate::builtins::type_error_builtin::ToTypeError;
|
||||
use crate::native_frame_function::NativeFrameFunction;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::stack_frame::FrameStepResult;
|
||||
use crate::stack_frame::{CallResult, FrameStepOk, StackFrameTrait};
|
||||
use crate::stack_frame::{FrameStepResult, StackFrame};
|
||||
use crate::vs_array::VsArray;
|
||||
use crate::vs_value::{LoadFunctionResult, ToVal, Val, ValTrait};
|
||||
|
||||
@@ -22,6 +22,7 @@ pub static SORT: NativeFrameFunction = NativeFrameFunction {
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SortFrame {
|
||||
this: Option<Rc<VsArray>>,
|
||||
|
||||
@@ -32,6 +33,7 @@ struct SortFrame {
|
||||
started: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct VecPos<T> {
|
||||
vec: Vec<T>,
|
||||
pos: usize,
|
||||
@@ -43,6 +45,7 @@ struct VecSlice<'a, T> {
|
||||
end: usize,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SortTreeNode {
|
||||
data: SortTreeNodeData,
|
||||
}
|
||||
@@ -199,6 +202,7 @@ impl SortTreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum SortTreeNodeData {
|
||||
Branch(Box<SortTreeNode>, Box<SortTreeNode>),
|
||||
Sorting(Vec<Val>, VecPos<Val>, VecPos<Val>),
|
||||
@@ -318,4 +322,8 @@ impl StackFrameTrait for SortFrame {
|
||||
fn catch_exception(&mut self, _exception: Val) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn clone_to_stack_frame(&self) -> StackFrame {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user