summaryrefslogtreecommitdiff
path: root/src/typecheck.rs
diff options
context:
space:
mode:
authorNanoTech2016-12-11 23:27:36 -0600
committerNanoTech2017-03-10 23:48:29 -0600
commit5c00b978b0bc0c1eeb64682cba6aa338fea320bf (patch)
treed9935794062180474b5bd6a51ab1d5f4bd864a4e /src/typecheck.rs
parentefd52ef33c15316dbd39390d55022708488a53b8 (diff)
Start implementing error explanations
Diffstat (limited to 'src/typecheck.rs')
-rw-r--r--src/typecheck.rs38
1 files changed, 35 insertions, 3 deletions
diff --git a/src/typecheck.rs b/src/typecheck.rs
index 12555b1..8abc64c 100644
--- a/src/typecheck.rs
+++ b/src/typecheck.rs
@@ -1,6 +1,7 @@
#![allow(non_snake_case)]
use std::collections::BTreeMap;
use std::collections::HashSet;
+use std::fmt;
use context::Context;
use core;
@@ -566,9 +567,9 @@ pub enum TypeMessage<'i, S> {
/// A structured type error that includes context
#[derive(Debug)]
pub struct TypeError<'i, S> {
- context: Context<'i, Expr<'i, S, X>>,
- current: Expr<'i, S, X>,
- type_message: TypeMessage<'i, S>,
+ pub context: Context<'i, Expr<'i, S, X>>,
+ pub current: Expr<'i, S, X>,
+ pub type_message: TypeMessage<'i, S>,
}
impl<'i, S: Clone> TypeError<'i, S> {
@@ -583,3 +584,34 @@ impl<'i, S: Clone> TypeError<'i, S> {
}
}
}
+
+impl<'i, S: fmt::Debug> ::std::error::Error for TypeMessage<'i, S> {
+ fn description(&self) -> &str {
+ match self {
+ &UnboundVariable => "Unbound variable",
+ &InvalidInputType(_) => "Invalid function input",
+ &InvalidOutputType(_) => "Invalid function output",
+ &NotAFunction(_, _) => "Not a function",
+ &TypeMismatch(_, _, _, _) => "Wrong type of function argument",
+ _ => "Unhandled error",
+ }
+ }
+}
+
+impl<'i, S> fmt::Display for TypeMessage<'i, S> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ match self {
+ &UnboundVariable => f.write_str(include_str!("errors/UnboundVariable.txt")),
+ &TypeMismatch(ref e0, ref e1, ref e2, ref e3) => {
+ let template = include_str!("errors/TypeMismatch.txt");
+ let s = template
+ .replace("$txt0", &format!("{}", e0))
+ .replace("$txt1", &format!("{}", e1))
+ .replace("$txt2", &format!("{}", e2))
+ .replace("$txt3", &format!("{}", e3));
+ f.write_str(&s)
+ }
+ _ => f.write_str("Unhandled error message"),
+ }
+ }
+}