Making main.rs more concise
This commit is contained in:
parent
1aa4e3b942
commit
c88d59401c
@ -22,6 +22,7 @@ pub struct EvalOptions {
|
|||||||
pub debug_type: bool,
|
pub debug_type: bool,
|
||||||
pub show_llvm_ir: bool,
|
pub show_llvm_ir: bool,
|
||||||
pub trace_evaluation: bool,
|
pub trace_evaluation: bool,
|
||||||
|
pub compile: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
|
55
src/main.rs
55
src/main.rs
@ -16,9 +16,8 @@ extern crate rocket_contrib;
|
|||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::{Read, Write};
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::io::Write;
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
|
||||||
mod schala_lang;
|
mod schala_lang;
|
||||||
@ -39,14 +38,11 @@ fn main() {
|
|||||||
Box::new(robo_lang::Robo::new()),
|
Box::new(robo_lang::Robo::new()),
|
||||||
];
|
];
|
||||||
|
|
||||||
let option_matches =
|
let option_matches = program_options().parse(std::env::args()).unwrap_or_else(|e| {
|
||||||
match program_options().parse(std::env::args()) {
|
println!("{:?}", e);
|
||||||
Ok(o) => o,
|
std::process::exit(1);
|
||||||
Err(e) => {
|
});
|
||||||
println!("{:?}", e);
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if option_matches.opt_present("list-languages") {
|
if option_matches.opt_present("list-languages") {
|
||||||
for lang in languages {
|
for lang in languages {
|
||||||
println!("{}", lang.get_language_name());
|
println!("{}", lang.get_language_name());
|
||||||
@ -54,48 +50,49 @@ fn main() {
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if option_matches.opt_present("h") {
|
if option_matches.opt_present("help") {
|
||||||
println!("{}", program_options().usage("Schala metainterpreter"));
|
println!("{}", program_options().usage("Schala metainterpreter"));
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if option_matches.opt_present("w") {
|
if option_matches.opt_present("webapp") {
|
||||||
webapp::web_main();
|
webapp::web_main();
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
|
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
|
||||||
let initial_index: usize =
|
let initial_index: usize =
|
||||||
option_matches.opt_str("l")
|
option_matches.opt_str("lang")
|
||||||
.and_then(|lang| { language_names.iter().position(|x| { *x == lang }) })
|
.and_then(|lang| { language_names.iter().position(|x| { *x == lang }) })
|
||||||
.unwrap_or(0);
|
.unwrap_or(0);
|
||||||
|
|
||||||
let mut options = EvalOptions::default();
|
let mut options = EvalOptions::default();
|
||||||
options.trace_evaluation = option_matches.opt_present("t");
|
options.compile = match option_matches.opt_str("eval-style") {
|
||||||
|
Some(ref s) if s == "compile" => true,
|
||||||
let compile = !option_matches.opt_present("i");
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
match option_matches.free[..] {
|
match option_matches.free[..] {
|
||||||
[] | [_] => {
|
[] | [_] => {
|
||||||
let mut repl = Repl::new(languages, initial_index);
|
let mut repl = Repl::new(languages, initial_index);
|
||||||
repl.options.show_llvm_ir = option_matches.opt_present("v");
|
repl.options.show_llvm_ir = true; //TODO make this be configurable
|
||||||
repl.run();
|
repl.run();
|
||||||
}
|
}
|
||||||
[_, ref filename, _..] => {
|
[_, ref filename, _..] => {
|
||||||
let mut language = maaru_lang::Maaru::new();
|
run_noninteractive(filename, options);
|
||||||
run_noninteractive(filename, &mut language, options, compile);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, options: EvalOptions, compile: bool) {
|
fn run_noninteractive(filename: &str, options: EvalOptions) {
|
||||||
|
let mut language = maaru_lang::Maaru::new();
|
||||||
let mut source_file = File::open(&Path::new(filename)).unwrap();
|
let mut source_file = File::open(&Path::new(filename)).unwrap();
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
source_file.read_to_string(&mut buffer).unwrap();
|
source_file.read_to_string(&mut buffer).unwrap();
|
||||||
|
|
||||||
if compile {
|
if options.compile {
|
||||||
if !language.can_compile() {
|
if !language.can_compile() {
|
||||||
panic!("Trying to compile a non-compileable language");
|
panic!("Trying to compile a non-compileable language");
|
||||||
} else {
|
} else {
|
||||||
let llvm_bytecode = language.compile(&buffer);
|
let llvm_bytecode = language.compile(&buffer);
|
||||||
compilation_sequence(llvm_bytecode, filename);
|
compilation_sequence(llvm_bytecode, filename);
|
||||||
@ -331,15 +328,11 @@ pub fn compilation_sequence(llvm_code: LLVMCodeString, sourcefile: &str) {
|
|||||||
|
|
||||||
fn program_options() -> getopts::Options {
|
fn program_options() -> getopts::Options {
|
||||||
let mut options = getopts::Options::new();
|
let mut options = getopts::Options::new();
|
||||||
options.optflag("i",
|
options.optopt("s",
|
||||||
"interpret",
|
"eval-style",
|
||||||
"Interpret source file instead of compiling");
|
"Specify whether to compile (if supported) or interpret the language. If not specified, the default is language-specific",
|
||||||
options.optflag("t",
|
"[compile|interpret]"
|
||||||
"trace-evaluation",
|
);
|
||||||
"Print out trace of evaluation");
|
|
||||||
options.optflag("v",
|
|
||||||
"llvm-in-repl",
|
|
||||||
"Show LLVM IR in REPL");
|
|
||||||
options.optflag("",
|
options.optflag("",
|
||||||
"list-languages",
|
"list-languages",
|
||||||
"Show a list of all supported languages");
|
"Show a list of all supported languages");
|
||||||
|
Loading…
Reference in New Issue
Block a user