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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#![forbid(unsafe_code)]

use glsp::{arr, DequeAccess, DequeOps, stock_syms::*, Sym, Val};
use glsp_stdlib::Runtime;
use proc_macro::{Literal, TokenStream, TokenTree};
use proc_macro2::{Literal as Literal2, TokenStream as TokenStream2, TokenTree as TokenTree2};
use quote::quote;
use std::fmt::Write;
use std::path::PathBuf;
use std::str::FromStr;
use syn::{LitStr, parse::Parser, punctuated::Punctuated, Token};

/**
Pre-compiles GameLisp code and embeds it into the executable as a byte slice.

This macro is only available when the ["compiler" feature 
flag](https://gamelisp.rs/reference/feature-flags.html#compiler) is enabled.

The input must be a comma-separated list of filepaths, which are looked up relative to the 
[`CARGO_MANIFEST_DIR`](https://doc.rust-lang.org/cargo/reference/environment-variables.html).
	
	compile!["scripts/first.glsp", "scripts/second.glsp"]

When the `compile!` macro is expanded by rustc, it starts up a generic, empty 
[`Runtime`](struct.Runtime.html); loads the specified files using 
[`glsp::load_and_compile`](fn.load_and_compile.html); and embeds the resulting binary 
into the program as a `&'static [u8]` which can be passed to 
[`glsp::load_compiled`](fn.load_compiled.html).
*/

#[proc_macro]
pub fn compile(input: TokenStream) -> TokenStream {
	//wrangle input
	let parser = Punctuated::<LitStr, Token![,]>::parse_separated_nonempty;
	let args = parser.parse(input).expect("compile![] expects a list of string literals");
	let files: Vec<String> = args.iter().map(|lit_str| lit_str.value()).collect();

	//check that each of the files exist
	let paths: Vec<PathBuf> = files.iter().map(|st| PathBuf::from_str(st).unwrap()).collect();
	for path in &paths {
		assert!(path.is_file(), "nonexistent file {:?} passed to compile![]", path); 
	}

	//generate a series of (load) forms: (load "a.glsp") (load "b.glsp")...
	let mut load_forms = String::new();
	for path in &paths {
		write!(&mut load_forms, "(load {:?})\n", path).unwrap();
	}

	//spin up a glsp Runtime and use it to compile those (load) forms into a Vec<u8>
	let runtime = Runtime::new();
	let bytes = runtime.run(|| {
		let (_, bytes) = glsp::load_and_compile_str(&load_forms, "compile-proc-macro")?;
		Ok(bytes)
	}).unwrap();

	//convert the bytes into a byte string literal. (there doesn't seem to be any better way to
	//embed arbitrary bytes into a Rust source file, but luckily it doesn't seem to slow down
	//the compiler.)
	TokenTree::Literal(Literal::byte_string(&bytes[..])).into()
}

