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/type/check.rs | |
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/type/check.rs')
-rw-r--r-- | src/type/check.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/type/check.rs b/src/type/check.rs index c0f4b1f..96a4408 100644 --- a/src/type/check.rs +++ b/src/type/check.rs @@ -60,6 +60,25 @@ impl SExp { /// ``` /// ...so please don't ask what their type is. /// + /// **Sum types** + /// + /// Instantiation types: + /// ```rust + /// use myslip::{ + /// r#type::{*, Type::*, TypeError::*, util::*}, + /// sexp::{SExp::*, SLeaf::*, util::*}, + /// }; + /// assert_eq!( + /// scons(Coprod, scons(7, scons(Ty(Boolean), Nil))).type_check(), + /// Ok(sumtype(Integer, Boolean)) + /// ); + /// assert_eq!( + /// scons(Coprod, scons(Ty(Boolean), scons(False, Nil))).type_check(), + /// Ok(sumtype(Boolean, Boolean)) + /// ); + /// ``` + /// Destructuring is tested in the tests of case. + /// /// Some common operators get arrow types: /// ```rust /// use myslip::{ @@ -161,7 +180,28 @@ impl SExp { /// Ok(Integer) /// ); /// ``` + /// + /// Here's a test on pattern matching on sum types: + /// ```rust + /// use myslip::{ + /// r#type::{*, Type::*, TypeError::*, util::*}, + /// sexp::{SExp::*, SLeaf::*, util::*}, + /// parse::parsetree::parse_to_ast + /// }; /// + /// fn main() { + /// let exp = parse_to_ast( + /// "(let mysum (coprod Bool 7)) \ + /// (case mysum \ + /// ((inl b) (if b 0 1)) \ + /// ((inr x) x))" + /// ).unwrap(); + /// assert_eq!( + /// exp.type_check(), + /// Ok(Integer) + /// ); + /// } + /// ``` /// /// Though perhaps the most important task of the type system /// is to increase safety by being able to warn about errors @@ -260,6 +300,9 @@ impl SExp { vecof(vt("T")), List(vec![VecType, vecof(vt("T"))]) )), + Atom(Coprod) => todo!(), + Atom(Inl) => todo!(), + Atom(Inr) => todo!(), Atom(Let) => Ok(LetType), Atom(Print) => Ok(arr(vt("_"), NilType)), Atom(Ty(_)) => Ok(TypeLit), |