From d6d1ec80ffcc0b13234b170a91b920371078a027 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 10 Aug 2025 19:16:51 +0300 Subject: Added tests for functions --- src/sexp/display.rs | 1 + src/sexp/mod.rs | 10 ++++++++++ src/sexp/step.rs | 16 ++++++++++++++++ 3 files changed, 27 insertions(+) (limited to 'src/sexp') diff --git a/src/sexp/display.rs b/src/sexp/display.rs index 5d78e93..eff3904 100644 --- a/src/sexp/display.rs +++ b/src/sexp/display.rs @@ -29,6 +29,7 @@ impl fmt::Display for SLeaf { Vector => "vector".to_string(), Print => "print".to_string(), Let => "let".to_string(), + Fun => "fn".to_string(), Ty(t) => t.to_string(), Arr => "->".to_string(), Nil => "()".to_string(), diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs index 11c185f..4bb4fa2 100644 --- a/src/sexp/mod.rs +++ b/src/sexp/mod.rs @@ -35,6 +35,8 @@ pub enum SLeaf { Let, + Fun, + Ty(Type), Arr, @@ -75,6 +77,7 @@ impl SExp { pub fn is_value(&self) -> bool { match self { SCons(a, b) => + scons(a.clone(), b.clone()).is_fun() || SCons(a.clone(), b.clone()).check_let().is_some() || ( (**a == Atom(Quote) || **a == Atom(Vector) || (**a).is_type_lit()) @@ -102,4 +105,11 @@ impl SExp { _ => false } } + + pub fn is_fun(&self) -> bool { + match self { + SCons(a, _) if **a == Atom(Fun) => true, + _ => false + } + } } diff --git a/src/sexp/step.rs b/src/sexp/step.rs index c26dce9..8cc1e64 100644 --- a/src/sexp/step.rs +++ b/src/sexp/step.rs @@ -270,6 +270,22 @@ impl SExp { /// ); /// ``` /// + /// **Functions** + /// ```rust + /// use myslip::{sexp::{SExp::*, SLeaf::*, util::*}, r#type::{Type::*, util::*}}; + /// + /// let varlist = scons(var("a"), scons(var("b"), Nil)); + /// let typelist = scons(Ty(List(vec![Integer])), Nil); + /// let ret = Atom(Ty(Boolean)); + /// let body = scons(Gt, varlist.clone()); + /// let fun = scons(Fun, scons(varlist, scons(typelist, scons(ret, scons(body, Nil))))); + /// let args = scons(2, scons(1, Nil)); + /// assert_eq!( + /// scons(fun, scons(args, Nil)).multistep(), + /// Ok(Atom(True)) + /// ); + /// ``` + /// /// Shadowing: /// ```rust /// use myslip::sexp::{SExp::*, SLeaf::*, util::*}; -- cgit v1.2.3