schala/schala-repl/src/lib.rs

157 lines
4.7 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;
use std::default::Default;
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;
2018-03-23 18:56:09 -07:00
const VERSION_STRING: &'static str = "0.1.0";
2017-10-30 20:06:20 -07:00
include!(concat!(env!("OUT_DIR"), "/static.rs"));
pub use language::{ProgrammingLanguageInterface, EvalOptions,
2018-07-15 15:01:50 -07:00
ExecutionMethod, TraceArtifact, FinishedComputation, UnfinishedComputation, PassDebugOptionsDescriptor, PassDescriptor};
pub type PLIGenerator = Box<Fn() -> Box<ProgrammingLanguageInterface> + Send + Sync>;
2017-10-30 20:06:20 -07:00
2018-03-23 19:04:32 -07:00
pub fn repl_main(generators: Vec<PLIGenerator>) {
2017-10-30 20:06:20 -07:00
let languages: Vec<Box<ProgrammingLanguageInterface>> = generators.iter().map(|x| x()).collect();
let option_matches = program_options().parse(std::env::args()).unwrap_or_else(|e| {
println!("{:?}", e);
exit(1);
});
if option_matches.opt_present("list-languages") {
for lang in languages {
println!("{}", lang.get_language_name());
}
exit(1);
}
if option_matches.opt_present("help") {
println!("{}", program_options().usage("Schala metainterpreter"));
exit(0);
}
if option_matches.opt_present("webapp") {
webapp::web_main(generators);
exit(0);
}
2018-03-27 00:50:31 -07:00
let mut options = EvalOptions::default();
let debug_passes = if let Some(opts) = option_matches.opt_str("debug") {
let output: Vec<String> = opts.split_terminator(",").map(|s| s.to_string()).collect();
output
} else {
vec![]
};
2018-03-27 00:50:31 -07:00
2017-10-30 20:06:20 -07:00
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
let initial_index: usize =
option_matches.opt_str("lang")
.and_then(|lang| { language_names.iter().position(|x| { x.to_lowercase() == lang.to_lowercase() }) })
2017-10-30 20:06:20 -07:00
.unwrap_or(0);
2018-03-20 20:29:07 -07:00
options.execution_method = match option_matches.opt_str("eval-style") {
Some(ref s) if s == "compile" => ExecutionMethod::Compile,
_ => ExecutionMethod::Interpret,
2017-10-30 20:06:20 -07:00
};
match option_matches.free[..] {
[] | [_] => {
2018-09-21 19:43:50 -07:00
let mut repl = repl::Repl::new(languages, initial_index);
2017-10-30 20:06:20 -07:00
repl.run();
}
[_, ref filename, _..] => {
run_noninteractive(filename, languages, options, debug_passes);
2017-10-30 20:06:20 -07:00
}
};
}
fn run_noninteractive(filename: &str, languages: Vec<Box<ProgrammingLanguageInterface>>, mut options: EvalOptions, debug_passes: Vec<String>) {
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);
});
let mut language = Box::new(languages.into_iter().find(|lang| lang.get_source_file_suffix() == ext)
.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();
for pass in debug_passes.into_iter() {
2018-07-06 03:17:37 -07:00
if let Some(_) = language.get_passes().iter().find(|desc| desc.name == pass) {
2018-07-15 15:01:50 -07:00
options.debug_passes.insert(pass, PassDebugOptionsDescriptor { opts: vec![] });
}
}
2018-03-20 20:29:07 -07:00
match options.execution_method {
ExecutionMethod::Compile => {
/*
2017-10-30 20:06:20 -07:00
let llvm_bytecode = language.compile(&buffer);
compilation_sequence(llvm_bytecode, filename);
2018-03-20 20:29:07 -07:00
*/
panic!("Not ready to go yet");
},
ExecutionMethod::Interpret => {
2018-04-29 22:53:17 -07:00
let output = language.execute_pipeline(&buffer, &options);
2018-03-20 20:29:07 -07:00
output.to_noninteractive().map(|text| println!("{}", text));
}
2017-10-30 20:06:20 -07:00
}
}
fn program_options() -> getopts::Options {
let mut options = getopts::Options::new();
options.optopt("s",
"eval-style",
"Specify whether to compile (if supported) or interpret the language. If not specified, the default is language-specific",
"[compile|interpret]"
);
options.optflag("",
"list-languages",
"Show a list of all supported languages");
options.optopt("l",
"lang",
"Start up REPL in a language",
"LANGUAGE");
options.optflag("h",
"help",
"Show help text");
options.optflag("w",
"webapp",
"Start up web interpreter");
2018-03-27 00:50:31 -07:00
options.optopt("d",
"debug",
"Debug a stage (l = tokenizer, a = AST, r = parse trace, s = symbol table)",
"[l|a|r|s]");
2017-10-30 20:06:20 -07:00
options
}