AstToken -> Ast <- AstValue

This commit is contained in:
🪞👃🪞 2025-05-26 23:32:59 +03:00
parent f77139c8fd
commit e9d4c7e0bc
5 changed files with 30 additions and 40 deletions

View file

@ -4,35 +4,33 @@ use std::fmt::Debug;
#[derive(Default, Debug)] pub struct InputLayers(Vec<InputLayer>);
#[derive(Default, Debug)] pub struct InputLayer(Option<Cst<'static>>, Cst<'static>);
#[derive(Default, Debug)] pub struct InputLayer(Option<Ast>, Ast);
impl InputLayers {
pub fn new (layer: Cst<'static>) -> Self {
pub fn new (layer: Ast) -> Self {
Self(vec![]).layer(layer)
}
pub fn layer (mut self, layer: Cst<'static>) -> Self {
pub fn layer (mut self, layer: Ast) -> Self {
self.add_layer(layer); self
}
pub fn layer_if (mut self, condition: Cst<'static>, layer: Cst<'static>) -> 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: Cst<'static>) -> &mut 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<Cst<'static>>, binding: Cst<'static>) -> &mut Self {
self.0.push(InputLayer { condition, binding, __: Default::default() });
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
self.0.push(InputLayer(condition, binding));
self
}
pub fn handle <S: Eval<dyn Ast, bool>, I, O: Command<S>> (
&self, state: &mut S, input: I
) -> Perhaps<O> {
pub fn handle <S, I, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
InputHandle(state, self.0.as_slice()).try_eval(input)
}
}
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> {
impl<'a, S, 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() {