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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
use lazy_static::lazy_static;
use proc_macro::TokenStream;
use quote::quote;
use std::{borrow::Cow, sync::Mutex};
use syn::{Expr, Pat, Token, parse::{Parse, ParseStream, Result}, parse_macro_input};
struct ExternBlock {
pattern: Pat,
_arrow: Token![->],
return_ty: Expr,
_fat_arrow: Token![=>],
block: Expr,
_comma: Token![,],
}
struct ExternFunction {
function: syn::Ident,
blocks: Vec<ExternBlock>
}
impl Parse for ExternBlock {
fn parse(input: ParseStream) -> Result<Self> {
Ok(Self {
pattern: input.parse()?,
_arrow: input.parse()?,
return_ty: input.parse()?,
_fat_arrow: input.parse()?,
block: input.parse()?,
_comma: input.parse()?,
})
}
}
impl Parse for ExternFunction {
fn parse(input: ParseStream) -> Result<Self> {
let mut res = Self {
function: input.parse()?,
blocks: Vec::new(),
};
while !input.is_empty() {
res.blocks.push(input.parse()?);
}
Ok(res)
}
}
#[proc_macro]
pub fn extern_function(tokens: TokenStream) -> TokenStream {
let parsed: ExternFunction = parse_macro_input!(tokens);
let function = parsed.function;
let typecheck_blocks: Vec<_> = parsed.blocks.iter().map(|block| {
let pat = block.pattern.clone();
let ty = block.return_ty.clone();
quote! {
#pat => { Ok(sylt::Value::from(#ty)) }
}
}).collect();
let eval_blocks: Vec<_> = parsed.blocks.iter().map(|block| {
let pat = block.pattern.clone();
let expr = block.block.clone();
quote! {
#pat => #expr
}
}).collect();
let tokens = quote! {
#[sylt_macro::extern_link]
pub fn #function (
__values: &[sylt::Value],
__typecheck: bool
) -> ::std::result::Result<sylt::Value, sylt::error::ErrorKind>
{
if __typecheck {
#[allow(unused_variables)]
match __values {
#(#typecheck_blocks),*
_ => Err(sylt::error::ErrorKind::ExternTypeMismatch(
stringify!(#function).to_string(),
__values.iter().map(|v| sylt::Type::from(v)).collect()
))
}
} else {
match __values {
#(#eval_blocks),*
_ => Err(sylt::error::ErrorKind::ExternTypeMismatch(
stringify!(#function).to_string(),
__values.iter().map(|v| sylt::Type::from(v)).collect()
))
}
}
}
};
TokenStream::from(tokens)
}
lazy_static! {
static ref LINKED_FUNCTIONS: Mutex<Vec<(String, String)>> = Mutex::new(Vec::new());
}
#[proc_macro_attribute]
pub fn extern_link(attr: TokenStream, tokens: TokenStream) -> TokenStream {
let parsed: syn::ItemFn = parse_macro_input!(tokens);
let name = if attr.is_empty() {
Cow::Borrowed(&parsed.sig.ident)
} else {
let attr: syn::Ident = parse_macro_input!(attr);
Cow::Owned(attr)
};
let tokens = quote! {
#parsed
};
LINKED_FUNCTIONS.lock().unwrap().push((name.to_string(), name.to_string()));
TokenStream::from(tokens)
}
#[proc_macro]
pub fn links(tokens: TokenStream) -> TokenStream {
assert!(tokens.is_empty());
let linked_functions: Vec<_> = LINKED_FUNCTIONS
.lock()
.unwrap()
.iter()
.map(|(name, path)| format!("({}, {})", name, path))
.collect();
let tokens = quote! {
(|| {
let ret: Vec<&str> = vec![ #(#linked_functions),* ];
ret
})()
};
TokenStream::from(tokens)
}
|