aboutsummaryrefslogtreecommitdiff
path: root/src/sexp
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-02 17:53:09 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-02 17:53:09 +0300
commit9121a0b782d2cd6551a393f1d3a79c7b092e4873 (patch)
treedea5e6644131d76b4456296d30f60d9c846af919 /src/sexp
parent0f9542109275de75641185d4d94dbe7c35a49088 (diff)
downloadmyslip-9121a0b782d2cd6551a393f1d3a79c7b092e4873.tar.gz
myslip-9121a0b782d2cd6551a393f1d3a79c7b092e4873.zip
Added tests for type_check. Implemented std::fmt::Display for many enums. Added type variants List(Type), and UndefinedType for use in error messages. Implemented type utility arr(a, b).
Diffstat (limited to 'src/sexp')
-rw-r--r--src/sexp/display.rs45
-rw-r--r--src/sexp/mod.rs2
2 files changed, 47 insertions, 0 deletions
diff --git a/src/sexp/display.rs b/src/sexp/display.rs
new file mode 100644
index 0000000..e6dbb7c
--- /dev/null
+++ b/src/sexp/display.rs
@@ -0,0 +1,45 @@
+
+use std::fmt;
+use crate::sexp::{SLeaf, SExp, SLeaf::*, SExp::*, util::*};
+
+
+impl fmt::Display for SLeaf {
+
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}", match self {
+ Add => "+".to_string(),
+ Sub => "-".to_string(),
+ Mul => "*".to_string(),
+ Div => "/".to_string(),
+ Int(x) => x.to_string(),
+ Var(s) => s.to_string(),
+ Quote => "quote".to_string(),
+ Nil => "()".to_string(),
+ })
+ }
+
+}
+
+
+impl fmt::Display for SExp {
+
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Atom(leaf) => write!(f, "{}", leaf.to_string()),
+ SCons(a, b) => {
+ match scons(a.clone(), b.clone()).into_vec() {
+ Ok(l) => write!(
+ f,
+ "({})",
+ l.into_iter()
+ .map(|x| x.to_string())
+ .collect::<Vec<String>>()
+ .join(" ")
+ ),
+ Err(_) => write!(f, "({} {})", *a, *b),
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs
index 71e62e9..0fb5e25 100644
--- a/src/sexp/mod.rs
+++ b/src/sexp/mod.rs
@@ -2,6 +2,7 @@
pub mod step;
pub mod util;
pub mod subst;
+pub mod display;
/// A leaf node for S-Expressions.
///
@@ -36,6 +37,7 @@ pub enum SExp {
use SExp::*;
use SLeaf::*;
+
impl SExp {
pub fn is_value(&self) -> bool {
match self {