Extended ScriptContext; added Option to builtins

This commit is contained in:
alessandrokonrad
2022-11-17 00:51:52 +01:00
committed by Lucas
parent 72bf27d467
commit 6c5ec9bb25
2 changed files with 105 additions and 9 deletions

View File

@@ -21,6 +21,7 @@ pub const LIST: &str = "List";
pub const NIL: &str = "Nil";
pub const RESULT: &str = "Result";
pub const STRING: &str = "String";
pub const OPTION: &str = "Option";
/// Build a prelude that can be injected
/// into a compiler pipeline
@@ -224,6 +225,60 @@ pub fn prelude(id_gen: &IdGenerator) -> TypeInfo {
),
);
// Option(value, none)
let option_value = generic_var(id_gen.next());
let option_none = generic_var(id_gen.next());
prelude.types.insert(
OPTION.to_string(),
TypeConstructor {
location: Span::empty(),
parameters: vec![option_value.clone(), option_none.clone()],
tipo: option(option_value, option_none),
module: "".to_string(),
public: true,
},
);
prelude.types_constructors.insert(
OPTION.to_string(),
vec!["Some".to_string(), "None".to_string()],
);
let some = generic_var(id_gen.next());
let none = generic_var(id_gen.next());
let _ = prelude.values.insert(
"Some".to_string(),
ValueConstructor::public(
function(vec![some.clone()], option(some, none)),
ValueConstructorVariant::Record {
module: "".into(),
name: "Some".to_string(),
field_map: None::<FieldMap>,
arity: 1,
location: Span::empty(),
constructors_count: 2,
},
),
);
let some = generic_var(id_gen.next());
let none = generic_var(id_gen.next());
let _ = prelude.values.insert(
"None".to_string(),
ValueConstructor::public(
function(vec![none.clone()], option(some, none)),
ValueConstructorVariant::Record {
module: "".into(),
name: "None".to_string(),
field_map: None::<FieldMap>,
arity: 0,
location: Span::empty(),
constructors_count: 2,
},
),
);
prelude
}
@@ -486,6 +541,15 @@ pub fn result(a: Arc<Type>, e: Arc<Type>) -> Arc<Type> {
})
}
pub fn option(a: Arc<Type>, e: Arc<Type>) -> Arc<Type> {
Arc::new(Type::App {
public: true,
name: OPTION.to_string(),
module: "".to_string(),
args: vec![a, e],
})
}
pub fn function(args: Vec<Arc<Type>>, ret: Arc<Type>) -> Arc<Type> {
Arc::new(Type::Fn { ret, args })
}