While loop

This commit is contained in:
mars 2022-03-02 14:51:18 -07:00
parent 1f66a5f94b
commit 0f90e8108c
2 changed files with 20 additions and 1 deletions

View File

@ -15,9 +15,10 @@ pub enum Token {
#[token("struct")] Struct,
#[token("fn")] Function,
#[token("for")] For,
#[token("in")] In,
#[token("while")] While,
#[token("if")] If,
#[token("else")] Else,
#[token("in")] In,
#[token("let")] Let,
#[token("mut")] Mut,
#[token("interface")] Interface,

View File

@ -317,6 +317,20 @@ impl<'a> BranchBody<'a> {
next = Some(tail);
}
}
Token::While => {
let test_expr = match Expr::build(lexer) {
(Some(test_expr), Token::BraceOpen) => test_expr,
(Some(_), _) => lexer.panic_message("Expected opening brace"),
_ => lexer.panic_message("Expected test expression"),
};
let loop_body = BranchBody::build(lexer);
statements.push(Statement::While {
test_expr,
loop_body,
});
}
Token::BraceClose => break None,
_ => match Expr::build_start(tok, lexer) {
(None, Token::Semicolon | Token::BraceClose) => {}
@ -516,6 +530,10 @@ pub enum Literal<'a> {
pub enum Statement<'a> {
Expr(Expr<'a>),
If(IfStmt<'a>),
While {
test_expr: Expr<'a>,
loop_body: BranchBody<'a>,
},
Let {
var: &'a str,
mutable: bool,