1
0

Remove expressions from break statements

This commit is contained in:
Jeff 2024-03-11 17:44:52 -04:00
parent 780ea0858b
commit 565fd450a9
6 changed files with 26 additions and 53 deletions

View File

@ -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))
}
}

View File

@ -42,7 +42,7 @@ pub trait AbstractTree {
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum Action {
Break(Option<Value>),
Break,
Return(Value),
None,
}
@ -50,15 +50,8 @@ pub enum Action {
impl Action {
pub fn as_value(self) -> Result<Value, ValidationError> {
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),
}
}

View File

@ -9,7 +9,7 @@ use super::{AbstractTree, Action, Assignment, Block, Expression, IfElse, Loop, T
pub enum Statement {
Assignment(Assignment),
Block(Block),
Break(Option<Expression>),
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),

View File

@ -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,
},

View File

@ -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()

View File

@ -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]