diff options
| author | Arnaud Bailly <arnaud@pankzsoft.com> | 2024-10-09 15:34:54 +0200 |
|---|---|---|
| committer | Arnaud Bailly <arnaud@pankzsoft.com> | 2024-10-09 15:35:13 +0200 |
| commit | 318f2008e708ea8fc2fde69525fd8ef530e68cc7 (patch) | |
| tree | 5f3ce24364716804e473df419ed38cd58f40bc58 /rust/src/ast.rs | |
| parent | 2d64cfbafc81cc92bcca3fe758391fb94e5fea9f (diff) | |
| download | lambda-nantes-318f2008e708ea8fc2fde69525fd8ef530e68cc7.tar.gz | |
Fix tests and app spines representations
Diffstat (limited to 'rust/src/ast.rs')
| -rw-r--r-- | rust/src/ast.rs | 37 |
1 files changed, 25 insertions, 12 deletions
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<Value>, Box<Value>), } +use Value::*; + +impl Value { + /// Return the spine of an application + fn spine(&self) -> Vec<Value> { + 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::<Vec<String>>() + .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<String> { @@ -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::*; |
