1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/*
* lowering pass to create an ir from a catskill source tree.
*
* the idea is to fully de-sugar a catskill source file
* into an ir that can be very easily re-expressed into
* a low-level language, like our transpilation target, c.
*
* the lowering pass is itself split into two passes, one
* initial pass, which collects all top-level type & function
* declarations to build up a table of all available language objects,
* and then a second pass going over each function body.
* this de-couples usage of the functions and types from their ordering
* allowing for more free-flowing files than c would allow.
*
* additionally, we handle type dependencies by collecting every
* direct reference a type has to another, and topologically
* sort the types to create the correct ordering of them,
* pointing out any unbreakable cycles to the user as they come up.
*
* Copyright (c) 2026, Mel G. <mel@rnrd.eu>
*
* SPDX-License-Identifier: MPL-2.0
*/
#pragma once
#include "catboot.h"
Type_Id
lower_seed_primitive(struct Unit* unit, const ascii* name)
{
Type_Id id = array_length(&unit->types.entries);
struct String name_str = string_from_static_c_string(name);
struct Type* type = type_new(id, TYPE_PRIMITIVE, name_str, span_empty());
array_push(&unit->types.entries, &type);
struct Type_Name_To_Id mapping = { .name = name_str, .id = id };
array_push(&unit->types.by_name, &mapping);
return id;
}
void
lower_tree(struct Tree* tree, struct Source_File source, struct Unit* unit)
{
// TODO: implement!
(void)tree;
(void)source;
unit->types.entries = array_new(struct Type*, 256);
unit->types.by_hash = array_new(struct Type_Hash_To_Id, 256);
unit->types.by_name = array_new(struct Type_Name_To_Id, 256);
unit->functions.entries = array_new(struct Function*, 256);
unit->functions.by_name = array_new(struct Function_Name_To_Id, 256);
unit->imports = array_new(struct Import, 32);
unit->type_emission_order = array_new(Type_Id, 256);
unit->had_error = false;
unit->diagnostics = array_new(struct Diagnostic, 64);
unit->types.primitive_int_id = lower_seed_primitive(unit, "int");
unit->types.primitive_uint_id = lower_seed_primitive(unit, "uint");
unit->types.primitive_bool_id = lower_seed_primitive(unit, "bool");
unit->types.primitive_string_id = lower_seed_primitive(unit, "string");
unit->types.primitive_float_id = lower_seed_primitive(unit, "float");
unit->types.primitive_byte_id = lower_seed_primitive(unit, "byte");
unit->types.primitive_ascii_id = lower_seed_primitive(unit, "ascii");
unit->types.primitive_void_id = lower_seed_primitive(unit, "void");
}
|