Add deriving of immediates for the Hash ability

This commit is contained in:
Ayaz Hafiz 2022-10-04 11:09:08 -05:00
parent 7bd6523175
commit 427528e659
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
7 changed files with 134 additions and 0 deletions

View file

@ -15,10 +15,12 @@
pub mod decoding;
pub mod encoding;
pub mod hash;
mod util;
use decoding::{FlatDecodable, FlatDecodableKey};
use encoding::{FlatEncodable, FlatEncodableKey};
use hash::{FlatHash, FlatHashKey};
use roc_module::symbol::Symbol;
use roc_types::subs::{Subs, Variable};
@ -37,6 +39,7 @@ pub enum DeriveError {
pub enum DeriveKey {
ToEncoder(FlatEncodableKey),
Decoder(FlatDecodableKey),
Hash(FlatHashKey),
}
impl DeriveKey {
@ -44,6 +47,7 @@ impl DeriveKey {
match self {
DeriveKey::ToEncoder(key) => format!("toEncoder_{}", key.debug_name()),
DeriveKey::Decoder(key) => format!("decoder_{}", key.debug_name()),
DeriveKey::Hash(key) => format!("hash_{}", key.debug_name()),
}
}
}
@ -64,6 +68,7 @@ pub enum Derived {
pub enum DeriveBuiltin {
ToEncoder,
Decoder,
Hash,
}
impl TryFrom<Symbol> for DeriveBuiltin {
@ -73,6 +78,7 @@ impl TryFrom<Symbol> for DeriveBuiltin {
match value {
Symbol::ENCODE_TO_ENCODER => Ok(DeriveBuiltin::ToEncoder),
Symbol::DECODE_DECODER => Ok(DeriveBuiltin::Decoder),
Symbol::HASH_HASH => Ok(DeriveBuiltin::Hash),
_ => Err(value),
}
}
@ -93,6 +99,10 @@ impl Derived {
FlatDecodable::Immediate(imm) => Ok(Derived::Immediate(imm)),
FlatDecodable::Key(repr) => Ok(Derived::Key(DeriveKey::Decoder(repr))),
},
DeriveBuiltin::Hash => match hash::FlatHash::from_var(subs, var)? {
FlatHash::Immediate(imm) => Ok(Derived::Immediate(imm)),
FlatHash::Key(repr) => Ok(Derived::Key(DeriveKey::Hash(repr))),
},
}
}
}