From 0975bff6ddcf48de4072561adea67a5c1cd4456f Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 14 Aug 2025 12:17:41 +0300 Subject: fix: step scrutinee of case & can instantiate empty vec --- src/sexp/step.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/sexp/step.rs') diff --git a/src/sexp/step.rs b/src/sexp/step.rs index 97675fa..bae47a5 100644 --- a/src/sexp/step.rs +++ b/src/sexp/step.rs @@ -286,6 +286,20 @@ impl SExp { /// ); /// ``` /// + /// **Pattern matching** + /// ```rust + /// use myslip::{sexp::{SExp::*, SLeaf::*, util::*}, r#type::{Type::*, util::*}}; + /// use myslip::parse::parsetree::parse_to_ast; + /// + /// let exp = "case (+ 1 2) (3 true) (_ false)"; + /// let exp = parse_to_ast(exp); + /// let exp = exp.and_then(|e| e.step()); + /// let expshould = parse_to_ast("case 3 (3 true) (_ false)"); + /// assert_eq!(exp, expshould); + /// let exp = exp.and_then(|e| e.step()); + /// assert_eq!(exp, Ok(Atom(True))); + /// ``` + /// /// Shadowing: /// ```rust /// use myslip::sexp::{SExp::*, SLeaf::*, util::*}; @@ -352,6 +366,10 @@ impl SExp { // case expressions SCons(op, l) if scons(op.clone(), l.clone()).check_case().is_some() => { let (scrutinee, patarms) = scons(op, l).check_case().unwrap(); + if !scrutinee.is_value() { + return Ok(SExp::back_to_case(scrutinee.step()?, patarms)); + // return scons(Case, scons(scrutinee.step()?)) + } let scrutinee = match scrutinee { SCons(q, v) if *q == Atom(Quote) || *q == Atom(Vector) => *v, @@ -627,6 +645,11 @@ impl SExp { } }, + // Nil in list + SCons(op, l) if *op == Atom(Nil) => { + Ok(scons(Vector, Nil)) + }, + // Print SCons(op, l) if *op == Atom(Print) => { println!("{}", *l); -- cgit v1.2.3