2016-01-17 00:16:01 -08:00
|
|
|
#![feature(advanced_slice_patterns, slice_patterns, box_patterns)]
|
2016-12-28 15:56:02 -08:00
|
|
|
extern crate getopts;
|
2017-01-12 20:56:23 -08:00
|
|
|
extern crate linefeed;
|
2015-12-18 23:40:30 -08:00
|
|
|
|
2015-08-14 17:07:02 -07:00
|
|
|
use std::path::Path;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2017-01-12 20:56:23 -08:00
|
|
|
use std::process;
|
2017-01-23 19:11:50 -08:00
|
|
|
use std::io::Write;
|
2017-02-01 02:15:18 -08:00
|
|
|
use std::default::Default;
|
2015-07-22 03:02:55 -07:00
|
|
|
|
2017-08-29 04:15:31 -07:00
|
|
|
mod schala_lang;
|
2017-01-25 20:09:51 -08:00
|
|
|
mod maaru_lang;
|
2017-08-29 00:28:19 -07:00
|
|
|
mod robo_lang;
|
2017-01-25 20:09:51 -08:00
|
|
|
|
2017-01-21 01:49:45 -08:00
|
|
|
mod language;
|
2017-08-31 16:26:55 -07:00
|
|
|
use language::{ProgrammingLanguageInterface, EvalOptions, LLVMCodeString};
|
2017-01-21 01:49:45 -08:00
|
|
|
|
2016-12-26 23:29:24 -08:00
|
|
|
mod llvm_wrap;
|
2016-02-11 10:49:45 -08:00
|
|
|
|
2017-07-23 02:12:05 -07:00
|
|
|
mod virtual_machine;
|
|
|
|
use virtual_machine::{run_vm, run_assembler};
|
|
|
|
|
2015-07-16 01:40:37 -07:00
|
|
|
fn main() {
|
2017-09-01 02:08:26 -07:00
|
|
|
let languages: Vec<Box<ProgrammingLanguageInterface>> =
|
|
|
|
vec![
|
|
|
|
Box::new(schala_lang::Schala::new()),
|
|
|
|
Box::new(maaru_lang::Maaru::new()),
|
|
|
|
Box::new(robo_lang::Robo::new()),
|
|
|
|
];
|
|
|
|
|
|
|
|
let option_matches =
|
|
|
|
match program_options().parse(std::env::args()) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
2017-02-02 01:04:15 -08:00
|
|
|
std::process::exit(1);
|
2017-09-01 02:08:26 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if option_matches.opt_present("list-languages") {
|
|
|
|
for lang in languages {
|
|
|
|
println!("{}", lang.get_language_name());
|
2017-02-02 01:04:15 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2017-02-02 11:33:07 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
if option_matches.opt_present("h") {
|
|
|
|
println!("{}", program_options().usage("Schala metainterpreter"));
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2017-07-23 02:12:05 -07:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
if option_matches.opt_present("m") {
|
|
|
|
let file_name = option_matches.free.get(1);
|
|
|
|
run_vm(file_name);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2017-02-03 01:08:51 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
if option_matches.opt_present("a") {
|
|
|
|
let file_name = option_matches.free.get(1);
|
|
|
|
run_assembler(file_name);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
2017-02-03 01:08:51 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let language_names: Vec<String> = languages.iter().map(|lang| {lang.get_language_name()}).collect();
|
|
|
|
let initial_index: usize =
|
|
|
|
option_matches.opt_str("l")
|
|
|
|
.and_then(|lang| { language_names.iter().position(|x| { *x == lang }) })
|
|
|
|
.unwrap_or(0);
|
2017-08-31 16:26:55 -07:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let mut options = EvalOptions::default();
|
|
|
|
options.trace_evaluation = option_matches.opt_present("t");
|
2017-02-02 11:33:07 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let compile = !option_matches.opt_present("i");
|
2016-12-28 15:56:02 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
match option_matches.free[..] {
|
|
|
|
[] | [_] => {
|
|
|
|
let mut repl = Repl::new(languages, initial_index);
|
|
|
|
repl.options.show_llvm_ir = option_matches.opt_present("v");
|
|
|
|
repl.run();
|
|
|
|
}
|
|
|
|
[_, ref filename, _..] => {
|
|
|
|
let mut language = maaru_lang::Maaru::new();
|
|
|
|
run_noninteractive(filename, &mut language, options, compile);
|
|
|
|
}
|
|
|
|
};
|
2016-03-03 22:18:16 -08:00
|
|
|
}
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language: &mut T, options: EvalOptions, compile: bool) {
|
2017-08-30 23:36:13 -07:00
|
|
|
let mut source_file = File::open(&Path::new(filename)).unwrap();
|
|
|
|
let mut buffer = String::new();
|
|
|
|
source_file.read_to_string(&mut buffer).unwrap();
|
|
|
|
|
2017-08-31 19:15:32 -07:00
|
|
|
if compile {
|
|
|
|
if !language.can_compile() {
|
|
|
|
panic!("Trying to compile a non-compileable language");
|
|
|
|
} else {
|
|
|
|
let llvm_bytecode = language.compile(&buffer);
|
|
|
|
compilation_sequence(llvm_bytecode, filename);
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-01 02:08:26 -07:00
|
|
|
let interpretor_output = language.evaluate_in_repl(&buffer, &options);
|
2017-08-31 20:59:43 -07:00
|
|
|
interpretor_output.print_to_screen();
|
2017-08-30 23:36:13 -07:00
|
|
|
}
|
2015-07-16 02:55:03 -07:00
|
|
|
}
|
|
|
|
|
2017-01-12 22:43:26 -08:00
|
|
|
type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>;
|
2017-02-02 00:23:48 -08:00
|
|
|
struct Repl {
|
2017-09-01 02:08:26 -07:00
|
|
|
options: EvalOptions,
|
|
|
|
languages: Vec<Box<ProgrammingLanguageInterface>>,
|
|
|
|
current_language_index: usize,
|
|
|
|
interpreter_directive_sigil: char,
|
|
|
|
reader: LineReader,
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
|
|
|
|
2017-02-02 00:23:48 -08:00
|
|
|
impl Repl {
|
2017-09-01 02:08:26 -07:00
|
|
|
fn new(languages: Vec<Box<ProgrammingLanguageInterface>>, initial_index: usize) -> Repl {
|
|
|
|
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
|
|
|
|
reader.set_prompt(">> ");
|
|
|
|
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
|
|
|
Repl {
|
|
|
|
options: EvalOptions::default(),
|
|
|
|
languages: languages,
|
|
|
|
current_language_index: i,
|
|
|
|
interpreter_directive_sigil: '.',
|
|
|
|
reader: reader,
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
}
|
|
|
|
fn run(&mut self) {
|
|
|
|
use linefeed::ReadResult::*;
|
|
|
|
println!("MetaInterpreter v 0.05");
|
|
|
|
println!("Using language: {}", self.languages[self.current_language_index].get_language_name());
|
|
|
|
loop {
|
|
|
|
match self.reader.read_line() {
|
|
|
|
Err(e) => {
|
|
|
|
println!("Terminal read error: {}", e);
|
|
|
|
},
|
|
|
|
Ok(Eof) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Input(ref input)) => {
|
|
|
|
self.reader.add_history(input.clone());
|
|
|
|
if self.handle_interpreter_directive(input) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let output = self.input_handler(input);
|
|
|
|
println!("=> {}", output);
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
_ => (),
|
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
println!("Exiting...");
|
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
fn input_handler(&mut self, input: &str) -> String {
|
|
|
|
let ref mut language = self.languages[self.current_language_index];
|
|
|
|
let interpretor_output = language.evaluate_in_repl(input, &self.options);
|
|
|
|
interpretor_output.to_string()
|
|
|
|
}
|
2017-08-30 22:58:57 -07:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
|
|
|
match input.chars().nth(0) {
|
|
|
|
Some(ch) if ch == self.interpreter_directive_sigil => (),
|
|
|
|
_ => return false
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let mut iter = input.chars();
|
|
|
|
iter.next();
|
|
|
|
let trimmed_sigil: &str = iter.as_str();
|
2017-01-12 20:56:23 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let commands: Vec<&str> = trimmed_sigil
|
|
|
|
.split_whitespace()
|
|
|
|
.collect();
|
2017-01-13 17:53:23 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
let cmd: &str = match commands.get(0).clone() {
|
|
|
|
None => return true,
|
|
|
|
Some(s) => s
|
|
|
|
};
|
2017-01-12 20:56:23 -08:00
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
match cmd {
|
|
|
|
"exit" | "quit" => process::exit(0),
|
|
|
|
"history" => {
|
|
|
|
for item in self.reader.history() {
|
|
|
|
println!("{}", item);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"lang" => {
|
|
|
|
match commands.get(1) {
|
|
|
|
Some(&"show") => {
|
|
|
|
for (i, lang) in self.languages.iter().enumerate() {
|
|
|
|
if i == self.current_language_index {
|
|
|
|
println!("* {}", lang.get_language_name());
|
|
|
|
} else {
|
|
|
|
println!("{}", lang.get_language_name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(&"next") => {
|
|
|
|
self.current_language_index = (self.current_language_index + 1) % self.languages.len();
|
|
|
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
|
|
|
}
|
|
|
|
Some(&"prev") | Some(&"previous") => {
|
|
|
|
self.current_language_index = if self.current_language_index == 0 { self.languages.len() - 1 } else { self.current_language_index - 1 };
|
|
|
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
|
|
|
},
|
|
|
|
Some(e) => println!("Bad `lang` argument: {}", e),
|
|
|
|
None => println!("`lang` - valid arguments `show`, `next`, `prev`|`previous`"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"set" => {
|
|
|
|
let show = match commands.get(1) {
|
|
|
|
Some(&"show") => true,
|
|
|
|
Some(&"hide") => false,
|
|
|
|
Some(e) => {
|
|
|
|
println!("Bad `set` argument: {}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
println!("`set` - valid arguments `show {{option}}`, `hide {{option}}`");
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-13 17:53:23 -08:00
|
|
|
};
|
2017-09-01 02:08:26 -07:00
|
|
|
match commands.get(2) {
|
|
|
|
Some(&"tokens") => self.options.debug_tokens = show,
|
|
|
|
Some(&"parse") => self.options.debug_parse = show,
|
|
|
|
Some(&"eval") => {
|
|
|
|
//let ref mut language = self.languages[self.current_language_index];
|
|
|
|
//language.set_option("trace_evaluation", show);
|
|
|
|
},
|
|
|
|
Some(&"llvm") => self.options.show_llvm_ir = show,
|
|
|
|
Some(e) => {
|
|
|
|
println!("Bad `show`/`hide` argument: {}", e);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
println!("`show`/`hide` requires an argument");
|
|
|
|
return true;
|
|
|
|
}
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
},
|
|
|
|
e => println!("Unknown command: {}", e)
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
2017-09-01 02:08:26 -07:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
2017-01-21 01:49:45 -08:00
|
|
|
|
2017-01-23 19:11:50 -08: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[..] {
|
2017-08-29 00:28:19 -07:00
|
|
|
&[name, "maaru"] => name,
|
2017-01-23 19:11:50 -08:00
|
|
|
_ => 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-01 02:08:26 -07:00
|
|
|
fn program_options() -> getopts::Options {
|
|
|
|
let mut options = getopts::Options::new();
|
|
|
|
options.optflag("i",
|
|
|
|
"interpret",
|
|
|
|
"Interpret source file instead of compiling");
|
|
|
|
options.optflag("t",
|
|
|
|
"trace-evaluation",
|
|
|
|
"Print out trace of evaluation");
|
|
|
|
options.optflag("v",
|
|
|
|
"llvm-in-repl",
|
|
|
|
"Show LLVM IR in REPL");
|
|
|
|
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("m",
|
|
|
|
"virtual-machine",
|
|
|
|
"Start up a virtual machine instead of an interpreter");
|
|
|
|
options.optflag("a",
|
|
|
|
"assembler",
|
|
|
|
"Assemble file into bytecode");
|
|
|
|
options
|
|
|
|
}
|
|
|
|
|