Skip to content

Commit

Permalink
Implement loop syntax, short for while Bool.True: ...
Browse files Browse the repository at this point in the history
  • Loading branch information
osa1 committed Jan 25, 2025
1 parent 5fbe49c commit 39f81e2
Show file tree
Hide file tree
Showing 5 changed files with 5,703 additions and 5,437 deletions.
4 changes: 2 additions & 2 deletions lib/Str.fir
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Iterator for SplitWhitespace:
let charIdxIter = self._str.substr(wordStart, self._str.len()).charIndices()

# Skip initial whitespace, initialize `wordStart` and `wordEnd`.
while Bool.True:
loop:
match charIdxIter.next():
Option.Some((char = char, idx = idx)):
if !char.isAsciiWhitespace():
Expand All @@ -210,7 +210,7 @@ impl Iterator for SplitWhitespace:
return Option.None

# Scan word, update `wordEnd`.
while Bool.True:
loop:
match charIdxIter.next():
Option.Some((char = char, idx = idx)):
wordEnd = self._idx + idx
Expand Down
1 change: 1 addition & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ lexgen::lexer! {
"type" = TokenKind::Type,
"var" = TokenKind::Var,
"while" = TokenKind::While,
"loop" = TokenKind::Loop,

// Delimiters
"(" = TokenKind::LParen,
Expand Down
25 changes: 19 additions & 6 deletions src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::token::*;
use std::convert::Infallible;
use std::rc::Rc;

use smol_str::SmolStr;

use lexgen_util::{LexerError, Loc};

grammar<'a>(module: &'a Rc<str>);
Expand Down Expand Up @@ -76,6 +78,7 @@ extern {
"prim" => Token { kind: TokenKind::Prim, .. },
"trait" => Token { kind: TokenKind::Trait, .. },
"impl" => Token { kind: TokenKind::Impl, .. },
"loop" => Token { kind: TokenKind::Loop, .. },
IntLit => Token { kind: TokenKind::Int { .. }, .. },
HexIntLit => Token { kind: TokenKind::HexInt { .. }, .. },
BinIntLit => Token { kind: TokenKind::BinInt { .. }, .. },
Expand Down Expand Up @@ -564,26 +567,36 @@ Stmt: Stmt = {
<l:@L> <expr:BlockExpr> <r:@R> =>
Stmt::Expr(L::new(module, l, r, expr)),

"for" <pat:LPat> "in" <expr:LExpr> ":" NEWLINE INDENT <statements:LStmts> DEDENT =>
"for" <pat:LPat> "in" <expr:LExpr> ":" NEWLINE INDENT <body:LStmts> DEDENT =>
Stmt::For(ForStmt {
pat,
ty: None,
expr,
expr_ty: None,
body: statements,
body,
}),

"while" <cond:LExpr> ":" NEWLINE INDENT <statements:LStmts> DEDENT =>
"while" <cond:LExpr> ":" NEWLINE INDENT <body:LStmts> DEDENT =>
Stmt::While(WhileStmt {
cond,
body:statements,
body,
}),

"while" "let" <pat:LPat> "=" <cond:LExpr> ":" NEWLINE INDENT <statements:LStmts> DEDENT =>
"while" "let" <pat:LPat> "=" <cond:LExpr> ":" NEWLINE INDENT <body:LStmts> DEDENT =>
Stmt::WhileLet(WhileLetStmt {
pat,
cond,
body:statements,
body,
}),

<l:@L> "loop" <r:@R> ":" NEWLINE INDENT <body:LStmts> DEDENT =>
Stmt::While(WhileStmt {
cond: L::new(module, l, r, Expr::ConstrSelect(ConstrSelectExpr {
ty: SmolStr::new_static("Bool"),
constr: SmolStr::new_static("True"),
ty_args: vec![],
})),
body,
}),
}

Expand Down
Loading

0 comments on commit 39f81e2

Please sign in to comment.