Show err output when evaluating non-interactively

This commit is contained in:
greg 2018-03-24 17:27:54 -07:00
parent 4bea717e72
commit 3295242115
3 changed files with 13 additions and 5 deletions

View File

@ -102,17 +102,17 @@ impl<'a> State<'a> {
State { parent_frame: Some(parent), values: HashMap::new() } State { parent_frame: Some(parent), values: HashMap::new() }
} }
pub fn evaluate(&mut self, ast: AST) -> Vec<String> { pub fn evaluate(&mut self, ast: AST) -> Vec<Result<String, String>> {
let mut acc = vec![]; let mut acc = vec![];
for statement in ast.0 { for statement in ast.0 {
match self.eval_statement(statement) { match self.eval_statement(statement) {
Ok(output) => { Ok(output) => {
if let Some(fully_evaluated) = output { if let Some(fully_evaluated) = output {
acc.push(fully_evaluated.to_string()); acc.push(Ok(fully_evaluated.to_string()));
} }
}, },
Err(error) => { Err(error) => {
acc.push(format!("Eval error: {}", error)); acc.push(Err(format!("Eval error: {}", error)));
return acc; return acc;
}, },
} }

View File

@ -107,7 +107,12 @@ impl ProgrammingLanguageInterface for Schala {
} }
let evaluation_outputs = self.state.evaluate(ast); let evaluation_outputs = self.state.evaluate(ast);
let text_output: String = evaluation_outputs.into_iter().intersperse(format!("\n")).collect(); let text_output: Result<Vec<String>, String> = evaluation_outputs
evaluation.output(Ok(text_output)) .into_iter()
.collect();
let eval_output = text_output
.map(|v| { v.into_iter().intersperse(format!("\n")).collect() });
evaluation.output(eval_output)
} }
} }

View File

@ -4,6 +4,9 @@ fn main() {
a + b a + b
} }
foo
print(main()) print(main())