make objects work better, especially destructuring

This commit is contained in:
Mark
2024-09-28 01:51:20 +02:00
parent 9c8e918440
commit c17ea580b2
41 changed files with 899 additions and 453 deletions

View File

@@ -7,29 +7,46 @@ use crate::{
};
pub fn to_mers_func(
out: impl Fn(&Type) -> Result<Type, CheckError> + Send + Sync + 'static,
run: impl Fn(Data) -> Result<Data, CheckError> + Send + Sync + 'static,
out: impl Fn(&Type, &mut crate::program::run::CheckInfo) -> Result<Type, CheckError>
+ Send
+ Sync
+ 'static,
run: impl Fn(Data, &mut crate::program::run::Info) -> Result<Data, CheckError>
+ Send
+ Sync
+ 'static,
) -> data::function::Function {
data::function::Function {
info: Info::neverused(),
info_check: Arc::new(Mutex::new(Info::neverused())),
out: Ok(Arc::new(move |a, _| out(a))),
run: Arc::new(move |a, _| run(a)),
out: Ok(Arc::new(move |a, i| out(a, i))),
run: Arc::new(move |a, i| run(a, i)),
inner_statements: None,
}
}
pub fn to_mers_func_with_in_type(
in_type: Type,
out: impl Fn(&Type) -> Result<Type, CheckError> + Send + Sync + 'static,
run: impl Fn(Data) -> Result<Data, CheckError> + Send + Sync + 'static,
out: impl Fn(&Type, &mut crate::program::run::CheckInfo) -> Result<Type, CheckError>
+ Send
+ Sync
+ 'static,
run: impl Fn(Data, &mut crate::program::run::Info) -> Result<Data, CheckError>
+ Send
+ Sync
+ 'static,
) -> data::function::Function {
to_mers_func(
move |a| {
move |a, i| {
if a.is_included_in(&in_type) {
out(a)
out(a, i)
} else {
Err(format!("Function argument must be {in_type}, but was {a}.").into())
Err(format!(
"Function argument must be {}, but was {}.",
in_type.with_info(i),
a.with_info(i)
)
.into())
}
},
run,
@@ -39,16 +56,19 @@ pub fn to_mers_func_with_in_type(
pub fn to_mers_func_with_in_out_types(
in_type: Type,
out_type: Type,
run: impl Fn(Data) -> Result<Data, CheckError> + Send + Sync + 'static,
run: impl Fn(Data, &mut crate::program::run::Info) -> Result<Data, CheckError>
+ Send
+ Sync
+ 'static,
) -> data::function::Function {
data::function::Function::new_static(vec![(in_type, out_type)], move |a, _| run(a))
data::function::Function::new_static(vec![(in_type, out_type)], move |a, i| run(a, i))
}
pub fn to_mers_func_concrete_string_to_any(
out_type: Type,
f: impl Fn(&str) -> Result<Data, CheckError> + Send + Sync + 'static,
) -> data::function::Function {
to_mers_func_with_in_out_types(Type::new(data::string::StringT), out_type, move |a| {
to_mers_func_with_in_out_types(Type::new(data::string::StringT), out_type, move |a, _| {
f(a.get()
.as_any()
.downcast_ref::<data::string::String>()
@@ -75,7 +95,7 @@ pub fn to_mers_func_concrete_string_string_to_any(
Type::new(data::string::StringT),
])),
out_type,
move |a| {
move |a, _| {
let a = a.get();
let a = &a.as_any().downcast_ref::<data::tuple::Tuple>().unwrap().0;
let l = a[0].get();