-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from IamTheCarl/devel
0.6 update
- Loading branch information
Showing
65 changed files
with
7,970 additions
and
7,375 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,13 @@ | ||
[package] | ||
name = "macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1.0" | ||
syn = { version = "2.0", features = ["derive"] } | ||
proc-macro2 = "1.0" |
This file contains 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,112 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, DeriveInput, Fields, Meta}; | ||
|
||
#[proc_macro_derive(Struct, attributes(default))] | ||
pub fn derive_struct(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as DeriveInput); | ||
|
||
let struct_name = input.ident; | ||
|
||
let contains_span = input.generics.type_params().any(|param| param.ident == "S"); | ||
|
||
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl(); | ||
let struct_name_string = struct_name.to_string(); | ||
let struct_definition_name = struct_name.to_string(); | ||
let struct_definition_name2 = struct_definition_name.clone(); | ||
|
||
let function_generics = if !contains_span { | ||
quote! { <S: crate::script::parsing::Span> } | ||
} else { | ||
quote! {} | ||
}; | ||
|
||
let fields = match input.data { | ||
syn::Data::Struct(data) => match data.fields { | ||
Fields::Named(fields) => fields.named, | ||
_ => panic!("Struct must have named fields."), | ||
}, | ||
_ => { | ||
panic!("Only structs can be made available to scripts."); | ||
} | ||
}; | ||
let define_fields = fields.clone().into_iter().map(|field| { | ||
let name = field.ident.unwrap().to_string(); | ||
let ty = field.ty; | ||
|
||
let mut default = quote! { None }; | ||
|
||
for attribute in field.attrs.iter() { | ||
if let Meta::NameValue(name_value) = &attribute.meta { | ||
if name_value.path.is_ident("default") { | ||
let value = &name_value.value; | ||
default = quote! { Some(crate::script::parsing::Litteral::parse(S::from_str(#value)).unwrap().1) }; | ||
} | ||
} | ||
} | ||
|
||
quote! { | ||
MemberVariable { | ||
name: S::from_str(#name), | ||
ty: MemberVariableType { | ||
ty: <#ty as crate::script::execution::types::TypedObject>::get_type(), | ||
constraints: None, | ||
default_value: #default, | ||
} | ||
} | ||
} | ||
}); | ||
|
||
let extract_fields = fields.clone().into_iter().map(|field| { | ||
let name = field.ident.unwrap(); | ||
let name_string = name.to_string(); | ||
let ty = field.ty; | ||
|
||
quote! { | ||
let #name = members.remove(#name_string).unwrap(); | ||
let #name = #name.downcast::<#ty>(span)?; | ||
} | ||
}); | ||
|
||
let import_fields = fields.clone().into_iter().map(|field| { | ||
let name = field.ident.unwrap(); | ||
quote! { #name } | ||
}); | ||
|
||
quote! { | ||
impl #impl_generics #struct_name #ty_generics #where_clause { | ||
pub fn unpack_struct #function_generics (span: &S, structure: crate::script::execution::types::Structure<S>) -> std::result::Result<Self, crate::script::execution::Failure<S>> { | ||
// Check that it's the correct type. | ||
if structure.name() == #struct_name_string { | ||
let mut members = Rc::unwrap_or_clone(structure.members); | ||
|
||
// Extract fields. | ||
#(#extract_fields)* | ||
|
||
// Import fields into the struct. | ||
Ok(#struct_name { | ||
#(#import_fields),* | ||
}) | ||
} else { | ||
Err(crate::script::execution::Failure::ExpectedGot(span.clone(), #struct_name_string.into(), structure.name().to_string().into())) | ||
} | ||
} | ||
} | ||
impl #impl_generics #struct_name #ty_generics #where_clause { | ||
fn define_struct #function_generics (context: &mut crate::script::execution::ExecutionContext<'_, S>) { | ||
context.stack.new_variable_str( | ||
#struct_definition_name, | ||
StructDefinition { | ||
definition: Rc::new(parsing::StructDefinition { | ||
name: S::from_str(#struct_definition_name2), | ||
members: vec![ | ||
#(#define_fields),* | ||
], | ||
}), | ||
} | ||
.into(), | ||
); | ||
} | ||
} | ||
}.into() | ||
} |
This file contains 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
Oops, something went wrong.