summaryrefslogtreecommitdiff
path: root/src/chomp/ast/mod.rs
diff options
context:
space:
mode:
authorGreg Brown <gmb60@cam.ac.uk>2021-04-26 11:14:21 +0100
committerGreg Brown <gmb60@cam.ac.uk>2021-04-30 14:45:12 +0100
commitef485d6f3e4df6e1a424ba3797388fa0bba6eb2e (patch)
tree33d6cd11b21791e5727f29a428051578d3ab17fc /src/chomp/ast/mod.rs
parentbf46a471fb268f7c0798a179740b295f8aaa1a31 (diff)
Replace substitution with translation.
Diffstat (limited to 'src/chomp/ast/mod.rs')
-rw-r--r--src/chomp/ast/mod.rs36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/chomp/ast/mod.rs b/src/chomp/ast/mod.rs
index d833da4..232381a 100644
--- a/src/chomp/ast/mod.rs
+++ b/src/chomp/ast/mod.rs
@@ -116,16 +116,20 @@ impl Display for Call {
/// A function abstraction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Lambda {
- pub first: Name,
- pub rest: Vec<Name>,
+ pub args: Vec<Name>,
pub inner: Box<NamedExpression>,
}
impl Display for Lambda {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- write!(f, "/{}", self.first)?;
- for name in self.rest {
- write!(f, ", {}", name)?;
+ write!(f, "/")?;
+ let mut iter = self.args.iter();
+ if let Some(arg) = iter.next() {
+ write!(f, "{}", arg)?;
+
+ for arg in iter {
+ write!(f, ", {}", arg)?;
+ }
}
write!(f, "/ {}", self.inner)
}
@@ -151,6 +155,16 @@ pub enum Expression {
Lambda(Lambda),
}
+impl Expression {
+ pub fn try_into_lambda(self) -> Result<Lambda, Self> {
+ if let Self::Lambda(l) = self {
+ Ok(l)
+ } else {
+ Err(self)
+ }
+ }
+}
+
impl Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -297,23 +311,15 @@ impl PartialEq for NamedExpression {
impl Eq for NamedExpression {}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Let {
pub name: Name,
pub val: NamedExpression,
pub inner: Box<TopLevel>,
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, Eq, PartialEq)]
pub enum TopLevel {
Let(Let),
Goal(NamedExpression),
}
-
-impl PartialEq for Function {
- fn eq(&self, other: &Self) -> bool {
- self.name == other.name && self.params == other.params && self.expr == other.expr
- }
-}
-
-impl Eq for Function {}