From 9b2294d5a9ec471e9b954401f544f6e045967419 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Tue, 29 Apr 2025 06:29:32 -0600 Subject: [PATCH] wip --- src/bootstrap.zig | 2 +- src/compiler.bbb | 176 ++++++++++++++++++++++++++++++ src/main.zig | 1 + tests/self_referential_struct.bbb | 11 ++ 4 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 tests/self_referential_struct.bbb diff --git a/src/bootstrap.zig b/src/bootstrap.zig index 53df3a0..266bc9b 100644 --- a/src/bootstrap.zig +++ b/src/bootstrap.zig @@ -1569,7 +1569,7 @@ pub const Module = struct { pub fn get_pointer_type(module: *Module, pointer: Pointer) *Type { const p = Type.Pointer{ .type = pointer.type, - .alignment = if (pointer.alignment) |a| a else if (pointer.type.bb != .unresolved) pointer.type.get_byte_alignment() else null, + .alignment = if (pointer.alignment) |a| a else if (pointer.type.bb != .unresolved and pointer.type.bb != .forward_declaration) pointer.type.get_byte_alignment() else null, }; const all_types = module.types.get_slice(); const pointer_type = for (module.pointer_types.get_slice()) |pointer_type_index| { diff --git a/src/compiler.bbb b/src/compiler.bbb index c30f9a3..93efee1 100644 --- a/src/compiler.bbb +++ b/src/compiler.bbb @@ -586,6 +586,24 @@ CompileOptions = struct silent: u1, } +ScopeKind = enum +{ + global, + function, + local, + for_each, + macro_declaration, + macro_instantiation_function, +} + +Scope = struct +{ + parent: &Scope, + line: u32, + column: u32, + kind: ScopeKind, +} + TypeId = enum { void, @@ -655,6 +673,13 @@ module_noreturn_type = fn (module: &Module) &Type return module_void_type(module) + 1; } +left_bracket: u8 = '['; +right_bracket: u8 = '['; +left_parenthesis: u8 = '('; +right_parenthesis: u8 = ')'; +left_brace: u8 = '{'; +right_brace: u8 = '}'; + is_space = fn (ch: u8) u1 { return ch == ' ' or ch == '\n' or ch == '\t' or ch == '\r'; @@ -817,8 +842,159 @@ parse_identifier = fn (module: &Module) []u8 return result; } +parse_type = fn (module: &Module) &Type +{ +} + +ValueBuilder = struct +{ + foo: u32, +} + +parse_value = fn (module: &Module, scope: &Scope, v: ValueBuilder) &Value +{ +} + +GlobalKeyword = enum +{ + export, + extern, +} + +GlobalKind = enum +{ + bits, + enum, + fn, + macro, + struct, + typealias, + union, +} + parse = fn (module: &Module) void { + while (1) + { + skip_space(module); + + if (module.offset == module.content.length) + { + break; + } + + >is_export: u1 = 0; + >is_extern: u1 = 0; + + >global_line = get_line(module); + >global_column = get_column(module); + + if (consume_character_if_match(module, left_bracket)) + { + while (module.offset < module.content.length) + { + >global_keyword_string = parse_identifier(module); + >global_keyword_s2e = #string_to_enum(GlobalKeyword, global_keyword_string); + if (!global_keyword_s2e.is_valid) + { + report_error(); + } + + >global_keyword = global_keyword_s2e.enum_value; + switch (global_keyword) + { + .export => { is_export = 1; } + .extern => { is_extern = 1; } + } + + if (consume_character_if_match(module, right_bracket)) + { + break; + } + + report_error(); + } + + skip_space(module); + } + + >global_name = parse_identifier(module); + + // TODO: check if the name is repeated in global namespace + + skip_space(module); + + >global_type: &Type = zero; + + if (consume_character_if_match(module, ':')) + { + skip_space(module); + + global_type = parse_type(module); + + skip_space(module); + } + + expect_character(module, '='); + + skip_space(module); + + >has_global_keyword: u1 = 0; + + if (is_identifier_start(module.content[module.offset])) + { + >identifier_offset = module.offset; + + >global_string = parse_identifier(module); + + skip_space(module); + + >global_kind_s2e = #string_to_enum(GlobalKind, global_string); + + if (global_kind_s2e.is_valid) + { + >global_kind = global_kind_s2e.enum_value; + + switch (global_kind) + { + .bits => { + #trap(); + }, + .enum => { + #trap(); + }, + .fn => { + #trap(); + }, + .macro => { + #trap(); + }, + .struct => { + #trap(); + }, + .typealias => { + #trap(); + }, + .union => { + #trap(); + }, + } + } + else + { + module.offset = identifier_offset; + } + } + + if (!has_global_keyword) + { + >v = parse_value(&module.scope, zero); + skip_space(module); + expect_character(module, ';'); + + #trap(); + } + } } emit = fn (module: &Module) void diff --git a/src/main.zig b/src/main.zig index 1f23525..c207f41 100644 --- a/src/main.zig +++ b/src/main.zig @@ -332,4 +332,5 @@ const names = &[_][]const u8{ "generic_pointer_macro", "break_continue", "noreturn_macro", + "self_referential_struct", }; diff --git a/tests/self_referential_struct.bbb b/tests/self_referential_struct.bbb new file mode 100644 index 0000000..f2f5463 --- /dev/null +++ b/tests/self_referential_struct.bbb @@ -0,0 +1,11 @@ +S = struct +{ + self: &S, +} + +[export] main = fn [cc(c)] () s32 +{ + >s: S = zero; + s.self = &s; + return 0; +}