Add 'Ordering' data-type to the Prelude.

Useful to have around to build comparison functions.
This commit is contained in:
KtorZ 2023-02-09 11:07:30 +01:00
parent fd14da0720
commit 62fe321ddf
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
2 changed files with 107 additions and 0 deletions

View File

@ -22,6 +22,7 @@ pub const VOID: &str = "Void";
pub const RESULT: &str = "Result"; pub const RESULT: &str = "Result";
pub const STRING: &str = "String"; pub const STRING: &str = "String";
pub const OPTION: &str = "Option"; pub const OPTION: &str = "Option";
pub const ORDERING: &str = "Ordering";
/// Build a prelude that can be injected /// Build a prelude that can be injected
/// into a compiler pipeline /// into a compiler pipeline
@ -119,6 +120,72 @@ pub fn prelude(id_gen: &IdGenerator) -> TypeInfo {
}, },
); );
// Ordering
prelude.types_constructors.insert(
ORDERING.to_string(),
vec![
"Less".to_string(),
"Equal".to_string(),
"Greater".to_string(),
],
);
prelude.values.insert(
"Less".to_string(),
ValueConstructor::public(
ordering(),
ValueConstructorVariant::Record {
module: "".into(),
name: "Less".to_string(),
field_map: None::<FieldMap>,
arity: 0,
location: Span::empty(),
constructors_count: 3,
},
),
);
prelude.values.insert(
"Equal".to_string(),
ValueConstructor::public(
ordering(),
ValueConstructorVariant::Record {
module: "".into(),
name: "Equal".to_string(),
field_map: None::<FieldMap>,
arity: 0,
location: Span::empty(),
constructors_count: 3,
},
),
);
prelude.values.insert(
"Greater".to_string(),
ValueConstructor::public(
ordering(),
ValueConstructorVariant::Record {
module: "".into(),
name: "Greater".to_string(),
field_map: None::<FieldMap>,
arity: 0,
location: Span::empty(),
constructors_count: 3,
},
),
);
prelude.types.insert(
ORDERING.to_string(),
TypeConstructor {
location: Span::empty(),
parameters: vec![],
tipo: ordering(),
module: "".to_string(),
public: true,
},
);
// not // not
prelude.values.insert( prelude.values.insert(
"not".to_string(), "not".to_string(),
@ -908,6 +975,15 @@ pub fn option(a: Arc<Type>) -> Arc<Type> {
}) })
} }
pub fn ordering() -> Arc<Type> {
Arc::new(Type::App {
public: true,
name: ORDERING.to_string(),
module: "".to_string(),
args: vec![],
})
}
pub fn function(args: Vec<Arc<Type>>, ret: Arc<Type>) -> Arc<Type> { pub fn function(args: Vec<Arc<Type>>, ret: Arc<Type>) -> Arc<Type> {
Arc::new(Type::Fn { ret, args }) Arc::new(Type::Fn { ret, args })
} }

View File

@ -127,6 +127,37 @@ impl Annotated<Schema> {
]))), ]))),
}), }),
"Ordering" => Ok(Annotated {
title: Some("Ordering".to_string()),
description: None,
annotated: Schema::Data(Some(Data::AnyOf(vec![
Annotated {
title: Some("Less".to_string()),
description: None,
annotated: Constructor {
index: 0,
fields: vec![],
},
},
Annotated {
title: Some("Equal".to_string()),
description: None,
annotated: Constructor {
index: 1,
fields: vec![],
},
},
Annotated {
title: Some("Greater".to_string()),
description: None,
annotated: Constructor {
index: 2,
fields: vec![],
},
},
]))),
}),
"Option" => { "Option" => {
let generic = let generic =
Annotated::from_type(modules, args.get(0).unwrap(), type_parameters) Annotated::from_type(modules, args.get(0).unwrap(), type_parameters)