From 44cebec81881b71ef3c5f6fffe78ab9f7eb10092 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Thu, 18 Nov 2021 22:12:13 -0800 Subject: [PATCH] For expr --- schala-lang/src/parsing/combinator.rs | 36 +++++++++++++++++++++++++++ schala-lang/src/parsing/test.rs | 4 +-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 1d031f9..ba910e5 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -448,6 +448,7 @@ fn primary_expr_no_struct(input: Span) -> ParseResult { "primary-expr-no-struct", alt(( while_expr, + for_expr, list_expr, paren_expr, string_literal, @@ -481,6 +482,41 @@ fn while_expr(input: Span) -> ParseResult { })(input) } +fn for_expr(input: Span) -> ParseResult { + println!("IN FOR EXPR: {}", input.fragment()); + fn for_enumerators(input: Span) -> ParseResult> { + println!("NEUMS: {}", input.fragment()); + alt(( + delimited(tok(char('{')), separated_list0(tok(char(',')), enumerator), tok(char('}'))), + map(enumerator, |enumerator| vec![enumerator]), + ))(input) + } + + //TODO add guards, etc. + fn enumerator(input: Span) -> ParseResult { + alt(( + map(separated_pair(tok(identifier), kw("<-"), expression_no_struct), |(ident, generator)| { + Enumerator { id: rc_string(ident.fragment()), generator } + }), + //TODO distinguish these two cases in AST + map(separated_pair(tok(identifier), kw("="), expression_no_struct), |(ident, generator)| { + Enumerator { id: rc_string(ident.fragment()), generator } + }), + ))(input) + } + + fn for_body(input: Span) -> ParseResult> { + alt(( + preceded(kw("return"), map(expression_no_struct, |expr| Box::new(ForBody::MonadicReturn(expr)))), + map(block, |body| Box::new(ForBody::StatementBlock(body))), + ))(input) + } + + map(preceded(kw("for"), pair(for_enumerators, for_body)), |(enumerators, body)| { + ExpressionKind::ForExpression { enumerators, 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 03ec6dc..0d608eb 100644 --- a/schala-lang/src/parsing/test.rs +++ b/schala-lang/src/parsing/test.rs @@ -389,7 +389,7 @@ fn while_expression() { fn for_expression() { use ExpressionKind::*; - assert_expr!( + assert_expr_comb!( "for { a <- garodzny::maybeValue } return 1", expr(ForExpression { enumerators: vec![Enumerator { id: rc("a"), generator: expr(Value(qn!(garodzny, maybeValue))) }], @@ -397,7 +397,7 @@ fn for_expression() { }) ); - assert_expr!( + assert_expr_comb!( "for n <- someRange { f(n) ; }", expr(ForExpression { enumerators: vec![Enumerator { id: rc("n"), generator: expr(Value(qn!(someRange))) }],