aboutsummaryrefslogtreecommitdiff
path: root/src/eval/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval/mod.rs')
-rw-r--r--src/eval/mod.rs34
1 files changed, 24 insertions, 10 deletions
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index 589be37..9e0d033 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -3,8 +3,8 @@
3use crate::{ast, Wrap}; 3use crate::{ast, Wrap};
4use std::{collections::HashMap, fmt, io}; 4use std::{collections::HashMap, fmt, io};
5 5
6mod builtins;
7mod analysis; 6mod analysis;
7mod builtins;
8#[cfg(test)] 8#[cfg(test)]
9mod test; 9mod test;
10 10
@@ -453,7 +453,8 @@ pub struct Context<'s> {
453 cursor: Option<tree_sitter::TreeCursor<'static>>, 453 cursor: Option<tree_sitter::TreeCursor<'static>>,
454 tree: Option<&'static tree_sitter::Tree>, 454 tree: Option<&'static tree_sitter::Tree>,
455 cache: HashMap<NodeId, tree_sitter::Node<'static>>, 455 cache: HashMap<NodeId, tree_sitter::Node<'static>>,
456 output_stream: Option<Box<dyn io::Write + 's>>, 456 output_stream: Box<dyn io::Write + 's>,
457 error_stream: Box<dyn io::Write + 's>,
457} 458}
458 459
459impl<'s> fmt::Debug for Context<'s> { 460impl<'s> fmt::Debug for Context<'s> {
@@ -484,7 +485,8 @@ impl<'s> Context<'s> {
484 cursor: None, 485 cursor: None,
485 tree: None, 486 tree: None,
486 cache: HashMap::default(), 487 cache: HashMap::default(),
487 output_stream: Some(Box::new(io::stdout()) as Box<dyn io::Write + 's>), 488 output_stream: Box::new(io::stdout()) as Box<dyn io::Write + 's>,
489 error_stream: Box::new(io::stderr()) as Box<dyn io::Write + 's>,
488 } 490 }
489 } 491 }
490 492
@@ -539,7 +541,12 @@ impl<'s> Context<'s> {
539 } 541 }
540 542
541 pub fn with_output_stream(mut self, stream: Box<dyn io::Write + 's>) -> Self { 543 pub fn with_output_stream(mut self, stream: Box<dyn io::Write + 's>) -> Self {
542 self.output_stream = Some(stream); 544 self.output_stream = stream;
545 self
546 }
547
548 pub fn with_error_stream(mut self, stream: Box<dyn io::Write + 's>) -> Self {
549 self.error_stream = stream;
543 self 550 self
544 } 551 }
545 552
@@ -793,13 +800,11 @@ impl<'s> Context<'s> {
793 .unwrap_or_default() 800 .unwrap_or_default()
794 } 801 }
795 802
796 pub fn analyze(&mut self) -> Result { 803 pub fn analyze(&mut self) -> analysis::Analysis {
797 let analysis = analysis::run(&*self); 804 analysis::run(&*self)
798 analysis.print();
799 Err(Error::Analysis)
800 } 805 }
801 806
802 pub fn eval(&mut self) -> Result { 807 fn eval_program(&mut self) -> Result {
803 let program = std::mem::take(&mut self.program); 808 let program = std::mem::take(&mut self.program);
804 let mut has_next = true; 809 let mut has_next = true;
805 let mut postorder = Vec::new(); 810 let mut postorder = Vec::new();
@@ -847,6 +852,15 @@ impl<'s> Context<'s> {
847 852
848 Ok(Value::Unit) 853 Ok(Value::Unit)
849 } 854 }
855
856 pub fn eval(&mut self) -> Result {
857 let analysis = self.analyze();
858 write!(self.error_stream, "{analysis}").unwrap();
859 if analysis.contains_error() {
860 return Err(Error::Analysis);
861 }
862 self.eval_program()
863 }
850} 864}
851 865
852pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result { 866pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result {
@@ -860,8 +874,8 @@ pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> R
860 .with_input(file.to_owned()) 874 .with_input(file.to_owned())
861 .with_tree(tree) 875 .with_tree(tree)
862 .with_output_stream(Box::new(io::stdout())) 876 .with_output_stream(Box::new(io::stdout()))
877 .with_error_stream(Box::new(io::stderr()))
863 .with_program(program); 878 .with_program(program);
864 879
865 ctx.analyze()?;
866 ctx.eval() 880 ctx.eval()
867} 881}