While expr

This commit is contained in:
Greg Shuflin 2021-11-18 20:08:46 -08:00
parent 6454cc5ad1
commit 58a1782162
2 changed files with 21 additions and 2 deletions

View File

@ -424,6 +424,16 @@ fn primary_expr(input: Span) -> ParseResult<ExpressionKind> {
context(
"primary-expr",
alt((
primary_expr_no_struct,
)),
)(input)
}
fn primary_expr_no_struct(input: Span) -> ParseResult<ExpressionKind> {
context(
"primary-expr-no-struct",
alt((
while_expr,
list_expr,
paren_expr,
string_literal,
@ -435,6 +445,15 @@ fn primary_expr(input: Span) -> ParseResult<ExpressionKind> {
)(input)
}
fn while_expr(input: Span) -> ParseResult<ExpressionKind> {
let id = fresh_id(&input);
map(preceded(kw("while"), pair(opt(expression), block)),
move |(condition, body)| ExpressionKind::WhileExpression {
condition: condition.map(Box::new),
body,
})(input)
}
fn paren_expr(input: Span) -> ParseResult<ExpressionKind> {
delimited(
tok(char('(')),

View File

@ -375,8 +375,8 @@ fn index() {
fn while_expression() {
use ExpressionKind::*;
assert_expr!("while { }", expr(WhileExpression { condition: None, body: Block::default() }));
assert_expr!(
// assert_expr_comb!("while { }", expr(WhileExpression { condition: None, body: Block::default() }));
assert_expr_comb!(
"while a == b { }",
expr(WhileExpression {
condition: Some(bx(binop("==", expr(Value(qn!(a))), expr(Value(qn!(b)))))),