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"
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",

View File

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

View File

@ -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"

View File

@ -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<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);
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<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 ext = path
.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() {
let langs: Vec<Box<dyn ProgrammingLanguageInterface>> = vec![Box::new(schala_lang::Schala::new())];
start_repl(langs);
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);
} 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
}