summaryrefslogtreecommitdiff
path: root/dhall/src
diff options
context:
space:
mode:
authorNadrieril2020-03-13 20:35:42 +0000
committerNadrieril2020-03-31 21:44:01 +0100
commit6a2a407f27b8068769f208383119ac855f629e96 (patch)
treed0aaad7611e081ea7de9fd69cc78577d235ecd10 /dhall/src
parentb3a05d930dd8f2bbc705145219025af96e5dec86 (diff)
Move conversion to SimpleVal/Ty to simple module
Diffstat (limited to '')
-rw-r--r--dhall/src/lib.rs4
-rw-r--r--dhall/src/semantics/nze/nir.rs81
-rw-r--r--dhall/src/simple.rs83
3 files changed, 84 insertions, 84 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs
index 799d86f..07df2b2 100644
--- a/dhall/src/lib.rs
+++ b/dhall/src/lib.rs
@@ -132,8 +132,8 @@ impl Normalized {
pub fn to_value(&self) -> Value {
Value {
hir: self.to_hir(),
- as_simple_val: self.0.to_simple_value(),
- as_simple_ty: self.0.to_simple_type(),
+ as_simple_val: SimpleValue::from_nir(&self.0),
+ as_simple_ty: SimpleType::from_nir(&self.0),
}
}
diff --git a/dhall/src/semantics/nze/nir.rs b/dhall/src/semantics/nze/nir.rs
index 7365463..1148e31 100644
--- a/dhall/src/semantics/nze/nir.rs
+++ b/dhall/src/semantics/nze/nir.rs
@@ -11,7 +11,6 @@ use crate::syntax::{
NumKind, Span,
};
use crate::ToExprOptions;
-use crate::{STyKind, SValKind, SimpleType, SimpleValue};
/// Stores a possibly unevaluated value. Gets (partially) normalized on-demand, sharing computation
/// automatically. Uses a Rc<RefCell> to share computation.
@@ -143,86 +142,6 @@ impl Nir {
pub(crate) fn to_expr_tyenv(&self, tyenv: &TyEnv) -> Expr {
self.to_hir(tyenv.as_varenv()).to_expr_tyenv(tyenv)
}
- pub(crate) fn to_simple_value(&self) -> Option<SimpleValue> {
- Some(SimpleValue::new(match self.kind() {
- NirKind::Num(lit) => SValKind::Num(lit.clone()),
- NirKind::TextLit(x) => SValKind::Text(
- x.as_text()
- .expect("Normal form should ensure the text is a string"),
- ),
- NirKind::EmptyOptionalLit(_) => SValKind::Optional(None),
- NirKind::NEOptionalLit(x) => {
- SValKind::Optional(Some(x.to_simple_value()?))
- }
- NirKind::EmptyListLit(_) => SValKind::List(vec![]),
- NirKind::NEListLit(xs) => SValKind::List(
- xs.iter()
- .map(|v| v.to_simple_value())
- .collect::<Option<_>>()?,
- ),
- NirKind::RecordLit(kvs) => SValKind::Record(
- kvs.iter()
- .map(|(k, v)| Some((k.into(), v.to_simple_value()?)))
- .collect::<Option<_>>()?,
- ),
- NirKind::UnionLit(field, x, _) => {
- SValKind::Union(field.into(), Some(x.to_simple_value()?))
- }
- NirKind::UnionConstructor(field, ty)
- if ty.get(field).map(|f| f.is_some()) == Some(false) =>
- {
- SValKind::Union(field.into(), None)
- }
- _ => return None,
- }))
- }
- pub(crate) fn to_simple_type(&self) -> Option<SimpleType> {
- Some(SimpleType::new(match self.kind() {
- NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. })
- if args.is_empty() =>
- {
- match b {
- Builtin::Bool => STyKind::Bool,
- Builtin::Natural => STyKind::Natural,
- Builtin::Integer => STyKind::Integer,
- Builtin::Double => STyKind::Double,
- Builtin::Text => STyKind::Text,
- _ => return None,
- }
- }
- NirKind::AppliedBuiltin(BuiltinClosure {
- b: Builtin::Optional,
- args,
- ..
- }) if args.len() == 1 => {
- STyKind::Optional(args[0].to_simple_type()?)
- }
- NirKind::AppliedBuiltin(BuiltinClosure {
- b: Builtin::List,
- args,
- ..
- }) if args.len() == 1 => STyKind::List(args[0].to_simple_type()?),
- NirKind::RecordType(kts) => STyKind::Record(
- kts.iter()
- .map(|(k, v)| Some((k.into(), v.to_simple_type()?)))
- .collect::<Option<_>>()?,
- ),
- NirKind::UnionType(kts) => STyKind::Union(
- kts.iter()
- .map(|(k, v)| {
- Some((
- k.into(),
- v.as_ref()
- .map(|v| Ok(v.to_simple_type()?))
- .transpose()?,
- ))
- })
- .collect::<Option<_>>()?,
- ),
- _ => return None,
- }))
- }
-
pub(crate) fn normalize(&self) {
self.0.normalize()
}
diff --git a/dhall/src/simple.rs b/dhall/src/simple.rs
index 32c9c79..2e5d3c4 100644
--- a/dhall/src/simple.rs
+++ b/dhall/src/simple.rs
@@ -1,6 +1,6 @@
use std::collections::BTreeMap;
-use crate::semantics::{Hir, HirKind};
+use crate::semantics::{BuiltinClosure, Hir, HirKind, Nir, NirKind};
use crate::syntax::{Builtin, ExprKind, NumKind, Span};
use crate::Value;
@@ -43,6 +43,40 @@ impl SimpleValue {
kind: Box::new(kind),
}
}
+ pub(crate) fn from_nir(nir: &Nir) -> Option<Self> {
+ Some(SimpleValue::new(match nir.kind() {
+ NirKind::Num(lit) => SValKind::Num(lit.clone()),
+ NirKind::TextLit(x) => SValKind::Text(
+ x.as_text()
+ .expect("Normal form should ensure the text is a string"),
+ ),
+ NirKind::EmptyOptionalLit(_) => SValKind::Optional(None),
+ NirKind::NEOptionalLit(x) => {
+ SValKind::Optional(Some(Self::from_nir(x)?))
+ }
+ NirKind::EmptyListLit(_) => SValKind::List(vec![]),
+ NirKind::NEListLit(xs) => SValKind::List(
+ xs.iter()
+ .map(|v| Self::from_nir(v))
+ .collect::<Option<_>>()?,
+ ),
+ NirKind::RecordLit(kvs) => SValKind::Record(
+ kvs.iter()
+ .map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
+ .collect::<Option<_>>()?,
+ ),
+ NirKind::UnionLit(field, x, _) => {
+ SValKind::Union(field.into(), Some(Self::from_nir(x)?))
+ }
+ NirKind::UnionConstructor(field, ty)
+ if ty.get(field).map(|f| f.is_some()) == Some(false) =>
+ {
+ SValKind::Union(field.into(), None)
+ }
+ _ => return None,
+ }))
+ }
+
pub fn kind(&self) -> &SValKind {
self.kind.as_ref()
}
@@ -54,6 +88,53 @@ impl SimpleType {
kind: Box::new(kind),
}
}
+ pub(crate) fn from_nir(nir: &Nir) -> Option<Self> {
+ Some(SimpleType::new(match nir.kind() {
+ NirKind::AppliedBuiltin(BuiltinClosure { b, args, .. })
+ if args.is_empty() =>
+ {
+ match b {
+ Builtin::Bool => STyKind::Bool,
+ Builtin::Natural => STyKind::Natural,
+ Builtin::Integer => STyKind::Integer,
+ Builtin::Double => STyKind::Double,
+ Builtin::Text => STyKind::Text,
+ _ => return None,
+ }
+ }
+ NirKind::AppliedBuiltin(BuiltinClosure {
+ b: Builtin::Optional,
+ args,
+ ..
+ }) if args.len() == 1 => {
+ STyKind::Optional(Self::from_nir(&args[0])?)
+ }
+ NirKind::AppliedBuiltin(BuiltinClosure {
+ b: Builtin::List,
+ args,
+ ..
+ }) if args.len() == 1 => STyKind::List(Self::from_nir(&args[0])?),
+ NirKind::RecordType(kts) => STyKind::Record(
+ kts.iter()
+ .map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
+ .collect::<Option<_>>()?,
+ ),
+ NirKind::UnionType(kts) => STyKind::Union(
+ kts.iter()
+ .map(|(k, v)| {
+ Some((
+ k.into(),
+ v.as_ref()
+ .map(|v| Ok(Self::from_nir(v)?))
+ .transpose()?,
+ ))
+ })
+ .collect::<Option<_>>()?,
+ ),
+ _ => return None,
+ }))
+ }
+
pub fn kind(&self) -> &STyKind {
self.kind.as_ref()
}