feat: add new Data type to prelude and allow it to unify with any user defined type
This commit is contained in:
parent
123e729137
commit
3f952cdf0e
|
@ -13,13 +13,14 @@ use crate::{
|
||||||
IdGenerator,
|
IdGenerator,
|
||||||
};
|
};
|
||||||
|
|
||||||
const BYTE_ARRAY: &str = "ByteArray";
|
pub const BYTE_ARRAY: &str = "ByteArray";
|
||||||
const BOOL: &str = "Bool";
|
pub const BOOL: &str = "Bool";
|
||||||
const INT: &str = "Int";
|
pub const INT: &str = "Int";
|
||||||
const LIST: &str = "List";
|
pub const DATA: &str = "Data";
|
||||||
const NIL: &str = "Nil";
|
pub const LIST: &str = "List";
|
||||||
const RESULT: &str = "Result";
|
pub const NIL: &str = "Nil";
|
||||||
const STRING: &str = "String";
|
pub const RESULT: &str = "Result";
|
||||||
|
pub const STRING: &str = "String";
|
||||||
|
|
||||||
/// Build a prelude that can be injected
|
/// Build a prelude that can be injected
|
||||||
/// into a compiler pipeline
|
/// into a compiler pipeline
|
||||||
|
@ -46,6 +47,18 @@ pub fn prelude(id_gen: &IdGenerator) -> TypeInfo {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Data
|
||||||
|
prelude.types.insert(
|
||||||
|
DATA.to_string(),
|
||||||
|
TypeConstructor {
|
||||||
|
parameters: vec![],
|
||||||
|
tipo: data(),
|
||||||
|
location: Span::empty(),
|
||||||
|
module: "".to_string(),
|
||||||
|
public: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
// ByteArray
|
// ByteArray
|
||||||
prelude.types.insert(
|
prelude.types.insert(
|
||||||
BYTE_ARRAY.to_string(),
|
BYTE_ARRAY.to_string(),
|
||||||
|
@ -372,14 +385,12 @@ pub fn from_default_function(
|
||||||
DefaultFunction::UnIData => None,
|
DefaultFunction::UnIData => None,
|
||||||
DefaultFunction::UnBData => None,
|
DefaultFunction::UnBData => None,
|
||||||
DefaultFunction::EqualsData => {
|
DefaultFunction::EqualsData => {
|
||||||
let arg = generic_var(id_gen.next());
|
let tipo = function(vec![data(), data()], bool());
|
||||||
|
|
||||||
let tipo = function(vec![arg.clone(), arg], bool());
|
|
||||||
|
|
||||||
Some((tipo, 1))
|
Some((tipo, 1))
|
||||||
}
|
}
|
||||||
DefaultFunction::SerialiseData => {
|
DefaultFunction::SerialiseData => {
|
||||||
let tipo = function(vec![generic_var(id_gen.next())], byte_array());
|
let tipo = function(vec![data()], byte_array());
|
||||||
|
|
||||||
Some((tipo, 1))
|
Some((tipo, 1))
|
||||||
}
|
}
|
||||||
|
@ -412,6 +423,15 @@ pub fn int() -> Arc<Type> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn data() -> Arc<Type> {
|
||||||
|
Arc::new(Type::App {
|
||||||
|
public: true,
|
||||||
|
name: DATA.to_string(),
|
||||||
|
module: "".to_string(),
|
||||||
|
args: vec![],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn byte_array() -> Arc<Type> {
|
pub fn byte_array() -> Arc<Type> {
|
||||||
Arc::new(Type::App {
|
Arc::new(Type::App {
|
||||||
args: vec![],
|
args: vec![],
|
||||||
|
|
|
@ -12,7 +12,7 @@ use crate::{
|
||||||
RecordConstructor, RecordConstructorArg, Span, TypeAlias, TypedDefinition,
|
RecordConstructor, RecordConstructorArg, Span, TypeAlias, TypedDefinition,
|
||||||
UnqualifiedImport, UntypedDefinition, Use, PIPE_VARIABLE,
|
UnqualifiedImport, UntypedDefinition, Use, PIPE_VARIABLE,
|
||||||
},
|
},
|
||||||
builtins::{function, generic_var, unbound_var},
|
builtins::{self, function, generic_var, unbound_var},
|
||||||
tipo::fields::FieldMap,
|
tipo::fields::FieldMap,
|
||||||
IdGenerator,
|
IdGenerator,
|
||||||
};
|
};
|
||||||
|
@ -29,6 +29,14 @@ pub struct ScopeResetData {
|
||||||
local_values: HashMap<String, ValueConstructor>,
|
local_values: HashMap<String, ValueConstructor>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const EXCLUDE_DATA_UNIFY: [&str; 5] = [
|
||||||
|
builtins::INT,
|
||||||
|
builtins::BYTE_ARRAY,
|
||||||
|
builtins::STRING,
|
||||||
|
builtins::BOOL,
|
||||||
|
builtins::LIST,
|
||||||
|
];
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Environment<'a> {
|
pub struct Environment<'a> {
|
||||||
/// Accessors defined in the current module
|
/// Accessors defined in the current module
|
||||||
|
@ -1099,6 +1107,18 @@ impl<'a> Environment<'a> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let (Type::App { name: name1, .. }, Type::App { name: name2, .. }) =
|
||||||
|
(t1.deref(), t2.deref())
|
||||||
|
{
|
||||||
|
if name1 == "Data" && !EXCLUDE_DATA_UNIFY.contains(&name2.as_str()) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if name2 == "Data" && !EXCLUDE_DATA_UNIFY.contains(&name1.as_str()) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Collapse right hand side type links. Left hand side will be collapsed in the next block.
|
// Collapse right hand side type links. Left hand side will be collapsed in the next block.
|
||||||
if let Type::Var { tipo } = t2.deref() {
|
if let Type::Var { tipo } = t2.deref() {
|
||||||
if let TypeVar::Link { tipo } = tipo.borrow().deref() {
|
if let TypeVar::Link { tipo } = tipo.borrow().deref() {
|
||||||
|
|
Loading…
Reference in New Issue