From 54c16f019091729e4afae8ede4f53728a018e915 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 10 Oct 2017 17:29:28 -0700 Subject: [PATCH] Partial handling of user defined types --- src/schala_lang/type_check.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/schala_lang/type_check.rs b/src/schala_lang/type_check.rs index 63b5eee..7aa5f28 100644 --- a/src/schala_lang/type_check.rs +++ b/src/schala_lang/type_check.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::rc::Rc; -use schala_lang::parsing::{AST, Statement, Declaration, Signature, Expression, ExpressionType, Operation, TypeName}; +use schala_lang::parsing::{AST, Statement, Declaration, Signature, Expression, ExpressionType, Operation, Variant, TypeName}; // from Niko's talk /* fn type_check(expression, expected_ty) -> Ty { @@ -39,6 +39,7 @@ impl TypeVar { #[derive(Debug, PartialEq, Clone)] pub enum TypeConst { + UserT(Rc), Integer, Float, StringT, @@ -73,6 +74,8 @@ impl TypeContext { } pub fn add_symbols(&mut self, ast: &AST) { use self::Declaration::*; + use self::Type::*; + use self::TypeConst::*; for statement in ast.0.iter() { match *statement { @@ -81,7 +84,32 @@ impl TypeContext { match *decl { FuncSig(_) => (), Impl { .. } => (), - TypeDecl { .. } => (), + TypeDecl(ref type_constructor, ref body) => { + for variant in body.0.iter() { + let (spec, type_var) = match variant { + &Variant::UnitStruct(ref data_constructor) => { + let spec = PathSpecifier(data_constructor.clone()); + let type_var = TConst(UserT(type_constructor.clone())); + (spec, type_var) + }, + &Variant::TupleStruct(ref data_construcor, ref args) => { + //TODO fix + let arg = args.get(0).unwrap(); + let type_arg = self.from_anno(arg); + let spec = PathSpecifier(data_construcor.clone()); + let type_var = TConst(FunctionT( + Box::new(type_arg), + Box::new(TConst(UserT(type_constructor.clone()))), + )); + (spec, type_var) + + }, + &Variant::Record(_, _) => unimplemented!(), + }; + let entry = TypeContextEntry { type_var, constant: true }; + self.symbol_table.insert(spec, entry); + } + }, TypeAlias { .. } => (), Binding {ref name, ref constant, ref expr} => { let spec = PathSpecifier(name.clone());