From 7374d0524ccd53b256107667b213597c05720d2d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 31 Mar 2019 19:08:08 +0200 Subject: Move recursion out of Expr --- dhall_core/src/core.rs | 76 +++++++++----------- dhall_core/src/parser.rs | 172 +++++++++++++++++++++++----------------------- dhall_core/src/printer.rs | 8 +-- dhall_core/src/text.rs | 37 +++++----- 4 files changed, 141 insertions(+), 152 deletions(-) (limited to 'dhall_core/src') diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index b49515a..2d11068 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -173,43 +173,39 @@ pub type DhallExpr = ResolvedExpr; #[derive(Debug, PartialEq, Eq)] pub struct SubExpr(pub Rc>); +pub type Expr = ExprF, Note, Embed>; + /// Syntax tree for expressions +// Having the recursion out of the enum definition enables writing +// much more generic code and improves pattern-matching behind +// smart pointers. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum Expr { +pub enum ExprF { /// `Const c ~ c` Const(Const), /// `Var (V x 0) ~ x`
/// `Var (V x n) ~ x@n` Var(V), /// `Lam x A b ~ λ(x : A) -> b` - Lam(Label, SubExpr, SubExpr), + Lam(Label, SubExpr, SubExpr), /// `Pi "_" A B ~ A -> B` /// `Pi x A B ~ ∀(x : A) -> B` - Pi(Label, SubExpr, SubExpr), + Pi(Label, SubExpr, SubExpr), /// `App f A ~ f A` - App(SubExpr, Vec>), + App(SubExpr, Vec), /// `Let x Nothing r e ~ let x = r in e` /// `Let x (Just t) r e ~ let x : t = r in e` - Let( - Label, - Option>, - SubExpr, - SubExpr, - ), + Let(Label, Option, SubExpr, SubExpr), /// `Annot x t ~ x : t` - Annot(SubExpr, SubExpr), + Annot(SubExpr, SubExpr), /// Built-in values Builtin(Builtin), // Binary operations - BinOp(BinOp, SubExpr, SubExpr), + BinOp(BinOp, SubExpr, SubExpr), /// `BoolLit b ~ b` BoolLit(bool), /// `BoolIf x y z ~ if x then y else z` - BoolIf( - SubExpr, - SubExpr, - SubExpr, - ), + BoolIf(SubExpr, SubExpr, SubExpr), /// `NaturalLit n ~ +n` NaturalLit(Natural), /// `IntegerLit n ~ n` @@ -217,39 +213,31 @@ pub enum Expr { /// `DoubleLit n ~ n` DoubleLit(Double), /// `TextLit t ~ t` - TextLit(InterpolatedText), + TextLit(InterpolatedText), /// [] : List t` - EmptyListLit(SubExpr), + EmptyListLit(SubExpr), /// [x, y, z] - NEListLit(Vec>), + NEListLit(Vec), /// None t - EmptyOptionalLit(SubExpr), + EmptyOptionalLit(SubExpr), /// Some e - NEOptionalLit(SubExpr), + NEOptionalLit(SubExpr), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - RecordType(BTreeMap>), + RecordType(BTreeMap), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap>), + RecordLit(BTreeMap), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - UnionType(BTreeMap>), + UnionType(BTreeMap), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` - UnionLit( - Label, - SubExpr, - BTreeMap>, - ), + UnionLit(Label, SubExpr, BTreeMap), /// `Merge x y t ~ merge x y : t` - Merge( - SubExpr, - SubExpr, - Option>, - ), + Merge(SubExpr, SubExpr, Option), /// e.x - Field(SubExpr, Label), + Field(SubExpr, Label), /// e.{ x, y, z } - Projection(SubExpr, Vec