1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use rune::ast;
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_(["passthrough"], passthrough_impl)?;
module.macro_(["stringy_math"], stringy_math_macro::stringy_math)?;
module.macro_(["make_function"], make_function)?;
Ok(module)
}
fn passthrough_impl(_: &mut MacroContext<'_>, stream: &TokenStream) -> rune::Result<TokenStream> {
Ok(stream.clone())
}
fn make_function(ctx: &mut MacroContext<'_>, stream: &TokenStream) -> rune::Result<TokenStream> {
let mut parser = Parser::from_token_stream(stream, ctx.stream_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(ctx))
}