schala/schala-repl/src/lib.rs

85 lines
2.2 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;
2019-01-01 02:22:12 -08:00
#[macro_use]
2017-10-30 20:06:20 -07:00
extern crate rocket;
extern crate rocket_contrib;
extern crate includedir;
extern crate phf;
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
mod webapp;
2019-03-27 02:20:43 -07:00
pub use language::{ProgrammingLanguageInterface, ComputationRequest, ComputationResponse, LangMetaRequest, LangMetaResponse, DebugRequest, DebugResponse, 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
}
[_, ref filename, _..] => {
run_noninteractive(filename, langs);
2017-10-30 20:06:20 -07:00
}
};
}
fn run_noninteractive(filename: &str, languages: Vec<Box<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-03-16 18:45:40 -07:00
let 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();
println!("NON-INTERACTIVE OUTPUT DOESN'T WORK YET");
panic!();
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
}