/**
A convenient way to evaluate simple GameLisp code.

This macro is only available when the ["compiler" feature 
flag](https://gamelisp.rs/reference/feature-flags.html#compiler) is enabled.

The input must be a single string literal which represents zero or more valid GameLisp forms. 
Those forms are evaluated in order, and the final form's result is returned. The return type is 
a generic [`GResult<T>`](type.GResult.html) for any `T` which implements 
[`FromVal`](trait.FromVal.html) - you will usually need to bind it to a local variable with
an explicit type.
	
	let result: Root<Arr> = eval!(r#"
		(let n (+ 1 2 3 4))
		(arr n n n)
	"#)?;

Rust's local variables can be captured, and/or mutated, using the 
[`unquote`](https://gamelisp.rs/std/unquote) form (abbreviated as `~`).
They are copied into the form using the [`IntoVal` trait](trait.IntoVal.html). 
If they're mutated using the [`=` form](https://gamelisp.rs/std/set), then
the local variables' values are updated when `eval!()` returns, using the 
[`FromVal` trait](trait.FromVal.html).
	
	let input = 100_i64;
	let mut output = "hello".to_string();
	let assigned_but_not_read: f32;

	let result: Val = eval!(r#"
		(= ~output (str ~input))
		(= ~assigned_but_not_read 100.0)
	"#)?;

Some Rust collection types, such as tuples, slices, `Strings` and `HashMaps`, will allocate
a new GameLisp array, string or table when captured as a local variable. This is potentially 
expensive, especially for large collections. Also, if the resulting collection is mutated, 
those changes are not usually copied back into the local variable when `eval!()` returns.

	let rust_tuple = (60_u8, 120_u16, 240_u32);
	let glsp_arr = arr![60_u8, 120_u16, 240_u32];

	let _: Val = eval!(r#"
		(= [~rust_tuple 2] 480)
		(= [~glsp_arr 2] 480)
	"#)?;

	//rust collection types make a copy, but glsp collection types are passed in by reference
	assert!(rust_tuple.2 == 240);
	assert!(glsp_arr.get::<u32>(2)? == 480);

`eval!()` is much faster than GameLisp's own [`(eval)`](https://gamelisp.rs/std/eval) function, 
because its body is compiled in advance. The first time each `eval!()` is executed, it has a 
small lazy-initialization cost (usually less than 0.1 milliseconds for small inputs). On each 
subsequent execution, the overhead is roughly one microsecond per `eval!()` call.

The input is macro-expanded using a generic, empty [`Runtime`](struct.Runtime.html) which does not
have access to any of your own macros. The only macros you can use in `eval!()` are those provided 
by GameLisp's standard library, or those defined within the `eval!()` form itself.
*/

