aboutsummaryrefslogtreecommitdiff
path: root/src/type/check.rs
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-05 11:40:00 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-05 11:40:00 +0300
commitc629fac4297b8f13bdab00100f3b05549174154e (patch)
treecadc8f0c9b16021338134a9c7a90478bcd8f2bdc /src/type/check.rs
parent0d9c5b7fd7dec374ec357581f721f5cdc828b7ae (diff)
downloadmyslip-c629fac4297b8f13bdab00100f3b05549174154e.tar.gz
myslip-c629fac4297b8f13bdab00100f3b05549174154e.zip
Added boilerplate and tests for booleans, integer comparisons and boolean operators.
Diffstat (limited to 'src/type/check.rs')
-rw-r--r--src/type/check.rs31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/type/check.rs b/src/type/check.rs
index 9369d97..e9917c7 100644
--- a/src/type/check.rs
+++ b/src/type/check.rs
@@ -16,6 +16,7 @@ impl SExp {
/// };
///
/// assert_eq!(Atom(Int(1)).type_check(), Ok(Integer));
+ /// assert_eq!(Atom(False).type_check(), Ok(Boolean));
/// ```
///
/// Quotes are given list types:
@@ -26,8 +27,8 @@ impl SExp {
/// };
///
/// assert_eq!(
- /// scons(Quote, scons(1, scons(2, Nil))).type_check(),
- /// Ok(List(vec![Integer, Integer]))
+ /// scons(Quote, scons(1, scons(False, Nil))).type_check(),
+ /// Ok(List(vec![Integer, Boolean]))
/// );
/// ```
/// Though so is Nil given too:
@@ -54,6 +55,16 @@ impl SExp {
/// assert_eq!(Atom(Mul).type_check(), Ok(arr(List(vec![Integer, Integer]), Integer)));
/// assert_eq!(Atom(Sub).type_check(), Ok(arr(List(vec![Integer, Integer]), Integer)));
/// assert_eq!(Atom(Div).type_check(), Ok(arr(List(vec![Integer, Integer]), Integer)));
+ /// assert_eq!(Atom(Eq) .type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(Neq).type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(Gt) .type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(Lt) .type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(Ge) .type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(Le) .type_check(), Ok(arr(List(vec![Integer, Integer]), Boolean)));
+ /// assert_eq!(Atom(And).type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean)));
+ /// assert_eq!(Atom(Or) .type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean)));
+ /// assert_eq!(Atom(Xor).type_check(), Ok(arr(List(vec![Boolean, Boolean]), Boolean)));
+ /// assert_eq!(Atom(Not).type_check(), Ok(arr(Boolean, Boolean)));
/// ```
///
/// Though perhaps the most important task of the type system
@@ -94,6 +105,10 @@ impl SExp {
/// "passing '-' as an argument to '+' should return in InvalidArgList error"
/// ),
/// };
+ ///
+ /// assert!(scons(And, scons(1, scons(Atom(True), Nil))).type_check().is_err());
+ /// assert!(scons(Mul, scons(1, scons(Atom(True), Nil))).type_check().is_err());
+ /// assert!(scons(Not, scons(1, Nil)).type_check().is_err());
/// ```
///
/// Also, free variables should result in an error
@@ -121,6 +136,8 @@ impl SExp {
match self {
Atom(Int(_)) => Ok(Integer),
+ Atom(True) => todo!(),
+ Atom(False) => todo!(),
Atom(Var(name)) => ctx.get(name)
.ok_or(UndefinedVariable(name.to_string()))
.cloned(),
@@ -128,6 +145,16 @@ impl SExp {
Atom(Mul) => Ok(arr(List(vec![Integer, Integer]), Integer)), // TODO varlen
Atom(Sub) => Ok(arr(List(vec![Integer, Integer]), Integer)),
Atom(Div) => Ok(arr(List(vec![Integer, Integer]), Integer)),
+ Atom(Eq) => todo!(),
+ Atom(Neq) => todo!(),
+ Atom(Lt) => todo!(),
+ Atom(Gt) => todo!(),
+ Atom(Le) => todo!(),
+ Atom(Ge) => todo!(),
+ Atom(Or) => todo!(),
+ Atom(And) => todo!(),
+ Atom(Xor) => todo!(),
+ Atom(Not) => todo!(),
Atom(Nil) => Ok(List(vec![])),
Atom(Quote) => Ok(arr(
VarType("T".to_string()),