dsl: give! and take! macros
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
🪞👃🪞 2025-05-24 00:29:50 +03:00
parent cbd28a5934
commit 5a2177cc77
4 changed files with 131 additions and 94 deletions

View file

@ -17,13 +17,6 @@ pub trait Give<Type> {
} }
} }
} }
impl<'n, T: Take<'n, U>, U> Give<T> for U {
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<T> {
T::take(self, words)
}
}
/// [Give]s instances of [Self] given [TokenIter]. /// [Give]s instances of [Self] given [TokenIter].
pub trait Take<'n, State>: Sized + 'n { pub trait Take<'n, State>: Sized + 'n {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>; fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self>;
@ -39,6 +32,68 @@ pub trait Take<'n, State>: Sized + 'n {
} }
} }
#[macro_export] macro_rules! take {
() => {
impl<'n, Type: 'n, State: Give<Type> + 'n> Take<'n, State> for Type {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
(box) => {
impl<'n, T, State: Give<Box<T>> + 'n> Take<'n, State> for Box<T> {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:ty:$State:ty) => {
impl<'n> Take<'n, $State> for $Type {
fn take <'source> (state: &$State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
state.give(words)
}
}
};
($Type:path$(,$Arg:ident)*|$state:ident:$State:path,$words:ident|$expr:expr) => {
impl<'n, State: $State + 'n $(, $Arg: 'n)*> Take<'n, State> for $Type {
fn take <'source> ($state: &State, $words: &mut TokenIter<'source>) -> Perhaps<Self> {
$expr
}
}
};
}
#[macro_export] macro_rules! give {
() => {
impl<'n, Type: Take<'n, State>, State> Give<Type> for State {
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Type> {
Type::take(self, words)
}
}
};
(box) => {
//impl<'n, T, Type: Take<'n, Box<T>>> Give<Box<T>> for Box<Type> {
//fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<Box<Type>> {
//Type::take(self, words)
//}
//}
};
($Type:ty: $State:ty) => {
impl<'n, $Type: Take<'n, $State>> Give<$Type> for $State {
fn give <'source> (&self, words: &mut TokenIter<'source>) -> Perhaps<$Type> {
$Type::take(self, words)
}
}
};
($Type:ty|$state:ident:$State:ident,$words:ident|$expr:expr) => {
impl Give<$Type> for $State {
fn give <'source> (&self, $words: &mut TokenIter<'source>) -> Perhaps<$Type> {
let $state = self;
$expr
}
}
};
}
/// Implement the [Give] trait, which boils down to /// Implement the [Give] trait, which boils down to
/// specifying two types and providing an expression. /// specifying two types and providing an expression.
#[macro_export] macro_rules! from_dsl { #[macro_export] macro_rules! from_dsl {

View file

