Switch to contentful output types
This commit is contained in:
parent
c97e58c2aa
commit
b5a6c5903e
@ -24,8 +24,60 @@ pub struct EvalOptions {
|
|||||||
pub trace_evaluation: bool,
|
pub trace_evaluation: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct ReplOutput {
|
||||||
|
output: String,
|
||||||
|
artifacts: Vec<TraceArtifact>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ReplOutput {
|
||||||
|
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() {
|
||||||
|
acc.push_str(&line.debug_output);
|
||||||
|
}
|
||||||
|
acc.push_str(&self.output);
|
||||||
|
acc
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn print_to_screen(&self) {
|
||||||
|
for line in self.artifacts.iter() {
|
||||||
|
println!("{}: {}", line.stage_name, line.debug_output);
|
||||||
|
}
|
||||||
|
println!("{}", self.output);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
//TODO I'll probably wanna implement this later
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CompilationOutput {
|
||||||
|
output: LLVMCodeString,
|
||||||
|
artifacts: Vec<TraceArtifact>,
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct TraceArtifact {
|
||||||
|
stage_name: String,
|
||||||
|
debug_output: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TraceArtifact {
|
||||||
|
pub fn new(stage: &str, debug: String) -> TraceArtifact {
|
||||||
|
TraceArtifact { stage_name: stage.to_string(), debug_output: debug }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub trait ProgrammingLanguageInterface {
|
pub trait ProgrammingLanguageInterface {
|
||||||
fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> Vec<String>;
|
fn evaluate_in_repl(&mut self, input: &str, eval_options: EvalOptions) -> ReplOutput;
|
||||||
fn get_language_name(&self) -> String;
|
fn get_language_name(&self) -> String;
|
||||||
fn compile(&mut self, _input: &str) -> LLVMCodeString {
|
fn compile(&mut self, _input: &str) -> LLVMCodeString {
|
||||||
LLVMCodeString("".to_string())
|
LLVMCodeString("".to_string())
|
||||||
|
@ -3,7 +3,7 @@ pub mod parser;
|
|||||||
pub mod eval;
|
pub mod eval;
|
||||||
pub mod compilation;
|
pub mod compilation;
|
||||||
|
|
||||||
use language::{ProgrammingLanguageInterface, EvalOptions, LLVMCodeString};
|
use language::{ProgrammingLanguageInterface, EvalOptions, ReplOutput, TraceArtifact, LLVMCodeString};
|
||||||
|
|
||||||
pub use self::eval::Evaluator as MaaruEvaluator;
|
pub use self::eval::Evaluator as MaaruEvaluator;
|
||||||
|
|
||||||
@ -24,18 +24,18 @@ impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
|||||||
"Maaru".to_string()
|
"Maaru".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> Vec<String> {
|
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> ReplOutput {
|
||||||
let mut output = vec![];
|
let mut output = ReplOutput::default();
|
||||||
|
|
||||||
let tokens = match tokenizer::tokenize(input) {
|
let tokens = match tokenizer::tokenize(input) {
|
||||||
Ok(tokens) => {
|
Ok(tokens) => {
|
||||||
if options.debug_tokens {
|
if options.debug_tokens {
|
||||||
output.push(format!("{:?}", tokens));
|
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens)));
|
||||||
}
|
}
|
||||||
tokens
|
tokens
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let msg = format!("Tokenization error: {:?}\n", err.msg);
|
output.add_output(format!("Tokenization error: {:?}\n", err.msg));
|
||||||
output.push(msg);
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -43,20 +43,20 @@ impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
|||||||
let ast = match parser::parse(&tokens, &[]) {
|
let ast = match parser::parse(&tokens, &[]) {
|
||||||
Ok(ast) => {
|
Ok(ast) => {
|
||||||
if options.debug_parse {
|
if options.debug_parse {
|
||||||
output.push(format!("{:?}", ast));
|
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||||
}
|
}
|
||||||
ast
|
ast
|
||||||
},
|
},
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
let msg = format!("Parse error: {:?}\n", err.msg);
|
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||||
output.push(msg);
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let mut evaluation_output = String::new();
|
||||||
let evaluation_output = self.evaluator.run(ast);
|
for s in self.evaluator.run(ast).iter() {
|
||||||
output.extend(evaluation_output);
|
evaluation_output.push_str(s);
|
||||||
|
}
|
||||||
|
output.add_output(evaluation_output);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/main.rs
10
src/main.rs
@ -132,9 +132,7 @@ fn run_noninteractive<T: ProgrammingLanguageInterface>(filename: &str, language:
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let interpretor_output = language.evaluate_in_repl(&buffer, options);
|
let interpretor_output = language.evaluate_in_repl(&buffer, options);
|
||||||
for line in interpretor_output {
|
interpretor_output.print_to_screen();
|
||||||
println!("{}", line);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,11 +200,7 @@ impl Repl {
|
|||||||
|
|
||||||
|
|
||||||
let interpretor_output = language.evaluate_in_repl(input, options);
|
let interpretor_output = language.evaluate_in_repl(input, options);
|
||||||
let mut acc = String::new();
|
interpretor_output.to_string()
|
||||||
for i in interpretor_output {
|
|
||||||
acc.push_str(&i)
|
|
||||||
}
|
|
||||||
acc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
extern crate itertools;
|
extern crate itertools;
|
||||||
use self::itertools::Itertools;
|
use self::itertools::Itertools;
|
||||||
|
|
||||||
use language::{ProgrammingLanguageInterface, EvalOptions, TokenError};
|
use language::{ProgrammingLanguageInterface, EvalOptions, ReplOutput, TokenError};
|
||||||
|
|
||||||
pub struct Robo {
|
pub struct Robo {
|
||||||
}
|
}
|
||||||
@ -137,17 +137,17 @@ impl ProgrammingLanguageInterface for Robo {
|
|||||||
"Robo".to_string()
|
"Robo".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> Vec<String> {
|
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> ReplOutput {
|
||||||
let mut output = vec!();
|
let mut output = ReplOutput::default();
|
||||||
let tokens = match tokenize(input) {
|
let tokens = match tokenize(input) {
|
||||||
Ok(tokens) => tokens,
|
Ok(tokens) => tokens,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
output.push(format!("Tokenize error: {:?}", e));
|
output.add_output(format!("Tokenize error: {:?}", e));
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
output.push(format!("{:?}", tokens));
|
output.add_output(format!("{:?}", tokens));
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use language::{ProgrammingLanguageInterface, EvalOptions};
|
use language::{ProgrammingLanguageInterface, EvalOptions, TraceArtifact, ReplOutput};
|
||||||
|
|
||||||
mod parsing;
|
mod parsing;
|
||||||
|
|
||||||
@ -16,23 +16,36 @@ impl ProgrammingLanguageInterface for Schala {
|
|||||||
"Schala".to_string()
|
"Schala".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_in_repl(&mut self, input: &str, _eval_options: EvalOptions) -> Vec<String> {
|
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> ReplOutput {
|
||||||
let mut output = vec!(format!("test eval"));
|
let mut output = ReplOutput::default();
|
||||||
|
|
||||||
let tokens = match parsing::tokenize(input) {
|
let tokens = match parsing::tokenize(input) {
|
||||||
Ok(tokens) => tokens,
|
Ok(tokens) => {
|
||||||
Err(e) => { output.push(format!("{}", e.msg));
|
if options.debug_tokens {
|
||||||
|
output.add_artifact(TraceArtifact::new("tokens", format!("{:?}", tokens)));
|
||||||
|
}
|
||||||
|
tokens
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
output.add_output(format!("Tokenization error: {:?}\n", err.msg));
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let _ast = match parsing::parse(tokens) {
|
let ast = match parsing::parse(tokens) {
|
||||||
Ok(ast) => ast,
|
Ok(ast) => {
|
||||||
Err(e) => { output.push(format!("{}", e.msg));
|
if options.debug_parse {
|
||||||
|
output.add_artifact(TraceArtifact::new("ast", format!("{:?}", ast)));
|
||||||
|
}
|
||||||
|
ast
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
output.add_output(format!("Parse error: {:?}\n", err.msg));
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
output
|
let evaluation_output = format!("test eval");
|
||||||
|
output.add_output(evaluation_output);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user