This commit is contained in:
David Gonzalez Martin 2025-04-29 06:29:32 -06:00
parent 0eee2a4ff3
commit 9b2294d5a9
4 changed files with 189 additions and 1 deletions

View File

@ -1569,7 +1569,7 @@ pub const Module = struct {
pub fn get_pointer_type(module: *Module, pointer: Pointer) *Type { pub fn get_pointer_type(module: *Module, pointer: Pointer) *Type {
const p = Type.Pointer{ const p = Type.Pointer{
.type = pointer.type, .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 all_types = module.types.get_slice();
const pointer_type = for (module.pointer_types.get_slice()) |pointer_type_index| { const pointer_type = for (module.pointer_types.get_slice()) |pointer_type_index| {

View File

@ -586,6 +586,24 @@ CompileOptions = struct
silent: u1, 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 TypeId = enum
{ {
void, void,
@ -655,6 +673,13 @@ module_noreturn_type = fn (module: &Module) &Type
return module_void_type(module) + 1; 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 is_space = fn (ch: u8) u1
{ {
return ch == ' ' or ch == '\n' or ch == '\t' or ch == '\r'; return ch == ' ' or ch == '\n' or ch == '\t' or ch == '\r';
@ -817,8 +842,159 @@ parse_identifier = fn (module: &Module) []u8
return result; 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 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 emit = fn (module: &Module) void

View File

@ -332,4 +332,5 @@ const names = &[_][]const u8{
"generic_pointer_macro", "generic_pointer_macro",
"break_continue", "break_continue",
"noreturn_macro", "noreturn_macro",
"self_referential_struct",
}; };

View File

@ -0,0 +1,11 @@
S = struct
{
self: &S,
}
[export] main = fn [cc(c)] () s32
{
>s: S = zero;
s.self = &s;
return 0;
}