From 5cd18f220a42d3d4072566b6e0424c5edf692860 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 3 Mar 2019 00:37:16 +0100 Subject: Parse builtins --- dhall/src/core.rs | 50 ++++++++++++++++++++++++++++++-------------------- dhall/src/parser.rs | 11 +++++++++-- 2 files changed, 39 insertions(+), 22 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/core.rs b/dhall/src/core.rs index c5b6d86..88eff36 100644 --- a/dhall/src/core.rs +++ b/dhall/src/core.rs @@ -184,46 +184,26 @@ pub enum Expr<'i, S, A> { /// Built-ins #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Builtin { - /// `Bool ~ Bool` Bool, - /// `Natural ~ Natural` Natural, - /// `Integer ~ Integer` Integer, - /// `Double ~ Double` Double, - /// `Text ~ Text` Text, - /// `List ~ List` List, - /// `Optional ~ Optional` Optional, - /// `NaturalFold ~ Natural/fold` NaturalFold, - /// `NaturalBuild ~ Natural/build` NaturalBuild, - /// `NaturalIsZero ~ Natural/isZero` NaturalIsZero, - /// `NaturalEven ~ Natural/even` NaturalEven, - /// `NaturalOdd ~ Natural/odd` NaturalOdd, NaturalShow, - /// `ListBuild ~ List/build` ListBuild, - /// `ListFold ~ List/fold` ListFold, - /// `ListLength ~ List/length` ListLength, - /// `ListHead ~ List/head` ListHead, - /// `ListLast ~ List/last` ListLast, - /// `ListIndexed ~ List/indexed` ListIndexed, - /// `ListReverse ~ List/reverse` ListReverse, - /// `OptionalFold ~ Optional/fold` OptionalFold, } @@ -474,6 +454,36 @@ impl Display for Builtin { } } +impl Builtin { + pub fn parse(s: &str) -> Option { + use self::Builtin::*; + match s { + "Bool" => Some(Bool), + "Natural" => Some(Natural), + "Integer" => Some(Integer), + "Double" => Some(Double), + "Text" => Some(Text), + "List" => Some(List), + "Optional" => Some(Optional), + "Natural/fold" => Some(NaturalFold), + "Natural/build" => Some(NaturalBuild), + "Natural/isZero" => Some(NaturalIsZero), + "Natural/even" => Some(NaturalEven), + "Natural/odd" => Some(NaturalOdd), + "Natural/show" => Some(NaturalShow), + "List/build" => Some(ListBuild), + "List/fold" => Some(ListFold), + "List/length" => Some(ListLength), + "List/head" => Some(ListHead), + "List/last" => Some(ListLast), + "List/indexed" => Some(ListIndexed), + "List/reverse" => Some(ListReverse), + "Optional/fold" => Some(OptionalFold), + _ => None, + } + } +} + impl<'i> Display for V<'i> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let V(x, n) = *self; diff --git a/dhall/src/parser.rs b/dhall/src/parser.rs index 40272e0..7a4919e 100644 --- a/dhall/src/parser.rs +++ b/dhall/src/parser.rs @@ -3,7 +3,7 @@ use lalrpop_util; use crate::grammar; use crate::grammar_util::{BoxExpr, ParsedExpr}; use crate::lexer::{Lexer, LexicalError, Tok}; -use crate::core::{bx, Expr, V}; +use crate::core::{bx, Expr, Builtin, V}; pub type ParseError<'i> = lalrpop_util::ParseError, LexicalError>; @@ -127,7 +127,14 @@ fn parse_expression(pair: Pair) -> BoxExpr { Rule::identifier_raw => parse!(pair; (name: str, idx?: natural) => { - bx(Expr::Var(V(name, idx.unwrap_or(0)))) + match Builtin::parse(name) { + Some(b) => bx(Expr::Builtin(b)), + None => match name { + "True" => bx(Expr::BoolLit(true)), + "False" => bx(Expr::BoolLit(false)), + name => bx(Expr::Var(V(name, idx.unwrap_or(0)))), + } + } }), Rule::ifthenelse_expression => -- cgit v1.2.3