Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ndc_lib/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub enum Expression {
value: Box<ExpressionLocation>,
},
Break,
Continue,
RangeInclusive {
start: Option<Box<ExpressionLocation>>,
end: Option<Box<ExpressionLocation>>,
Expand Down Expand Up @@ -374,6 +375,7 @@ impl fmt::Debug for ExpressionLocation {
.finish(),
Expression::Return { value } => f.debug_struct("Return").field("value", value).finish(),
Expression::Break => f.debug_struct("Break").finish(),
Expression::Continue => f.debug_struct("Continue").finish(),
Expression::RangeInclusive { start, end } => f
.debug_struct("RangeInclusive")
.field("start", start)
Expand Down
3 changes: 3 additions & 0 deletions ndc_lib/src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,9 @@ impl Parser {
} else if let Some(token_location) = self.consume_token_if(&[Token::Break]) {
let expression = Expression::Break;
return Ok(expression.to_location(token_location.span));
} else if let Some(token_location) = self.consume_token_if(&[Token::Continue]) {
let expression = Expression::Continue;
return Ok(expression.to_location(token_location.span));
}
// matches curly bracketed block expression `{ }`
else if self.match_token(&[Token::LeftCurlyBracket]).is_some() {
Expand Down
12 changes: 10 additions & 2 deletions ndc_lib/src/interpreter/evaluate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ pub(crate) fn evaluate_expression(
let result = evaluate_expression(loop_body, environment);
match result {
Err(FunctionCarrier::Break(value)) => return Ok(value),
Err(FunctionCarrier::Continue) => {}
Err(err) => return Err(err),
Ok(_) => {}
}
Expand Down Expand Up @@ -365,6 +366,7 @@ pub(crate) fn evaluate_expression(

match result {
Err(FunctionCarrier::Break(break_value)) => return Ok(break_value),
Err(FunctionCarrier::Continue) => unreachable!(),
Err(err) => return Err(err),
Ok(_) => {}
}
Expand Down Expand Up @@ -399,6 +401,7 @@ pub(crate) fn evaluate_expression(
}
// TODO: for now we just put unit in here so we can improve break functionality later
Expression::Break => return Err(FunctionCarrier::Break(Value::unit())),
Expression::Continue => return Err(FunctionCarrier::Continue),
Expression::Index {
value: lhs_expr,
index: index_expr,
Expand Down Expand Up @@ -906,7 +909,8 @@ fn call_function(
FunctionCarrier::EvaluationError(_)
| FunctionCarrier::FunctionNotFound
// TODO: for now we just pass the break from inside the function to outside the function. This would allow some pretty funky code and might introduce weird bugs?
| FunctionCarrier::Break(_),
| FunctionCarrier::Break(_)
| FunctionCarrier::Continue
) => e,
Err(carrier @ FunctionCarrier::IntoEvaluationError(_)) => Err(carrier.lift_if(span)),
}
Expand Down Expand Up @@ -971,7 +975,11 @@ fn execute_for_iterations(
declare_or_assign_variable(l_value, r_value, true, &mut scope, span)?;

if tail.is_empty() {
execute_body(body, &mut scope, out_values)?;
match execute_body(body, &mut scope, out_values) {
Err(FunctionCarrier::Continue) => {},
Err(error) => return Err(error),
Ok(_value) => {}
}
} else {
execute_for_iterations(tail, body, out_values, &mut scope, span)?;
}
Expand Down
2 changes: 2 additions & 0 deletions ndc_lib/src/interpreter/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ pub enum FunctionCarrier {
Return(Value),
#[error("not an error")]
Break(Value),
#[error("not an error")]
Continue,
#[error("evaluation error {0}")]
EvaluationError(#[from] EvaluationError),
#[error("function does not exist")]
Expand Down
6 changes: 6 additions & 0 deletions ndc_lib/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ impl Interpreter {
expr.span,
))?;
}
Err(FunctionCarrier::Continue) => {
Err(EvaluationError::syntax_error(
"unexpected continue statement outside of loop body".to_string(),
expr.span,
))?;
}
Err(FunctionCarrier::EvaluationError(e)) => return Err(InterpreterError::from(e)),
_ => {
panic!(
Expand Down
3 changes: 3 additions & 0 deletions ndc_lib/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum Token {
Else,
Return,
Break,
Continue,
For,
In,
While,
Expand Down Expand Up @@ -140,6 +141,7 @@ impl fmt::Display for Token {
Self::Else => "else",
Self::Return => "return",
Self::Break => "break",
Self::Continue => "continue",
Self::For => "for",
Self::In => "in",
Self::While => "while",
Expand Down Expand Up @@ -320,6 +322,7 @@ impl From<String> for Token {
"false" => Self::False,
"return" => Self::Return,
"break" => Self::Break,
"continue" => Self::Continue,
"pure" => Self::Pure,
_ => Self::Identifier(value),
}
Expand Down
24 changes: 24 additions & 0 deletions tests/programs/004_basic/035_continue.ndct
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--PROGRAM--
let counter = 0;
for i in 1..100 {
if i == 23 {
continue;
}
counter += 1;
}
assert_eq(counter, 98);

// Nested break
let counter = 0;
for i in 0..50 {
for j in 1..5 {
if j == 2 { continue; }
counter += 1;
}
}

assert_eq(counter, 150);

print("ok");
--EXPECT--
ok
4 changes: 4 additions & 0 deletions tests/programs/004_basic/036_continue_outside_loop.ndct
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--PROGRAM--
continue;
--EXPECT-ERROR--
unexpected continue statement outside of loop
13 changes: 13 additions & 0 deletions tests/programs/004_basic/037_continue_while.ndct
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
--PROGRAM--
let i = 0;
let sum = 0;
while i < 100 {
i += 1;
if i == 30 { continue; }
sum += i;
}

assert_eq(sum, 5020);
print("ok");
--EXPECT--
ok