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

153 lines
3.4 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 {
}
type InferResult<T> = Result<T, TypeError>;
2018-11-06 16:47:34 -08:00
#[derive(Debug, Clone)]
2018-11-08 02:26:02 -08:00
struct TypeError { msg: String }
impl TypeError {
fn new<A>(msg: &str) -> InferResult<A> {
Err(TypeError { msg: msg.to_string() })
}
}
2018-11-06 16:47:34 -08:00
2018-11-07 13:44:28 -08:00
#[derive(Debug, Clone)]
2018-11-07 03:39:31 -08:00
enum MonoType {
Var(Rc<String>),
2018-11-07 15:39:40 -08:00
Const(TConst),
2018-11-08 02:26:02 -08:00
Arrow(Box<MonoType>, Box<MonoType>)
}
impl TypeIdentifier {
fn to_monotype(&self) -> MonoType {
unimplemented!()
}
2018-11-07 03:39:31 -08:00
}
2018-11-06 16:47:34 -08:00
2018-11-07 15:39:40 -08:00
#[derive(Debug, Clone)]
enum TConst {
User(Rc<String>),
Unit,
Nat,
2018-11-07 16:39:32 -08:00
Int,
Float,
StringT,
2018-11-08 02:12:01 -08:00
Bool,
2018-11-07 15:39:40 -08:00
}
impl TConst {
fn user(name: &str) -> TConst {
TConst::User(Rc::new(name.to_string()))
}
}
2018-11-07 13:44:28 -08:00
#[derive(Debug, Clone)]
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-07 13:44:28 -08:00
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {
2018-11-06 16:47:34 -08:00
match self.infer_ast(ast) {
2018-11-07 13:44:28 -08:00
Ok(t) => Ok(format!("{:?}", t)),
2018-11-06 16:47:34 -08:00
Err(err) => Err(format!("Type error: {:?}", err))
}
}
}
impl TypeContext {
fn infer_ast(&mut self, ast: &AST) -> InferResult<MonoType> {
2018-11-07 15:39:40 -08:00
let mut output = MonoType::Const(TConst::Unit);
2018-11-07 03:39:31 -08:00
for statement in ast.0.iter() {
2018-11-07 13:44:28 -08:00
output = match statement {
2018-11-07 03:39:31 -08:00
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) -> InferResult<MonoType> {
2018-11-07 03:39:31 -08:00
match expr {
2018-11-08 02:26:02 -08:00
Expression(expr_type, Some(type_anno)) => {
let tx = self.infer_expr_type(expr_type)?;
let ty = type_anno.to_monotype();
self.unify(&ty, &tx)
},
2018-11-07 03:39:31 -08:00
Expression(expr_type, None) => self.infer_expr_type(expr_type)
}
}
fn infer_decl(&mut self, expr: &Declaration) -> InferResult<MonoType> {
2018-11-07 15:39:40 -08:00
Ok(MonoType::Const(TConst::user("unimplemented")))
2018-11-06 16:47:34 -08:00
}
2018-11-07 03:39:31 -08:00
fn infer_expr_type(&mut self, expr_type: &ExpressionType) -> InferResult<MonoType> {
2018-11-07 03:39:31 -08:00
use self::ExpressionType::*;
2018-11-08 02:12:01 -08:00
Ok(match expr_type {
NatLiteral(_) => MonoType::Const(TConst::Nat),
FloatLiteral(_) => MonoType::Const(TConst::Float),
StringLiteral(_) => MonoType::Const(TConst::StringT),
BoolLiteral(_) => MonoType::Const(TConst::Bool),
2018-11-08 02:26:02 -08:00
Call { f, arguments } => {
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
match tf {
MonoType::Arrow(t1, t2) => {
self.unify(&t1, &targ)?;
*t2.clone()
},
_ => return TypeError::new("not a function")
}
},
2018-11-08 02:12:01 -08:00
_ => MonoType::Const(TConst::user("unimplemented"))
})
}
2018-11-08 02:26:02 -08:00
fn unify(&mut self, t1: &MonoType, t2: &MonoType) -> InferResult<MonoType> {
2018-11-08 02:12:01 -08:00
unimplemented!()
2018-11-07 03:39:31 -08:00
}
2018-11-06 16:47:34 -08:00
}
#[cfg(test)]
mod tests {
use super::*;
2018-11-07 16:39:32 -08:00
fn parse(input: &str) -> AST {
let tokens: Vec<::tokenizing::Token> = ::tokenizing::tokenize(input);
let mut parser = ::parsing::Parser::new(tokens);
parser.parse().unwrap()
}
macro_rules! type_test {
($input:expr, $correct:expr) => {
{
let mut tc = TypeContext::new();
let ast = parse($input);
tc.add_symbols(&ast);
assert_eq!($correct, tc.type_check(&ast).unwrap())
}
}
}
2018-11-06 16:47:34 -08:00
#[test]
fn basic_inference() {
2018-11-06 13:44:52 -08:00
}
}