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
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(), } } } }