From bf7533cdbe0c3a6ae74ba646a2f9f1a1773bbcd4 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 28 Apr 2018 03:31:53 -0700 Subject: [PATCH] Some experimentation --- schala-codegen/Cargo.toml | 3 ++ schala-codegen/src/lib.rs | 66 ++++++++++++++++++++++++++++++++++++--- schala-lang/src/lib.rs | 2 +- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/schala-codegen/Cargo.toml b/schala-codegen/Cargo.toml index 28c33b8..ae26c45 100644 --- a/schala-codegen/Cargo.toml +++ b/schala-codegen/Cargo.toml @@ -4,6 +4,9 @@ version = "0.1.0" authors = ["greg "] [dependencies] +quote = "0.5.2" +syn = { version = "0.13.1", features = ["full"] } + [lib] proc-macro = true diff --git a/schala-codegen/src/lib.rs b/schala-codegen/src/lib.rs index 4f78247..943a06b 100644 --- a/schala-codegen/src/lib.rs +++ b/schala-codegen/src/lib.rs @@ -1,15 +1,71 @@ #![feature(proc_macro)] extern crate proc_macro; +#[macro_use] +extern crate syn; +#[macro_use] +extern crate quote; + use proc_macro::TokenStream; +use syn::{Expr, Lit, ExprLit}; +use syn::punctuated::Punctuated; +use syn::synom::Synom; + +fn tok() -> String { + "ONE THING FROM A MACRO|".to_string() +} + +fn pars() -> String { + "ANOTHER MACRO THING|".to_string() +} #[proc_macro] pub fn compiler_pass_sequence(input: TokenStream) -> TokenStream { - r#" - fn new_execute(&mut self, input: &str, _options: &EvalOptions) -> FinishedComputation { - let mut evaluation = UnfinishedComputation::default(); - evaluation.output(Err("this comes at ye from the macro".to_string())) + /* + for token_tree in input { + //println!("ITEM: {:?}", token_tree.kind); + match token_tree.kind { + TokenNode::Literal(l) => println!("{:?}", l), + _ => () + } } - "#.parse().unwrap() + */ + + + let mut contained_strings = Vec::new(); + + let input: Expr = syn::parse(input).unwrap(); + match input { + Expr::Array(array) => { + for item in array.elems { + if let Expr::Lit(ExprLit { lit: Lit::Str(s), ..}) = item { + contained_strings.push(s.value()); + } else { + panic!("BAD INPUT"); + } + } + }, + _ => panic!("BAD INPUT"), + } + + + let mut from_macro = String::new(); + for item in contained_strings { + if item == "tok" { + from_macro.push_str(tok().as_ref()); + } + if item == "pars" { + from_macro.push_str(pars().as_ref()); + } + } + + + let output = quote! { + fn new_execute(&mut self, input: &str, _options: &EvalOptions) -> FinishedComputation { + let mut evaluation = UnfinishedComputation::default(); + evaluation.output(Err(#from_macro.to_string())) + } + }; + output.into() } /* #[compiler_pass(*/ diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index bfdb6f5..978ba00 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -43,7 +43,7 @@ impl Schala { impl ProgrammingLanguageInterface for Schala { - schala_codegen::compiler_pass_sequence!("tok", "pars", "exec"); + schala_codegen::compiler_pass_sequence!(["tok"]); fn get_language_name(&self) -> String { "Schala".to_string()