From 318f2008e708ea8fc2fde69525fd8ef530e68cc7 Mon Sep 17 00:00:00 2001 From: Arnaud Bailly Date: Wed, 9 Oct 2024 15:34:54 +0200 Subject: Fix tests and app spines representations --- rust/src/ast.rs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'rust/src/ast.rs') diff --git a/rust/src/ast.rs b/rust/src/ast.rs index cfc0e40..d0f1d6f 100644 --- a/rust/src/ast.rs +++ b/rust/src/ast.rs @@ -16,18 +16,36 @@ pub enum Value { Let(String, Box, Box), } +use Value::*; + +impl Value { + /// Return the spine of an application + fn spine(&self) -> Vec { + match self { + App(l, r) => { + let mut spine = l.spine(); + spine.push(*r.clone()); + spine + } + _ => vec![self.clone()], + } + } +} + impl Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Value::Num(i) => write!(f, "{}", i), Value::Bool(b) => write!(f, "{}", b), Value::Sym(s) => write!(f, "{}", s), - Value::App(l, r) => { - if let App(l1, l2) = *l.clone() { - write!(f, "({} {} {})", l1, l2, r) - } else { - write!(f, "({} {})", l, r) - } + Value::App(_, _) => { + let app = self + .spine() + .iter() + .map(|v| v.to_string()) + .collect::>() + .join(" "); + write!(f, "({})", app) } Value::Lam(var, body) => write!(f, "(lam {} {})", var, body), Value::Def(var, value) => write!(f, "(def {} {})", var, value), @@ -36,8 +54,6 @@ impl Display for Value { } } -use Value::*; - pub const IDENTIFIER: &str = "\\pL(\\pL|\\pN)*"; pub fn identifier() -> RegexGeneratorStrategy { @@ -81,10 +97,7 @@ impl Arbitrary for Value { #[cfg(test)] mod ast_tests { - use super::{ - identifier, - Value::{self, *}, - }; + use super::Value::{self, *}; use proptest::collection::vec; use proptest::prelude::*; -- cgit v1.2.3