2018-03-11 14:53:08 -07:00
|
|
|
use std::collections::HashMap;
|
2018-03-24 15:14:24 -07:00
|
|
|
use colored::*;
|
2018-03-24 13:20:10 -07:00
|
|
|
use std::fmt::Write;
|
2017-09-08 03:47:04 -07:00
|
|
|
|
2017-01-23 19:11:50 -08:00
|
|
|
pub struct LLVMCodeString(pub String);
|
|
|
|
|
2017-09-17 18:57:47 -07:00
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
2017-08-30 19:09:22 -07:00
|
|
|
pub struct EvalOptions {
|
2018-03-20 20:29:07 -07:00
|
|
|
pub debug: DebugOptions,
|
|
|
|
pub execution_method: ExecutionMethod
|
|
|
|
}
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub enum ExecutionMethod {
|
|
|
|
Compile,
|
|
|
|
Interpret,
|
|
|
|
}
|
|
|
|
impl Default for ExecutionMethod {
|
|
|
|
fn default() -> ExecutionMethod {
|
|
|
|
ExecutionMethod::Interpret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct DebugOptions {
|
|
|
|
pub tokens: bool,
|
|
|
|
pub parse_tree: bool,
|
|
|
|
pub ast: bool,
|
|
|
|
pub type_checking: bool,
|
|
|
|
pub symbol_table: bool,
|
|
|
|
pub evaluation: bool,
|
|
|
|
pub llvm_ir: bool,
|
2017-08-30 19:09:22 -07:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:59:43 -07:00
|
|
|
#[derive(Debug, Default)]
|
2018-03-07 22:07:13 -08:00
|
|
|
pub struct LanguageOutput {
|
2017-08-31 20:59:43 -07:00
|
|
|
output: String,
|
2018-03-09 00:50:06 -08:00
|
|
|
artifacts: Vec<TraceArtifact>,
|
2018-03-11 12:56:51 -07:00
|
|
|
pub failed: bool,
|
2017-08-31 20:59:43 -07:00
|
|
|
}
|
|
|
|
|
2018-03-07 22:07:13 -08:00
|
|
|
impl LanguageOutput {
|
2017-08-31 20:59:43 -07:00
|
|
|
pub fn add_artifact(&mut self, artifact: TraceArtifact) {
|
|
|
|
self.artifacts.push(artifact);
|
|
|
|
}
|
|
|
|
pub fn add_output(&mut self, output: String) {
|
|
|
|
self.output = output;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_string(&self) -> String {
|
|
|
|
let mut acc = String::new();
|
|
|
|
for line in self.artifacts.iter() {
|
2017-09-08 03:47:04 -07:00
|
|
|
acc.push_str(&line.debug_output.color(line.text_color).to_string());
|
|
|
|
acc.push_str(&"\n");
|
2017-08-31 20:59:43 -07:00
|
|
|
}
|
|
|
|
acc.push_str(&self.output);
|
|
|
|
acc
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn print_to_screen(&self) {
|
|
|
|
for line in self.artifacts.iter() {
|
2017-10-12 10:43:54 -07:00
|
|
|
let color = line.text_color;
|
|
|
|
let stage = line.stage_name.color(color).to_string();
|
|
|
|
let output = line.debug_output.color(color).to_string();
|
|
|
|
println!("{}: {}", stage, output);
|
2017-08-31 20:59:43 -07:00
|
|
|
}
|
|
|
|
println!("{}", self.output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-11 14:53:08 -07:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct UnfinishedComputation {
|
|
|
|
artifacts: HashMap<String, TraceArtifact>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FinishedComputation {
|
|
|
|
artifacts: HashMap<String, TraceArtifact>,
|
|
|
|
text_output: Result<String, String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UnfinishedComputation {
|
|
|
|
pub fn add_artifact(&mut self, artifact: TraceArtifact) {
|
|
|
|
self.artifacts.insert(artifact.stage_name.clone(), artifact);
|
|
|
|
}
|
2018-04-29 04:00:41 -07:00
|
|
|
pub fn finish(self, text_output: Result<String, String>) -> FinishedComputation {
|
|
|
|
FinishedComputation {
|
|
|
|
artifacts: self.artifacts,
|
|
|
|
text_output
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 14:53:08 -07:00
|
|
|
pub fn output(self, output: Result<String, String>) -> FinishedComputation {
|
|
|
|
FinishedComputation {
|
|
|
|
artifacts: self.artifacts,
|
|
|
|
text_output: output
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:57:54 -07:00
|
|
|
impl FinishedComputation {
|
2018-03-20 20:29:07 -07:00
|
|
|
pub fn to_repl(&self) -> String {
|
2018-04-24 23:11:04 -07:00
|
|
|
let mut buf = String::new();
|
|
|
|
for stage in ["tokens", "parse_trace", "ast", "symbol_table", "type_check"].iter() {
|
|
|
|
if let Some(artifact) = self.artifacts.get(&stage.to_string()) {
|
|
|
|
let color = artifact.text_color;
|
|
|
|
let stage = stage.color(color).bold();
|
|
|
|
let output = artifact.debug_output.color(color);
|
|
|
|
write!(&mut buf, "{}: {}\n", stage, output).unwrap();
|
2018-03-24 13:20:10 -07:00
|
|
|
}
|
2018-03-19 22:57:54 -07:00
|
|
|
}
|
2018-04-24 23:11:04 -07:00
|
|
|
match self.text_output {
|
|
|
|
Ok(ref output) => write!(&mut buf, "{}", output).unwrap(),
|
|
|
|
Err(ref err) => write!(&mut buf, "{} {}", "Error: ".red().bold(), err).unwrap(),
|
|
|
|
}
|
|
|
|
buf
|
2018-03-19 22:57:54 -07:00
|
|
|
}
|
2018-03-20 20:29:07 -07:00
|
|
|
pub fn to_noninteractive(&self) -> Option<String> {
|
|
|
|
match self.text_output {
|
2018-03-27 00:50:31 -07:00
|
|
|
Ok(_) => {
|
|
|
|
let mut buf = String::new();
|
|
|
|
for stage in ["tokens", "parse_trace", "ast", "symbol_table", "type_check"].iter() {
|
|
|
|
if let Some(artifact) = self.artifacts.get(&stage.to_string()) {
|
|
|
|
let color = artifact.text_color;
|
|
|
|
let stage = stage.color(color).bold();
|
|
|
|
let output = artifact.debug_output.color(color);
|
|
|
|
write!(&mut buf, "{}: {}\n", stage, output).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buf == "" { None } else { Some(buf) }
|
|
|
|
},
|
2018-03-22 03:37:48 -07:00
|
|
|
Err(ref s) => Some(format!("{} {}", "Error: ".red().bold(), s))
|
2018-03-20 20:29:07 -07:00
|
|
|
}
|
|
|
|
}
|
2018-03-19 22:57:54 -07:00
|
|
|
}
|
|
|
|
|
2017-08-31 20:59:43 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TraceArtifact {
|
|
|
|
stage_name: String,
|
|
|
|
debug_output: String,
|
2017-09-08 03:47:04 -07:00
|
|
|
text_color: &'static str,
|
2017-08-31 20:59:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TraceArtifact {
|
|
|
|
pub fn new(stage: &str, debug: String) -> TraceArtifact {
|
2017-09-16 14:29:22 -07:00
|
|
|
let color = match stage {
|
2017-10-08 22:17:29 -07:00
|
|
|
"parse_trace" | "ast" => "red",
|
2017-09-16 14:29:22 -07:00
|
|
|
"tokens" => "green",
|
2017-10-01 00:48:08 -07:00
|
|
|
"type_check" => "magenta",
|
2017-09-16 14:29:22 -07:00
|
|
|
_ => "blue",
|
|
|
|
};
|
|
|
|
TraceArtifact { stage_name: stage.to_string(), debug_output: debug, text_color: color}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_parse_trace(trace: Vec<String>) -> TraceArtifact {
|
2017-09-16 15:05:11 -07:00
|
|
|
let mut output = String::new();
|
|
|
|
|
|
|
|
for t in trace {
|
|
|
|
output.push_str(&t);
|
|
|
|
output.push_str("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
TraceArtifact { stage_name: "parse_trace".to_string(), debug_output: output, text_color: "red"}
|
2017-08-31 20:59:43 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:09:22 -07:00
|
|
|
pub trait ProgrammingLanguageInterface {
|
2018-03-20 20:29:07 -07:00
|
|
|
/* old */
|
2018-03-24 19:02:16 -07:00
|
|
|
fn evaluate_in_repl(&mut self, _: &str, _: &EvalOptions) -> LanguageOutput {
|
2018-03-19 22:57:54 -07:00
|
|
|
LanguageOutput { output: format!("Defunct"), artifacts: vec![], failed: false }
|
|
|
|
}
|
2018-03-20 20:29:07 -07:00
|
|
|
/* old */
|
|
|
|
|
2018-04-29 00:04:31 -07:00
|
|
|
fn execute_pipeline(&mut self, _input: &str, _eval_options: &EvalOptions) -> FinishedComputation {
|
|
|
|
FinishedComputation { artifacts: HashMap::new(), text_output: Err(format!("Execution pipeline not done")) }
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:09:22 -07:00
|
|
|
fn get_language_name(&self) -> String;
|
2017-10-02 23:07:05 -07:00
|
|
|
fn get_source_file_suffix(&self) -> String;
|
2018-04-29 03:22:36 -07:00
|
|
|
fn handle_custom_interpreter_directives(&mut self, _commands: &Vec<&str>) -> Option<String> {
|
2018-04-23 21:33:15 -07:00
|
|
|
None
|
|
|
|
}
|
|
|
|
fn custom_interpreter_directives_help(&self) -> String {
|
|
|
|
format!(">> No custom interpreter directives specified <<")
|
|
|
|
}
|
2017-08-30 19:09:22 -07:00
|
|
|
}
|
2018-04-29 00:04:31 -07:00
|
|
|
|
2018-04-29 19:45:04 -07:00
|
|
|
/* a pass_chain function signature looks like:
|
|
|
|
* fn(&mut ProgrammingLanguageInterface, A, Option<&mut DebugHandler>) -> Result<B, String>
|
|
|
|
*
|
|
|
|
* TODO use some kind of failure-handling library to make this better
|
|
|
|
*/
|
|
|
|
|
2018-04-29 00:04:31 -07:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pass_chain {
|
2018-04-29 22:17:10 -07:00
|
|
|
($state:expr, $options:expr; $($pass:path), *) => {
|
2018-04-29 20:54:45 -07:00
|
|
|
|text_input| {
|
|
|
|
let mut comp = UnfinishedComputation::default();
|
2018-04-29 22:17:10 -07:00
|
|
|
pass_chain_helper! { ($state, comp, $options); text_input $(, $pass)* }
|
2018-04-29 20:54:45 -07:00
|
|
|
}
|
2018-04-29 00:04:31 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pass_chain_helper {
|
2018-04-29 22:17:10 -07:00
|
|
|
(($state:expr, $comp:expr, $options:expr); $input:expr, $pass:path $(, $rest:path)*) => {
|
2018-04-29 00:55:39 -07:00
|
|
|
{
|
|
|
|
let pass_name = stringify!($pass);
|
2018-04-29 21:15:19 -07:00
|
|
|
let output = {
|
2018-04-29 22:26:40 -07:00
|
|
|
let debug_pointer: Option<&mut UnfinishedComputation> = {
|
|
|
|
//TODO this is janky fix it
|
|
|
|
let debug_condition = ($options.debug.tokens && pass_name == "tokenizing_stage")
|
2018-04-29 22:51:01 -07:00
|
|
|
|| ($options.debug.parse_tree && pass_name == "parsing_stage")
|
|
|
|
|| ($options.debug.symbol_table && pass_name == "symbol_table_stage")
|
|
|
|
|| (pass_name == "typechecking_stage");
|
2018-04-29 22:26:40 -07:00
|
|
|
if debug_condition { Some(&mut $comp) } else { None }
|
|
|
|
};
|
2018-04-29 21:15:19 -07:00
|
|
|
$pass($state, $input, debug_pointer)
|
|
|
|
};
|
2018-04-29 00:55:39 -07:00
|
|
|
match output {
|
2018-04-29 22:17:10 -07:00
|
|
|
Ok(result) => pass_chain_helper! { ($state, $comp, $options); result $(, $rest)* },
|
2018-04-29 00:55:39 -07:00
|
|
|
Err(err) => {
|
2018-04-29 20:58:08 -07:00
|
|
|
$comp.output(Err(format!("Pass {} failed with {:?}", pass_name, err)))
|
2018-04-29 00:55:39 -07:00
|
|
|
}
|
2018-04-29 00:04:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Done
|
2018-04-29 22:17:10 -07:00
|
|
|
(($state:expr, $comp:expr, $options:expr); $final_output:expr) => {
|
2018-04-29 00:55:39 -07:00
|
|
|
{
|
2018-04-29 20:58:08 -07:00
|
|
|
let final_output: FinishedComputation = $comp.finish(Ok($final_output));
|
2018-04-29 00:55:39 -07:00
|
|
|
final_output
|
|
|
|
}
|
|
|
|
};
|
2018-04-29 00:04:31 -07:00
|
|
|
}
|