2018-02-21 02:31:28 -08:00
|
|
|
use std::rc::Rc;
|
2018-02-22 03:21:58 -08:00
|
|
|
use std::collections::HashMap;
|
2018-05-15 03:25:40 -07:00
|
|
|
//use std::char;
|
2018-03-01 22:32:38 -08:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::Write;
|
2018-02-21 02:31:28 -08:00
|
|
|
|
2018-03-03 11:52:07 -08:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
2018-03-23 18:43:43 -07:00
|
|
|
use parsing;
|
2018-02-21 02:31:28 -08:00
|
|
|
|
2018-05-10 21:41:28 -07:00
|
|
|
pub struct TypeContext {
|
2018-05-15 03:25:40 -07:00
|
|
|
//type_var_count: u64,
|
2018-02-27 03:01:05 -08:00
|
|
|
bindings: HashMap<Rc<String>, Type>,
|
2018-05-14 09:46:25 -07:00
|
|
|
pub symbol_table: SymbolTable
|
2018-05-10 21:41:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//cf. p. 150 or so of Language Implementation Patterns
|
2018-05-14 15:01:37 -07:00
|
|
|
pub struct SymbolTable {
|
2018-05-13 15:17:25 -07:00
|
|
|
pub values: HashMap<Rc<String>, Symbol> //TODO this will eventually have real type information
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SymbolTable {
|
|
|
|
fn new() -> SymbolTable {
|
|
|
|
SymbolTable { values: HashMap::new() }
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 21:41:28 -07:00
|
|
|
|
2018-05-13 15:42:10 -07:00
|
|
|
#[derive(Debug)]
|
2018-05-14 15:01:37 -07:00
|
|
|
pub struct Symbol {
|
2018-05-13 15:54:15 -07:00
|
|
|
pub name: Rc<String>,
|
|
|
|
pub ty: Type
|
2018-02-22 03:21:58 -08:00
|
|
|
}
|
2018-02-21 02:31:28 -08:00
|
|
|
|
2018-02-21 03:39:40 -08:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2018-02-21 02:31:28 -08:00
|
|
|
pub enum Type {
|
2018-02-21 04:32:17 -08:00
|
|
|
Const(TConst),
|
2018-05-15 21:47:08 -07:00
|
|
|
Var(TVar),
|
2018-02-23 01:49:37 -08:00
|
|
|
Func(Box<Type>, Box<Type>),
|
2018-05-15 03:25:40 -07:00
|
|
|
//UVar(String),
|
|
|
|
//EVar(u64),
|
2018-05-15 21:47:08 -07:00
|
|
|
Sum(Vec<Type>),
|
2018-02-21 04:32:17 -08:00
|
|
|
Void
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:47:08 -07:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub struct TVar(String);
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
|
|
|
pub enum TConst {
|
|
|
|
Unit,
|
|
|
|
Nat,
|
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
StringT,
|
|
|
|
Bool,
|
|
|
|
Custom(String),
|
|
|
|
}
|
|
|
|
|
2018-03-01 22:32:38 -08:00
|
|
|
impl fmt::Display for Type {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-05-15 21:47:08 -07:00
|
|
|
write!(f, "{:?}", self)
|
|
|
|
/*
|
2018-03-01 22:32:38 -08:00
|
|
|
use self::Type::*;
|
|
|
|
match self {
|
|
|
|
&Const(ref c) => write!(f, "{:?}", c),
|
2018-03-03 11:52:07 -08:00
|
|
|
&Sum(ref types) => {
|
|
|
|
write!(f, "(")?;
|
|
|
|
for item in types.iter().map(|ty| Some(ty)).intersperse(None) {
|
|
|
|
match item {
|
|
|
|
Some(ty) => write!(f, "{}", ty)?,
|
|
|
|
None => write!(f, ",")?,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
write!(f, ")")
|
|
|
|
},
|
2018-03-01 22:32:38 -08:00
|
|
|
&Func(ref a, ref b) => write!(f, "{} -> {}", a, b),
|
2018-05-15 03:25:40 -07:00
|
|
|
//&UVar(ref s) => write!(f, "{}_u", s),
|
|
|
|
//&EVar(ref n) => write!(f, "{}_e", n),
|
2018-03-01 22:32:38 -08:00
|
|
|
&Void => write!(f, "Void")
|
|
|
|
}
|
2018-05-15 21:47:08 -07:00
|
|
|
*/
|
2018-03-01 22:32:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 03:25:40 -07:00
|
|
|
/*
|
2018-03-01 03:35:09 -08:00
|
|
|
#[derive(Default)]
|
|
|
|
struct UVarGenerator {
|
|
|
|
n: u32,
|
|
|
|
}
|
|
|
|
impl UVarGenerator {
|
|
|
|
fn new() -> UVarGenerator {
|
|
|
|
UVarGenerator::default()
|
|
|
|
}
|
|
|
|
fn next(&mut self) -> Type {
|
|
|
|
//TODO handle this in the case where someone wants to make a function with more than 26 variables
|
|
|
|
let s = format!("{}", unsafe { char::from_u32_unchecked(self.n + ('a' as u32)) });
|
|
|
|
self.n += 1;
|
|
|
|
Type::UVar(s)
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 03:25:40 -07:00
|
|
|
*/
|
2018-03-01 03:35:09 -08:00
|
|
|
|
2018-05-15 03:25:40 -07:00
|
|
|
//TODO get rid of this, just instantiate builtin types to the environment
|
2018-02-23 02:30:34 -08:00
|
|
|
impl parsing::TypeName {
|
|
|
|
fn to_type(&self) -> TypeResult<Type> {
|
|
|
|
use self::parsing::TypeSingletonName;
|
|
|
|
use self::parsing::TypeName::*;
|
|
|
|
use self::Type::*; use self::TConst::*;
|
|
|
|
Ok(match self {
|
|
|
|
&Tuple(_) => return Err(format!("Tuples not yet implemented")),
|
|
|
|
&Singleton(ref name) => match name {
|
|
|
|
&TypeSingletonName { ref name, .. } => match &name[..] {
|
2018-05-12 01:44:03 -07:00
|
|
|
"Nat" => Const(Nat),
|
2018-02-23 02:30:34 -08:00
|
|
|
"Int" => Const(Int),
|
|
|
|
"Float" => Const(Float),
|
|
|
|
"Bool" => Const(Bool),
|
|
|
|
"String" => Const(StringT),
|
|
|
|
n => Const(Custom(n.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-24 17:37:23 -08:00
|
|
|
pub type TypeResult<T> = Result<T, String>;
|
2018-02-21 02:31:28 -08:00
|
|
|
|
|
|
|
impl TypeContext {
|
|
|
|
pub fn new() -> TypeContext {
|
2018-05-15 03:25:40 -07:00
|
|
|
TypeContext { bindings: HashMap::new(), /*type_var_count: 0*/ symbol_table: SymbolTable::new() }
|
2018-02-28 05:45:20 -08:00
|
|
|
}
|
2018-05-15 03:25:40 -07:00
|
|
|
/*
|
2018-02-28 05:45:20 -08:00
|
|
|
pub fn fresh(&mut self) -> Type {
|
|
|
|
let ret = self.type_var_count;
|
|
|
|
self.type_var_count += 1;
|
2018-03-01 03:35:09 -08:00
|
|
|
Type::EVar(ret)
|
2018-02-21 02:31:28 -08:00
|
|
|
}
|
2018-05-15 03:25:40 -07:00
|
|
|
*/
|
2018-02-26 21:43:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeContext {
|
2018-02-27 03:01:05 -08:00
|
|
|
pub fn add_top_level_types(&mut self, ast: &parsing::AST) -> TypeResult<()> {
|
2018-05-13 15:42:10 -07:00
|
|
|
use self::parsing::{Statement, TypeName, Variant, TypeSingletonName, TypeBody};
|
2018-02-27 03:01:05 -08:00
|
|
|
use self::parsing::Declaration::*;
|
2018-03-03 11:32:38 -08:00
|
|
|
use self::Type::*;
|
2018-02-27 03:01:05 -08:00
|
|
|
for statement in ast.0.iter() {
|
2018-05-13 15:42:10 -07:00
|
|
|
if let Statement::Declaration(decl) = statement {
|
2018-02-27 03:01:05 -08:00
|
|
|
match decl {
|
2018-05-13 15:42:10 -07:00
|
|
|
FuncSig(signature) | FuncDecl(signature, _) => {
|
2018-05-15 03:25:40 -07:00
|
|
|
/*
|
2018-03-01 03:35:09 -08:00
|
|
|
let mut uvar_gen = UVarGenerator::new();
|
|
|
|
let mut ty: Type = signature.type_anno.as_ref().map(|name: &TypeName| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
|
|
|
for &(_, ref type_name) in signature.params.iter().rev() {
|
|
|
|
let arg_type = type_name.as_ref().map(|name| name.to_type()).unwrap_or_else(|| {Ok(uvar_gen.next())} )?;
|
2018-02-27 03:01:05 -08:00
|
|
|
ty = Func(bx!(arg_type), bx!(ty));
|
|
|
|
}
|
|
|
|
self.bindings.insert(signature.name.clone(), ty);
|
2018-05-15 03:25:40 -07:00
|
|
|
*/
|
2018-05-13 15:54:15 -07:00
|
|
|
self.symbol_table.values.insert(
|
|
|
|
signature.name.clone(),
|
|
|
|
Symbol { name: signature.name.clone(), ty: Func(Box::new(Void), Box::new(Void)) }
|
|
|
|
);
|
2018-05-13 15:42:10 -07:00
|
|
|
},
|
|
|
|
TypeDecl(TypeSingletonName { name, ..}, TypeBody(variants)) => {
|
|
|
|
for var in variants {
|
|
|
|
match var {
|
|
|
|
Variant::UnitStruct(variant_name) => {
|
|
|
|
//TODO will have to make this a function to this type eventually
|
|
|
|
let ty = Type::Const(TConst::Custom(format!("{}", name)));
|
|
|
|
self.symbol_table.values.insert(variant_name.clone(), Symbol { name: variant_name.clone(), ty });
|
|
|
|
},
|
|
|
|
e => return Err(format!("{:?} not supported in typing yet", e)),
|
|
|
|
}
|
|
|
|
}
|
2018-02-27 03:01:05 -08:00
|
|
|
},
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2018-02-26 21:43:53 -08:00
|
|
|
}
|
|
|
|
pub fn debug_symbol_table(&self) -> String {
|
2018-05-13 15:42:10 -07:00
|
|
|
let mut output = format!("Symbol table\n");
|
|
|
|
for (sym, ty) in &self.symbol_table.values {
|
|
|
|
write!(output, "{} -> {:?}\n", sym, ty).unwrap();
|
2018-03-01 22:32:38 -08:00
|
|
|
}
|
|
|
|
output
|
2018-02-26 21:43:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeContext {
|
2018-02-21 02:31:28 -08:00
|
|
|
pub fn type_check_ast(&mut self, ast: &parsing::AST) -> TypeResult<Type> {
|
2018-02-22 19:59:53 -08:00
|
|
|
use self::Type::*; use self::TConst::*;
|
|
|
|
let mut ret_type = Const(Unit);
|
2018-02-21 02:31:28 -08:00
|
|
|
for statement in ast.0.iter() {
|
|
|
|
ret_type = self.type_check_statement(statement)?;
|
|
|
|
}
|
|
|
|
Ok(ret_type)
|
|
|
|
}
|
|
|
|
fn type_check_statement(&mut self, statement: &parsing::Statement) -> TypeResult<Type> {
|
|
|
|
use self::parsing::Statement::*;
|
|
|
|
match statement {
|
2018-05-15 14:26:11 -07:00
|
|
|
ExpressionStatement(expr) => self.infer(expr),
|
|
|
|
Declaration(decl) => self.add_declaration(decl),
|
2018-02-21 02:31:28 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 03:21:58 -08:00
|
|
|
fn add_declaration(&mut self, decl: &parsing::Declaration) -> TypeResult<Type> {
|
|
|
|
use self::parsing::Declaration::*;
|
2018-02-21 02:31:28 -08:00
|
|
|
use self::Type::*;
|
2018-02-22 03:21:58 -08:00
|
|
|
match decl {
|
2018-05-15 03:25:40 -07:00
|
|
|
Binding { name, expr, .. } => {
|
2018-02-22 19:59:53 -08:00
|
|
|
let ty = self.infer(expr)?;
|
2018-02-22 03:21:58 -08:00
|
|
|
self.bindings.insert(name.clone(), ty);
|
|
|
|
},
|
|
|
|
_ => return Err(format!("other formats not done"))
|
|
|
|
}
|
2018-02-22 03:25:05 -08:00
|
|
|
Ok(Void)
|
2018-02-21 02:31:28 -08:00
|
|
|
}
|
2018-02-22 19:59:53 -08:00
|
|
|
fn infer(&mut self, expr: &parsing::Expression) -> TypeResult<Type> {
|
2018-02-21 03:39:40 -08:00
|
|
|
use self::parsing::Expression;
|
|
|
|
match expr {
|
2018-05-15 14:26:11 -07:00
|
|
|
Expression(e, Some(anno)) => {
|
2018-02-23 02:30:34 -08:00
|
|
|
let anno_ty = anno.to_type()?;
|
2018-02-22 03:21:58 -08:00
|
|
|
let ty = self.infer_exprtype(&e)?;
|
2018-02-21 04:32:17 -08:00
|
|
|
self.unify(ty, anno_ty)
|
|
|
|
},
|
2018-05-15 14:26:11 -07:00
|
|
|
Expression(e, None) => self.infer_exprtype(e)
|
2018-02-21 14:14:24 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 03:21:58 -08:00
|
|
|
fn infer_exprtype(&mut self, expr: &parsing::ExpressionType) -> TypeResult<Type> {
|
2018-02-21 14:14:24 -08:00
|
|
|
use self::parsing::ExpressionType::*;
|
|
|
|
use self::Type::*; use self::TConst::*;
|
|
|
|
match expr {
|
2018-05-15 14:26:11 -07:00
|
|
|
NatLiteral(_) => Ok(Const(Nat)),
|
|
|
|
FloatLiteral(_) => Ok(Const(Float)),
|
|
|
|
StringLiteral(_) => Ok(Const(StringT)),
|
|
|
|
BoolLiteral(_) => Ok(Const(Bool)),
|
|
|
|
BinExp(op, lhs, rhs) => { /* remember there are both the haskell convention talk and the write you a haskell ways to do this! */
|
2018-02-24 17:37:23 -08:00
|
|
|
match op.get_type()? {
|
2018-02-23 01:49:37 -08:00
|
|
|
Func(box t1, box Func(box t2, box t3)) => {
|
|
|
|
let lhs_ty = self.infer(lhs)?;
|
|
|
|
let rhs_ty = self.infer(rhs)?;
|
|
|
|
self.unify(t1, lhs_ty)?;
|
|
|
|
self.unify(t2, rhs_ty)?;
|
|
|
|
Ok(t3)
|
|
|
|
},
|
2018-02-26 18:23:10 -08:00
|
|
|
other => Err(format!("{:?} is not a binary function type", other))
|
2018-02-23 01:49:37 -08:00
|
|
|
}
|
2018-02-22 19:59:53 -08:00
|
|
|
},
|
2018-05-15 14:26:11 -07:00
|
|
|
PrefixExp(op, expr) => match op.get_type()? {
|
2018-02-26 02:21:21 -08:00
|
|
|
Func(box t1, box t2) => {
|
|
|
|
let expr_ty = self.infer(expr)?;
|
|
|
|
self.unify(t1, expr_ty)?;
|
|
|
|
Ok(t2)
|
|
|
|
},
|
2018-02-26 18:23:10 -08:00
|
|
|
other => Err(format!("{:?} is not a prefix op function type", other))
|
|
|
|
},
|
2018-05-15 14:26:11 -07:00
|
|
|
Value(name) => {
|
2018-02-26 18:23:10 -08:00
|
|
|
match self.bindings.get(name) {
|
|
|
|
Some(ty) => Ok(ty.clone()),
|
|
|
|
None => Err(format!("No binding found for variable: {}", name)),
|
|
|
|
}
|
2018-02-26 02:21:21 -08:00
|
|
|
},
|
2018-05-15 14:26:11 -07:00
|
|
|
Call { f, arguments } => {
|
2018-02-26 21:28:11 -08:00
|
|
|
let mut tf = self.infer(f)?;
|
|
|
|
for arg in arguments.iter() {
|
|
|
|
match tf {
|
|
|
|
Func(box t, box rest) => {
|
|
|
|
let t_arg = self.infer(arg)?;
|
|
|
|
self.unify(t, t_arg)?;
|
|
|
|
tf = rest;
|
|
|
|
},
|
|
|
|
other => return Err(format!("Function call failed to unify; last type: {:?}", other)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(tf)
|
2018-02-26 21:00:36 -08:00
|
|
|
},
|
2018-05-15 14:26:11 -07:00
|
|
|
TupleLiteral(expressions) => {
|
2018-03-03 11:55:20 -08:00
|
|
|
let mut types = vec![];
|
|
|
|
for expr in expressions {
|
|
|
|
types.push(self.infer(expr)?);
|
|
|
|
}
|
|
|
|
Ok(Sum(types))
|
|
|
|
},
|
2018-02-21 14:14:24 -08:00
|
|
|
_ => Err(format!("Type not yet implemented"))
|
2018-02-21 03:39:40 -08:00
|
|
|
}
|
|
|
|
}
|
2018-02-21 04:32:17 -08:00
|
|
|
fn unify(&mut self, t1: Type, t2: Type) -> TypeResult<Type> {
|
2018-02-22 03:25:05 -08:00
|
|
|
use self::Type::*;// use self::TConst::*;
|
2018-02-21 04:32:17 -08:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2018-05-15 03:25:40 -07:00
|
|
|
|
|
|
|
//fn bind(&mut self, var: TVar, ty: Type) -> `Infer Subst` {
|
|
|
|
//
|
|
|
|
//}
|
2018-02-21 02:31:28 -08:00
|
|
|
}
|
|
|
|
|