Even more types

This commit is contained in:
greg 2018-11-08 20:30:17 -08:00
parent 9056e9b0e1
commit d44bb02d61
2 changed files with 46 additions and 8 deletions

View File

@ -43,7 +43,7 @@ mod eval;
pub struct Schala { pub struct Schala {
state: eval::State<'static>, state: eval::State<'static>,
symbol_table: Rc<RefCell<symbol_table::SymbolTable>>, symbol_table: Rc<RefCell<symbol_table::SymbolTable>>,
type_context: typechecking::TypeContext, type_context: typechecking::TypeContext<'static>,
active_parser: Option<parsing::Parser>, active_parser: Option<parsing::Parser>,
} }

View File

@ -1,11 +1,12 @@
use std::rc::Rc; use std::rc::Rc;
use ast::*; use ast::*;
use util::ScopeStack;
pub type TypeName = Rc<String>; pub type TypeName = Rc<String>;
pub struct TypeContext { pub struct TypeContext<'a> {
variable_map: ScopeStack<'a, Rc<String>, MonoType>
} }
type InferResult<T> = Result<T, TypeError>; type InferResult<T> = Result<T, TypeError>;
@ -28,7 +29,19 @@ enum MonoType {
impl TypeIdentifier { impl TypeIdentifier {
fn to_monotype(&self) -> MonoType { fn to_monotype(&self) -> MonoType {
unimplemented!() match self {
TypeIdentifier::Tuple(items) => unimplemented!(),
TypeIdentifier::Singleton(TypeSingletonName { name, .. }) => {
match &name[..] {
"Nat" => MonoType::Const(TConst::Nat),
"Int" => MonoType::Const(TConst::Int),
"Float" => MonoType::Const(TConst::Float),
"Bool" => MonoType::Const(TConst::Bool),
"String" => MonoType::Const(TConst::StringT),
_ => unimplemented!()
}
}
}
} }
} }
@ -55,9 +68,11 @@ struct PolyType {
ty: MonoType ty: MonoType
} }
impl TypeContext { impl<'a> TypeContext<'a> {
pub fn new() -> TypeContext { pub fn new() -> TypeContext<'a> {
TypeContext { } TypeContext {
variable_map: ScopeStack::new(None),
}
} }
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> { pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {
@ -68,7 +83,7 @@ impl TypeContext {
} }
} }
impl TypeContext { impl<'a> TypeContext<'a> {
fn infer_ast(&mut self, ast: &AST) -> InferResult<MonoType> { fn infer_ast(&mut self, ast: &AST) -> InferResult<MonoType> {
let mut output = MonoType::Const(TConst::Unit); let mut output = MonoType::Const(TConst::Unit);
for statement in ast.0.iter() { for statement in ast.0.iter() {
@ -102,6 +117,15 @@ impl TypeContext {
FloatLiteral(_) => MonoType::Const(TConst::Float), FloatLiteral(_) => MonoType::Const(TConst::Float),
StringLiteral(_) => MonoType::Const(TConst::StringT), StringLiteral(_) => MonoType::Const(TConst::StringT),
BoolLiteral(_) => MonoType::Const(TConst::Bool), BoolLiteral(_) => MonoType::Const(TConst::Bool),
Value(name) => {
//TODO handle the distinction between 0-arg constructors and variables at some point
// need symbol table for that
match self.variable_map.lookup(name) {
Some(ty) => ty.clone(),
None => return TypeError::new(&format!("Variable {} not found", name))
}
},
IfExpression { discriminator, body } => self.infer_if_expr(discriminator, body)?,
Call { f, arguments } => { Call { f, arguments } => {
let tf: MonoType = self.infer_expr(f)?; //has to be an Arrow MonoType let tf: MonoType = self.infer_expr(f)?; //has to be an Arrow MonoType
let targ = self.infer_expr(&arguments[0])?; // TODO make this work with functions with more than one arg let targ = self.infer_expr(&arguments[0])?; // TODO make this work with functions with more than one arg
@ -117,6 +141,20 @@ impl TypeContext {
}) })
} }
fn infer_if_expr(&mut self, discriminator: &Discriminator, body: &IfExpressionBody) -> InferResult<MonoType> {
let test = match discriminator {
Discriminator::Simple(expr) => expr,
_ => return TypeError::new("Dame desu")
};
let (then_clause, maybe_else_clause) = match body {
IfExpressionBody::SimpleConditional(a, b) => (a, b),
_ => return TypeError::new("Dont work")
};
unimplemented!()
}
fn unify(&mut self, t1: &MonoType, t2: &MonoType) -> InferResult<MonoType> { fn unify(&mut self, t1: &MonoType, t2: &MonoType) -> InferResult<MonoType> {
unimplemented!() unimplemented!()
} }