diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-19 13:55:05 +0300 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2025-08-19 13:55:05 +0300 |
commit | a70dcaa949f41c585f9aea5e79f2550053d8e857 (patch) | |
tree | 9366f0b1e026c009708ebc243d006a79cbf452c9 /src/sexp | |
parent | d4d6e972650370d529363f7db3946e58bdbd7bca (diff) | |
download | myslip-a70dcaa949f41c585f9aea5e79f2550053d8e857.tar.gz myslip-a70dcaa949f41c585f9aea5e79f2550053d8e857.zip |
test: added boilerplate and tests for coproduct parsing, type checking and evaluation
Diffstat (limited to 'src/sexp')
-rw-r--r-- | src/sexp/case.rs | 12 | ||||
-rw-r--r-- | src/sexp/display.rs | 3 | ||||
-rw-r--r-- | src/sexp/mod.rs | 3 | ||||
-rw-r--r-- | src/sexp/step.rs | 8 |
4 files changed, 26 insertions, 0 deletions
diff --git a/src/sexp/case.rs b/src/sexp/case.rs index 4c2c7ba..cc643f3 100644 --- a/src/sexp/case.rs +++ b/src/sexp/case.rs @@ -52,6 +52,18 @@ impl SExp { * ]) * ); * ``` + * + * Coproducts must be handled individually: + * ```rust + * use myslip::r#type::Type::*; + * use myslip::sexp::{SExp::*, SLeaf::*, util::*}; + * + * assert_eq!( + * scons(Coprod, scons(Ty(Boolean), scons(2, Nil))) + * .matches_pat(&scons(Inr, scons(var("x"), Nil))), + * Some(vec![(Var("x".to_string()), Atom(Int(2)))]) + * ); + * ``` */ pub fn matches_pat(&self, pat: &SExp) -> Option<Vec<(SLeaf, SExp)>> { match (self, pat) { diff --git a/src/sexp/display.rs b/src/sexp/display.rs index ec1b421..703bda3 100644 --- a/src/sexp/display.rs +++ b/src/sexp/display.rs @@ -28,6 +28,9 @@ impl fmt::Display for SLeaf { RestPat(s) => format!("..{s}"), Quote => "quote".to_string(), Vector => "vector".to_string(), + Coprod => "coprod".to_string(), + Inl => "inl".to_string(), + Inr => "inr".to_string(), Concat => "<>".to_string(), Print => "print".to_string(), Let => "let".to_string(), diff --git a/src/sexp/mod.rs b/src/sexp/mod.rs index 1a90f52..14731dd 100644 --- a/src/sexp/mod.rs +++ b/src/sexp/mod.rs @@ -34,6 +34,9 @@ pub enum SLeaf { Quote, Vector, + Coprod, + Inl, + Inr, Let, diff --git a/src/sexp/step.rs b/src/sexp/step.rs index 59b201c..3168c5b 100644 --- a/src/sexp/step.rs +++ b/src/sexp/step.rs @@ -297,6 +297,14 @@ impl SExp { /// assert_eq!(exp, expshould); /// let exp = exp.and_then(|e| e.step()); /// assert_eq!(exp, Ok(Atom(True))); + /// + /// let exp = "case (coprod Bool (+ 1 2)) ((inl b) (if b 1 0)) ((inr x) (- x 1))"; + /// let exp = parse_to_ast(exp); + /// let exp = exp.and_then(|e| e.step()); + /// let expshould = parse_to_ast("case (coprod Bool 3) ((inl b) (if b 1 0)) ((inr x) (- x 1))"); + /// assert_eq!(exp, expshould); + /// let exp = exp.and_then(|e| e.step()); + /// assert_eq!(exp, Ok(scons(Sub, scons(3, scons(1, Nil))))); /// ``` /// /// Shadowing: |