Add trait implementations to the type system.

This commit is contained in:
James Harton 2018-08-29 20:21:09 +12:00
parent 96677aa499
commit 2076d74ece
4 changed files with 35 additions and 0 deletions

View file

@ -52,6 +52,17 @@ impl Context {
self.types.new_trait(name, required)
}
pub fn new_trait_implementation(&mut self, type_id: TypeId, trait_id: TypeId) -> TypeId {
let ty_name_id = self.types.get(type_id.clone()).unwrap().name();
let tr_name_id = self.types.get(trait_id.clone()).unwrap().name();
let ty_name = self.strings.get(ty_name_id).unwrap().clone();
let tr_name = self.strings.get(tr_name_id).unwrap().clone();
let name = self.strings.set(&format!("{}<{}>", tr_name, ty_name));
self.types.new_implementation(name, type_id, trait_id)
}
pub fn get_type(&self, id: TypeId) -> Option<&Type> {
self.types.get(id)
}

View file

@ -9,4 +9,5 @@ pub enum Inner {
Buffer,
Compound(Vec<(StaticStringId, TypeId)>),
Trait(Vec<SignatureId>),
Implementation(TypeId, TypeId),
}

View file

@ -57,6 +57,21 @@ impl Types {
id
}
pub fn new_implementation(
&mut self,
name: StaticStringId,
type_id: TypeId,
trait_id: TypeId,
) -> TypeId {
let id = self.next_type_id();
self.0.insert(
id.clone(),
Type::new_impl(id.clone(), name, type_id, trait_id),
);
id
}
pub fn get(&self, id: TypeId) -> Option<&Type> {
self.0.get(&id)
}

View file

@ -57,6 +57,14 @@ impl Type {
}
}
pub fn new_impl(id: TypeId, name: StaticStringId, type_id: TypeId, trait_id: TypeId) -> Self {
Type {
name: name,
id: id,
inner: Inner::Implementation(type_id, trait_id),
}
}
pub fn id(&self) -> TypeId {
self.id.clone()
}