schala/schala-lang/language/src/typechecking.rs

80 lines
1.6 KiB
Rust
Raw Normal View History

2018-02-21 02:31:28 -08:00
use std::rc::Rc;
2018-11-06 16:47:34 -08:00
use ast::*;
2018-11-06 13:44:52 -08:00
2018-05-27 02:44:06 -07:00
pub type TypeName = Rc<String>;
2018-11-06 13:44:52 -08:00
pub struct TypeContext {
}
2018-11-06 16:47:34 -08:00
type TypeResult<T> = Result<T, TypeError>;
#[derive(Debug, Clone)]
struct TypeError { }
2018-11-07 03:39:31 -08:00
enum MonoType {
Var(Rc<String>),
Const(Rc<String>),
Arrow(Rc<String>)
}
2018-11-06 16:47:34 -08:00
2018-11-07 03:39:31 -08:00
struct PolyType {
vars: Vec<Rc<String>>,
ty: MonoType
2018-11-06 16:47:34 -08:00
}
2018-11-06 13:44:52 -08:00
impl TypeContext {
pub fn new() -> TypeContext {
TypeContext { }
}
2018-11-06 16:47:34 -08:00
pub fn typecheck(&mut self, ast: &AST) -> Result<(), String> {
match self.infer_ast(ast) {
Ok(_) => Ok(()),
Err(err) => Err(format!("Type error: {:?}", err))
}
}
}
impl TypeContext {
2018-11-07 03:39:31 -08:00
fn infer_ast(&mut self, ast: &AST) -> TypeResult<MonoType> {
let mut output = MonoType::Const(Rc::new("Unit".to_string()));
for statement in ast.0.iter() {
match statement {
Statement::ExpressionStatement(ref expr) => self.infer_expr(expr)?,
Statement::Declaration(ref decl) => self.infer_decl(decl)?,
};
}
Ok(output)
}
fn infer_expr(&mut self, expr: &Expression) -> TypeResult<MonoType> {
match expr {
Expression(expr_type, Some(type_anno)) => unimplemented!(),
Expression(expr_type, None) => self.infer_expr_type(expr_type)
}
}
fn infer_decl(&mut self, expr: &Declaration) -> TypeResult<MonoType> {
2018-11-06 16:47:34 -08:00
unimplemented!()
}
2018-11-07 03:39:31 -08:00
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()))),
_ => unimplemented!()
}
}
2018-11-06 16:47:34 -08:00
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn basic_inference() {
2018-11-06 13:44:52 -08:00
}
}