From a2f922c59ed40100c82df3abc87a79e18b9da820 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Sat, 7 Jun 2025 09:14:54 -0600 Subject: [PATCH] wip --- src/compiler.bbb | 71 +++++++++++++++++++++++++++++++++- tests/field_parent_pointer.bbb | 43 ++++++++++++++++++++ 2 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tests/field_parent_pointer.bbb diff --git a/src/compiler.bbb b/src/compiler.bbb index 5614547..1d8f922 100644 --- a/src/compiler.bbb +++ b/src/compiler.bbb @@ -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 diff --git a/tests/field_parent_pointer.bbb b/tests/field_parent_pointer.bbb new file mode 100644 index 0000000..4e10524 --- /dev/null +++ b/tests/field_parent_pointer.bbb @@ -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; +}