Use name TypeVariable
This commit is contained in:
parent
9161e2751f
commit
f2c6556c2a
@ -46,8 +46,8 @@ impl SymbolTable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn lookup(&mut self, binding: &Rc<String>) -> Option<SchalaType> {
|
fn lookup(&mut self, binding: &Rc<String>) -> Option<TypeVariable> {
|
||||||
use self::SchalaType::*;
|
use self::TypeVariable::*;
|
||||||
Some(Function(Box::new(Integer), Box::new(Boolean)))
|
Some(Function(Box::new(Integer), Box::new(Boolean)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -69,17 +69,17 @@ impl TypeContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum SchalaType {
|
pub enum TypeVariable {
|
||||||
Integer,
|
Integer,
|
||||||
Boolean,
|
Boolean,
|
||||||
Unit,
|
Unit,
|
||||||
Function(Box<SchalaType>, Box<SchalaType>),
|
Function(Box<TypeVariable>, Box<TypeVariable>),
|
||||||
Bottom,
|
Bottom,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SchalaType {
|
impl TypeVariable {
|
||||||
fn from_anno(anno: &TypeName) -> SchalaType {
|
fn from_anno(anno: &TypeName) -> TypeVariable {
|
||||||
use self::SchalaType::*;
|
use self::TypeVariable::*;
|
||||||
|
|
||||||
match anno {
|
match anno {
|
||||||
&TypeName::Singleton { ref name, .. } => {
|
&TypeName::Singleton { ref name, .. } => {
|
||||||
@ -94,7 +94,7 @@ impl SchalaType {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypeCheckResult = Result<SchalaType, String>;
|
type TypeCheckResult = Result<TypeVariable, String>;
|
||||||
|
|
||||||
// from Niko's talk
|
// from Niko's talk
|
||||||
/* fn type_check(expression, expected_ty) -> Ty {
|
/* fn type_check(expression, expected_ty) -> Ty {
|
||||||
@ -115,7 +115,7 @@ type TypeCheckResult = Result<SchalaType, String>;
|
|||||||
|
|
||||||
impl TypeContext {
|
impl TypeContext {
|
||||||
pub fn type_check(&mut self, ast: &AST) -> TypeCheckResult {
|
pub fn type_check(&mut self, ast: &AST) -> TypeCheckResult {
|
||||||
let mut last = SchalaType::Unit;
|
let mut last = TypeVariable::Unit;
|
||||||
for statement in ast.0.iter() {
|
for statement in ast.0.iter() {
|
||||||
match statement {
|
match statement {
|
||||||
&Statement::Declaration(ref _decl) => {
|
&Statement::Declaration(ref _decl) => {
|
||||||
@ -135,10 +135,10 @@ impl TypeContext {
|
|||||||
Ok(match (&expr.0, &expr.1) {
|
Ok(match (&expr.0, &expr.1) {
|
||||||
(ref _t, &Some(ref anno)) => {
|
(ref _t, &Some(ref anno)) => {
|
||||||
//TODO make this better,
|
//TODO make this better,
|
||||||
SchalaType::from_anno(anno)
|
TypeVariable::from_anno(anno)
|
||||||
},
|
},
|
||||||
(&IntLiteral(_), _) => SchalaType::Integer,
|
(&IntLiteral(_), _) => TypeVariable::Integer,
|
||||||
(&BoolLiteral(_), _) => SchalaType::Boolean,
|
(&BoolLiteral(_), _) => TypeVariable::Boolean,
|
||||||
(&Variable(ref name), _) => self.symbol_table
|
(&Variable(ref name), _) => self.symbol_table
|
||||||
.lookup(name)
|
.lookup(name)
|
||||||
.ok_or(format!("Couldn't find {}", name))?,
|
.ok_or(format!("Couldn't find {}", name))?,
|
||||||
@ -147,18 +147,18 @@ impl TypeContext {
|
|||||||
let f_type = self.infer(&*f)?;
|
let f_type = self.infer(&*f)?;
|
||||||
let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later
|
let arg_type = self.infer(arguments.get(0).unwrap())?; // TODO fix later
|
||||||
match f_type {
|
match f_type {
|
||||||
SchalaType::Function(box t1, box ret_type) => {
|
TypeVariable::Function(box t1, box ret_type) => {
|
||||||
let _ = self.unify(&t1, &arg_type)?;
|
let _ = self.unify(&t1, &arg_type)?;
|
||||||
ret_type
|
ret_type
|
||||||
},
|
},
|
||||||
_ => return Err(format!("Type error"))
|
_ => return Err(format!("Type error"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => SchalaType::Unit,
|
_ => TypeVariable::Unit,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unify(&mut self, t1: &SchalaType, t2: &SchalaType) -> TypeCheckResult {
|
fn unify(&mut self, t1: &TypeVariable, t2: &TypeVariable) -> TypeCheckResult {
|
||||||
if t1 == t2 {
|
if t1 == t2 {
|
||||||
Ok(t1.clone())
|
Ok(t1.clone())
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user