This commit is contained in:
David Gonzalez Martin 2025-06-07 09:14:54 -06:00
parent 0b1a070250
commit a2f922c59e
2 changed files with 112 additions and 2 deletions

View File

@ -2264,6 +2264,28 @@ Statement = struct
id: StatementId,
}
scope_to_block = fn (scope: &Scope) &Block
{
assert(scope.kind == .local);
>result: &Block = #field_parent_pointer(scope, "scope");
return result;
}
new_local = fn (module: &Module, scope: &Scope) &Local
{
>result = arena_allocate[Local](module.arena, 1);
result.& = zero;
switch (scope.kind)
{
.local =>
{
>block = scope_to_block(scope);
#trap();
},
}
}
new_global = fn (module: &Module) &Global
{
>result = arena_allocate[Global](module.arena, 1);
@ -3632,7 +3654,43 @@ parse_statement = fn (module: &Module, scope: &Scope) &Statement
{
'>' =>
{
#trap();
module.offset += 1;
skip_space(module);
>local_name = parse_identifier(module);
skip_space(module);
>local_type: &Type = zero;
if (consume_character_if_match(module, ':'))
{
skip_space(module);
local_type = parse_type(module, scope);
skip_space(module);
}
expect_character(module, '=');
>initial_value = parse_value(module, scope, zero);
>local = new_local(module);
local.& = {
.variable = {
.initial_value = initial_value,
.type = local_type,
.scope = scope,
.name = local_name,
.line = statement_line,
.column = statement_column,
zero,
},
zero,
};
statement.content = {
.local = local,
};
statement.id = .local;
},
'#' =>
{
@ -6480,7 +6538,16 @@ names: [_][]u8 = [
"constant_xor",
"constant_shift_left",
"constant_shift_right",
"minimal_stack",
"minimal_stack_arithmetic",
"minimal_stack_arithmetic2",
"minimal_stack_arithmetic3",
"stack_negation",
"stack_add",
"stack_sub",
"extend",
"integer_max",
"integer_hex",
];
[export] main = fn [cc(c)] (argument_count: u32, argv: &&u8, envp: &&u8) s32

View File

@ -0,0 +1,43 @@
S = struct
{
a: u8,
b: u32,
c: u8,
}
assert = fn (ok: u1) void
{
if (!ok) #trap();
}
[export] main = fn [cc(c)] () s32
{
>s: S = {
.a = 241,
.b = 12356,
.c = 128,
};
>p_a = &s.a;
>p_a_struct: &S = #field_parent_pointer(p_a, "a");
assert(p_a_struct == &s);
assert(p_a_struct.a == s.a);
assert(p_a_struct.b == s.b);
assert(p_a_struct.c == s.c);
>p_b = &s.b;
>p_b_struct: &S = #field_parent_pointer(p_b, "b");
assert(p_b_struct == &s);
assert(p_b_struct.a == s.a);
assert(p_b_struct.b == s.b);
assert(p_b_struct.c == s.c);
>p_c = &s.c;
>p_c_struct: &S = #field_parent_pointer(p_c, "c");
assert(p_c_struct == &s);
assert(p_c_struct.a == s.a);
assert(p_c_struct.b == s.b);
assert(p_c_struct.c == s.c);
return 0;
}