From 58a1782162cf12ecf95b4163049589d4aa2dc254 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 18 Nov 2021 20:08:46 -0800 Subject: [PATCH] While expr --- schala-lang/src/parsing/combinator.rs | 19 +++++++++++++++++++ schala-lang/src/parsing/test.rs | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 75dd493..2a54ee6 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -424,6 +424,16 @@ fn primary_expr(input: Span) -> ParseResult { context( "primary-expr", alt(( + primary_expr_no_struct, + )), + )(input) +} + +fn primary_expr_no_struct(input: Span) -> ParseResult { + context( + "primary-expr-no-struct", + alt(( + while_expr, list_expr, paren_expr, string_literal, @@ -435,6 +445,15 @@ fn primary_expr(input: Span) -> ParseResult { )(input) } +fn while_expr(input: Span) -> ParseResult { + 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 { delimited( tok(char('(')), diff --git a/schala-lang/src/parsing/test.rs b/schala-lang/src/parsing/test.rs index 84ce6a2..3139fa8 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -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)))))),