From b3f3f08c9631f5a124bc736178b15c5785aced3c Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Mon, 20 May 2019 22:02:24 -0400 Subject: [PATCH] Add Expr::Let --- src/expr.rs | 1 + src/parse.rs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/expr.rs b/src/expr.rs index d2dbe0a6eb..d2f0daff28 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -8,6 +8,7 @@ pub enum Expr { Char(char), Var(String), + Let(String, Box, Box), // Functions Func(String, Box), diff --git a/src/parse.rs b/src/parse.rs index 34e2f04a23..c24f44e5ac 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -66,6 +66,7 @@ parser! { number_literal(), char_literal(), if_expr(), + let_expr(), func_or_var(), )) .and( @@ -155,6 +156,25 @@ where I: Stream, )) } +pub fn let_expr() -> impl Parser +where I: Stream, + I::Error: ParseError +{ + ident() + .skip(spaces()) + .skip(char('=')) + .skip(spaces()) + .and(expr_body()) + // TODO check for indent in this expr, then parse a second expr post-outdent + .and(expr_body()) + .map(|((var_name, var_expr), in_expr)| { + match var_name { + Ok(str) => Expr::Let(str, Box::new(var_expr), Box::new(in_expr)), + Err(_ident_problem) => Expr::SyntaxProblem("TODO put _ident_problem here".to_owned()) + } + }) +} + pub fn func_or_var() -> impl Parser where I: Stream, I::Error: ParseError