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-01-23 03:03:12 -08:00
|
|
|
mod schala_lang;
|
2017-01-24 20:09:14 -08:00
|
|
|
use schala_lang::SchalaEvaluator;
|
2017-01-23 03:03:12 -08:00
|
|
|
use schala_lang::Schala;
|
2016-01-18 02:24:14 -08:00
|
|
|
|
2017-01-25 20:09:51 -08:00
|
|
|
mod maaru_lang;
|
|
|
|
|
2017-01-21 01:49:45 -08:00
|
|
|
mod language;
|
2017-02-01 02:15:18 -08:00
|
|
|
use language::{ProgrammingLanguage, LanguageInterface, LLVMCodeString, EvaluationMachine};
|
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-02-02 01:21:40 -08:00
|
|
|
let languages: Vec<Box<LanguageInterface>> =
|
|
|
|
vec![
|
|
|
|
Box::new((Schala::new(), SchalaEvaluator::new(None))),
|
|
|
|
Box::new((maaru_lang::Maaru::new(), maaru_lang::MaaruEvaluator::new())),
|
|
|
|
];
|
2017-02-02 01:04:15 -08:00
|
|
|
|
2016-12-29 02:04:03 -08:00
|
|
|
let option_matches =
|
2017-01-09 22:56:29 -08:00
|
|
|
match program_options().parse(std::env::args()) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
2017-02-02 01:04:15 -08:00
|
|
|
if option_matches.opt_present("list-languages") {
|
|
|
|
for lang in languages {
|
|
|
|
println!("{}", lang.get_language_name());
|
|
|
|
}
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
2017-02-02 11:33:07 -08:00
|
|
|
|
2017-02-03 01:08:51 -08:00
|
|
|
if option_matches.opt_present("h") {
|
|
|
|
println!("{}", program_options().usage("Schala metainterpreter"));
|
2017-07-23 02:12:05 -07:00
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if option_matches.opt_present("m") {
|
|
|
|
let file_name = option_matches.free.get(1);
|
|
|
|
run_vm(file_name);
|
|
|
|
std::process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-02-02 11:35:15 -08:00
|
|
|
let show_llvm_ir = option_matches.opt_present("v");
|
2017-02-02 11:33:07 -08:00
|
|
|
let compile = !option_matches.opt_present("i");
|
|
|
|
let trace_evaluation = option_matches.opt_present("t");
|
|
|
|
|
2016-12-28 15:56:02 -08:00
|
|
|
match option_matches.free[..] {
|
|
|
|
[] | [_] => {
|
2017-02-03 01:08:51 -08:00
|
|
|
let mut repl = Repl::new(languages, initial_index);
|
2017-02-02 11:33:07 -08:00
|
|
|
repl.show_llvm_ir = show_llvm_ir;
|
2017-01-12 20:56:23 -08:00
|
|
|
repl.run();
|
2016-12-29 02:04:03 -08:00
|
|
|
}
|
|
|
|
[_, ref filename, _..] => {
|
2017-01-31 00:53:31 -08:00
|
|
|
let language = Schala::new();
|
2017-02-02 11:33:07 -08:00
|
|
|
run_noninteractive(filename, &language, trace_evaluation, compile);
|
2016-12-28 15:56:02 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn program_options() -> getopts::Options {
|
|
|
|
let mut options = getopts::Options::new();
|
2016-12-29 02:04:03 -08:00
|
|
|
options.optflag("i",
|
|
|
|
"interpret",
|
|
|
|
"Interpret source file instead of compiling");
|
2017-01-09 04:26:42 -08:00
|
|
|
options.optflag("t",
|
|
|
|
"trace-evaluation",
|
|
|
|
"Print out trace of evaluation");
|
2017-02-02 11:35:15 -08:00
|
|
|
options.optflag("v",
|
2017-01-16 02:47:05 -08:00
|
|
|
"llvm-in-repl",
|
|
|
|
"Show LLVM IR in REPL");
|
2017-02-02 01:04:15 -08:00
|
|
|
options.optflag("",
|
|
|
|
"list-languages",
|
|
|
|
"Show a list of all supported languages");
|
2017-02-03 01:08:51 -08:00
|
|
|
options.optopt("l",
|
|
|
|
"lang",
|
|
|
|
"Start up REPL in a language",
|
|
|
|
"LANGUAGE");
|
|
|
|
options.optflag("h",
|
|
|
|
"help",
|
|
|
|
"Show help text");
|
2017-07-23 02:12:05 -07:00
|
|
|
options.optflag("m",
|
|
|
|
"virtual-machine",
|
|
|
|
"Start up a virtual machine instead of an interpreter");
|
|
|
|
options.optflag("a",
|
|
|
|
"assembler",
|
|
|
|
"Assemble file into bytecode");
|
2016-12-28 15:56:02 -08:00
|
|
|
options
|
2016-03-03 22:18:16 -08:00
|
|
|
}
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2017-07-23 02:12:05 -07:00
|
|
|
|
2017-02-02 11:33:07 -08:00
|
|
|
fn run_noninteractive<'a, T: ProgrammingLanguage>(filename: &str, _language: &T, trace_evaluation: bool, compile: bool) {
|
2016-03-03 22:18:16 -08:00
|
|
|
let mut source_file = File::open(&Path::new(filename)).unwrap();
|
|
|
|
let mut buffer = String::new();
|
|
|
|
source_file.read_to_string(&mut buffer).unwrap();
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2017-01-23 01:32:57 -08:00
|
|
|
let tokens = match T::tokenize(&buffer) {
|
2016-12-28 22:52:23 -08:00
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => {
|
2016-12-28 23:55:13 -08:00
|
|
|
println!("Tokenization error: {}", e.msg);
|
|
|
|
std::process::exit(1)
|
2016-12-28 22:52:23 -08:00
|
|
|
}
|
2016-03-03 22:18:16 -08:00
|
|
|
};
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2017-01-23 01:32:57 -08:00
|
|
|
let ast = match T::parse(tokens) {
|
2016-03-03 22:18:16 -08:00
|
|
|
Ok(ast) => ast,
|
2016-12-28 23:55:13 -08:00
|
|
|
Err(err) => {
|
2016-12-31 17:02:49 -08:00
|
|
|
println!("Parse error: {:?}", err.msg);
|
2017-01-23 01:32:57 -08:00
|
|
|
/*println!("Remaining tokens: {:?}", err.remaining_tokens);*/
|
2016-12-28 23:55:13 -08:00
|
|
|
std::process::exit(1)
|
|
|
|
}
|
2016-03-03 22:18:16 -08:00
|
|
|
};
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2016-03-04 14:32:22 -08:00
|
|
|
if compile {
|
2017-01-23 01:32:57 -08:00
|
|
|
compilation_sequence(T::compile(ast), filename);
|
2016-03-04 14:32:22 -08:00
|
|
|
} else {
|
2017-01-31 00:53:31 -08:00
|
|
|
let mut evaluator = <T as ProgrammingLanguage>::Evaluator::new();
|
2017-01-24 20:09:14 -08:00
|
|
|
if trace_evaluation {
|
|
|
|
evaluator.set_option("trace_evaluation", true);
|
|
|
|
}
|
2017-01-23 01:32:57 -08:00
|
|
|
let results = T::evaluate(ast, &mut evaluator);
|
2017-02-20 20:14:59 -08:00
|
|
|
if let Some(r) = results.iter().last() {
|
|
|
|
println!("{}", r);
|
2016-03-04 14:32:22 -08:00
|
|
|
}
|
2015-08-14 17:07:02 -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-02-02 11:33:07 -08:00
|
|
|
pub show_tokens: bool,
|
|
|
|
pub show_parse: bool,
|
|
|
|
pub show_llvm_ir: bool,
|
2017-02-01 02:15:18 -08:00
|
|
|
languages: Vec<Box<LanguageInterface>>,
|
2017-02-02 02:00:26 -08:00
|
|
|
current_language_index: usize,
|
2017-01-12 20:56:23 -08:00
|
|
|
interpreter_directive_sigil: char,
|
2017-01-12 22:43:26 -08:00
|
|
|
reader: LineReader,
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
|
|
|
|
2017-02-02 00:23:48 -08:00
|
|
|
impl Repl {
|
2017-02-03 01:08:51 -08:00
|
|
|
fn new(languages: Vec<Box<LanguageInterface>>, initial_index: usize) -> Repl {
|
|
|
|
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Metainterpreter").unwrap();
|
2017-01-12 22:43:26 -08:00
|
|
|
reader.set_prompt(">> ");
|
2017-02-03 01:08:51 -08:00
|
|
|
let i = if initial_index < languages.len() { initial_index } else { 0 };
|
2017-01-12 20:56:23 -08:00
|
|
|
Repl {
|
|
|
|
show_tokens: false,
|
|
|
|
show_parse: false,
|
2017-02-02 01:04:15 -08:00
|
|
|
show_llvm_ir: false,
|
|
|
|
languages: languages,
|
2017-02-03 01:08:51 -08:00
|
|
|
current_language_index: i,
|
2017-01-12 20:56:23 -08:00
|
|
|
interpreter_directive_sigil: '.',
|
2017-01-12 22:43:26 -08:00
|
|
|
reader: reader,
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn run(&mut self) {
|
|
|
|
use linefeed::ReadResult::*;
|
2017-02-02 11:19:29 -08:00
|
|
|
println!("MetaInterpreter v 0.05");
|
2017-02-03 01:08:51 -08:00
|
|
|
println!("Using language: {}", self.languages[self.current_language_index].get_language_name());
|
2017-01-12 20:56:23 -08:00
|
|
|
loop {
|
2017-01-12 22:43:26 -08:00
|
|
|
match self.reader.read_line() {
|
2017-01-12 20:56:23 -08:00
|
|
|
Err(e) => {
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("Terminal read error: {}", e);
|
2017-01-12 20:56:23 -08:00
|
|
|
},
|
|
|
|
Ok(Eof) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Input(ref input)) => {
|
2017-01-13 12:10:02 -08:00
|
|
|
self.reader.add_history(input.clone());
|
2017-01-12 21:51:00 -08:00
|
|
|
if self.handle_interpreter_directive(input) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
let output = self.input_handler(input);
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("=> {}", output);
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
2017-01-13 12:10:02 -08:00
|
|
|
_ => (),
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("Exiting...");
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn input_handler(&mut self, input: &str) -> String {
|
2017-02-02 02:00:26 -08:00
|
|
|
let ref mut language = self.languages[self.current_language_index];
|
2017-02-03 10:30:00 -08:00
|
|
|
|
|
|
|
let mut options = language::LanguageInterfaceOptions::default();
|
|
|
|
options.show_tokens = self.show_tokens;
|
|
|
|
options.show_parse = self.show_parse;
|
|
|
|
options.show_llvm_ir = self.show_llvm_ir;
|
|
|
|
|
|
|
|
language.evaluate_in_repl(input, options)
|
2017-01-12 20:56:23 -08: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-13 17:53:23 -08:00
|
|
|
let mut iter = input.chars();
|
|
|
|
iter.next();
|
|
|
|
let trimmed_sigil: &str = iter.as_str();
|
|
|
|
|
|
|
|
let commands: Vec<&str> = trimmed_sigil
|
2017-01-12 20:56:23 -08:00
|
|
|
.split_whitespace()
|
|
|
|
.collect();
|
|
|
|
|
2017-01-13 17:53:23 -08:00
|
|
|
let cmd: &str = match commands.get(0).clone() {
|
|
|
|
None => return true,
|
|
|
|
Some(s) => s
|
|
|
|
};
|
2017-01-12 20:56:23 -08:00
|
|
|
|
|
|
|
match cmd {
|
|
|
|
"exit" | "quit" => process::exit(0),
|
|
|
|
"history" => {
|
2017-01-13 17:53:23 -08:00
|
|
|
for item in self.reader.history() {
|
|
|
|
println!("{}", item);
|
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
},
|
2017-02-02 02:00:26 -08:00
|
|
|
"lang" => {
|
2017-02-02 02:12:28 -08:00
|
|
|
match commands.get(1) {
|
|
|
|
Some(&"show") => {
|
2017-02-02 02:00:26 -08:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-02-02 02:12:28 -08:00
|
|
|
Some(&"next") => {
|
2017-02-02 02:00:26 -08:00
|
|
|
self.current_language_index = (self.current_language_index + 1) % self.languages.len();
|
2017-02-02 11:19:29 -08:00
|
|
|
println!("Switching to {}", self.languages[self.current_language_index].get_language_name());
|
2017-02-02 02:00:26 -08:00
|
|
|
}
|
2017-02-02 02:12:28 -08:00
|
|
|
Some(&"prev") | Some(&"previous") => {
|
2017-02-02 11:19:29 -08:00
|
|
|
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());
|
2017-02-02 02:00:26 -08:00
|
|
|
},
|
2017-02-02 02:12:28 -08:00
|
|
|
Some(e) => println!("Bad `lang` argument: {}", e),
|
|
|
|
None => println!("`lang` - valid arguments `show`, `next`, `prev`|`previous`"),
|
2017-02-02 02:00:26 -08:00
|
|
|
}
|
|
|
|
},
|
2017-01-13 17:53:23 -08:00
|
|
|
"set" => {
|
2017-02-02 02:12:28 -08:00
|
|
|
let show = match commands.get(1) {
|
|
|
|
Some(&"show") => true,
|
|
|
|
Some(&"hide") => false,
|
|
|
|
Some(e) => {
|
2017-01-13 17:53:23 -08:00
|
|
|
println!("Bad `set` argument: {}", e);
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-02 02:12:28 -08:00
|
|
|
None => {
|
|
|
|
println!("`set` - valid arguments `show {{option}}`, `hide {{option}}`");
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-13 17:53:23 -08:00
|
|
|
};
|
2017-02-02 02:12:28 -08:00
|
|
|
match commands.get(2) {
|
|
|
|
Some(&"tokens") => self.show_tokens = show,
|
|
|
|
Some(&"parse") => self.show_parse = show,
|
2017-02-05 21:23:11 -08:00
|
|
|
Some(&"eval") => {
|
|
|
|
let ref mut language = self.languages[self.current_language_index];
|
|
|
|
language.set_option("trace_evaluation", show);
|
|
|
|
},
|
2017-02-02 02:12:28 -08:00
|
|
|
Some(&"llvm") => self.show_llvm_ir = show,
|
|
|
|
Some(e) => {
|
2017-01-13 17:53:23 -08:00
|
|
|
println!("Bad `show`/`hide` argument: {}", e);
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-02 02:12:28 -08:00
|
|
|
None => {
|
|
|
|
println!("`show`/`hide` requires an argument");
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-13 17:53:23 -08:00
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
},
|
2017-01-13 18:59:34 -08:00
|
|
|
e => println!("Unknown command: {}", e)
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
2017-01-13 17:53:23 -08: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[..] {
|
|
|
|
&[name, "schala"] => 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|