mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
This commit is contained in:
parent
17506726cb
commit
c8827b43c3
9 changed files with 114 additions and 135 deletions
|
|
@ -4,7 +4,10 @@ use std::fmt::Debug;
|
|||
|
||||
#[derive(Default, Debug)] pub struct InputLayers(Vec<InputLayer>);
|
||||
|
||||
#[derive(Default, Debug)] pub struct InputLayer(Option<Ast>, Ast);
|
||||
#[derive(Default, Debug)] pub struct InputLayer {
|
||||
condition: Option<Ast>,
|
||||
bindings: Ast,
|
||||
}
|
||||
|
||||
impl InputLayers {
|
||||
pub fn new (layer: Ast) -> Self {
|
||||
|
|
@ -19,8 +22,8 @@ impl InputLayers {
|
|||
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));
|
||||
pub fn add_layer_if (&mut self, condition: Option<Ast>, bindings: Ast) -> &mut Self {
|
||||
self.0.push(InputLayer { condition, bindings });
|
||||
self
|
||||
}
|
||||
pub fn handle <S: Eval<Ast, bool> + Eval<Ast, O>, I: Eval<Ast, bool>, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
|
||||
|
|
@ -33,13 +36,13 @@ pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
|
|||
impl<'a, S: Eval<Ast, bool> + Eval<Ast, O>, I: Eval<Ast, bool>, 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() {
|
||||
for InputLayer { condition, bindings } in layers.iter() {
|
||||
let mut matches = true;
|
||||
if let Some(condition) = condition {
|
||||
matches = state.eval(condition.clone(), ||"input: no condition")?;
|
||||
}
|
||||
if matches
|
||||
&& let Some(exp) = binding.exp()
|
||||
&& let Some(exp) = bindings.exp()
|
||||
&& let Some(ast) = exp.peek()
|
||||
&& input.eval(ast.clone(), ||"InputLayers: input.eval(binding) failed")?
|
||||
&& let Some(command) = state.try_eval(ast)? {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue