Skip to content

Commit

Permalink
implement continue statement
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 20, 2024
1 parent fb99e8e commit 40d3f26
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,23 @@ fn main() {
}
fn main() -> int {
let result1 = fib1(19);
let n = 0;
let counter = 0;
loop {
if counter == 20 {
break;
}
counter = counter + 1;
if counter == 10 {
continue;
}
n = n + 1;
}
let result1 = fib1(n);
let result2 = fib2(19);
if result1 == result2 {
Expand Down
1 change: 1 addition & 0 deletions src/repr/ast/base/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ macro_rules! generate_ast {
pub type ExpressionStatement =
ast::ExpressionStatement<$ty_info, $fn_identifier, $ident_identifier>;
pub type BreakStatement = ast::BreakStatement<$ty_info>;
pub type ContinueStatement = ast::ContinueStatement<$ty_info>;
};
}
6 changes: 6 additions & 0 deletions src/repr/ast/base/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ast_node!(
Let(LetStatement<TyInfo, FnIdentifier, IdentIdentifier>),
Expression(ExpressionStatement<TyInfo, FnIdentifier, IdentIdentifier>),
Break(BreakStatement<TyInfo>),
Continue(ContinueStatement<TyInfo>),
}
);

Expand Down Expand Up @@ -62,3 +63,8 @@ ast_node! {
typed struct BreakStatement<TyInfo> {
}
}

ast_node! {
typed struct ContinueStatement<TyInfo> {
}
}
3 changes: 3 additions & 0 deletions src/repr/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ pub enum Token {
Loop,
#[token("break")]
Break,
#[token("continue")]
Continue,

/*
* Primitive type
Expand Down Expand Up @@ -130,6 +132,7 @@ impl Display for Token {
Token::Else => write!(f, "else"),
Token::Loop => write!(f, "loop"),
Token::Break => write!(f, "break"),
Token::Continue => write!(f, "continue"),
Token::Int => write!(f, "int"),
Token::Bool => write!(f, "bool"),
Token::True => write!(f, "true"),
Expand Down
9 changes: 9 additions & 0 deletions src/stage/lower_ir/lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ fn lower_block(
let (_, loop_end) = builder.loop_stack.last().unwrap();
builder.set_terminator(Terminator::Jump(*loop_end));
}
ast::Statement::Continue(ast::ContinueStatement { .. }) => {
assert!(
!builder.loop_stack.is_empty(),
"can only continue within a loop"
);

let (loop_start, _) = builder.loop_stack.last().unwrap();
builder.set_terminator(Terminator::Jump(*loop_start));
}
ast::Statement::Expression(ast::ExpressionStatement {
expression,
ty_info,
Expand Down
7 changes: 6 additions & 1 deletion src/stage/parse/statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::repr::ast::base::BreakStatement;
use crate::repr::ast::base::{BreakStatement, ContinueStatement};

use super::*;

Expand Down Expand Up @@ -64,6 +64,11 @@ pub fn parse_statement(

Statement::Break(BreakStatement::new(break_span))
}
Token::Continue => {
let (_, continue_span) = tokens.next_spanned().unwrap();

Statement::Continue(ContinueStatement::new(continue_span))
}
_ => {
// Parse expression
let expression = parse_expression(compiler, tokens, Precedence::Lowest)?;
Expand Down
17 changes: 17 additions & 0 deletions src/stage/type_check/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl parse_ast::Statement {
Statement::Expression(s.ty_solve(compiler, scope)?)
}
parse_ast::Statement::Break(s) => Statement::Break(s.ty_solve(compiler, scope)?),
parse_ast::Statement::Continue(s) => Statement::Continue(s.ty_solve(compiler, scope)?),
})
}
}
Expand Down Expand Up @@ -86,6 +87,22 @@ impl parse_ast::BreakStatement {
}
}

impl parse_ast::ContinueStatement {
pub fn ty_solve(
self,
_compiler: &mut Compiler,
_scope: &mut Scope,
) -> Result<ContinueStatement, TyError> {
Ok(ContinueStatement {
ty_info: TyInfo {
ty: Ty::Never,
return_ty: None,
},
span: self.span,
})
}
}

impl parse_ast::ExpressionStatement {
pub fn ty_solve(
self,
Expand Down

0 comments on commit 40d3f26

Please sign in to comment.