Print out types to REPL

This commit is contained in:
greg 2018-11-07 13:44:28 -08:00
parent 9a09f40222
commit 69b7b9f528
2 changed files with 16 additions and 7 deletions

View File

@ -128,9 +128,16 @@ fn symbol_table(handle: &mut Schala, input: ast::AST, comp: Option<&mut Unfinish
}
}
fn typechecking(handle: &mut Schala, input: ast::AST, _comp: Option<&mut UnfinishedComputation>) -> Result<ast::AST, String> {
fn typechecking(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result<ast::AST, String> {
handle.type_context.typecheck(&input).map(|_| input)
handle.type_context.typecheck(&input).map(|ty| {
comp.map(|comp| {
let artifact = TraceArtifact::new("type", ty);
comp.add_artifact(artifact);
});
input
})
}
fn ast_reducing(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result<reduced_ast::ReducedAST, String> {

View File

@ -13,12 +13,14 @@ type TypeResult<T> = Result<T, TypeError>;
#[derive(Debug, Clone)]
struct TypeError { }
#[derive(Debug, Clone)]
enum MonoType {
Var(Rc<String>),
Const(Rc<String>),
Arrow(Rc<String>)
}
#[derive(Debug, Clone)]
struct PolyType {
vars: Vec<Rc<String>>,
ty: MonoType
@ -29,9 +31,9 @@ impl TypeContext {
TypeContext { }
}
pub fn typecheck(&mut self, ast: &AST) -> Result<(), String> {
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {
match self.infer_ast(ast) {
Ok(_) => Ok(()),
Ok(t) => Ok(format!("{:?}", t)),
Err(err) => Err(format!("Type error: {:?}", err))
}
}
@ -41,7 +43,7 @@ impl TypeContext {
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 {
output = match statement {
Statement::ExpressionStatement(ref expr) => self.infer_expr(expr)?,
Statement::Declaration(ref decl) => self.infer_decl(decl)?,
};
@ -57,14 +59,14 @@ impl TypeContext {
}
fn infer_decl(&mut self, expr: &Declaration) -> TypeResult<MonoType> {
unimplemented!()
Ok(MonoType::Const(Rc::new("Unimplemented".to_string())))
}
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!()
_ => Ok(MonoType::Const(Rc::new("Unimplemented".to_string())))
}
}
}