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

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