Refactor main.rs

main.rs controls options, calls into interactive or non-interactive
start function from schala-repl.
This commit is contained in:
Greg Shuflin 2021-10-07 01:41:54 -07:00
parent ec6f4b510e
commit 6012e8cf9d
5 changed files with 35 additions and 35 deletions

2
Cargo.lock generated
View File

@ -821,6 +821,7 @@ checksum = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
name = "schala" name = "schala"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"getopts",
"includedir_codegen", "includedir_codegen",
"schala-lang", "schala-lang",
"schala-repl", "schala-repl",
@ -857,7 +858,6 @@ name = "schala-repl"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"colored", "colored",
"getopts",
"includedir", "includedir",
"includedir_codegen", "includedir_codegen",
"itertools", "itertools",

View File

@ -6,6 +6,7 @@ edition = "2018"
resolver = "2" resolver = "2"
[dependencies] [dependencies]
getopts = "0.2.21"
schala-repl = { path = "schala-repl" } schala-repl = { path = "schala-repl" }
schala-lang = { path = "schala-lang/language" } schala-lang = { path = "schala-lang/language" }

View File

@ -9,7 +9,6 @@ resolver = "2"
llvm-sys = "70.0.2" llvm-sys = "70.0.2"
take_mut = "0.2.2" take_mut = "0.2.2"
itertools = "0.10" itertools = "0.10"
getopts = "0.2.18"
lazy_static = "0.2.8" lazy_static = "0.2.8"
maplit = "*" maplit = "*"
colored = "1.8" colored = "1.8"

View File

@ -1,7 +1,6 @@
#![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)] #![feature(box_patterns, box_syntax, proc_macro_hygiene, decl_macro)]
#![feature(plugin)] #![feature(plugin)]
extern crate colored; extern crate colored;
extern crate getopts;
extern crate itertools; extern crate itertools;
extern crate linefeed; extern crate linefeed;
@ -29,30 +28,12 @@ include!(concat!(env!("OUT_DIR"), "/static.rs"));
const VERSION_STRING: &'static str = "0.1.0"; const VERSION_STRING: &'static str = "0.1.0";
pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) { pub fn start_repl(langs: Vec<Box<dyn ProgrammingLanguageInterface>>) {
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); let mut repl = repl::Repl::new(langs);
repl.run_repl(); repl.run_repl();
}
[_, ref filename, ..] => {
run_noninteractive(filename, langs);
}
};
} }
fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) { //TODO should really expect a list of paths
pub fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
let path = Path::new(filename); let path = Path::new(filename);
let ext = path let ext = path
.extension() .extension()
@ -87,9 +68,3 @@ fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguage
}; };
} }
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
}

View File

@ -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() { fn main() {
let langs: Vec<Box<dyn ProgrammingLanguageInterface>> = vec![Box::new(schala_lang::Schala::new())]; let args: Vec<String> = 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<Box<dyn ProgrammingLanguageInterface>> =
vec![Box::new(schala_lang::Schala::new())];
if matches.free.is_empty() {
start_repl(langs); 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
}