#[proc_macro]
pub fn eval(input: TokenStream) -> TokenStream {
	//wrangle input
	let lit: LitStr = syn::parse(input).expect("the input to eval!() \
	                                            must be a single string literal");
	let text: String = lit.value();

	//spin up a glsp Runtime
	let runtime = Runtime::new();
	runtime.run(|| {
		glsp::seed_gensym();

		//parse the input string
		let forms = glsp::parse_all(&text, Some("eval-proc-macro"))
		                 .expect("eval!() received invalid glsp syntax");

		//make it mutable
		let forms: Vec<Val> = forms.iter().map(|v| v.deep_clone().unwrap()).collect();

		//we compile the forms into a single (fn (arg) ...) form. this means that, when executed
		//at runtime, it will return a Root<GFn> which we can stash in a lazy_val.

		//the argument to the fn is a newly-allocated arr where each item corresponds to an input 
		//or output variable. after the fn returns, any output variables are updated with the 
		//value in their corresponding arr item. (we can't recycle this arr because it would
		//prevent each eval!() from recursively calling itself.)
		let arg_gensym = glsp::gensym();
		let fn_form: Val = Val::Arr(arr![
			FN_SYM,
			arr![arg_gensym],
			..forms
		]);

		//many macros will not handle (unquote x) forms transparently. for example, (= ~x 1)
		//will consider `unquote` to be an accessor function. in order to solve this, we perform
		//two transformation passes: the first detects (unquote x) forms and replaces them with
		//(access arg_gensym index) before macro expansion. the second runs after macro-expansion,
		//searches for (access arg_gensym index) and (access= arg_gensym index _) forms, and marks 
		//those indexes as "accessed" and "mutated" respectively. this should catch most mutations 
		//that we care about... (= ~x 1), (inc! ~x), etc.
		#[derive(PartialEq, Copy, Clone)]
		struct NameEntry {
			name: Sym,
			input: bool,
			output: bool
		}

		fn pass0(val: &Val, names: &mut Vec<NameEntry>, arg_gensym: Sym) {
			if let Val::Arr(ref arr) = *val {
				if arr.len() > 0 && arr.get::<Val>(0).unwrap().is_sym() {
					let sym = arr.get::<Sym>(0).unwrap();

					if sym == QUOTE_SYM {
						()
					} else if sym == UNQUOTE_SYM {
						assert!(arr.len() == 2, "invalid unquote form: {}", arr);
						let identifier = arr.get::<Sym>(1).expect("invalid unquote form");
						assert!(is_valid_identifier(&identifier.name()),
						        "invalid unquoted identifier: {}", identifier);

						for (i, entry) in names.iter_mut().enumerate() {
							if entry.name == identifier {
								arr.clear().unwrap();
								arr.push(ACCESS_SYM).unwrap();
								arr.push(arg_gensym).unwrap();
								arr.push(i).unwrap();

								return
							}
						}

						names.push(NameEntry {
							name: identifier,
							input: false,
							output: false
						});

						arr.clear().unwrap();
						arr.push(ACCESS_SYM).unwrap();
						arr.push(arg_gensym).unwrap();
						arr.push(names.len() - 1).unwrap();

					} else {
						for item in arr.iter() {
							pass0(&item, names, arg_gensym);
						}
					}
				} else {
					for item in arr.iter() {
						pass0(&item, names, arg_gensym);
					}
				}
			}
		}

		fn pass1(
			val: &Val,
			names: &mut Vec<NameEntry>,
			arg_gensym: Sym
		) {
			if let Val::Arr(ref arr) = *val {
				if arr.len() >= 3 && 
				   arr.get::<Val>(0).unwrap().is_sym() && 
				   arr.get::<Val>(1).unwrap() == Val::Sym(arg_gensym) &&
				   arr.get::<Val>(2).unwrap().is_int() {

					let callee = arr.get::<Sym>(0).unwrap();
					let index = arr.get::<i32>(2).unwrap() as usize;

					if callee == ACCESS_SYM {
						names[index].input = true;
					} else if callee == SET_ACCESS_SYM {
						names[index].output = true;
					}
				}

				for item in arr.iter() {
					pass1(&item, names, arg_gensym);
				}
			}
		}

		let mut names = Vec::new();
		pass0(&fn_form, &mut names, arg_gensym);

		let fn_form = glsp::expand(&fn_form, None).expect("error when expanding eval!()'s input");
		
		pass1(&fn_form, &mut names, arg_gensym);

		//compile the fn form
		let (_, bytes) = glsp::load_and_compile_vals(&[fn_form], "eval-proc-macro")
			.expect("error when compiling eval!()'s input");
		let byte_string = TokenTree2::Literal(Literal2::byte_string(&bytes[..]));

		//emit the lazy-initialization code
		let mut vars_to_read = Vec::<TokenStream2>::new();
		let mut vars_to_write = Vec::<TokenStream2>::new();
		let mut indexes_to_write = Vec::<usize>::new();

		for (i, entry) in names.iter().enumerate() {
			let name = entry.name.name();

			if entry.input {
				vars_to_read.push(name.parse().unwrap());
			}

			if entry.output {
				if !entry.input {
					vars_to_read.push("Val::Nil".parse().unwrap());
				}
				vars_to_write.push(name.parse().unwrap());
				indexes_to_write.push(i);
			}
		}

		Ok(quote! {
			::glsp::with_lazy_val(::glsp::lazy_key!(),
				|| ::glsp::load_compiled(#byte_string).unwrap(),
				|__glsp_eval_val| {
					let __glsp_eval_args = ::glsp::try_arr![#(&#vars_to_read),*]?;
					let __glsp_eval_result: ::glsp::Val = match __glsp_eval_val {
						::glsp::Val::GFn(gfn) => ::glsp::call(gfn, &[&__glsp_eval_args])?,
						_ => ::std::panic!()
					};
					#(
						#vars_to_write = match ::glsp::FromVal::from_val(
							&__glsp_eval_args.get::<::glsp::Val>(#indexes_to_write).unwrap()
						) {
							Ok(converted) => converted,
							Err(err) => {
								let __glsp_eval_err = ::glsp::error!(
									"type mismatch when mutating variable {}",
									stringify!(#vars_to_write)
								);
								return Err(__glsp_eval_err.with_source(err))
							}
						};
					)*
					::glsp::FromVal::from_val(&__glsp_eval_result)
				}
			)
		}.into())
	}).unwrap()
}

fn is_valid_identifier(st: &str) -> bool {
	if st.len() == 0 {
		false
	} else {
		let first = st.chars().next().unwrap();
		if first == '_' {
			st.len() >= 2 && st[1..].chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_')
		} else if first.is_ascii_alphabetic() {
			st[1..].chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_')
		} else {
			false
		}
	}
}