Add builtin functions to the prelude

Starting with 'not', will add 'always' and 'identity' later.
This commit is contained in:
KtorZ 2022-12-29 12:05:30 +01:00
parent 602a6a03a8
commit 1701cacb52
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 81 additions and 2 deletions

View File

@ -5,7 +5,9 @@ use strum::IntoEnumIterator;
use uplc::builtins::DefaultFunction;
use crate::{
ast::{ModuleKind, Span},
ast::{Arg, ArgName, Function, ModuleKind, Span, TypedFunction, UnOp},
builder::FunctionAccessKey,
expr::TypedExpr,
tipo::{
fields::FieldMap, Type, TypeConstructor, TypeInfo, TypeVar, ValueConstructor,
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)
let list_parameter = generic_var(id_gen.next());
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> {
Arc::new(Type::App {
public: true,

View File

@ -486,8 +486,12 @@ where
let mut imports = 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(
DataTypeKey {
module_name: "".to_string(),
@ -582,6 +586,11 @@ where
let mut imports = 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()));
data_types.insert(