From 63f2cd5a7cdeb68a91b0683c10b75eb34f92b0b7 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Sat, 19 Apr 2025 20:47:54 -0600 Subject: [PATCH] wip --- src/bootstrap.zig | 31 +++++++++++++++++++++++++++++++ src/compiler.bbb | 11 ++++++++++- tests/enum_name.bbb | 24 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/enum_name.bbb diff --git a/src/bootstrap.zig b/src/bootstrap.zig index eb0a6bf..773f739 100644 --- a/src/bootstrap.zig +++ b/src/bootstrap.zig @@ -207,6 +207,7 @@ pub const ResolvedType = struct { pub const Enumerator = struct { fields: []const Enumerator.Field, string_to_enum: ?StringToEnum = null, + enum_to_string: ?*llvm.Function = null, backing_type: *Type, line: u32, implicit_backing_type: bool, @@ -813,6 +814,7 @@ pub const Value = struct { const Intrinsic = union(Id) { byte_size: *Type, + enum_name: *Value, extend: *Value, integer_max: *Type, int_from_enum: *Value, @@ -829,6 +831,7 @@ pub const Value = struct { const Id = enum { byte_size, + enum_name, extend, integer_max, int_from_enum, @@ -2944,6 +2947,20 @@ pub const Module = struct { }, }; }, + .enum_name => blk: { + module.skip_space(); + module.expect_character(left_parenthesis); + module.skip_space(); + const arg_value = module.parse_value(function, scope, .{}); + module.expect_character(right_parenthesis); + break :blk .{ + .bb = .{ + .intrinsic = .{ + .enum_name = arg_value, + }, + }, + }; + }, .extend => blk: { module.skip_space(); module.expect_character(left_parenthesis); @@ -5483,6 +5500,20 @@ pub const Module = struct { break :blk expected_type; }, + .enum_name => |enum_value| blk: { + const string_type = module.get_slice_type(.{ .type = module.integer_type(8, false) }); + module.typecheck(analysis, string_type); + module.analyze_value_type(function, enum_value, .{}); + if (enum_value.type.?.bb != .enumerator) { + module.report_error(); + } + + // TODO: share globals for enum names between string to enum and enum to string functions + if (enum_value.type.?.bb.enumerator.enum_to_string == null) { + @trap(); + } + break :blk string_type; + }, .extend => |extended_value| blk: { const expected_type = analysis.type orelse module.report_error(); module.analyze_value_type(function, extended_value, .{}); diff --git a/src/compiler.bbb b/src/compiler.bbb index 7196156..dbf48b2 100644 --- a/src/compiler.bbb +++ b/src/compiler.bbb @@ -277,7 +277,7 @@ arena_join_string = fn (arena: &Arena, pieces: [][]u8) []u8 assert(i == size); pointer[i] = 0; - return pointer[0..size]; + return pointer[..size]; } GlobalState = struct @@ -325,6 +325,8 @@ CompileFile = struct silent: u1, } +base_cache_dir = "bb-cache"; + compile_file = fn (arena: &Arena, compile: CompileFile) void { >relative_file_path = compile.relative_file_path; @@ -354,6 +356,13 @@ compile_file = fn (arena: &Arena, compile: CompileFile) void >base_name = relative_file_path[base_start..extension_start]; >is_compiler = string_equal(relative_file_path, "src/compiler.bbb"); + >outputh_path_dir = arena_join_string(arena, [ + base_cache_dir, + #select(is_compiler, "/compiler/", "/"), + #enum_name(compile.build_mode), + "_", + #select(compile.has_debug_info, "di", "nodi"), + ][..]); } [export] main = fn [cc(c)] (argument_count: u32, argv: &&u8) s32 diff --git a/tests/enum_name.bbb b/tests/enum_name.bbb new file mode 100644 index 0000000..2a03c95 --- /dev/null +++ b/tests/enum_name.bbb @@ -0,0 +1,24 @@ +E = enum +{ + my_expected_result, + a, + b, +} + +[extern] memcmp = fn [cc(c)] (a: &u8, b: &u8, byte_count: u64) s32; +string_equal = fn (slice_a: []u8, slice_b: []u8) u1 +{ + >result = slice_a.length == slice_b.length; + if (result) + { + result = memcmp(slice_a.pointer, slice_b.pointer, slice_a.length) == 0; + } + + return result; +} + +[export] main = fn [cc(c)] () s32 +{ + >some_enum: E = .my_expected_result; + return #extend(!string_equal(#enum_name(some_enum), "my_expected_result")); +}