Browse Source

add support for parsing lambdas

Getty Ritter 3 years ago
parent
commit
9446bf9286
4 changed files with 125 additions and 1 deletions
  1. 19 1
      src/grammar.lalrpop
  2. 3 0
      src/lexer.rs
  3. 4 0
      tests/exprs.matzo
  4. 99 0
      tests/exprs.parsed

+ 19 - 1
src/grammar.lalrpop

@@ -20,6 +20,7 @@ extern {
         ";" => Token::Semi,
         "." => Token::Dot,
         ".." => Token::DotDot,
+        "=>" => Token::Arrow,
         ":=" => Token::Assn,
         "::=" => Token::LitAssn,
         "puts" => Token::Puts,
@@ -92,11 +93,28 @@ pub Leaf: Expr = {
         es.push(e);
         Expr::Tup(es)
     },
-    "let" <name:Name> ":=" <e1:Expr> "{" <e2:Expr> "}" =>
+    "let" <name:Name> ":=" <e1:Expr> "in" "{" <e2:Expr> "}" =>
         Expr::Let(name, Box::new(e1), Box::new(e2)),
+    "{" <mut cs:(<Case> ";")*> <c:Case> "}" => {
+        cs.push(c);
+        Expr::Fun(cs)
+    },
     "(" <e:Expr> ")" => e,
 };
 
+pub Case: Case = {
+    <pat:Pat> "=>" <expr:Expr> => Case { pat, expr },
+};
+
+pub Pat: Pat = {
+    <Literal> => Pat::Lit(<>),
+    <Name> => Pat::Var(<>),
+    "<" <mut ps:(<Pat> ",")*> <p:Pat> ">" => {
+        ps.push(p);
+        Pat::Tup(ps)
+    },
+};
+
 pub Literal: Literal = {
     "num" => Literal::Num(<>),
     "str" => Literal::Str(<>),

+ 3 - 0
src/lexer.rs

@@ -60,6 +60,9 @@ pub enum Token<'a> {
     #[token("..")]
     DotDot,
 
+    #[token("=>")]
+    Arrow,
+
     #[token(":=")]
     Assn,
 

+ 4 - 0
tests/exprs.matzo

@@ -21,3 +21,7 @@ puts x..y;
 
 (* application *)
 puts f.x;
+
+(* lambdas *)
+puts { x => x };
+puts { True => "yes" ; False => "no" };

+ 99 - 0
tests/exprs.parsed

@@ -368,4 +368,103 @@
             ],
         ),
     ),
+    Puts(
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Fun(
+                                [
+                                    Case {
+                                        pat: Var(
+                                            "x",
+                                        ),
+                                        expr: Chc(
+                                            [
+                                                Choice {
+                                                    weight: None,
+                                                    value: Cat(
+                                                        [
+                                                            Var(
+                                                                "x",
+                                                            ),
+                                                        ],
+                                                    ),
+                                                },
+                                            ],
+                                        ),
+                                    },
+                                ],
+                            ),
+                        ],
+                    ),
+                },
+            ],
+        ),
+    ),
+    Puts(
+        Chc(
+            [
+                Choice {
+                    weight: None,
+                    value: Cat(
+                        [
+                            Fun(
+                                [
+                                    Case {
+                                        pat: Lit(
+                                            Atom(
+                                                "True",
+                                            ),
+                                        ),
+                                        expr: Chc(
+                                            [
+                                                Choice {
+                                                    weight: None,
+                                                    value: Cat(
+                                                        [
+                                                            Lit(
+                                                                Str(
+                                                                    "yes",
+                                                                ),
+                                                            ),
+                                                        ],
+                                                    ),
+                                                },
+                                            ],
+                                        ),
+                                    },
+                                    Case {
+                                        pat: Lit(
+                                            Atom(
+                                                "False",
+                                            ),
+                                        ),
+                                        expr: Chc(
+                                            [
+                                                Choice {
+                                                    weight: None,
+                                                    value: Cat(
+                                                        [
+                                                            Lit(
+                                                                Str(
+                                                                    "no",
+                                                                ),
+                                                            ),
+                                                        ],
+                                                    ),
+                                                },
+                                            ],
+                                        ),
+                                    },
+                                ],
+                            ),
+                        ],
+                    ),
+                },
+            ],
+        ),
+    ),
 ]