wip: dsl: more rework

This commit is contained in:
🪞👃🪞 2025-05-26 01:30:13 +03:00
parent f1b24d436a
commit 7097333993
4 changed files with 145 additions and 159 deletions

View file

@ -1,67 +1,53 @@
use crate::*;
use std::marker::PhantomData;
use std::fmt::Debug;
/// List of input layers with optional conditional filters.
#[derive(Default, Debug)] pub struct InputLayers<S>(Vec<InputLayer<S>>);
#[derive(Default, Debug)] pub struct InputLayer<S>{
__: PhantomData<S>,
condition: Option<Box<dyn Ast>>,
bindings: Box<dyn Ast>,
condition: Option<Ast>,
binding: Ast,
}
impl<S> InputLayers<S> {
pub fn new (layer: Box<dyn Ast>) -> Self
{ Self(vec![]).layer(layer) }
pub fn layer (mut self, layer: Box<dyn Ast>) -> Self
{ self.add_layer(layer); self }
pub fn layer_if (mut self, condition: Box<dyn Ast>, layer: Box<dyn Ast>) -> Self
{ self.add_layer_if(Some(condition), layer); self }
pub fn add_layer (&mut self, layer: Box<dyn Ast>) -> &mut Self
{ self.add_layer_if(None, layer.into()); self }
pub fn add_layer_if (
&mut self,
condition: Option<Box<dyn Ast>>,
bindings: Box<dyn Ast>
) -> &mut Self {
self.0.push(InputLayer { condition, bindings, __: Default::default() });
pub fn new (layer: Ast) -> Self {
Self(vec![]).layer(layer)
}
pub fn layer (mut self, layer: Ast) -> Self {
self.add_layer(layer); self
}
pub fn layer_if (mut self, condition: Ast, layer: Ast) -> Self {
self.add_layer_if(Some(condition), layer); self
}
pub fn add_layer (&mut self, layer: Ast) -> &mut Self {
self.add_layer_if(None, layer.into()); self
}
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
self.0.push(InputLayer { condition, binding, __: Default::default() });
self
}
}
impl<S: Eval<I, O>, I: Input, O: Command<S>> Eval<I, O> for InputLayers<S> {
fn eval (&self, input: I) -> Perhaps<O> {
for layer in self.0.iter() {
if let Some(command) = Ok(if let Some(condition) = layer.condition.as_ref() {
if self.matches(condition)? {
self.state().eval(*self)?
} else {
None
impl<S: Eval<Ast, bool> + Eval<Ast, C>, C: Command<S>, I: Debug + Eval<Ast, bool>> Eval<(S, I), C> for InputLayers<S> {
fn try_eval (&self, (state, input): (S, I)) -> Perhaps<C> {
for InputLayer { condition, binding, .. } in self.0.iter() {
let mut matches = true;
if let Some(condition) = condition {
matches = state.eval(condition.clone(), ||"input: no condition")?;
}
if matches {
if let Ast::Exp(e) = binding {
if let Some(ast) = e.peek() {
if input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
&& let Some(command) = state.try_eval(ast)? {
return Ok(Some(command))
}
} else {
unreachable!("InputLayer")
}
} else {
panic!("InputLayer: expected expression, got: {input:?}")
}
} else {
self.state().eval(*self)?
})? {
return Ok(Some(command))
}
}
}
}
/// [Input] state that can be matched against a [CstValue].
pub trait InputState<S>: Input {
fn state (&self) -> &S;
fn matches (&self, condition: &Box<dyn Ast>) -> Usually<bool>;
}
impl<S: Eval<I, O>, I: InputState<S>, O: Command<S>> Eval<I, O>
for InputLayer<S> {
fn eval (&self, input: I) -> Perhaps<O> {
if let Some(AstToken::Exp(exp)) = iter.peek() {
let mut e = exp.clone();
if let Some(AstToken::Sym(binding)) = e.next()
&& input.matches_dsl(binding)
&& let Some(command) = Dsl::from_dsl(input.state(), e)? {
return Ok(Some(command))
} else {
unreachable!("InputLayer: expected symbol, got: {e:?}")
}
} else {
panic!("InputLayer: expected expression, got: {self:?}")
}
Ok(None)
}
}