mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 19:56:44 +01:00
AstToken -> Ast <- AstValue
This commit is contained in:
parent
f77139c8fd
commit
e9d4c7e0bc
5 changed files with 30 additions and 40 deletions
|
|
@ -2,15 +2,21 @@ use crate::*;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// Owns its values, and has no metadata.
|
|
||||||
#[derive(PartialEq, Clone, Default, Debug)]
|
|
||||||
pub struct AstToken(pub AstValue, pub AstMeta);
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
#[derive(Debug, Copy, Clone, Default, PartialEq)]
|
||||||
pub struct AstMeta;
|
pub struct AstIter;
|
||||||
|
impl<'source> From<SourceIter<'source>> for AstIter {
|
||||||
pub type AstValue = Value<Arc<str>, Vec<AstToken>>;
|
fn from (source: SourceIter<'source>) -> Self {
|
||||||
impl<'source> From<CstValue<'source>> for AstValue {
|
Self // TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// Owns its values, and has no metadata.
|
||||||
|
pub type Ast = Value<Arc<str>, AstIter>;
|
||||||
|
impl<'source> From<CstToken<'source>> for Ast {
|
||||||
|
fn from (token: CstToken<'source>) -> Self {
|
||||||
|
token.value().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'source> From<CstValue<'source>> for Ast {
|
||||||
fn from (value: CstValue<'source>) -> Self {
|
fn from (value: CstValue<'source>) -> Self {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
match value {
|
match value {
|
||||||
|
|
@ -24,11 +30,3 @@ impl<'source> From<CstValue<'source>> for AstValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait AstIter: DslIter<Token = AstToken> {}
|
|
||||||
|
|
||||||
//impl<'source> From<CstExp<'source>> for AstExp {
|
|
||||||
//fn from (exp: CstExp<'source>) -> AstExp {
|
|
||||||
//AstExp(exp.words.map(|token|Token(AstValue::from(token.value()), AstMeta,)).collect())
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
use std::fmt::{Debug, Display, Formatter, Error as FormatError};
|
||||||
impl Display for AstValue {
|
impl Display for Ast {
|
||||||
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
fn fmt (&self, out: &mut Formatter) -> Result<(), FormatError> {
|
||||||
use Value::*;
|
use Value::*;
|
||||||
write!(out, "{}", match self {
|
write!(out, "{}", match self {
|
||||||
|
|
|
||||||
|
|
@ -43,18 +43,12 @@ impl<'source> Into<Vec<CstToken<'source>>> for SourceIter<'source> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'source> Into<Vec<AstToken>> for SourceIter<'source> {
|
impl<'source> Into<Vec<Ast>> for SourceIter<'source> {
|
||||||
fn into (self) -> Vec<AstToken> {
|
fn into (self) -> Vec<Ast> {
|
||||||
self.map(Into::into).collect()
|
self.map(Into::into).collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'source> From<CstToken<'source>> for AstToken {
|
|
||||||
fn from (value: CstToken<'source>) -> Self {
|
|
||||||
Self(value.0.into(), AstMeta)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Implement the const iterator pattern.
|
/// Implement the const iterator pattern.
|
||||||
#[macro_export] macro_rules! const_iter {
|
#[macro_export] macro_rules! const_iter {
|
||||||
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
($(<$l:lifetime>)?|$self:ident: $Struct:ty| => $Item:ty => $expr:expr) => {
|
||||||
|
|
|
||||||
|
|
@ -4,35 +4,33 @@ use std::fmt::Debug;
|
||||||
|
|
||||||
#[derive(Default, Debug)] pub struct InputLayers(Vec<InputLayer>);
|
#[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 {
|
impl InputLayers {
|
||||||
pub fn new (layer: Cst<'static>) -> Self {
|
pub fn new (layer: Ast) -> Self {
|
||||||
Self(vec![]).layer(layer)
|
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
|
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
|
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
|
self.add_layer_if(None, layer.into()); self
|
||||||
}
|
}
|
||||||
pub fn add_layer_if (&mut self, condition: Option<Cst<'static>>, binding: Cst<'static>) -> &mut Self {
|
pub fn add_layer_if (&mut self, condition: Option<Ast>, binding: Ast) -> &mut Self {
|
||||||
self.0.push(InputLayer { condition, binding, __: Default::default() });
|
self.0.push(InputLayer(condition, binding));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn handle <S: Eval<dyn Ast, bool>, I, O: Command<S>> (
|
pub fn handle <S, I, O: Command<S>> (&self, state: &mut S, input: I) -> Perhaps<O> {
|
||||||
&self, state: &mut S, input: I
|
|
||||||
) -> Perhaps<O> {
|
|
||||||
InputHandle(state, self.0.as_slice()).try_eval(input)
|
InputHandle(state, self.0.as_slice()).try_eval(input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct InputHandle<'a, S>(&'a mut S, &'a [InputLayer]);
|
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> {
|
fn try_eval (&self, input: I) -> Perhaps<O> {
|
||||||
let Self(state, layers) = self;
|
let Self(state, layers) = self;
|
||||||
for InputLayer(condition, binding) in layers.iter() {
|
for InputLayer(condition, binding) in layers.iter() {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||||
fn try_provide (state: S, source: impl Ast) -> Perhaps<Self> {
|
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(exp) = source {
|
if let Ast::Exp(exp) = source {
|
||||||
let mut iter = source.clone();
|
let mut iter = source.clone();
|
||||||
if let Some(Ast::Key(id)) = iter.next() && *id == *"when" {
|
if let Some(Ast::Key(id)) = iter.next() && *id == *"when" {
|
||||||
|
|
@ -16,7 +16,7 @@ impl<S, A> Dsl<S> for When<A> where S: Eval<Ast, bool> + Eval<Ast, A> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> + Eval<Ast, B> {
|
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, source: impl Ast) -> Perhaps<Self> {
|
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||||
if let Ast::Exp(source) = source {
|
if let Ast::Exp(source) = source {
|
||||||
let mut iter = source.clone();
|
let mut iter = source.clone();
|
||||||
if let Some(Ast::Key(id)) = iter.next() && *id == *"either" {
|
if let Some(Ast::Key(id)) = iter.next() && *id == *"either" {
|
||||||
|
|
@ -32,7 +32,7 @@ impl<S, A, B> Dsl<S> for Either<A, B> where S: Eval<Ast, bool> + Eval<Ast, A> +
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<Ast>, B> {
|
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: impl Ast) -> Perhaps<Self> {
|
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
Ok(Some(if let Ast::Exp(source) = source {
|
||||||
let mut iter = source.clone();
|
let mut iter = source.clone();
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
|
|
@ -69,7 +69,7 @@ impl<S, A, B> Dsl<S> for Bsp<A, B> where S: Eval<Option<Ast>, A> + Eval<Option<A
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
impl<S, A> Dsl<S> for Align<A> where S: Eval<Option<Ast>, A> {
|
||||||
fn try_provide (state: S, source: impl Ast) -> Perhaps<Self> {
|
fn try_provide (state: S, source: Ast) -> Perhaps<Self> {
|
||||||
Ok(Some(if let Ast::Exp(source) = source {
|
Ok(Some(if let Ast::Exp(source) = source {
|
||||||
let mut iter = source.clone();
|
let mut iter = source.clone();
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue