Incomplete, will change all this code later
Some checks failed
/ Test-Suite (push) Failing after 20s

This commit is contained in:
Rowan Torbitzky-Lane 2025-04-17 01:26:05 -05:00
parent c66d35b980
commit d697e14173
8 changed files with 172 additions and 8 deletions

View File

@ -7,3 +7,4 @@ edition = "2024"
rand = "0.9.0" rand = "0.9.0"
paste = "1.0.15" paste = "1.0.15"
rust_decimal = { version = "1.37", features = ["macros", "maths"] } rust_decimal = { version = "1.37", features = ["macros", "maths"] }
rush_macro = { path = "rush_macro" }

View File

@ -3,7 +3,10 @@ name = "rush_macro"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2024"
[lib]
proc-macro = true
[dependencies] [dependencies]
syn = { version = "2.0.100" } syn = { version = "2.0.100", features = ["full"] }
quote = { version = "1.0.40" } quote = { version = "1.0.40" }
proc_macro2 = { version = "1.0.95" } proc-macro2 = { version = "1.0.95" }

View File

@ -1,5 +1,13 @@
pub fn add(left: u64, right: u64) -> u64 { use crate::utils::varfunc::VarFunc;
left + right use quote::quote;
use syn::parse_macro_input;
mod utils;
#[proc_macro]
pub fn run_func(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let f = parse_macro_input!(input as VarFunc);
quote! { #f }.into()
} }
#[cfg(test)] #[cfg(test)]
@ -8,7 +16,6 @@ mod tests {
#[test] #[test]
fn it_works() { fn it_works() {
let result = add(2, 2); assert!(true);
assert_eq!(result, 4);
} }
} }

View File

@ -0,0 +1,68 @@
// Extract: State (`,` Stack `:` Amount)*
//
// State: identifier
//
// Stack: identifier
//
// Amount: expression
use crate::utils::parse_zero_or_more;
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote};
use std::cmp::PartialEq;
use syn::parse::{Parse, ParseStream};
struct Extract {
state: State,
stacks: Vec<Stack>,
}
impl Parse for Extract {
fn parse(input: ParseStream) -> syn::Result<Self> {
let state = input.parse()?;
let stacks = parse_zero_or_more(input);
Ok(Extract { state, stacks })
}
}
impl ToTokens for Extract {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let state = &self.state;
let stacks = &self.stacks;
}
}
struct State(syn::Ident);
impl Parse for State {
fn parse(input: ParseStream) -> syn::Result<Self> {
syn::Ident::parse(input).map(Self)
}
}
impl ToTokens for State {
fn to_tokens(&self, tokens: &mut TokenStream2) {
self.0.to_tokens(tokens)
}
}
struct Stack(syn::Ident);
impl Parse for Stack {
fn parse(input: ParseStream) -> syn::Result<Self> {
_ = input.parse::<syn::Token![,]>();
syn::Ident::parse(input).map(Self)
}
}
impl ToTokens for Stack {
fn to_tokens(&self, tokens: &mut TokenStream2) {
self.0.to_tokens(tokens)
}
}
impl PartialEq<Stack> for &Stack {
fn eq(&self, other: &Stack) -> bool {
self.0 == other.0
}
}

View File

@ -0,0 +1,12 @@
use syn::parse::{Parse, ParseStream};
pub mod extractstate;
pub mod varfunc;
fn parse_zero_or_more<T: Parse>(input: ParseStream) -> Vec<T> {
let mut result = Vec::new();
while let Ok(item) = input.parse() {
result.push(item);
}
result
}

View File

@ -0,0 +1,62 @@
// Run: Function Argument*
//
// Function: identifier
//
// Argument: (, expression)
use crate::utils::parse_zero_or_more;
use proc_macro2::TokenStream as TokenStream2;
use quote::{ToTokens, quote};
use syn::parse::{Parse, ParseStream};
pub struct VarFunc {
func: Function,
args: Vec<Argument>,
}
impl Parse for VarFunc {
fn parse(input: ParseStream) -> syn::Result<Self> {
let func = input.parse()?;
let args = parse_zero_or_more(input);
Ok(Self { func, args })
}
}
impl ToTokens for VarFunc {
fn to_tokens(&self, tokens: &mut TokenStream2) {
let function = &self.func;
let args = &self.args;
tokens.extend(quote! {
#function(#(#args),*)
})
}
}
struct Function(syn::Ident);
impl Parse for Function {
fn parse(input: ParseStream) -> syn::Result<Self> {
syn::Ident::parse(input).map(Self)
}
}
impl ToTokens for Function {
fn to_tokens(&self, tokens: &mut TokenStream2) {
self.0.to_tokens(tokens)
}
}
struct Argument(syn::Expr);
impl Parse for Argument {
fn parse(input: ParseStream) -> syn::Result<Self> {
_ = input.parse::<syn::Token![,]>()?;
syn::Expr::parse(input).map(Self)
}
}
impl ToTokens for Argument {
fn to_tokens(&self, tokens: &mut TokenStream2) {
self.0.to_tokens(tokens)
}
}

View File

@ -0,0 +1,11 @@
use rush_macro::run_func;
fn uadd(x: usize, y: usize, z: usize) -> usize {
x + y + z
}
#[test]
fn run_func_test() {
let res = run_func!(uadd, 1, 4, 2);
assert_eq!(res, 7);
}

View File

@ -328,7 +328,7 @@ pub mod macros {
/// Runs a given function passed in the first argument with all the sequential arguments /// Runs a given function passed in the first argument with all the sequential arguments
/// in order. /// in order.
macro_rules! run_func { macro_rules! run_func_old {
() => {}; () => {};
($func:ident, $($args:expr), *) => { ($func:ident, $($args:expr), *) => {
$func($($args), *) $func($($args), *)
@ -794,7 +794,7 @@ mod tests {
} }
pub fn make_instruction_expr_test() -> usize { pub fn make_instruction_expr_test() -> usize {
run_func!(uadd, 1, 2, 3) run_func_old!(uadd, 1, 2, 3)
} }
let res = make_instruction_expr_test(); let res = make_instruction_expr_test();