diff options
Diffstat (limited to 'boot/lower.c')
| -rw-r--r-- | boot/lower.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/boot/lower.c b/boot/lower.c new file mode 100644 index 0000000..1fd1a7e --- /dev/null +++ b/boot/lower.c @@ -0,0 +1,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"); +} |
