From 4be32d0a5d13225d885aeff7253ee0235edc0d83 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 11 Mar 2024 18:21:40 -0400 Subject: [PATCH] Add while loops --- src/abstract_tree/block.rs | 3 ++- src/abstract_tree/while.rs | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index e9a3ee9..57e62a8 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -40,7 +40,8 @@ impl AbstractTree for Block { let action = statement.run(_context)?; previous = match action { Action::Return(value) => Action::Return(value), - r#break => return Ok(r#break), + Action::None => Action::None, + Action::Break => return Ok(action), }; } diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index 918cac5..d0466d6 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -19,14 +19,27 @@ impl While { impl AbstractTree for While { fn expected_type(&self, _context: &Context) -> Result { - todo!() + Ok(Type::None) } fn validate(&self, _context: &Context) -> Result<(), ValidationError> { - todo!() + self.expression.validate(_context)?; + self.block.validate(_context) } fn run(self, _context: &Context) -> Result { - todo!() + while self + .expression + .clone() + .run(_context)? + .as_return_value()? + .as_boolean()? + { + if let Action::Break = self.block.clone().run(_context)? { + break; + } + } + + Ok(Action::None) } }