Add logic for picking language with command line flags

This commit is contained in:
greg 2017-02-03 01:08:51 -08:00
parent e0c49abe56
commit 48b0b8d053

View File

@ -42,13 +42,24 @@ fn main() {
std::process::exit(1); std::process::exit(1);
} }
if option_matches.opt_present("h") {
println!("{}", program_options().usage("Schala metainterpreter"));
std::process::exit(1);
}
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
let initial_index: usize =
option_matches.opt_str("l")
.and_then(|lang| { language_names.iter().position(|x| { *x == lang }) })
.unwrap_or(0);
let show_llvm_ir = option_matches.opt_present("v"); let show_llvm_ir = option_matches.opt_present("v");
let compile = !option_matches.opt_present("i"); let compile = !option_matches.opt_present("i");
let trace_evaluation = option_matches.opt_present("t"); let trace_evaluation = option_matches.opt_present("t");
match option_matches.free[..] { match option_matches.free[..] {
[] | [_] => { [] | [_] => {
let mut repl = Repl::new(languages); let mut repl = Repl::new(languages, initial_index);
repl.show_llvm_ir = show_llvm_ir; repl.show_llvm_ir = show_llvm_ir;
repl.run(); repl.run();
} }
@ -73,6 +84,13 @@ fn program_options() -> getopts::Options {
options.optflag("", options.optflag("",
"list-languages", "list-languages",
"Show a list of all supported languages"); "Show a list of all supported languages");
options.optopt("l",
"lang",
"Start up REPL in a language",
"LANGUAGE");
options.optflag("h",
"help",
"Show help text");
options options
} }
@ -124,16 +142,16 @@ struct Repl {
} }
impl Repl { impl Repl {
fn new(languages: Vec<Box<LanguageInterface>>) -> Repl { fn new(languages: Vec<Box<LanguageInterface>>, initial_index: usize) -> Repl {
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap(); let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
reader.set_prompt(">> "); reader.set_prompt(">> ");
let i = if initial_index < languages.len() { initial_index } else { 0 };
Repl { Repl {
show_tokens: false, show_tokens: false,
show_parse: false, show_parse: false,
show_llvm_ir: false, show_llvm_ir: false,
languages: languages, languages: languages,
current_language_index: 0, current_language_index: i,
interpreter_directive_sigil: '.', interpreter_directive_sigil: '.',
reader: reader, reader: reader,
} }
@ -141,6 +159,7 @@ impl Repl {
fn run(&mut self) { fn run(&mut self) {
use linefeed::ReadResult::*; use linefeed::ReadResult::*;
println!("MetaInterpreter v 0.05"); println!("MetaInterpreter v 0.05");
println!("Using language: {}", self.languages[self.current_language_index].get_language_name());
loop { loop {
match self.reader.read_line() { match self.reader.read_line() {
Err(e) => { Err(e) => {