From 91e7dcb6d0c672879a7fd0040e58bb142e591e93 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 15 Mar 2020 22:10:38 -0700 Subject: [PATCH] Lambdas --- schala-lang/language/src/parser.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index 7267881..2a2bb62 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -150,10 +150,27 @@ fn primary_expr(text: &str) -> ParseResult { while_expr, literal, paren_expr, + lambda_expr, identifier_expr, ))(text) } +fn lambda_expr(text: &str) -> ParseResult { + let p = preceded(ws(tag("\\")), + tuple((ws(lambda_param_list), ws(opt(type_anno)), ws(block)))); + context("Lambda expression", + map(p, |(params, type_anno, body)| ExpressionKind::Lambda { params, type_anno, body }) + )(text) +} + +fn lambda_param_list(text: &str) -> ParseResult> { + alt(( + map(formal_param, |x| vec![x]), + formal_params + ))(text) +} + + fn while_expr(text: &str) -> ParseResult { let p = preceded(tag("while"), tuple((ws(while_cond), ws(block)))); let m = map(p, |(condition, body)| {