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 | |
| parent | 2d64cfbafc81cc92bcca3fe758391fb94e5fea9f (diff) | |
| download | lambda-nantes-318f2008e708ea8fc2fde69525fd8ef530e68cc7.tar.gz | |
Fix tests and app spines representations
| -rw-r--r-- | rust/src/ast.rs | 37 | ||||
| -rw-r--r-- | rust/src/web.rs | 11 |
2 files changed, 32 insertions, 16 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::*; diff --git a/rust/src/web.rs b/rust/src/web.rs index f676b3c..6bba34e 100644 --- a/rust/src/web.rs +++ b/rust/src/web.rs @@ -485,7 +485,11 @@ mod app_tests { let (input, _) = client.generate_expr(); - assert!(input.split(' ').count() > 2); + let parsed = parse(&input); + match &parsed[..] { + [Value::App(_, _)] => assert!(input.split(' ').count() >= 2), + _ => (), + } } #[test] @@ -496,9 +500,8 @@ mod app_tests { let (input, _) = client.generate_expr(); let parsed = parse(&input); - match &parsed[..] { - _ => panic!("Expected term, got {:?}", parsed), - } + + assert!(!parsed.is_empty()); } #[test] |
