Skip to content

Commit

Permalink
Add: Generate stage
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Van de Vyver <[email protected]>
  • Loading branch information
taqtiqa-mark committed Apr 13, 2022
1 parent 75c91b0 commit c761e1a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 55 deletions.
55 changes: 0 additions & 55 deletions minitrace-macro/src/trace/codegen.rs

This file was deleted.

71 changes: 71 additions & 0 deletions minitrace-macro/src/trace/generate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_quote, Stmt};

use crate::trace::lower::Assertion;

//pub type Rust = proc_macro2::TokenStream;

use crate::trace::lower::Quotable;
use crate::trace::lower::Quotables;

// Generate TokenStream to be returned by the proc-macro
//
// The "Lower" stage is responsible for turning the "Model" data into a
// syn::Item that can be reported here.
pub fn generate(quotes: Quotables<Quotable>) -> proc_macro2::TokenStream {
// Have a logic check earlier to error if there is not **at least one**
// `syn::Item`
#[allow(clippy::collapsible_match)]
#[allow(unreachable_patterns)]
let it = match quotes.get(0).expect("An item") {
Quotable::Item(item) => match item {
syn::Item::Fn(itemf) => Some(itemf),
_ => None,
},
_ => None, //?
};
let syn::ItemFn {
attrs,
vis,
sig,
block,
} = it.expect("An item variant");

quote!(
#(#attrs)*
#vis #sig {
#block
}
)
}

impl ToTokens for Assertion {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Assertion { expr, message } = self;
let stmt: Stmt = parse_quote!(assert!(#expr, #message););
stmt.to_tokens(tokens);
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn fn_in_fn_out() {
let models = crate::trace::analyze(
parse_quote!(#[trace]),
parse_quote!(
fn f(x: bool) {}
),
);
println!("{:?}", models.clone());
let ir = crate::trace::lower(models);
println!("{:?}", ir.clone());
let rust = crate::trace::generate(ir);
println!("{:?}", rust.clone());

assert!(syn::parse2::<syn::ItemFn>(rust).is_ok());
}
}

0 comments on commit c761e1a

Please sign in to comment.