Add builtin functions to the prelude
Starting with 'not', will add 'always' and 'identity' later.
This commit is contained in:
parent
602a6a03a8
commit
1701cacb52
|
@ -5,7 +5,9 @@ use strum::IntoEnumIterator;
|
||||||
use uplc::builtins::DefaultFunction;
|
use uplc::builtins::DefaultFunction;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{ModuleKind, Span},
|
ast::{Arg, ArgName, Function, ModuleKind, Span, TypedFunction, UnOp},
|
||||||
|
builder::FunctionAccessKey,
|
||||||
|
expr::TypedExpr,
|
||||||
tipo::{
|
tipo::{
|
||||||
fields::FieldMap, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor,
|
fields::FieldMap, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor,
|
||||||
ValueConstructorVariant,
|
ValueConstructorVariant,
|
||||||
|
@ -119,6 +121,21 @@ pub fn prelude(id_gen: &IdGenerator) -> TypeInfo {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
prelude.values.insert(
|
||||||
|
"not".to_string(),
|
||||||
|
ValueConstructor::public(
|
||||||
|
function(vec![bool()], bool()),
|
||||||
|
ValueConstructorVariant::ModuleFn {
|
||||||
|
name: "not".to_string(),
|
||||||
|
field_map: None,
|
||||||
|
module: "".to_string(),
|
||||||
|
arity: 1,
|
||||||
|
location: Span::empty(),
|
||||||
|
builtin: None,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
// List(a)
|
// List(a)
|
||||||
let list_parameter = generic_var(id_gen.next());
|
let list_parameter = generic_var(id_gen.next());
|
||||||
prelude.types.insert(
|
prelude.types.insert(
|
||||||
|
@ -454,6 +471,59 @@ pub fn from_default_function(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn prelude_functions() -> HashMap<FunctionAccessKey, TypedFunction> {
|
||||||
|
let mut functions = HashMap::new();
|
||||||
|
|
||||||
|
// /// Negate the argument. Useful for map/fold and pipelines.
|
||||||
|
// pub fn not(self: Bool) -> Bool {
|
||||||
|
// !self
|
||||||
|
// }
|
||||||
|
functions.insert(
|
||||||
|
FunctionAccessKey {
|
||||||
|
module_name: "".to_string(),
|
||||||
|
function_name: "not".to_string(),
|
||||||
|
variant_name: "".to_string(),
|
||||||
|
},
|
||||||
|
Function {
|
||||||
|
arguments: vec![Arg {
|
||||||
|
arg_name: ArgName::Named {
|
||||||
|
name: "self".to_string(),
|
||||||
|
label: "self".to_string(),
|
||||||
|
location: Span::empty(),
|
||||||
|
},
|
||||||
|
location: Span::empty(),
|
||||||
|
annotation: None,
|
||||||
|
tipo: bool(),
|
||||||
|
}],
|
||||||
|
doc: None,
|
||||||
|
location: Span::empty(),
|
||||||
|
name: "not".to_string(),
|
||||||
|
public: true,
|
||||||
|
return_annotation: None,
|
||||||
|
return_type: bool(),
|
||||||
|
end_position: 0,
|
||||||
|
body: TypedExpr::UnOp {
|
||||||
|
location: Span::empty(),
|
||||||
|
tipo: bool(),
|
||||||
|
op: UnOp::Negate,
|
||||||
|
value: Box::new(TypedExpr::Var {
|
||||||
|
location: Span::empty(),
|
||||||
|
constructor: ValueConstructor {
|
||||||
|
public: true,
|
||||||
|
tipo: bool(),
|
||||||
|
variant: ValueConstructorVariant::LocalVariable {
|
||||||
|
location: Span::empty(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
name: "self".to_string(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
functions
|
||||||
|
}
|
||||||
|
|
||||||
pub fn int() -> Arc<Type> {
|
pub fn int() -> Arc<Type> {
|
||||||
Arc::new(Type::App {
|
Arc::new(Type::App {
|
||||||
public: true,
|
public: true,
|
||||||
|
|
|
@ -486,8 +486,12 @@ where
|
||||||
let mut imports = HashMap::new();
|
let mut imports = HashMap::new();
|
||||||
let mut constants = HashMap::new();
|
let mut constants = HashMap::new();
|
||||||
|
|
||||||
let option_data_type = TypedDataType::option(generic_var(self.id_gen.next()));
|
let prelude_functions = builtins::prelude_functions();
|
||||||
|
for (access_key, func) in prelude_functions.iter() {
|
||||||
|
functions.insert(access_key.clone(), func);
|
||||||
|
}
|
||||||
|
|
||||||
|
let option_data_type = TypedDataType::option(generic_var(self.id_gen.next()));
|
||||||
data_types.insert(
|
data_types.insert(
|
||||||
DataTypeKey {
|
DataTypeKey {
|
||||||
module_name: "".to_string(),
|
module_name: "".to_string(),
|
||||||
|
@ -582,6 +586,11 @@ where
|
||||||
let mut imports = HashMap::new();
|
let mut imports = HashMap::new();
|
||||||
let mut constants = HashMap::new();
|
let mut constants = HashMap::new();
|
||||||
|
|
||||||
|
let prelude_functions = builtins::prelude_functions();
|
||||||
|
for (access_key, func) in prelude_functions.iter() {
|
||||||
|
functions.insert(access_key.clone(), func);
|
||||||
|
}
|
||||||
|
|
||||||
let option_data_type = TypedDataType::option(generic_var(self.id_gen.next()));
|
let option_data_type = TypedDataType::option(generic_var(self.id_gen.next()));
|
||||||
|
|
||||||
data_types.insert(
|
data_types.insert(
|
||||||
|
|
Loading…
Reference in New Issue