From 50b7b4bfc6524e1b348d14626a4a3527639c43a7 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 20 Mar 2024 23:13:21 -0400 Subject: [PATCH] Clean up --- dust-lang/src/abstract_tree/mod.rs | 56 +++++++++++++++++------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/abstract_tree/mod.rs index 413a30d..6c01f6e 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/abstract_tree/mod.rs @@ -89,6 +89,33 @@ impl AbstractTree { } pub fn run(self, context: &Context) -> Result, Vec> { + let valid_statements = self.run_type_definitions(context)?; + let mut previous_value = None; + + for statement in valid_statements { + let run = statement.node.run(context); + + match run { + Ok(action) => match action { + Action::Return(value) => previous_value = Some(value), + _ => {} + }, + Err(runtime_error) => { + return Err(vec![Error::Runtime { + error: runtime_error, + position: statement.position, + }]); + } + } + } + + Ok(previous_value) + } + + fn run_type_definitions( + self, + context: &Context, + ) -> Result>, Vec> { let mut errors = Vec::new(); let mut valid_statements = Vec::new(); @@ -118,32 +145,11 @@ impl AbstractTree { } } - if !errors.is_empty() { - return Err(errors); + if errors.is_empty() { + Ok(valid_statements) + } else { + Err(errors) } - - let mut previous_value = None; - - for statement in valid_statements { - let run = statement.node.run(context); - - match run { - Ok(action) => match action { - Action::Return(value) => previous_value = Some(value), - _ => {} - }, - Err(runtime_error) => { - errors.push(Error::Runtime { - error: runtime_error, - position: statement.position, - }); - - return Err(errors); - } - } - } - - Ok(previous_value) } }