Type name infra

This commit is contained in:
greg 2019-02-10 07:05:01 -08:00
parent afd9aa52c5
commit a80e1bd706

View File

@ -17,7 +17,7 @@ type InferResult<T> = Result<T, TypeError>;
struct TypeError { msg: String } struct TypeError { msg: String }
impl TypeError { impl TypeError {
fn new<A>(msg: &str) -> InferResult<A> { fn new<A>(msg: &str) -> InferResult<A> { //TODO make these kinds of error-producing functions CoW-ready
Err(TypeError { msg: msg.to_string() }) Err(TypeError { msg: msg.to_string() })
} }
} }
@ -40,6 +40,7 @@ pub enum TypeConst {
UserDefined UserDefined
} }
//TODO find a better way to capture the to/from string logic
impl Type { impl Type {
fn to_string(&self) -> String { fn to_string(&self) -> String {
use self::Type::*; use self::Type::*;
@ -55,6 +56,21 @@ impl Type {
_ => format!("UNKNOWN TYPE"), _ => format!("UNKNOWN TYPE"),
} }
} }
fn from_string(string: &str) -> Option<Type> {
use self::Type::*;
use self::TypeConst::*;
Some(match string {
"()" | "Unit" => Const(Unit),
"Nat" => Const(Nat),
"Int" => Const(Int),
"Float" => Const(Float),
"String" => Const(StringT),
"Bool" => Const(Bool),
"Order" => Const(Order),
_ => return None
})
}
} }
/* /*
@ -151,7 +167,17 @@ impl<'a> TypeContext<'a> {
} }
fn get_type_from_name(&self, name: &TypeIdentifier) -> InferResult<Type> { fn get_type_from_name(&self, name: &TypeIdentifier) -> InferResult<Type> {
Ok(Type::Const(TypeConst::Unit)) use self::TypeIdentifier::*;
Ok(match name {
Singleton(TypeSingletonName { name, params }) => {
match Type::from_string(&name) {
Some(ty) => ty,
None => return TypeError::new("Unknown type name")
}
},
Tuple(_) => return TypeError::new("tuples aren't ready yet"),
})
} }
pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> { pub fn typecheck(&mut self, ast: &AST) -> Result<String, String> {