2018-04-27 02:19:09 -07:00
|
|
|
#![feature(proc_macro)]
|
|
|
|
extern crate proc_macro;
|
2018-04-30 00:35:42 -07:00
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
|
|
|
extern crate syn;
|
2018-04-30 02:24:21 -07:00
|
|
|
|
2018-04-27 02:19:09 -07:00
|
|
|
use proc_macro::TokenStream;
|
2018-04-30 02:24:21 -07:00
|
|
|
use syn::DeriveInput;
|
2018-04-27 02:19:09 -07:00
|
|
|
|
|
|
|
#[proc_macro]
|
|
|
|
pub fn print_a_thing(_input: TokenStream) -> TokenStream {
|
|
|
|
"println!(\"Invoked from a proc macro\");".parse().unwrap()
|
|
|
|
}
|
|
|
|
|
2018-04-30 00:35:42 -07:00
|
|
|
|
2018-05-02 02:14:36 -07:00
|
|
|
#[proc_macro_derive(ProgrammingLanguageInterface, attributes(LanguageName, FileExtension, PipelineSteps))]
|
2018-04-30 00:35:42 -07:00
|
|
|
pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream {
|
2018-04-30 02:24:21 -07:00
|
|
|
let ast: DeriveInput = syn::parse(input).unwrap();
|
|
|
|
let name = &ast.ident;
|
2018-05-02 03:53:38 -07:00
|
|
|
let attrs = &ast.attrs;
|
|
|
|
|
2018-05-02 04:50:26 -07:00
|
|
|
|
2018-05-02 16:06:28 -07:00
|
|
|
let extracted_lang_name: Option<String> = attrs.iter().map(|attr| attr.interpret_meta()).find(|meta| {
|
2018-05-02 04:50:26 -07:00
|
|
|
match meta {
|
|
|
|
&Some(syn::Meta::NameValue(syn::MetaNameValue { ident, .. })) if ident.as_ref() == "LanguageName" => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}).and_then(|meta| {
|
|
|
|
match meta {
|
|
|
|
Some(syn::Meta::NameValue(syn::MetaNameValue { lit: syn::Lit::Str(litstr), .. })) => Some(litstr.value()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-05-02 16:06:28 -07:00
|
|
|
let language_name = extracted_lang_name.unwrap();
|
|
|
|
|
2018-05-02 04:50:26 -07:00
|
|
|
println!("LANG NAME: {:?}", language_name);
|
|
|
|
|
|
|
|
/*
|
2018-05-02 03:53:38 -07:00
|
|
|
println!("ATTRS {:?}", attrs);
|
2018-05-02 04:50:26 -07:00
|
|
|
let meta: Option<syn::Meta> = attrs.get(0).unwrap().interpret_meta();
|
|
|
|
println!("META: {:?}", meta);
|
|
|
|
match meta {
|
|
|
|
Some(syn::Meta::NameValue(syn::MetaNameValue { lit, .. })) => {
|
|
|
|
println!("GOT LIT: {:?}", lit);
|
|
|
|
match lit {
|
|
|
|
syn::Lit::Str(litstr) => println!("VAL: {}", litstr.value()),
|
|
|
|
_ => panic!("OI")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("YO")
|
|
|
|
}
|
|
|
|
*/
|
2018-05-02 03:53:38 -07:00
|
|
|
|
2018-05-02 02:14:36 -07:00
|
|
|
|
2018-04-30 02:24:21 -07:00
|
|
|
let tokens = quote! {
|
|
|
|
impl ProgrammingLanguageInterface for #name {
|
2018-05-02 02:14:36 -07:00
|
|
|
fn get_language_name(&self) -> String {
|
2018-05-02 16:06:28 -07:00
|
|
|
#language_name.to_string()
|
2018-05-02 02:14:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_file_suffix(&self) -> String {
|
2018-05-02 16:06:28 -07:00
|
|
|
#language_name.to_string()
|
2018-05-02 02:14:36 -07:00
|
|
|
}
|
|
|
|
fn execute_pipeline(&mut self, input: &str, options: &EvalOptions) -> FinishedComputation {
|
|
|
|
let mut chain = pass_chain![self, options;
|
|
|
|
tokenizing_stage,
|
|
|
|
parsing_stage,
|
|
|
|
symbol_table_stage,
|
|
|
|
typechecking_stage,
|
|
|
|
eval_stage
|
|
|
|
];
|
|
|
|
chain(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_stages(&self) -> Vec<String> {
|
|
|
|
vec![
|
|
|
|
format!("tokenizing_stage"),
|
|
|
|
format!("parsing_stage"), //TODO handle both types of this
|
|
|
|
format!("symbol_table_stage"),
|
|
|
|
format!("typechecking_stage"),
|
|
|
|
format!("eval_stage")
|
|
|
|
]
|
|
|
|
}
|
2018-04-30 02:24:21 -07:00
|
|
|
}
|
|
|
|
};
|
2018-05-02 02:14:36 -07:00
|
|
|
|
2018-04-30 02:24:21 -07:00
|
|
|
tokens.into()
|
2018-04-30 00:35:42 -07:00
|
|
|
}
|
|
|
|
|
2018-04-27 02:19:09 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn it_works() {
|
|
|
|
assert_eq!(2 + 2, 4);
|
|
|
|
}
|
|
|
|
}
|