|
| 1 | +use syn; |
| 2 | +use quote; |
| 3 | +use regex; |
| 4 | +// use syn::token::{Semi}; |
| 5 | +use syn::synom::{Synom}; |
| 6 | +use syn::punctuated::Punctuated; |
| 7 | +use quote::{ToTokens, Tokens}; |
| 8 | +use proc_macro2::{TokenStream}; |
| 9 | + |
| 10 | +struct ReqArg { name: syn::Ident, typ: syn::TypePath } |
| 11 | +struct OptArg { pub arg: ReqArg } |
| 12 | +trait Arg { |
| 13 | + fn get_name(&self) -> &syn::Ident; |
| 14 | + fn get_type(&self) -> &syn::TypePath; |
| 15 | + fn get_conv(&self) -> Tokens; |
| 16 | +} |
| 17 | + |
| 18 | +impl Arg for ReqArg { |
| 19 | + fn get_name(&self) -> &syn::Ident { &self.name } |
| 20 | + fn get_type(&self) -> &syn::TypePath { &self.typ } |
| 21 | + |
| 22 | + fn get_conv(&self) -> Tokens { |
| 23 | + quote!( |
| 24 | + Scm::<Untyped>::from_raw(n).into_type().unwrap(); |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl Arg for OptArg { |
| 30 | + fn get_name(&self) -> &syn::Ident { self.arg.get_name() } |
| 31 | + fn get_type(&self) -> &syn::TypePath { self.arg.get_type() } |
| 32 | + |
| 33 | + fn get_conv(&self) -> Tokens { |
| 34 | + quote!( |
| 35 | + // TODO: figure out how to convert SCM to Option<Scm<T>> |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Synom for ReqArg { |
| 41 | + named!(parse -> Self, do_parse!( |
| 42 | + name: syn!(syn::Ident) >> |
| 43 | + punct!(:) >> |
| 44 | + typ: syn!(syn::TypePath) >> |
| 45 | + cond_reduce!( |
| 46 | + typ.path.segments.last().unwrap().value().ident == "Scm") >> |
| 47 | + |
| 48 | + (ReqArg { name, typ }) |
| 49 | + )); |
| 50 | +} |
| 51 | + |
| 52 | +impl Synom for OptArg { |
| 53 | + named!(parse -> Self, do_parse!( |
| 54 | + name: syn!(syn::Ident) >> |
| 55 | + punct!(:) >> |
| 56 | + |
| 57 | + _opt: syn!(syn::Ident) >> |
| 58 | + cond_reduce!(_opt == "Option") >> |
| 59 | + |
| 60 | + punct!(<) >> |
| 61 | + typ: syn!(syn::TypePath) >> |
| 62 | + cond_reduce!( |
| 63 | + typ.path.segments.last().unwrap().value().ident == "Scm") >> |
| 64 | + punct!(>) >> |
| 65 | + |
| 66 | + (OptArg { arg: ReqArg { name, typ } }) |
| 67 | + )); |
| 68 | +} |
| 69 | + |
| 70 | +named!(pub parse_guile_define_subr -> TokenStream, do_parse!( |
| 71 | + name: syn!(syn::Expr) >> |
| 72 | + punct!(,) >> |
| 73 | + punct!(|) >> |
| 74 | + args: call!(Punctuated::<ReqArg, Token![,]>::parse_terminated) >> |
| 75 | + oargs: call!(Punctuated::<OptArg, Token![,]>::parse_terminated) >> |
| 76 | + punct!(|) >> |
| 77 | + punct!(->) >> |
| 78 | + ret: syn!(syn::TypePath) >> |
| 79 | + cond_reduce!( |
| 80 | + ret.path.segments.last().unwrap().value().ident == "Scm") >> |
| 81 | + body: syn!(syn::Expr) >> |
| 82 | + |
| 83 | + ({ |
| 84 | + let arg_names = args.iter().map(|ref a| a.get_name()) |
| 85 | + .collect::<Vec<_>>(); |
| 86 | + let arg_names2 = arg_names.clone(); |
| 87 | + let arg_types = args.iter().map(|ref a| a.get_type()); |
| 88 | + let arg_convs = args.iter().map(|ref a| a.get_conv()); |
| 89 | + let num_args = args.len(); |
| 90 | + |
| 91 | + let oarg_names = oargs.iter().map(|ref a| a.get_name()) |
| 92 | + .collect::<Vec<_>>(); |
| 93 | + let oarg_names2 = oarg_names.clone(); |
| 94 | + let oarg_types = oargs.iter().map(|ref a| a.get_type()); |
| 95 | + let oarg_convs = oargs.iter().map(|ref a| a.get_conv()); |
| 96 | + let num_oargs = oargs.len(); |
| 97 | + |
| 98 | + quote!({ |
| 99 | + unsafe { |
| 100 | + unsafe extern "C" ___tmp_c_fn(#(#arg_names: SCM),* , #(#oarg_names: SCM),* ) -> SCM { |
| 101 | + #(let #arg_names2: #arg_types = #arg_convs; )* |
| 102 | + #(let #oarg_names2: #oarg_types = #oarg_convs;)* |
| 103 | + |
| 104 | + let o: #ret = #body; |
| 105 | + |
| 106 | + o.into_raw() |
| 107 | + } |
| 108 | + |
| 109 | + let _ = scm_c_define_gsubr( |
| 110 | + CString::new(#name).unwrap().as_ptr(), |
| 111 | + #num_args, #num_oargs, 0, |
| 112 | + ___tmp_c_fn as scm_t_subr |
| 113 | + ); |
| 114 | + } |
| 115 | + }).into() |
| 116 | + }) |
| 117 | +)); |
0 commit comments