mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
Compare commits
2 commits
f1b24d436a
...
93b1cf1a5c
| Author | SHA1 | Date | |
|---|---|---|---|
| 93b1cf1a5c | |||
| 7097333993 |
4 changed files with 160 additions and 182 deletions
|
|
@ -2,40 +2,40 @@ use crate::*;
|
|||
use std::sync::Arc;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
|
||||
/// Emits tokens.
|
||||
pub trait Ast: Debug {
|
||||
fn peek (&self) -> Option<AstValue>;
|
||||
fn next (&mut self) -> Option<AstValue>;
|
||||
fn rest (self) -> Option<Box<dyn Ast>>;
|
||||
}
|
||||
|
||||
/// A [Cst] can be used as an [Ast].
|
||||
impl<'source: 'static> Ast for Cst<'source> {
|
||||
fn peek (&self) -> Option<AstValue> {
|
||||
Cst::peek(self).map(|token|token.value.into())
|
||||
}
|
||||
fn next (&mut self) -> Option<AstValue> {
|
||||
Iterator::next(self).map(|token|token.value.into())
|
||||
}
|
||||
fn rest (self) -> Option<Box<dyn Ast>> {
|
||||
self.peek().is_some().then(||Box::new(self) as Box<dyn Ast>)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug)]
|
||||
pub enum AstValue {
|
||||
pub enum Ast {
|
||||
#[default] Nil,
|
||||
Err(DslError),
|
||||
Num(usize),
|
||||
Sym(Arc<str>),
|
||||
Key(Arc<str>),
|
||||
Str(Arc<str>),
|
||||
Exp(Arc<Box<dyn Ast>>),
|
||||
Exp(Arc<Box<dyn AstIter>>),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AstValue {
|
||||
/// Emits tokens.
|
||||
pub trait AstIter: Debug {
|
||||
fn peek (&self) -> Option<Ast>;
|
||||
fn next (&mut self) -> Option<Ast>;
|
||||
fn rest (self) -> Option<Box<dyn AstIter>>;
|
||||
}
|
||||
|
||||
/// A [Cst] can be used as an [Ast].
|
||||
impl<'source: 'static> AstIter for Cst<'source> {
|
||||
fn peek (&self) -> Option<Ast> {
|
||||
Cst::peek(self).map(|token|token.value.into())
|
||||
}
|
||||
fn next (&mut self) -> Option<Ast> {
|
||||
Iterator::next(self).map(|token|token.value.into())
|
||||
}
|
||||
fn rest (self) -> Option<Box<dyn AstIter>> {
|
||||
self.peek().is_some().then(||Box::new(self) as Box<dyn AstIter>)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Ast {
|
||||
fn fmt (&self, out: &mut Formatter) -> Result<(), std::fmt::Error> {
|
||||
use AstValue::*;
|
||||
use Ast::*;
|
||||
write!(out, "{}", match self {
|
||||
Nil => String::new(),
|
||||
Err(e) => format!("[error: {e}]"),
|
||||
|
|
@ -48,7 +48,7 @@ impl std::fmt::Display for AstValue {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'source: 'static> From<CstValue<'source>> for AstValue {
|
||||
impl<'source: 'static> From<CstValue<'source>> for Ast {
|
||||
fn from (other: CstValue<'source>) -> Self {
|
||||
use CstValue::*;
|
||||
match other {
|
||||
|
|
@ -63,8 +63,8 @@ impl<'source: 'static> From<CstValue<'source>> for AstValue {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'source: 'static> Into<Box<dyn Ast>> for Cst<'source> {
|
||||
fn into (self) -> Box<dyn Ast> {
|
||||
impl<'source: 'static> Into<Box<dyn AstIter>> for Cst<'source> {
|
||||
fn into (self) -> Box<dyn AstIter> {
|
||||
Box::new(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,25 +1,37 @@
|
|||
use crate::*;
|
||||
|
||||
pub trait Eval<Input, Output> {
|
||||
fn eval (&self, input: Input) -> Perhaps<Output>;
|
||||
fn try_eval (&self, input: Input) -> Perhaps<Output>;
|
||||
fn eval <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
&self, input: Input, error: F
|
||||
) -> Usually<Output> {
|
||||
if let Some(value) = self.try_eval(input)? {
|
||||
Ok(value)
|
||||
} else {
|
||||
Result::Err(format!("Eval: {}", error().into()).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// May construct [Self] from token stream.
|
||||
pub trait Dsl: Sized {
|
||||
type State;
|
||||
fn try_provide (state: Self::State, source: impl Ast) -> Perhaps<Self>;
|
||||
pub trait Dsl<State>: Sized {
|
||||
fn try_provide (state: State, source: Ast) -> Perhaps<Self>;
|
||||
fn provide <E: Into<Box<dyn std::error::Error>>, F: Fn()->E> (
|
||||
state: Self::State, source: impl Ast, error: F
|
||||
state: State, source: Ast, error: F
|
||||
) -> Usually<Self> {
|
||||
let next = source.peek().clone();
|
||||
let next = source.clone();
|
||||
if let Some(value) = Self::try_provide(state, source)? {
|
||||
Ok(value)
|
||||
} else {
|
||||
Result::Err(format!("dsl: {}: {next:?}", error().into()).into())
|
||||
Result::Err(format!("Dsl: {}: {next:?}", error().into()).into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Dsl<S>, S> Eval<Ast, T> for S {
|
||||
fn try_eval (&self, input: Ast) -> Perhaps<T> { todo!() }
|
||||
}
|
||||
|
||||
//pub trait Give<'state, 'source, Type> {
|
||||
///// Implement this to be able to [Give] [Type] from the [Cst].
|
||||
///// Advance the stream if returning `Ok<Some<Type>>`.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,150 +1,130 @@
|
|||
use crate::*;
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, When<A>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("when")) => Some(Self(
|
||||
self.provide(source, ||"when: expected condition")?,
|
||||
self.provide(source, ||"when: expected content")?,
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
if let Ast::Exp(source) = source {
|
||||
if let Some(Ast::Key(id)) = source.next() && *id == *"when" {
|
||||
return Ok(Some(Self(
|
||||
state.eval(source.next().unwrap(), ||"when: expected condition")?,
|
||||
state.eval(source.next().unwrap(), ||"when: expected content")?,
|
||||
)))
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Either<A, B>> for S where
|
||||
S: Eval<AstValue, bool> + Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(match source.peek() {
|
||||
Some(Value::Key("either")) => Some(Self(
|
||||
self.provide(source, ||"either: expected condition")?,
|
||||
self.provide(source, ||"either: expected content 1")?,
|
||||
self.provide(source, ||"either: expected content 2")?
|
||||
)),
|
||||
_ => None
|
||||
})
|
||||
impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> + Eval<Ast, B> {
|
||||
fn try_provide (state: S, mut source: Ast) -> Perhaps<Self> {
|
||||
if let Ast::Exp(mut source) = source {
|
||||
if let Some(Ast::Key(id)) = source.next() && *id == *"either" {
|
||||
return Ok(Some(Self(
|
||||
state.eval(source.next().unwrap(), ||"either: expected condition")?,
|
||||
state.eval(source.next().unwrap(), ||"either: expected content 1")?,
|
||||
state.eval(source.next().unwrap(), ||"either: expected content 2")?,
|
||||
)))
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A, B> Eval<I, Bsp<A, B>> for S where
|
||||
S: Eval<AstValue, A> + Eval<AstValue, B>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"bsp/n" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/n: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/n: expected content 2")?;
|
||||
impl<S, A, B> Dsl<S> for Bsp<A, B>
|
||||
where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(Some(if let Ast::Exp(source) = source {
|
||||
match source.next() {
|
||||
Some(Value::Key("bsp/n")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/n: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/n: expected content 2")?;
|
||||
Self::n(a, b)
|
||||
},
|
||||
"bsp/s" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/s: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/s: expected content 2")?;
|
||||
Some(Value::Key("bsp/s")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/s: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/s: expected content 2")?;
|
||||
Self::s(a, b)
|
||||
},
|
||||
"bsp/e" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/e: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/e: expected content 2")?;
|
||||
Some(Value::Key("bsp/e")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/e: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/e: expected content 2")?;
|
||||
Self::e(a, b)
|
||||
},
|
||||
"bsp/w" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/w: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/w: expected content 2")?;
|
||||
Some(Value::Key("bsp/w")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/w: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/w: expected content 2")?;
|
||||
Self::w(a, b)
|
||||
},
|
||||
"bsp/a" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/a: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/a: expected content 2")?;
|
||||
Some(Value::Key("bsp/a")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/a: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/a: expected content 2")?;
|
||||
Self::a(a, b)
|
||||
},
|
||||
"bsp/b" => {
|
||||
let _ = source.next();
|
||||
let a: A = self.provide(source, ||"bsp/b: expected content 1")?;
|
||||
let b: B = self.provide(source, ||"bsp/b: expected content 2")?;
|
||||
Some(Value::Key("bsp/b")) => {
|
||||
let a: A = state.eval(source.next(), ||"bsp/b: expected content 1")?;
|
||||
let b: B = state.eval(source.next(), ||"bsp/b: expected content 2")?;
|
||||
Self::b(a, b)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
None
|
||||
})
|
||||
return Ok(None)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: Ast, S, A> Eval<I, Align<A>> for S where
|
||||
S: Eval<AstValue, A>
|
||||
{
|
||||
fn eval (&self, source: I) -> Perhaps<Self> {
|
||||
Ok(if let Some(Value::Key(key)) = source.peek() {
|
||||
Some(match key {
|
||||
"align/c" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/c: expected content")?;
|
||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
||||
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||
Ok(Some(if let Ast::Exp(source) = source {
|
||||
match source.next() {
|
||||
Some(Value::Key("align/c")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/c: expected content")?;
|
||||
Self::c(content)
|
||||
},
|
||||
"align/x" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/x: expected content")?;
|
||||
Some(Value::Key("align/x")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/x: expected content")?;
|
||||
Self::x(content)
|
||||
},
|
||||
"align/y" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/y: expected content")?;
|
||||
Some(Value::Key("align/y")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/y: expected content")?;
|
||||
Self::y(content)
|
||||
},
|
||||
"align/n" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/n: expected content")?;
|
||||
Some(Value::Key("align/n")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/n: expected content")?;
|
||||
Self::n(content)
|
||||
},
|
||||
"align/s" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/s: expected content")?;
|
||||
Some(Value::Key("align/s")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/s: expected content")?;
|
||||
Self::s(content)
|
||||
},
|
||||
"align/e" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/e: expected content")?;
|
||||
Some(Value::Key("align/e")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/e: expected content")?;
|
||||
Self::e(content)
|
||||
},
|
||||
"align/w" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/w: expected content")?;
|
||||
Some(Value::Key("align/w")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/w: expected content")?;
|
||||
Self::w(content)
|
||||
},
|
||||
"align/nw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/nw: expected content")?;
|
||||
Some(Value::Key("align/nw")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/nw: expected content")?;
|
||||
Self::nw(content)
|
||||
},
|
||||
"align/ne" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/ne: expected content")?;
|
||||
Some(Value::Key("align/ne")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/ne: expected content")?;
|
||||
Self::ne(content)
|
||||
},
|
||||
"align/sw" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/sw: expected content")?;
|
||||
Some(Value::Key("align/sw")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/sw: expected content")?;
|
||||
Self::sw(content)
|
||||
},
|
||||
"align/se" => {
|
||||
let _ = source.next();
|
||||
let content: A = self.provide(source, ||"align/se: expected content")?;
|
||||
Some(Value::Key("align/se")) => {
|
||||
let content: A = state.eval(source.next(), ||"align/se: expected content")?;
|
||||
Self::se(content)
|
||||
},
|
||||
_ => return Ok(None),
|
||||
})
|
||||
}
|
||||
} else {
|
||||
None
|
||||
})
|
||||
return Ok(None)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue