From 08ce718fb6c6790d60610bc606fe4dd9aa441ba3 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Mar 2020 16:03:22 -0700 Subject: [PATCH] While expression --- schala-lang/language/src/parser.rs | 23 ++++++++++++++++++++--- schala-lang/language/src/parsing/test.rs | 8 ++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index f0ee761..7267881 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -65,7 +65,7 @@ fn bool_literal(text: &str) -> ParseResult { value(true, tag("true")), value(false, tag("false")) )); - map(p, ExpressionKind::BoolLiteral)(text) + context("Bool literal", map(p, ExpressionKind::BoolLiteral))(text) } fn number_literal(text: &str) -> ParseResult { @@ -121,8 +121,7 @@ fn literal(text: &str) -> ParseResult { } fn paren_expr(text: &str) -> ParseResult { - use nom::character::complete::char; - context("Paren expression", delimited(char('('), expression_kind, char(')')))(text) + context("Paren expression", delimited(tag("("), ws(expression_kind), tag(")")))(text) } fn prefix_op(text: &str) -> ParseResult { @@ -148,12 +147,30 @@ fn primary_expr(text: &str) -> ParseResult { alt(( if_expr, for_expr, + while_expr, literal, paren_expr, identifier_expr, ))(text) } +fn while_expr(text: &str) -> ParseResult { + let p = preceded(tag("while"), tuple((ws(while_cond), ws(block)))); + let m = map(p, |(condition, body)| { + let condition = condition.map(Box::new); + ExpressionKind::WhileExpression {condition, body} + }); + context("While expression", m)(text) +} + +fn while_cond(text: &str) -> ParseResult> { + //TODO support is constructs? + context("While condition", + map(opt(ws(expression_kind)), + |maybe_expr_kind| maybe_expr_kind.map(|kind| Expression::new(ItemId::new(0), kind))) + )(text) +} + fn for_expr(text: &str) -> ParseResult { //TODO do I need something like no struct literal here? let en = alt(( diff --git a/schala-lang/language/src/parsing/test.rs b/schala-lang/language/src/parsing/test.rs index 0dc8475..e77de05 100644 --- a/schala-lang/language/src/parsing/test.rs +++ b/schala-lang/language/src/parsing/test.rs @@ -618,13 +618,13 @@ fn more_advanced_lambdas() { #[test] fn while_expr() { parse_test_wrap_ast! { - "while { }", - exst!(WhileExpression { condition: None, body: vec![] }) + "while { 3 }", + exst!(WhileExpression { condition: None, body: vec![ exst!(s "3")] }) } parse_test_wrap_ast! { - "while a == b { }", - exst!(WhileExpression { condition: Some(bx![ex![binexp!("==", val!("a"), val!("b"))]]), body: vec![] }) + "while a == b { 3 }", + exst!(WhileExpression { condition: Some(bx![ex![binexp!("==", val!("a"), val!("b"))]]), body: vec![ exst!(s "3")] }) } }