diff --git a/schala-lang/src/builtin.rs b/schala-lang/src/builtin.rs index 4e06032..e89bff1 100644 --- a/schala-lang/src/builtin.rs +++ b/schala-lang/src/builtin.rs @@ -1,9 +1,34 @@ use std::rc::Rc; use std::collections::HashMap; +use std::fmt; +use std::fmt::Write; -use typechecking::{Type, TypeResult, TConstOld}; use self::Type::*; use self::TConstOld::*; + +//TODO get rid of these types and replace them with the right MonoType or whatever ones later +#[derive(Debug, PartialEq, Clone)] +pub enum Type { + Const(TConstOld), + Func(Box, Box), +} + +#[derive(Debug, PartialEq, Clone)] +pub enum TConstOld { + Nat, + Int, + Float, + StringT, + Bool, + Custom(String), +} + +impl fmt::Display for Type { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + #[derive(Debug, PartialEq, Clone)] pub struct BinOp { sigil: Rc @@ -16,7 +41,7 @@ impl BinOp { pub fn sigil(&self) -> &Rc { &self.sigil } - pub fn get_type(&self) -> TypeResult { + pub fn get_type(&self) -> Result { let s = self.sigil.as_str(); BINOPS.get(s).map(|x| x.0.clone()).ok_or(format!("Binop {} not found", s)) } @@ -44,7 +69,7 @@ impl PrefixOp { pub fn is_prefix(op: &str) -> bool { PREFIX_OPS.get(op).is_some() } - pub fn get_type(&self) -> TypeResult { + pub fn get_type(&self) -> Result { let s = self.sigil.as_str(); PREFIX_OPS.get(s).map(|x| x.0.clone()).ok_or(format!("Prefix op {} not found", s)) } diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index e27b2e6..f47340b 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -36,7 +36,7 @@ mod eval; pub struct Schala { state: eval::State<'static>, symbol_table: Rc>, - //type_context + type_context: typechecking::TypeContext, } impl Schala { @@ -45,6 +45,7 @@ impl Schala { Schala { symbol_table: symbols.clone(), state: eval::State::new(symbols), + type_context: typechecking::TypeContext::new(), } } } diff --git a/schala-lang/src/typechecking.rs b/schala-lang/src/typechecking.rs index 1553ac3..5f02773 100644 --- a/schala-lang/src/typechecking.rs +++ b/schala-lang/src/typechecking.rs @@ -3,8 +3,6 @@ use std::collections::{HashSet, HashMap}; use std::collections::hash_set::Union; use std::iter::Iterator; //use std::char; -use std::fmt; -use std::fmt::Write; use itertools::Itertools; @@ -17,7 +15,13 @@ pub struct TypeContext { environment: TypeEnvironment, } -/* real meat of type stuff here */ +impl TypeContext { + pub fn new() -> TypeContext { + TypeContext { environment: TypeEnvironment::default() } + } +} + +pub type TypeResult = Result; #[derive(Debug, PartialEq, Clone)] enum MonoType { @@ -135,33 +139,9 @@ impl TypeEnvironment { } } - - - -#[derive(Debug, PartialEq, Clone)] -pub enum Type { - Const(TConstOld), - Func(Box, Box), -} - -#[derive(Debug, PartialEq, Clone)] -pub enum TConstOld { - Nat, - Int, - Float, - StringT, - Bool, - Custom(String), -} - -impl fmt::Display for Type { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self) - } -} - /* TODO this should just check the name against a map, and that map should be pre-populated with * types */ +/* impl parsing::TypeName { fn to_type(&self) -> TypeResult { use self::parsing::TypeSingletonName; @@ -184,15 +164,12 @@ impl parsing::TypeName { }) } } +*/ -pub type TypeResult = Result; + +/* impl TypeContext { - pub fn new() -> TypeContext { - TypeContext { environment: TypeEnvironment::default(), /*type_var_count: 0*/ } - } - - pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult { let ref block = ast.0; let mut infer = Infer::default(); @@ -221,6 +198,7 @@ enum InferError { type InferResult = Result; + impl Infer { fn fresh(&mut self) -> MonoType { let i = self._idents; @@ -293,30 +271,6 @@ impl Infer { }, None => return Err(InferError::UnknownIdentifier(name.clone())), }, - /* - PrefixExp(op, expr) => match op.get_type()? { - Func(box t1, box t2) => { - let expr_ty = self.infer(expr)?; - self.unify(t1, expr_ty)?; - Ok(t2) - }, - other => Err(format!("{:?} is not a prefix op function type", other)) - }, - */ - /* - BinExp(op, lhs, rhs) => { /* remember there are both the haskell convention talk and the write you a haskell ways to do this! */ - match op.get_type()? { - Func(box t1, box Func(box t2, box t3)) => { - let lhs_ty = self.infer(lhs)?; - let rhs_ty = self.infer(rhs)?; - self.unify(t1, lhs_ty)?; - self.unify(t2, rhs_ty)?; - Ok(t3) - }, - other => return Err(format!("{:?} is not a binary function type", other)) - } - }, - */ e => return Err(InferError::Custom(format!("Type inference for {:?} not done", e))) }) } @@ -332,6 +286,20 @@ impl Infer { ty.apply_substitution(&subst) } } +*/ + + + + + + + + + + + + +/* OLD STUFF DOWN HERE */