dsl gets the gordian treatment

This commit is contained in:
🪞👃🪞 2025-05-26 22:49:55 +03:00
parent 93b1cf1a5c
commit d6e8be6ce5
9 changed files with 540 additions and 456 deletions

View file

@ -1,40 +1,47 @@
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<Ast>,
binding: Ast,
}
impl<S> InputLayers<S> {
pub fn new (layer: Ast) -> Self {
#[derive(Default, Debug)] pub struct InputLayers(Vec<InputLayer>);
#[derive(Default, Debug)] pub struct InputLayer(Option<Cst<'static>>, Cst<'static>);
impl InputLayers {
pub fn new (layer: Cst<'static>) -> Self {
Self(vec![]).layer(layer)
}
pub fn layer (mut self, layer: Ast) -> Self {
pub fn layer (mut self, layer: Cst<'static>) -> Self {
self.add_layer(layer); self
}
pub fn layer_if (mut self, condition: Ast, layer: Ast) -> Self {
pub fn layer_if (mut self, condition: Cst<'static>, layer: Cst<'static>) -> Self {
self.add_layer_if(Some(condition), layer); self
}
pub fn add_layer (&mut self, layer: Ast) -> &mut Self {
pub fn add_layer (&mut self, layer: Cst<'static>) -> &mut Self {
self.add_layer_if(None, layer.into()); self
}
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
pub fn add_layer_if (&mut self, condition: Option<Cst<'static>>, binding: Cst<'static>) -> &mut Self {
self.0.push(InputLayer { condition, binding, __: Default::default() });
self
}
pub fn handle <S: Eval<dyn Ast, bool>, I, O: Command<S>> (
&self, state: &mut S, input: I
) -> Perhaps<O> {
InputHandle(state, self.0.as_slice()).try_eval(input)
}
}
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() {
pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
impl<'a, S: Eval<&'a dyn Ast, bool>, I, O: Command<S>> Eval<I, O> for InputHandle<'a, S> {
fn try_eval (&self, input: I) -> Perhaps<O> {
let Self(state, layers) = self;
for InputLayer(condition, binding) in layers.iter() {
let mut matches = true;
if let Some(condition) = condition {
matches = state.eval(condition.clone(), ||"input: no condition")?;
matches = state.eval(*condition, ||"input: no condition")?;
}
if matches {
if let Ast::Exp(e) = binding {
if let AstValue::Exp(e) = binding.peek() {
if let Some(ast) = e.peek() {
if input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
&& let Some(command) = state.try_eval(ast)? {