fix scope resolution

This commit is contained in:
2026-02-06 01:41:03 -06:00
parent 9a70a12d82
commit 1c38699b5a

View File

@@ -32509,13 +32509,25 @@ static cJSON *mach_gen_program (MachGenState *s, cJSON *ast) {
mach_scan_vars (s, stmt);
}
/* Pre-register function names as local variables (hoisting) */
cJSON *functions = cJSON_GetObjectItem (ast, "functions");
cJSON *func_node;
cJSON_ArrayForEach (func_node, functions) {
cJSON *name_obj = cJSON_GetObjectItem (func_node, "name");
if (name_obj && cJSON_IsString (name_obj)) {
const char *name = cJSON_GetStringValue (name_obj);
if (!mach_find_var_info (s, name)) {
int slot = 1 + s->nr_args + s->nr_local_slots++;
mach_add_var (s, name, slot, MACH_VAR_LOCAL, 1);
}
}
}
/* Adjust temp slot start after all locals are allocated */
s->next_temp_slot = 1 + s->nr_local_slots;
if (s->next_temp_slot > s->max_slot) s->max_slot = s->next_temp_slot;
/* Process top-level function declarations */
cJSON *functions = cJSON_GetObjectItem (ast, "functions");
cJSON *func_node;
cJSON_ArrayForEach (func_node, functions) {
cJSON *name_obj = cJSON_GetObjectItem (func_node, "name");
if (name_obj && cJSON_IsString (name_obj)) {