More type implementing - WIP
This has a borrowing bug currently
This commit is contained in:
parent
628eb28deb
commit
687d482853
2
TODO.md
2
TODO.md
@ -40,3 +40,5 @@ struct CompilerPass {
|
|||||||
name: String,
|
name: String,
|
||||||
run: fn(PrevPass) -> NextPass
|
run: fn(PrevPass) -> NextPass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
-change "Type...." names in parser.rs to "Anno..." for non-collision with names in typechecking.rs
|
||||||
|
@ -7,14 +7,20 @@ pub struct TypeContext { }
|
|||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum Type {
|
pub enum Type {
|
||||||
Unit,
|
Unit,
|
||||||
|
Const(TConst),
|
||||||
|
Void
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub enum TConst {
|
||||||
Int,
|
Int,
|
||||||
Float,
|
Float,
|
||||||
StringT,
|
StringT,
|
||||||
Bool,
|
Bool,
|
||||||
Custom(Rc<String>),
|
Custom(String),
|
||||||
Void
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type TypeResult<T> = Result<T, String>;
|
type TypeResult<T> = Result<T, String>;
|
||||||
|
|
||||||
impl TypeContext {
|
impl TypeContext {
|
||||||
@ -45,21 +51,33 @@ impl TypeContext {
|
|||||||
fn type_infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
|
fn type_infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
|
||||||
use self::parsing::Expression;
|
use self::parsing::Expression;
|
||||||
use self::parsing::ExpressionType::*;
|
use self::parsing::ExpressionType::*;
|
||||||
use self::Type::*;
|
use self::Type::*; use self::TConst::*;
|
||||||
match expr {
|
match expr {
|
||||||
&Expression(ref e, Some(ref ty)) => Err(format!("Anno not implemented")),
|
&Expression(ref e, Some(ref anno)) => {
|
||||||
|
let anno_ty = self.type_from_anno(anno)?;
|
||||||
|
let expr = Expression(*e, None);
|
||||||
|
let ty = self.type_infer(&expr)?;
|
||||||
|
self.unify(ty, anno_ty)
|
||||||
|
},
|
||||||
&Expression(ref e, None) => match e {
|
&Expression(ref e, None) => match e {
|
||||||
&IntLiteral(_) => Ok(Int),
|
&IntLiteral(_) => Ok(Const(Int)),
|
||||||
&FloatLiteral(_) => Ok(Float),
|
&FloatLiteral(_) => Ok(Const(Float)),
|
||||||
&StringLiteral(_) => Ok(StringT),
|
&StringLiteral(_) => Ok(Const(StringT)),
|
||||||
&BoolLiteral(_) => Ok(Bool),
|
&BoolLiteral(_) => Ok(Const(Bool)),
|
||||||
_ => Err(format!("Type not yet implemented"))
|
_ => Err(format!("Type not yet implemented"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
|
fn type_from_anno(&mut self, anno: &parsing::TypeName) -> TypeResult<Type> {
|
||||||
use self::Type::*;
|
use self::Type::*; use self::TConst::*;
|
||||||
Ok(Unit)
|
Ok(Unit)
|
||||||
}
|
}
|
||||||
|
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
|
||||||
|
use self::Type::*; use self::TConst::*;
|
||||||
|
match (t1, t2) {
|
||||||
|
(Const(ref a), Const(ref b)) if a == b => Ok(Const(a.clone())),
|
||||||
|
(a, b) => Err(format!("Types {:?} and {:?} don't unify", a, b))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user