-
Notifications
You must be signed in to change notification settings - Fork 10
refactor(interpreter): invert frame composition ownership #723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1f9d074
refactor(interpreter): invert concrete frame composition
zhenrongliew cd2b331
refactor(interpreter): apply composition-root ownership across frame …
zhenrongliew f5d52a2
removed logging test
zhenrongliew 7db415c
feat(derive): add Frame derive macro for single-field tuple enums
zhenrongliew 6419a99
test(frame): add unit test for member conversions and dispatch
zhenrongliew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)* | ||
| } | ||
| }) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, andDenseFrameBuild. I've not added new snapshots for the newframe.rs.There was a problem hiding this comment.
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