use rune::alloc::prelude::*;
use rune::ast;
use rune::compile;
use rune::macros::{quote, MacroContext, TokenStream};
use rune::parse::Parser;
use rune::T;
use rune::{ContextError, Module};
mod stringy_math_macro;
pub fn module(_stdio: bool) -> Result<Module, ContextError> {
let mut module = Module::with_crate_item("std", ["experiments"])?;
module.macro_meta(passthrough)?;
module.macro_meta(stringy_math_macro::stringy_math)?;
module.macro_meta(make_function)?;
Ok(module)
}
#[rune::macro_]
fn passthrough(
_: &mut MacroContext<'_, '_, '_>,
stream: &TokenStream,
) -> compile::Result<TokenStream> {
Ok(stream.try_clone()?)
}
#[rune::macro_]
fn make_function(
cx: &mut MacroContext<'_, '_, '_>,
stream: &TokenStream,
) -> compile::Result<TokenStream> {
let mut parser = Parser::from_token_stream(stream, cx.input_span());
let ident = parser.parse::<ast::Ident>()?;
let _ = parser.parse::<T![=>]>()?;
let output = parser.parse::<ast::ExprBlock>()?;
parser.eof()?;
Ok(quote!(fn #ident() { #output }).into_token_stream(cx)?)
}