From 565fd450a9c3c34709bfa893cd4439870871ae47 Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 11 Mar 2024 17:44:52 -0400 Subject: [PATCH] Remove expressions from break statements --- src/abstract_tree/loop.rs | 11 ++--------- src/abstract_tree/mod.rs | 11 ++--------- src/abstract_tree/statement.rs | 28 ++++------------------------ src/lib.rs | 8 +------- src/parser.rs | 4 +--- tests/statements.rs | 17 ++++++++++++++++- 6 files changed, 26 insertions(+), 53 deletions(-) diff --git a/src/abstract_tree/loop.rs b/src/abstract_tree/loop.rs index a9bc7c2..f97e31f 100644 --- a/src/abstract_tree/loop.rs +++ b/src/abstract_tree/loop.rs @@ -53,22 +53,15 @@ impl AbstractTree for Loop { #[cfg(test)] mod tests { - use crate::{ - abstract_tree::{Expression, ValueNode}, - Value, - }; - use super::*; #[test] fn basic_loop() { let result = Loop { - statements: vec![Statement::Break(Some(Expression::Value( - ValueNode::Integer(42), - )))], + statements: vec![Statement::Break], } .run(&Context::new()); - assert_eq!(result, Ok(Action::Break(Some(Value::integer(42))))) + assert_eq!(result, Ok(Action::Break)) } } diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index a488a4b..0f69933 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -42,7 +42,7 @@ pub trait AbstractTree { #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)] pub enum Action { - Break(Option), + Break, Return(Value), None, } @@ -50,15 +50,8 @@ pub enum Action { impl Action { pub fn as_value(self) -> Result { match self { - Action::Break(value) => { - if let Some(value) = value { - Ok(value) - } else { - Err(ValidationError::ExpectedValue) - } - } Action::Return(value) => Ok(value), - Action::None => Err(ValidationError::ExpectedValue), + _ => Err(ValidationError::ExpectedValue), } } diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index 03cad38..fee8fbd 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -9,7 +9,7 @@ use super::{AbstractTree, Action, Assignment, Block, Expression, IfElse, Loop, T pub enum Statement { Assignment(Assignment), Block(Block), - Break(Option), + Break, Expression(Expression), IfElse(IfElse), Loop(Loop), @@ -20,13 +20,7 @@ impl AbstractTree for Statement { match self { Statement::Assignment(assignment) => assignment.expected_type(_context), Statement::Block(block) => block.expected_type(_context), - Statement::Break(expression_option) => { - if let Some(expression) = expression_option { - expression.expected_type(_context) - } else { - Ok(Type::None) - } - } + Statement::Break => Ok(Type::None), Statement::Expression(expression) => expression.expected_type(_context), Statement::IfElse(if_else) => if_else.expected_type(_context), Statement::Loop(r#loop) => r#loop.expected_type(_context), @@ -37,13 +31,7 @@ impl AbstractTree for Statement { match self { Statement::Assignment(assignment) => assignment.validate(_context), Statement::Block(block) => block.validate(_context), - Statement::Break(expression_option) => { - if let Some(expression) = expression_option { - expression.validate(_context) - } else { - Ok(()) - } - } + Statement::Break => Ok(()), Statement::Expression(expression) => expression.validate(_context), Statement::IfElse(if_else) => if_else.validate(_context), Statement::Loop(r#loop) => r#loop.validate(_context), @@ -54,15 +42,7 @@ impl AbstractTree for Statement { match self { Statement::Assignment(assignment) => assignment.run(_context), Statement::Block(block) => block.run(_context), - Statement::Break(expression) => { - if let Some(expression) = expression { - let value = expression.run(_context)?.as_return_value()?; - - Ok(Action::Break(Some(value))) - } else { - Ok(Action::Break(None)) - } - } + Statement::Break => Ok(Action::Break), Statement::Expression(expression) => expression.run(_context), Statement::IfElse(if_else) => if_else.run(_context), Statement::Loop(r#loop) => r#loop.run(_context), diff --git a/src/lib.rs b/src/lib.rs index fb9e924..b67a81a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,13 +53,7 @@ impl Interpreter { for (statement, _span) in statements { value = match statement.run(&self.context) { Ok(action) => match action { - Action::Break(value_option) => { - if let Some(value) = value_option { - Some(value) - } else { - None - } - } + Action::Break => None, Action::Return(value) => Some(value), Action::None => continue, }, diff --git a/src/parser.rs b/src/parser.rs index 95769af..be29f53 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -304,9 +304,7 @@ pub fn parser<'src>() -> DustParser<'src> { .map(|expression| Statement::Expression(expression)) .boxed(); - let r#break = just(Token::Keyword("break")) - .ignore_then(expression.clone().or_not()) - .map(|expression| Statement::Break(expression)); + let r#break = just(Token::Keyword("break")).to(Statement::Break); let assignment = identifier .clone() diff --git a/tests/statements.rs b/tests/statements.rs index 7c64367..3b97afb 100644 --- a/tests/statements.rs +++ b/tests/statements.rs @@ -17,7 +17,22 @@ fn loops_and_breaks() { " ), Ok(Some(Value::integer(3))) - ) + ); + assert_eq!( + interpret( + " + foobar = { + while true { + break + } + 'foobar' + } + + foobar + " + ), + Ok(Some(Value::string("foobar".to_string()))) + ); } #[test]