Diffstat (limited to 'src/run.rs')
-rw-r--r--src/run.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/run.rs b/src/run.rs
new file mode 100644
index 0000000..2b084d6
--- /dev/null
+++ b/src/run.rs
@@ -0,0 +1,35 @@
+use proc_macro::TokenStream;
+use quote::quote;
+use uiua::SafeSys;
+use uiua::Uiua;
+
+pub fn exec(x: &str) -> TokenStream {
+ let mut weewa = Uiua::with_safe_sys().with_recursion_limit(100);
+ let x = x.trim_start_matches('r').trim_matches('#');
+ let x = x.strip_prefix('"').unwrap_or(x);
+ let x = x.strip_suffix('"').unwrap_or(x);
+ match weewa.run_str(x) {
+ Err(e) => {
+ weewa.print_reports();
+ let e = e.to_string();
+ quote! { compile_error!(#e) }.into()
+ }
+ Ok(_) => {
+ match weewa
+ .stack()
+ .get(0)
+ .map(|x| x.to_string())
+ .unwrap_or_else(|| {
+ String::from_utf8_lossy(
+ &weewa.downcast_backend::<SafeSys>().unwrap().take_stdout(),
+ )
+ .into_owned()
+ })
+ .parse::<TokenStream>()
+ {
+ Ok(x) => x,
+ Err(_) => quote! { compile_error!("lexer error") }.into(),
+ }
+ }
+ }
+}