Pass through type info to repl

This commit is contained in:
greg 2019-02-10 05:31:58 -08:00
parent 06e9452718
commit cf91f74912
2 changed files with 27 additions and 6 deletions

View File

@ -167,7 +167,10 @@ fn typechecking(input: ast::AST, handle: &mut Schala, comp: Option<&mut Unfinish
let result = handle.type_context.typecheck(&input);
comp.map(|comp| {
let artifact = TraceArtifact::new("type", format!("{:?}", result));
let artifact = TraceArtifact::new("type", match result {
Ok(typestring) => typestring,
Err(err) => format!("Type error: {}", err)
});
comp.add_artifact(artifact);
});

View File

@ -44,9 +44,22 @@ impl Type {
fn arrow(a: Type, b: Type) -> Type {
Type::Arrow(Box::new(a), Box::new(b))
}
fn to_string(&self) -> String {
use self::Type::*;
use self::TypeConst::*;
match self {
Const(Unit) => format!("()"),
Const(Nat) => format!("Nat"),
Const(Int) => format!("Int"),
Const(Float) => format!("Float"),
Const(StringT) => format!("String"),
Const(Bool) => format!("Bool"),
Const(Order) => format!("Order"),
_ => format!("UNKNOWN TYPE"),
}
}
}
/*
/// `Type` is parameterized by whether the type variables can be just universal, or universal or
@ -146,7 +159,7 @@ impl<'a> TypeContext<'a> {
for statement in ast.0.iter() {
returned_type = self.typecheck_statement(statement.node()).map_err(|err| { err.msg })?
}
Ok(format!("{:?}", returned_type))
Ok(returned_type.to_string())
}
fn typecheck_statement(&mut self, statement: &Statement) -> InferResult<Type> {
@ -156,7 +169,7 @@ impl<'a> TypeContext<'a> {
}
}
fn typecheck_decl(&mut self, decl: &Declaration) -> InferResult<Type> {
fn typecheck_decl(&mut self, _decl: &Declaration) -> InferResult<Type> {
Ok(Type::Const(TypeConst::Unit))
}
@ -171,6 +184,11 @@ impl<'a> TypeContext<'a> {
}
fn typecheck_expr_type(&mut self, expr: &ExpressionType) -> InferResult<Type> {
Ok(Type::Const(TypeConst::Unit))
use self::ExpressionType::*;
Ok(match expr {
NatLiteral(_) => Type::Const(TypeConst::Nat),
BoolLiteral(_) => Type::Const(TypeConst::Bool),
_ => Type::Const(TypeConst::Unit)
})
}
}