add loop syntax, remove loop function, change CLI

This commit is contained in:
Mark
2024-02-22 13:34:02 +01:00
parent 66c191ba2c
commit f3f2c13702
8 changed files with 133 additions and 131 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "mers_lib"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "library to use the mers language in other projects"

View File

@@ -1,7 +1,7 @@
use std::sync::Arc;
use mers_lib::{
data::{self, Data, MersType, Type},
data::{self, Data, Type},
errors::CheckError,
prelude_compile::{parse, CompInfo, Config, Source},
};

View File

@@ -440,6 +440,23 @@ pub fn parse_no_chain(
on_false,
})
}
"loop" => {
src.section_begin("loop".to_string());
src.skip_whitespace();
let inner = match parse(src, srca) {
Ok(Some(v)) => v,
Ok(None) => {
return Err(CheckError::new()
.src(vec![((pos_in_src, src.get_pos(), srca).into(), None)])
.msg(format!("EOF after `loop`")))
}
Err(e) => return Err(e),
};
Box::new(program::parsed::r#loop::Loop {
pos_in_src: (pos_in_src, src.get_pos(), srca).into(),
inner,
})
}
"true" => Box::new(program::parsed::value::Value {
pos_in_src: (pos_in_src, src.get_pos(), srca).into(),
data: Data::new(crate::data::bool::Bool(true)),

View File

@@ -137,55 +137,6 @@ impl Config {
}),
inner_statements: None,
}),
).add_var(
"loop".to_string(),
Data::new(data::function::Function {
info: Arc::new(Info::neverused()),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
out: Arc::new(|a, _i| {
let mut o = Type::empty();
for t in a.types.iter().flat_map(|v| if let Some(t) = v.as_any().downcast_ref::<data::tuple::TupleT>() {
if let Some(t) = t.0.get(1) {
t.types.iter().collect::<Vec<_>>()
} else { [v].into_iter().collect() }
} else { [v].into_iter().collect() }) {
if let Some(t) = t.as_any().downcast_ref::<data::function::FunctionT>() {
for t in t.o(&Type::empty_tuple())?.types {
if let Some(t) = t.as_any().downcast_ref::<data::tuple::TupleT>() {
if t.0.len() > 1 {
return Err(format!("called loop with funcion that might return a tuple of length > 1").into());
} else if let Some(v) = t.0.first() {
o.add(Arc::new(v.clone()))
}
} else {
return Err(format!("called loop with funcion that might return something other than a tuple").into());
}
}
} else {
return Err(format!("called loop on a non-function").into());
}
}
Ok(o)
}),
run: Arc::new(|a, _i| {
let a = a.get();
let delay_drop;
let function = if let Some(function) = a.as_any().downcast_ref::<data::function::Function>() {
function
} else if let Some(r) = a.as_any().downcast_ref::<data::tuple::Tuple>() {
delay_drop = r.0[1].get();
delay_drop.as_any().downcast_ref::<data::function::Function>().unwrap()
} else {
unreachable!("called loop on non-function")
};
loop {
if let Some(r) = function.run(Data::empty_tuple()).one_tuple_content() {
break r;
}
}
}),
inner_statements: None,
}),
)
.add_var(
"eq".to_string(),

View File

@@ -28,6 +28,8 @@ pub mod include_mers;
#[cfg(feature = "parse")]
pub mod init_to;
#[cfg(feature = "parse")]
pub mod r#loop;
#[cfg(feature = "parse")]
pub mod object;
#[cfg(feature = "parse")]
pub mod tuple;

View File

@@ -25,6 +25,8 @@ pub mod function;
#[cfg(feature = "run")]
pub mod r#if;
#[cfg(feature = "run")]
pub mod r#loop;
#[cfg(feature = "run")]
pub mod object;
#[cfg(feature = "run")]
pub mod tuple;