diff --git a/schala-lang/language/src/typechecking.rs b/schala-lang/language/src/typechecking.rs index c457964..19f3d84 100644 --- a/schala-lang/language/src/typechecking.rs +++ b/schala-lang/language/src/typechecking.rs @@ -17,7 +17,7 @@ type InferResult = Result; struct TypeError { msg: String } impl TypeError { - fn new(msg: &str) -> InferResult { + fn new(msg: &str) -> InferResult { //TODO make these kinds of error-producing functions CoW-ready Err(TypeError { msg: msg.to_string() }) } } @@ -40,6 +40,7 @@ pub enum TypeConst { UserDefined } +//TODO find a better way to capture the to/from string logic impl Type { fn to_string(&self) -> String { use self::Type::*; @@ -55,6 +56,21 @@ impl Type { _ => format!("UNKNOWN TYPE"), } } + + fn from_string(string: &str) -> Option { + use self::Type::*; + use self::TypeConst::*; + Some(match string { + "()" | "Unit" => Const(Unit), + "Nat" => Const(Nat), + "Int" => Const(Int), + "Float" => Const(Float), + "String" => Const(StringT), + "Bool" => Const(Bool), + "Order" => Const(Order), + _ => return None + }) + } } /* @@ -151,7 +167,17 @@ impl<'a> TypeContext<'a> { } fn get_type_from_name(&self, name: &TypeIdentifier) -> InferResult { - Ok(Type::Const(TypeConst::Unit)) + use self::TypeIdentifier::*; + Ok(match name { + Singleton(TypeSingletonName { name, params }) => { + match Type::from_string(&name) { + Some(ty) => ty, + None => return TypeError::new("Unknown type name") + } + }, + Tuple(_) => return TypeError::new("tuples aren't ready yet"), + }) + } pub fn typecheck(&mut self, ast: &AST) -> Result {