More basic types + test

This commit is contained in:
greg 2018-11-07 16:39:32 -08:00
parent 0d5ccd21fe
commit fab3fb8ec2

View File

@ -25,7 +25,9 @@ enum TConst {
User(Rc<String>), User(Rc<String>),
Unit, Unit,
Nat, Nat,
Int Int,
Float,
StringT,
} }
impl TConst { impl TConst {
@ -80,6 +82,8 @@ impl TypeContext {
use self::ExpressionType::*; use self::ExpressionType::*;
match expr_type { match expr_type {
NatLiteral(_) => Ok(MonoType::Const(TConst::Nat)), NatLiteral(_) => Ok(MonoType::Const(TConst::Nat)),
FloatLiteral(_) => Ok(MonoType::Const(TConst::Float)),
StringLiteral(_) => Ok(MonoType::Const(TConst::StringT)),
_ => Ok(MonoType::Const(TConst::user("unimplemented"))) _ => Ok(MonoType::Const(TConst::user("unimplemented")))
} }
} }
@ -88,6 +92,26 @@ impl TypeContext {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
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())
}
}
}
#[test] #[test]
fn basic_inference() { fn basic_inference() {