From 6012e8cf9d6cfe6b52441489c61f40dd987f324d Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 7 Oct 2021 01:41:54 -0700 Subject: [PATCH] Refactor main.rs main.rs controls options, calls into interactive or non-interactive start function from schala-repl. --- Cargo.lock | 2 +- Cargo.toml | 1 + schala-repl/Cargo.toml | 1 - schala-repl/src/lib.rs | 33 ++++----------------------------- src/main.rs | 33 +++++++++++++++++++++++++++++---- 5 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c70e141..1071a61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -821,6 +821,7 @@ checksum = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997" name = "schala" version = "0.1.0" dependencies = [ + "getopts", "includedir_codegen", "schala-lang", "schala-repl", @@ -857,7 +858,6 @@ name = "schala-repl" version = "0.1.0" dependencies = [ "colored", - "getopts", "includedir", "includedir_codegen", "itertools", diff --git a/Cargo.toml b/Cargo.toml index e46b064..688a0df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" resolver = "2" [dependencies] +getopts = "0.2.21" schala-repl = { path = "schala-repl" } schala-lang = { path = "schala-lang/language" } diff --git a/schala-repl/Cargo.toml b/schala-repl/Cargo.toml index b659b94..2385723 100644 --- a/schala-repl/Cargo.toml +++ b/schala-repl/Cargo.toml @@ -9,7 +9,6 @@ resolver = "2" llvm-sys = "70.0.2" take_mut = "0.2.2" itertools = "0.10" -getopts = "0.2.18" lazy_static = "0.2.8" maplit = "*" colored = "1.8" diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index ffcfa62..8c8410e 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -1,7 +1,6 @@ #![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)] #![feature(plugin)] extern crate colored; -extern crate getopts; extern crate itertools; extern crate linefeed; @@ -29,30 +28,12 @@ include!(concat!(env!("OUT_DIR"), "/static.rs")); const VERSION_STRING: &'static str = "0.1.0"; pub fn start_repl(langs: Vec>) { - let options = command_line_options() - .parse(std::env::args()) - .unwrap_or_else(|e| { - println!("{:?}", e); - exit(1); - }); - - if options.opt_present("help") { - println!("{}", command_line_options().usage("Schala metainterpreter")); - exit(0); - } - - match options.free[..] { - [] | [_] => { - let mut repl = repl::Repl::new(langs); - repl.run_repl(); - } - [_, ref filename, ..] => { - run_noninteractive(filename, langs); - } - }; + let mut repl = repl::Repl::new(langs); + repl.run_repl(); } -fn run_noninteractive(filename: &str, languages: Vec>) { +//TODO should really expect a list of paths +pub fn run_noninteractive(filename: &str, languages: Vec>) { let path = Path::new(filename); let ext = path .extension() @@ -87,9 +68,3 @@ fn run_noninteractive(filename: &str, languages: Vec getopts::Options { - let mut options = getopts::Options::new(); - options.optflag("h", "help", "Show help text"); - options.optflag("w", "webapp", "Start up web interpreter"); - options -} diff --git a/src/main.rs b/src/main.rs index 29a3b5b..9a72ea3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,34 @@ -use schala_repl::{ProgrammingLanguageInterface, start_repl}; +use schala_repl::{run_noninteractive, start_repl, ProgrammingLanguageInterface}; -extern { } +use std::process::exit; fn main() { - let langs: Vec> = vec![Box::new(schala_lang::Schala::new())]; - start_repl(langs); + let args: Vec = std::env::args().collect(); + let matches = command_line_options() + .parse(&args[1..]) + .unwrap_or_else(|e| { + eprintln!("Error parsing options: {}", e); + exit(1); + }); + + if matches.opt_present("help") { + println!("{}", command_line_options().usage("Schala metainterpreter")); + exit(0); + } + + let langs: Vec> = + vec![Box::new(schala_lang::Schala::new())]; + + if matches.free.is_empty() { + start_repl(langs); + } else { + run_noninteractive(&matches.free[0], langs); + } } +fn command_line_options() -> getopts::Options { + let mut options = getopts::Options::new(); + options.optflag("h", "help", "Show help text"); + options.optflag("w", "webapp", "Start up web interpreter"); + options +}