From b6a60a05ba42185eda880d94deafedbffda74f26 Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 9 Mar 2020 04:13:45 -0700 Subject: [PATCH] Starting for expr --- schala-lang/language/src/parser.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index aef1122..7747f13 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -149,12 +149,41 @@ fn primary_expr(text: &str) -> ParseResult { alt(( if_expr, + for_expr, literal, paren_expr, identifier_expr, ))(text) } +fn for_expr(text: &str) -> ParseResult { + //TODO do I need something like no struct literal here? + let en = alt(( + map(enumerator, |e| vec![e]), + delimited(tag("{"), enumerators, tag("}")) + )); + preceded(tag("for"), + map(tuple((en, for_expr_body)), + |(enumerators, body)| ExpressionKind::ForExpression { enumerators, body: Box::new(body) } + ))(text) +} + +fn enumerators(text: &str) -> ParseResult> { + separated_nonempty_list(alt((value((), tag(",")), statement_sep)), + enumerator)(text) +} + +fn enumerator(text: &str) -> ParseResult { + map( + tuple((identifier, tag("<-"), expression)), + |(id, _, generator)| Enumerator { id, generator } + )(text) +} + +fn for_expr_body(text: &str) -> ParseResult { + unimplemented!() +} + fn invocation_argument(text: &str) -> ParseResult { use nom::character::complete::char; alt((