From ac9ecc754e12487eb20a615ca3cf05cb163ea75a Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 3 Aug 2025 16:06:43 +0300 Subject: Changed UndefinedType to VarType(String) for generics and added tests for inferring them. Inferring is done by method Type::infer_generics. Whether it is executed is controlled by method Type::is_concrete. Also had to change the Display for Type and its tests, as the enum variants changed. --- src/type/mod.rs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'src/type/mod.rs') diff --git a/src/type/mod.rs b/src/type/mod.rs index 5e7076a..6c4be97 100644 --- a/src/type/mod.rs +++ b/src/type/mod.rs @@ -17,7 +17,10 @@ pub enum Type { List(Vec), - UndefinedType, // only for errors + /// Type for generics + /// and also error messages + /// with unknown types + VarType(String), } @@ -27,6 +30,8 @@ pub enum TypeError { UndefinedVariable(String), + UnboundGeneric(String), + InvalidOperator { operator: SExp, expected: Type, @@ -41,3 +46,30 @@ pub enum TypeError { OtherError } + + +impl Type { + /// Tests if the type has no variable types. + /// + /// **Examples** + /// ```rust + /// use melisp::r#type::{*, Type::*, TypeError::*, util::*}; + /// + /// assert!(Integer.is_concrete()); + /// assert!(!VarType("a".to_string()).is_concrete()); + /// + /// assert!(arr(Integer, Integer).is_concrete()); + /// assert!(!arr(VarType("b".to_string()), Integer).is_concrete()); + /// + /// assert!( + /// List(vec![Integer, Integer, arr(Integer, Integer)]).is_concrete() + /// ); + /// assert!( + /// !List(vec![Integer, VarType("a".to_string()), Integer]) + /// .is_concrete() + /// ); + /// ``` + pub fn is_concrete(&self) -> bool { + todo!() + } +} -- cgit v1.2.3