From 76575e9ba330eb990c459367341548d12ca6491a Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 21 Feb 2018 03:39:40 -0800 Subject: [PATCH] Starting basic type stuff --- TODO.md | 11 +++++++++++ src/schala_lang/typechecking.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index 464fd8b..4f1e01b 100644 --- a/TODO.md +++ b/TODO.md @@ -5,6 +5,17 @@ * idea for Schala - scoped types - be able to define a quick enum type scoped to a function ro something, that only is meant to be used as a quick bespoke interface between two other things +* another idea, allow: +type enum { + type enum MySubVariant { + SubVariant1, SubVariant2, etc. + } + Variant1(MySubVariant), + Variant2(...), + } + + + * idea for Schala: both currying *and* default arguments! ex. fn a(b: Int, c:Int, d:Int = 1) -> Int a(1,2) : Int diff --git a/src/schala_lang/typechecking.rs b/src/schala_lang/typechecking.rs index dbbecc3..3f73c2d 100644 --- a/src/schala_lang/typechecking.rs +++ b/src/schala_lang/typechecking.rs @@ -4,9 +4,14 @@ use schala_lang::parsing; pub struct TypeContext { } -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, Clone)] pub enum Type { Unit, + Int, + Float, + StringT, + Bool, + Custom(Rc), Void } @@ -25,9 +30,11 @@ impl TypeContext { Ok(ret_type) } fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult { + println!("statement should be: {:?}", statement); + use self::parsing::Statement::*; match statement { - &ExpressionStatement(ref expr) => self.type_check_expression(expr), + &ExpressionStatement(ref expr) => self.type_infer(expr), &Declaration(ref decl) => self.type_check_declaration(decl), } } @@ -35,9 +42,24 @@ impl TypeContext { use self::Type::*; Ok(Unit) } - fn type_check_expression(&mut self, expr: &parsing::Expression) -> TypeResult { + fn type_infer(&mut self, expr: &parsing::Expression) -> TypeResult { + use self::parsing::Expression; + use self::parsing::ExpressionType::*; use self::Type::*; - Ok(Void) + match expr { + &Expression(ref e, Some(ref ty)) => Err(format!("Anno not implemented")), + &Expression(ref e, None) => match e { + &IntLiteral(_) => Ok(Int), + &FloatLiteral(_) => Ok(Float), + &StringLiteral(_) => Ok(StringT), + &BoolLiteral(_) => Ok(Bool), + _ => Err(format!("Type not yet implemented")) + } + } + } + fn unify(&mut self, t1: Type, t2: Type) -> TypeResult { + use self::Type::*; + Ok(Unit) } }