From 8fd5fb5a0b9eb9e4f645ff89078632a09c709b31 Mon Sep 17 00:00:00 2001 From: greg Date: Thu, 2 Feb 2017 01:04:15 -0800 Subject: [PATCH] Added language name functionality --- src/language.rs | 5 +++++ src/maaru_lang/mod.rs | 4 ++++ src/main.rs | 20 ++++++++++++++++---- src/schala_lang/mod.rs | 4 ++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/language.rs b/src/language.rs index b07c107..43aceaf 100644 --- a/src/language.rs +++ b/src/language.rs @@ -24,6 +24,7 @@ pub trait ProgrammingLanguage { type AST: Debug; type Evaluator: EvaluationMachine; + fn name() -> String; fn tokenize(input: &str) -> Result, TokenError>; fn parse(input: Vec) -> Result; fn evaluate(ast: Self::AST, evaluator: &mut Self::Evaluator) -> Vec; @@ -44,9 +45,13 @@ pub struct LanguageInterfaceOptions { pub trait LanguageInterface { fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String; + fn get_language_name(&self) -> String; } impl LanguageInterface for (PL, PL::Evaluator) where PL: ProgrammingLanguage, T: Debug, A: Debug, E: EvaluationMachine { + fn get_language_name(&self) -> String { + PL::name() + } fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String { let mut output = String::new(); diff --git a/src/maaru_lang/mod.rs b/src/maaru_lang/mod.rs index 891305c..f35ae19 100644 --- a/src/maaru_lang/mod.rs +++ b/src/maaru_lang/mod.rs @@ -19,6 +19,10 @@ impl ProgrammingLanguage for Maaru { type AST = AST; type Evaluator = MaaruEvaluator; + fn name() -> String { + "Maaru".to_string() + } + fn tokenize(input: &str) -> Result, TokenError> { Ok(vec![Token { }]) } diff --git a/src/main.rs b/src/main.rs index 40c4c82..b0c3607 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,9 @@ use language::{ProgrammingLanguage, LanguageInterface, LLVMCodeString, Evaluatio mod llvm_wrap; fn main() { + + let languages: Vec> = vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))]; + let option_matches = match program_options().parse(std::env::args()) { Ok(o) => o, @@ -29,11 +32,17 @@ fn main() { std::process::exit(1); } }; + if option_matches.opt_present("list-languages") { + for lang in languages { + println!("{}", lang.get_language_name()); + } + std::process::exit(1); + } let trace = option_matches.opt_present("t"); let show_llvm = option_matches.opt_present("l"); match option_matches.free[..] { [] | [_] => { - let mut repl = Repl::new(trace, show_llvm); + let mut repl = Repl::new(languages); repl.run(); } [_, ref filename, _..] => { @@ -54,6 +63,9 @@ fn program_options() -> getopts::Options { options.optflag("l", "llvm-in-repl", "Show LLVM IR in REPL"); + options.optflag("", + "list-languages", + "Show a list of all supported languages"); options } @@ -104,15 +116,15 @@ struct Repl { } impl Repl { - fn new(trace_evaluation: bool, show_llvm: bool) -> Repl { + fn new(languages: Vec>) -> Repl { let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap(); reader.set_prompt(">> "); Repl { show_tokens: false, show_parse: false, - show_llvm_ir: show_llvm, - languages: vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))], + show_llvm_ir: false, + languages: languages, interpreter_directive_sigil: '.', reader: reader, } diff --git a/src/schala_lang/mod.rs b/src/schala_lang/mod.rs index c5a7e83..09d2ee6 100644 --- a/src/schala_lang/mod.rs +++ b/src/schala_lang/mod.rs @@ -20,6 +20,10 @@ impl<'a> ProgrammingLanguage for Schala<'a> { type AST = parser::AST; type Evaluator = SchalaEvaluator<'a>; + fn name() -> String { + "Schala".to_string() + } + fn tokenize(input: &str) -> Result, TokenError> { tokenizer::tokenize(input) }