This commit is contained in:
greg 2018-11-07 15:39:40 -08:00
parent 69b7b9f528
commit 0d5ccd21fe
1 changed files with 19 additions and 5 deletions

View File

@ -16,10 +16,24 @@ struct TypeError { }
#[derive(Debug, Clone)]
enum MonoType {
Var(Rc<String>),
Const(Rc<String>),
Const(TConst),
Arrow(Rc<String>)
}
#[derive(Debug, Clone)]
enum TConst {
User(Rc<String>),
Unit,
Nat,
Int
}
impl TConst {
fn user(name: &str) -> TConst {
TConst::User(Rc::new(name.to_string()))
}
}
#[derive(Debug, Clone)]
struct PolyType {
vars: Vec<Rc<String>>,
@ -41,7 +55,7 @@ impl TypeContext {
impl TypeContext {
fn infer_ast(&mut self, ast: &AST) -> TypeResult<MonoType> {
let mut output = MonoType::Const(Rc::new("Unit".to_string()));
let mut output = MonoType::Const(TConst::Unit);
for statement in ast.0.iter() {
output = match statement {
Statement::ExpressionStatement(ref expr) => self.infer_expr(expr)?,
@ -59,14 +73,14 @@ impl TypeContext {
}
fn infer_decl(&mut self, expr: &Declaration) -> TypeResult<MonoType> {
Ok(MonoType::Const(Rc::new("Unimplemented".to_string())))
Ok(MonoType::Const(TConst::user("unimplemented")))
}
fn infer_expr_type(&mut self, expr_type: &ExpressionType) -> TypeResult<MonoType> {
use self::ExpressionType::*;
match expr_type {
NatLiteral(_) => Ok(MonoType::Const(Rc::new("Nat".to_string()))),
_ => Ok(MonoType::Const(Rc::new("Unimplemented".to_string())))
NatLiteral(_) => Ok(MonoType::Const(TConst::Nat)),
_ => Ok(MonoType::Const(TConst::user("unimplemented")))
}
}
}