initial commit

This commit is contained in:
Goren Barak 2023-11-30 13:48:29 -05:00
commit 09d783169b

40
temp.rs Normal file
View file

@ -0,0 +1,40 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro::TokenTree;
use proc_macro::Ident;
use proc_macro::Span;
use std::iter::FromIterator;
static mut TEMP_COUNTER: i64 = 0;
#[proc_macro]
pub fn temp(item: TokenStream) -> TokenStream {
let mut iterator = item.clone().into_iter().peekable();
if item.is_empty() {
unsafe { TEMP_COUNTER += 1; }
} else if let Some(TokenTree::Literal(_)) = iterator.peek() {
if let Ok(number) = item.to_string().parse::<i64>() {
unsafe { TEMP_COUNTER = number; }
}
} else if let Some(TokenTree::Punct(i)) = iterator.next() {
if i.as_char() == '+' {
if let Some(TokenTree::Literal(i)) = iterator.next() {
if let Ok(number) = i.to_string().parse::<i64>() {
unsafe { TEMP_COUNTER += number; }
}
}
} else if i.as_char() == '-' {
if let Some(TokenTree::Literal(i)) = iterator.next() {
if let Ok(number) = i.to_string().parse::<i64>() {
unsafe { TEMP_COUNTER -= number; }
}
} else if let None = iterator.next() {
}
}
} else {
panic!("Invalid input to temporary variable generator.");
}
TokenStream::from_iter([
TokenTree::Ident(Ident::new(format!("temp{}", unsafe { TEMP_COUNTER }).as_str(), Span::call_site()))
])
}