From 83315e97ac62f34c87265e836371e85f84cf823d Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 23 Feb 2018 02:30:34 -0800 Subject: [PATCH] Move anno-to-type to a method on TypeName --- src/schala_lang/typechecking.rs | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/schala_lang/typechecking.rs b/src/schala_lang/typechecking.rs index f6e9d9e..6c23f3d 100644 --- a/src/schala_lang/typechecking.rs +++ b/src/schala_lang/typechecking.rs @@ -28,6 +28,26 @@ pub enum TConst { Custom(String), } +impl parsing::TypeName { + fn to_type(&self) -> TypeResult { + use self::parsing::TypeSingletonName; + use self::parsing::TypeName::*; + use self::Type::*; use self::TConst::*; + Ok(match self { + &Tuple(_) => return Err(format!("Tuples not yet implemented")), + &Singleton(ref name) => match name { + &TypeSingletonName { ref name, .. } => match &name[..] { + "Int" => Const(Int), + "Float" => Const(Float), + "Bool" => Const(Bool), + "String" => Const(StringT), + n => Const(Custom(n.to_string())) + } + } + }) + } +} + type TypeResult = Result; impl TypeContext { @@ -67,7 +87,7 @@ impl TypeContext { use self::parsing::Expression; match expr { &Expression(ref e, Some(ref anno)) => { - let anno_ty = self.type_from_anno(anno)?; + let anno_ty = anno.to_type()?; let ty = self.infer_exprtype(&e)?; self.unify(ty, anno_ty) }, @@ -119,23 +139,6 @@ impl TypeContext { //this is a shim; not all ops are binops from int -> int -> int Ok(Func(bx!(Const(Int)), bx!(Func(bx!(Const(Int)), bx!(Const(Int)))))) } - fn type_from_anno(&mut self, anno: &parsing::TypeName) -> TypeResult { - use self::parsing::TypeSingletonName; - use self::parsing::TypeName::*; - use self::Type::*; use self::TConst::*; - Ok(match anno { - &Tuple(_) => return Err(format!("Tuples not yet implemented")), - &Singleton(ref name) => match name { - &TypeSingletonName { ref name, .. } => match &name[..] { - "Int" => Const(Int), - "Float" => Const(Float), - "Bool" => Const(Bool), - "String" => Const(StringT), - n => Const(Custom(n.to_string())) - } - } - }) - } fn unify(&mut self, t1: Type, t2: Type) -> TypeResult { use self::Type::*;// use self::TConst::*; match (t1, t2) {