1use id_arena::Arena;
2
3use crate::{Alias, Atom, Pair, Type, TypeId, Union};
4
5#[derive(Debug, Clone, Copy)]
6pub struct BuiltinTypes {
7 pub unresolved: TypeId,
8 pub atom: TypeId,
9 pub bytes: TypeId,
10 pub bytes32: TypeId,
11 pub public_key: TypeId,
12 pub int: TypeId,
13 pub bool_true: TypeId,
14 pub bool_false: TypeId,
15 pub bool: TypeId,
16 pub nil: TypeId,
17 pub any: TypeId,
18 pub any_pair: TypeId,
19 pub list: TypeId,
20 pub list_pair: TypeId,
21 pub list_generic: TypeId,
22}
23
24impl BuiltinTypes {
25 pub fn new(arena: &mut Arena<Type>) -> Self {
26 let unresolved = arena.alloc(Type::Unresolved);
27 let atom = arena.alloc(Type::Atom(Atom::ANY));
28 let bytes = arena.alloc(Type::Atom(Atom::BYTES));
29 let bytes32 = arena.alloc(Type::Atom(Atom::BYTES_32));
30 let public_key = arena.alloc(Type::Atom(Atom::PUBLIC_KEY));
31 let int = arena.alloc(Type::Atom(Atom::INT));
32 let bool_true = arena.alloc(Type::Atom(Atom::TRUE));
33 let bool_false = arena.alloc(Type::Atom(Atom::FALSE));
34 let bool = arena.alloc(Type::Union(Union::new(vec![bool_true, bool_false])));
35 let nil = arena.alloc(Type::Atom(Atom::NIL));
36
37 let any = arena.alloc(Type::Unresolved);
38 let any_pair = arena.alloc(Type::Pair(Pair::new(any, any)));
39 *arena.get_mut(any).unwrap() = Type::Union(Union::new(vec![atom, any_pair]));
40
41 let list = arena.alloc(Type::Unresolved);
42 let list_generic = arena.alloc(Type::Generic);
43 let list_pair = arena.alloc(Type::Pair(Pair::new(list_generic, list)));
44 let list_union = arena.alloc(Type::Union(Union::new(vec![nil, list_pair])));
45 *arena.get_mut(list).unwrap() = Type::Alias(Alias {
46 name: None,
47 inner: list_union,
48 generics: vec![list_generic],
49 });
50
51 Self {
52 unresolved,
53 atom,
54 bytes,
55 bytes32,
56 public_key,
57 int,
58 bool_true,
59 bool_false,
60 bool,
61 nil,
62 any,
63 any_pair,
64 list,
65 list_pair,
66 list_generic,
67 }
68 }
69}