Added language name functionality

This commit is contained in:
greg 2017-02-02 01:04:15 -08:00
parent 455fe2abe2
commit 8fd5fb5a0b
4 changed files with 29 additions and 4 deletions

View File

@ -24,6 +24,7 @@ pub trait ProgrammingLanguage {
type AST: Debug; type AST: Debug;
type Evaluator: EvaluationMachine; type Evaluator: EvaluationMachine;
fn name() -> String;
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError>; fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError>;
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError>; fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError>;
fn evaluate(ast: Self::AST, evaluator: &mut Self::Evaluator) -> Vec<String>; fn evaluate(ast: Self::AST, evaluator: &mut Self::Evaluator) -> Vec<String>;
@ -44,9 +45,13 @@ pub struct LanguageInterfaceOptions {
pub trait LanguageInterface { pub trait LanguageInterface {
fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String; fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String;
fn get_language_name(&self) -> String;
} }
impl<PL, T, A, E> LanguageInterface for (PL, PL::Evaluator) where PL: ProgrammingLanguage<Token=T, AST=A, Evaluator=E>, T: Debug, A: Debug, E: EvaluationMachine { impl<PL, T, A, E> LanguageInterface for (PL, PL::Evaluator) where PL: ProgrammingLanguage<Token=T, AST=A, Evaluator=E>, 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 { fn evaluate_in_repl(&mut self, input: &str, options: LanguageInterfaceOptions) -> String {
let mut output = String::new(); let mut output = String::new();

View File

@ -19,6 +19,10 @@ impl ProgrammingLanguage for Maaru {
type AST = AST; type AST = AST;
type Evaluator = MaaruEvaluator; type Evaluator = MaaruEvaluator;
fn name() -> String {
"Maaru".to_string()
}
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> { fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
Ok(vec![Token { }]) Ok(vec![Token { }])
} }

View File

@ -21,6 +21,9 @@ use language::{ProgrammingLanguage, LanguageInterface, LLVMCodeString, Evaluatio
mod llvm_wrap; mod llvm_wrap;
fn main() { fn main() {
let languages: Vec<Box<LanguageInterface>> = vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))];
let option_matches = let option_matches =
match program_options().parse(std::env::args()) { match program_options().parse(std::env::args()) {
Ok(o) => o, Ok(o) => o,
@ -29,11 +32,17 @@ fn main() {
std::process::exit(1); 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 trace = option_matches.opt_present("t");
let show_llvm = option_matches.opt_present("l"); let show_llvm = option_matches.opt_present("l");
match option_matches.free[..] { match option_matches.free[..] {
[] | [_] => { [] | [_] => {
let mut repl = Repl::new(trace, show_llvm); let mut repl = Repl::new(languages);
repl.run(); repl.run();
} }
[_, ref filename, _..] => { [_, ref filename, _..] => {
@ -54,6 +63,9 @@ fn program_options() -> getopts::Options {
options.optflag("l", options.optflag("l",
"llvm-in-repl", "llvm-in-repl",
"Show LLVM IR in REPL"); "Show LLVM IR in REPL");
options.optflag("",
"list-languages",
"Show a list of all supported languages");
options options
} }
@ -104,15 +116,15 @@ struct Repl {
} }
impl Repl { impl Repl {
fn new(trace_evaluation: bool, show_llvm: bool) -> Repl { fn new(languages: Vec<Box<LanguageInterface>>) -> Repl {
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap(); let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap();
reader.set_prompt(">> "); reader.set_prompt(">> ");
Repl { Repl {
show_tokens: false, show_tokens: false,
show_parse: false, show_parse: false,
show_llvm_ir: show_llvm, show_llvm_ir: false,
languages: vec![Box::new((Schala::new(), SchalaEvaluator::new(None)))], languages: languages,
interpreter_directive_sigil: '.', interpreter_directive_sigil: '.',
reader: reader, reader: reader,
} }

View File

@ -20,6 +20,10 @@ impl<'a> ProgrammingLanguage for Schala<'a> {
type AST = parser::AST; type AST = parser::AST;
type Evaluator = SchalaEvaluator<'a>; type Evaluator = SchalaEvaluator<'a>;
fn name() -> String {
"Schala".to_string()
}
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> { fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
tokenizer::tokenize(input) tokenizer::tokenize(input)
} }