schala/schala-repl/src/lib.rs

93 lines
2.4 KiB
Rust
Raw Normal View History

2017-10-30 20:06:20 -07:00
#![feature(link_args)]
2019-01-01 02:22:12 -08:00
#![feature(slice_patterns, box_patterns, box_syntax, proc_macro_hygiene, decl_macro)]
2017-10-30 20:06:20 -07:00
#![feature(plugin)]
extern crate getopts;
extern crate linefeed;
2017-10-30 20:06:20 -07:00
extern crate itertools;
2018-03-24 15:14:24 -07:00
extern crate colored;
2018-03-20 20:29:07 -07:00
2017-10-30 20:06:20 -07:00
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate includedir;
extern crate phf;
2019-05-26 04:16:40 -07:00
use std::collections::HashSet;
2017-10-30 20:06:20 -07:00
use std::path::Path;
use std::fs::File;
2018-09-21 19:43:50 -07:00
use std::io::Read;
2017-10-30 20:06:20 -07:00
use std::process::exit;
2018-03-01 02:43:11 -08:00
2018-09-21 19:43:50 -07:00
mod repl;
mod language;
2017-10-30 20:06:20 -07:00
pub use language::{ProgrammingLanguageInterface,
ComputationRequest, ComputationResponse,
LangMetaRequest, LangMetaResponse,
2019-05-25 19:31:41 -07:00
DebugResponse, DebugAsk, GlobalOutputStats};
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| {
2017-10-30 20:06:20 -07:00
println!("{:?}", e);
exit(1);
});
if options.opt_present("help") {
println!("{}", command_line_options().usage("Schala metainterpreter"));
2017-10-30 20:06:20 -07:00
exit(0);
}
match options.free[..] {
2017-10-30 20:06:20 -07:00
[] | [_] => {
2019-03-14 03:42:39 -07:00
let mut repl = repl::Repl::new(langs);
repl.run_repl();
2017-10-30 20:06:20 -07:00
}
2019-09-03 02:59:19 -07:00
[_, ref filename, ..] => {
run_noninteractive(filename, langs);
2017-10-30 20:06:20 -07:00
}
};
}
2019-06-21 02:01:46 -07:00
fn run_noninteractive(filename: &str, languages: Vec<Box<dyn ProgrammingLanguageInterface>>) {
2017-10-30 20:06:20 -07:00
let path = Path::new(filename);
let ext = path.extension().and_then(|e| e.to_str()).unwrap_or_else(|| {
println!("Source file lacks extension");
exit(1);
});
2019-05-14 01:57:31 -07:00
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
2017-10-30 20:06:20 -07:00
.unwrap_or_else(|| {
println!("Extension .{} not recognized", ext);
exit(1);
}));
let mut source_file = File::open(path).unwrap();
let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap();
2019-05-14 01:57:31 -07:00
let request = ComputationRequest {
source: &buffer,
2019-05-26 04:16:40 -07:00
debug_requests: HashSet::new(),
2019-05-14 01:57:31 -07:00
};
let response = language.run_computation(request);
match response.main_output {
Ok(s) => println!("{}", s),
Err(s) => println!("{}", s)
};
2017-10-30 20:06:20 -07:00
}
fn command_line_options() -> getopts::Options {
2017-10-30 20:06:20 -07:00
let mut options = getopts::Options::new();
options.optflag("h",
"help",
"Show help text");
options.optflag("w",
"webapp",
"Start up web interpreter");
options
}