@ -27,25 +27,18 @@
//! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]); //! test(area, &Align::sw(two_by_four()), [10, 28, 4, 2]);
//! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]); //! test(area, &Align::w(two_by_four()), [10, 19, 4, 2]);
//! ``` //! ```
use crate::*; use crate::*;
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone, Default)]
pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W } pub enum Alignment { #[default] Center, X, Y, NW, N, NE, E, SE, S, SW, W }
pub struct Align<A>(Alignment, A); pub struct Align<A>(Alignment, A);
#[cfg(feature = "dsl")]take!(Align<A>, A|state: Give<A>, words|Ok(Some(match words.peek() {
#[cfg(feature = "dsl")] Some(Token { value: Value::Key(key), .. }) => match key {
impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
fn take <'source> (state: &State, words: &mut TokenIter<'source>) -> Perhaps<Self> {
if let Some(Token { value: Value::Key(key), .. }) = words.peek() {
match key {
"align/c"|"align/x"|"align/y"| "align/c"|"align/x"|"align/y"|
"align/n"|"align/s"|"align/e"|"align/w"| "align/n"|"align/s"|"align/e"|"al;qign/w"|
"align/nw"|"align/sw"|"align/ne"|"align/se" => { "align/nw"|"align/sw"|"align/ne"|"align/se" => {
let _ = words.next().unwrap(); let _ = words.next().unwrap();
let content = state.give_or_fail(&mut words.clone(), ||"expected content")?; let content = state.give_or_fail(&mut words.clone(), ||"expected content")?;
return Ok(Some(match key { match key {
"align/c" => Self::c(content), "align/c" => Self::c(content),
"align/x" => Self::x(content), "align/x" => Self::x(content),
"align/y" => Self::y(content), "align/y" => Self::y(content),
@ -58,14 +51,12 @@ impl<'n, State: Give<A>, A: 'n> Take<'n, State> for Align<A> {
"align/sw" => Self::sw(content), "align/sw" => Self::sw(content),
"align/se" => Self::se(content), "align/se" => Self::se(content),
_ => unreachable!() _ => unreachable!()
})) }
}, },
_ => {} _ => return Ok(None)
} },
} _ => return Ok(None)
Ok(None) })));
}
}
impl<A> Align<A> { impl<A> Align<A> {
#[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) } #[inline] pub const fn c (a: A) -> Self { Self(Alignment::Center, a) }

View file

@ -121,11 +121,7 @@ impl ToTokens for ExposeArm {
} }
} }
impl From<LitStr> for ExposeSym { impl From<LitStr> for ExposeSym { fn from (this: LitStr) -> Self { Self(this) } }
fn from (this: LitStr) -> Self {
Self(this)
}
}
impl PartialOrd for ExposeSym { impl PartialOrd for ExposeSym {
fn partial_cmp (&self, other: &Self) -> Option<Ordering> { fn partial_cmp (&self, other: &Self) -> Option<Ordering> {
@ -153,11 +149,7 @@ impl PartialEq for ExposeSym {
impl Eq for ExposeSym {} impl Eq for ExposeSym {}
impl From<Type> for ExposeType { impl From<Type> for ExposeType { fn from (this: Type) -> Self { Self(Box::new(this)) } }
fn from (this: Type) -> Self {
Self(Box::new(this))
}
}
impl PartialOrd for ExposeType { impl PartialOrd for ExposeType {
fn partial_cmp (&self, other: &Self) -> Option<Ordering> { fn partial_cmp (&self, other: &Self) -> Option<Ordering> {

View file

@ -4,9 +4,7 @@ use crate::*;
pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl); pub(crate) struct ViewDef(pub(crate) ViewMeta, pub(crate) ViewImpl);
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct ViewMeta { pub(crate) struct ViewMeta { pub(crate) output: Ident }
pub(crate) output: Ident,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct ViewImpl { pub(crate) struct ViewImpl {
@ -43,20 +41,18 @@ impl ToTokens for ViewDef {
fn to_tokens (&self, out: &mut TokenStream2) { fn to_tokens (&self, out: &mut TokenStream2) {
let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self; let Self(ViewMeta { output }, ViewImpl { block, exposed }) = self;
let self_ty = &block.self_ty; let self_ty = &block.self_ty;
let builtins: Vec<_> = builtins_with_types() // Expressions are handled by built-in functions
.iter() // that operate over constants and symbols.
.map(|ty|write_quote(quote! { let builtins: Vec<_> = builtins_with_types().iter().map(|ty|write_quote(quote! {
let value: Option<#ty> = Give::<#ty>::give(self, &mut exp.clone())?; ::tengri::dsl::Value::Exp(_, expr) => {
if let Some(value) = value { Give::<#ty>::give(&mut expr.clone()).map(|value|value.boxed())
return Ok(Some(value.boxed())) },
} })).collect();
})) // Symbols are handled by user-taked functions
.collect(); // that take no parameters but `&self`.
let exposed: Vec<_> = exposed let exposed: Vec<_> = exposed.iter().map(|(key, value)|write_quote(quote! {
.iter() ::tengri::dsl::Value::Sym(#key) => {
.map(|(key, value)|write_quote(quote! { Some(Box::new(Thunk::new(move||#self_ty::#value(self))))
#key => {
Some(Box::new(Thunk::new(move||#self_ty::#value(self))))//Box::new("todo"))
}, },
})).collect(); })).collect();
write_quote_to(out, quote! { write_quote_to(out, quote! {
@ -65,31 +61,34 @@ impl ToTokens for ViewDef {
/// Gives [#self_ty] the ability to construct the [Render]able /// Gives [#self_ty] the ability to construct the [Render]able
/// which might corresponds to a given [TokenStream], /// which might corresponds to a given [TokenStream],
/// while taking [#self_ty]'s state into consideration. /// while taking [#self_ty]'s state into consideration.
impl ::tengri::dsl::Give<Box<dyn Render<#output>>> for #self_ty { give!(Box<dyn Render<#output>>|state:#self_ty, words|Ok(None));
fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>) //impl <'n, State: ::tengri::dsl::Give<Box<dyn Render<#output>>>>
-> Perhaps<Box<dyn Render<#output>>> //Take<'n, Box<dyn Render<#output>> for #self_ty
{ //{
Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() { //fn give <'source> (&self, words: &mut ::tengri::dsl::TokenIter<'source>)
match value { //-> Perhaps<Box<dyn Render<#output>>>
// Expressions are handled by built-in functions //{
// that operate over constants and symbols. //Ok(if let Some(::tengri::dsl::Token { value, .. }) = words.peek() {
::tengri::dsl::Value::Exp(_, exp) => { //match value {
#(#builtins)* //// Expressions are handled by built-in functions
None //// that operate over constants and symbols.
}, //::tengri::dsl::Value::Exp(_, exp) => {
// Symbols are handled by user-taked functions //#(#builtins)*
// that take no parameters but `&self`. //None
::tengri::dsl::Value::Sym(sym) => match sym { //},
#(#exposed)* //// Symbols are handled by user-taked functions
_ => None //// that take no parameters but `&self`.
}, //::tengri::dsl::Value::Sym(sym) => match sym {
_ => None //#(#exposed)*
} //_ => None
} else { //},
None //_ => None
}) //}
} //} else {
} //None
//})
//}
//}
/// Generated by [tengri_proc]. /// Generated by [tengri_proc].
/// ///
/// Delegates the rendering of [#self_ty] to the [#self_ty::view} method, /// Delegates the rendering of [#self_ty] to the [#self_ty::view} method,