Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions AGENTS.md

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions crates/kirin-derive-interpreter/src/frame.rs

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason as to why the snapshot tests are no longer needed for this macro?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the deleted snapshot covered the old FrameBuild, AbstractFrameBuild, and DenseFrameBuild. I've not added new snapshots for the new frame.rs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a snapshot covering member frame conversions, the new [derive(Frame)] macro. 6419a99

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Composition-enum dispatch and ordinary member injections.

use proc_macro2::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_quote};

pub fn generate(input: &DeriveInput) -> syn::Result<TokenStream> {
let path: syn::Path = parse_quote!(::kirin_interpreter);
let syn::Data::Enum(data) = &input.data else {
return Err(syn::Error::new_spanned(input, "Frame requires an enum"));
};
let mut members = Vec::new();
for variant in &data.variants {
let syn::Fields::Unnamed(fields) = &variant.fields else {
return Err(syn::Error::new_spanned(
variant,
"expected one unnamed frame field",
));
};
if fields.unnamed.len() != 1 {
return Err(syn::Error::new_spanned(
variant,
"expected one unnamed frame field",
));
}
members.push((&variant.ident, &fields.unnamed[0].ty));
}
let Some((_, first)) = members.first() else {
return Err(syn::Error::new_spanned(
input,
"Frame requires at least one variant",
));
};
let name = &input.ident;
let (original_impl, ty_generics, original_where) = input.generics.split_for_impl();
let mut generics = input.generics.clone();
generics.params.push(parse_quote!(__FrameEngine));
let completion = quote!(<#first as #path::Frame<__FrameEngine, Self>>::Completion);
let predicates = &mut generics.make_where_clause().predicates;
predicates.push(parse_quote!(__FrameEngine: #path::FrameEngine));
predicates.push(parse_quote!(#first: #path::Frame<__FrameEngine, Self>));
for (_, ty) in members.iter().skip(1) {
predicates.push(parse_quote!(
#ty: #path::Frame<__FrameEngine, Self, Completion = #completion>
));
}
let (impl_generics, _, where_clause) = generics.split_for_impl();
let conversions = members.iter().map(|(variant, ty)| {
quote! {
#[automatically_derived]
impl #original_impl ::core::convert::From<#ty> for #name #ty_generics #original_where {
fn from(frame: #ty) -> Self { Self::#variant(frame) }
}
}
});
let methods = ["step_into", "resume_done_into", "resume_into"].map(|method| {
let method = syn::Ident::new(method, proc_macro2::Span::call_site());
let (parameter, argument) = if method == "resume_into" {
(quote!(completion: Self::Completion,), quote!(completion,))
} else {
(quote!(), quote!())
};
let arms = members.iter().map(|(variant, ty)| {
quote! {
Self::#variant(frame) =>
<#ty as #path::Frame<__FrameEngine, Self>>::#method(frame, #argument interp)
.map(|effect| effect.map_next(Self::#variant)),
}
});
quote! {
fn #method(self, #parameter interp: &mut __FrameEngine)
-> ::core::result::Result<
#path::FrameEffect<Self, Self::Completion>,
<__FrameEngine as #path::FrameEngine>::Error,
>
{
match self { #(#arms)* }
}
}
});
Ok(quote! {
#(#conversions)*
#[automatically_derived]
impl #impl_generics #path::Frame<__FrameEngine> for #name #ty_generics #where_clause {
type Completion = #completion;
#(#methods)*
}
})
}
Loading