2017-10-30 20:06:20 -07:00
|
|
|
#![feature(link_args)]
|
2018-04-03 23:24:13 -07:00
|
|
|
#![feature(slice_patterns, box_patterns, box_syntax)]
|
2017-10-30 20:06:20 -07:00
|
|
|
#![feature(plugin)]
|
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
extern crate getopts;
|
2018-07-02 00:20:52 -07:00
|
|
|
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 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;
|
2017-11-02 02:45:26 -07:00
|
|
|
mod language;
|
2017-10-30 20:06:20 -07:00
|
|
|
mod webapp;
|
2017-10-30 22:18:02 -07:00
|
|
|
pub mod llvm_wrap;
|
2017-10-30 20:06:20 -07:00
|
|
|
|
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"));
|
|
|
|
|
2018-07-03 03:02:16 -07:00
|
|
|
pub use language::{LLVMCodeString, ProgrammingLanguageInterface, EvalOptions,
|
2018-07-15 15:01:50 -07:00
|
|
|
ExecutionMethod, TraceArtifact, FinishedComputation, UnfinishedComputation, PassDebugOptionsDescriptor, PassDescriptor};
|
2018-07-03 03:02:16 -07:00
|
|
|
|
2017-10-30 22:18:02 -07:00
|
|
|
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();
|
2018-05-07 02:38:44 -07:00
|
|
|
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")
|
2017-12-09 18:19:07 -08:00
|
|
|
.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, _..] => {
|
|
|
|
|
2018-05-07 02:38:44 -07:00
|
|
|
run_noninteractive(filename, languages, options, debug_passes);
|
2017-10-30 20:06:20 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-07 02:38:44 -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();
|
|
|
|
|
2018-05-07 02:38:44 -07:00
|
|
|
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-05-07 02:38:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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));
|
2018-03-11 12:56:51 -07:00
|
|
|
}
|
2017-10-30 20:06:20 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 21:13:34 -07:00
|
|
|
/*
|
2017-10-30 20:06:20 -07:00
|
|
|
pub fn compilation_sequence(llvm_code: LLVMCodeString, sourcefile: &str) {
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
let ll_filename = "out.ll";
|
|
|
|
let obj_filename = "out.o";
|
|
|
|
let q: Vec<&str> = sourcefile.split('.').collect();
|
|
|
|
let bin_filename = match &q[..] {
|
|
|
|
&[name, "maaru"] => name,
|
|
|
|
_ => panic!("Bad filename {}", sourcefile),
|
|
|
|
};
|
|
|
|
|
|
|
|
let LLVMCodeString(llvm_str) = llvm_code;
|
|
|
|
|
|
|
|
println!("Compilation process finished for {}", ll_filename);
|
|
|
|
File::create(ll_filename)
|
|
|
|
.and_then(|mut f| f.write_all(llvm_str.as_bytes()))
|
|
|
|
.expect("Error writing file");
|
|
|
|
|
|
|
|
let llc_output = Command::new("llc")
|
|
|
|
.args(&["-filetype=obj", ll_filename, "-o", obj_filename])
|
|
|
|
.output()
|
|
|
|
.expect("Failed to run llc");
|
|
|
|
|
|
|
|
|
|
|
|
if !llc_output.status.success() {
|
|
|
|
println!("{}", String::from_utf8_lossy(&llc_output.stderr));
|
|
|
|
}
|
|
|
|
|
|
|
|
let gcc_output = Command::new("gcc")
|
|
|
|
.args(&["-o", bin_filename, &obj_filename])
|
|
|
|
.output()
|
|
|
|
.expect("failed to run gcc");
|
|
|
|
|
|
|
|
if !gcc_output.status.success() {
|
|
|
|
println!("{}", String::from_utf8_lossy(&gcc_output.stdout));
|
|
|
|
println!("{}", String::from_utf8_lossy(&gcc_output.stderr));
|
|
|
|
}
|
|
|
|
|
|
|
|
for filename in [obj_filename].iter() {
|
|
|
|
Command::new("rm")
|
|
|
|
.arg(filename)
|
|
|
|
.output()
|
|
|
|
.expect(&format!("failed to run rm {}", filename));
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 21:13:34 -07:00
|
|
|
*/
|
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
|
|
|
|
}
|