rm block scope
This commit is contained in:
@@ -93,9 +93,6 @@ typedef struct MachCompState {
|
||||
int loop_break; /* instruction index to patch, or -1 */
|
||||
int loop_continue; /* instruction index to patch, or -1 */
|
||||
|
||||
/* Scope depth for block scoping */
|
||||
int scope_depth;
|
||||
|
||||
/* Parent for nested function compilation */
|
||||
struct MachCompState *parent;
|
||||
|
||||
@@ -241,7 +238,6 @@ static void mach_add_var(MachCompState *cs, const char *name, int slot, int is_c
|
||||
v->slot = slot;
|
||||
v->is_const = is_const;
|
||||
v->is_closure = 0;
|
||||
v->scope_depth = cs->scope_depth;
|
||||
}
|
||||
|
||||
/* Find a variable in the current scope */
|
||||
@@ -1353,17 +1349,7 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
cJSON *right = cJSON_GetObjectItemCaseSensitive(stmt, "right");
|
||||
const char *name = cJSON_GetStringValue(cJSON_GetObjectItemCaseSensitive(left, "name"));
|
||||
if (!name) return;
|
||||
/* Check if var exists at current scope depth — if so, reuse it.
|
||||
If it exists at a shallower depth, shadow it with a new slot. */
|
||||
int slot = -1;
|
||||
for (int i = cs->var_count - 1; i >= 0; i--) {
|
||||
if (strcmp(cs->vars[i].name, name) == 0) {
|
||||
if (cs->vars[i].scope_depth == cs->scope_depth) {
|
||||
slot = cs->vars[i].slot; /* same scope — reuse */
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
int slot = mach_find_var(cs, name);
|
||||
if (slot < 0) {
|
||||
slot = mach_reserve_reg(cs);
|
||||
mach_add_var(cs, name, slot, strcmp(kind, "def") == 0);
|
||||
@@ -1438,8 +1424,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
|
||||
/* Block */
|
||||
if (strcmp(kind, "block") == 0) {
|
||||
int saved_var_count = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
cJSON *stmts = cJSON_GetObjectItemCaseSensitive(stmt, "statements");
|
||||
if (stmts && cJSON_IsArray(stmts)) {
|
||||
int count = cJSON_GetArraySize(stmts);
|
||||
@@ -1447,10 +1431,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
mach_compile_stmt(cs, cJSON_GetArrayItem(stmts, i));
|
||||
}
|
||||
}
|
||||
cs->scope_depth--;
|
||||
for (int i = saved_var_count; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = saved_var_count;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1470,8 +1450,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
|
||||
/* Compile then branch — "then" is a direct array of statements */
|
||||
if (then_body) {
|
||||
int saved_vc = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
if (cJSON_IsArray(then_body)) {
|
||||
int count = cJSON_GetArraySize(then_body);
|
||||
for (int i = 0; i < count; i++)
|
||||
@@ -1486,10 +1464,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
mach_compile_stmt(cs, then_body);
|
||||
}
|
||||
}
|
||||
cs->scope_depth--;
|
||||
for (int i = saved_vc; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = saved_vc;
|
||||
}
|
||||
|
||||
/* Check for else-if chain ("list") or plain else */
|
||||
@@ -1508,8 +1482,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
cs->code[jmpfalse_pc] = MACH_AsBx(MACH_JMPFALSE, cr, (int16_t)offset);
|
||||
|
||||
/* Compile else — could be a direct array, object, or else-if stmt */
|
||||
int saved_vc = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
if (cJSON_IsArray(else_body)) {
|
||||
int count = cJSON_GetArraySize(else_body);
|
||||
for (int i = 0; i < count; i++)
|
||||
@@ -1524,10 +1496,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
mach_compile_stmt(cs, else_body);
|
||||
}
|
||||
}
|
||||
cs->scope_depth--;
|
||||
for (int i = saved_vc; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = saved_vc;
|
||||
|
||||
/* Patch jmpend */
|
||||
offset = mach_current_pc(cs) - (jmpend_pc + 1);
|
||||
@@ -1560,8 +1528,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
|
||||
/* Compile body — "statements" on a child "block"/"body", or directly on the node */
|
||||
{
|
||||
int saved_vc = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
cJSON *body = cJSON_GetObjectItemCaseSensitive(stmt, "block");
|
||||
if (!body) body = cJSON_GetObjectItemCaseSensitive(stmt, "body");
|
||||
cJSON *stmts = body ? cJSON_GetObjectItemCaseSensitive(body, "statements") : NULL;
|
||||
@@ -1573,10 +1539,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
} else if (body) {
|
||||
mach_compile_stmt(cs, body);
|
||||
}
|
||||
cs->scope_depth--;
|
||||
for (int i = saved_vc; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = saved_vc;
|
||||
}
|
||||
|
||||
/* Patch continue chain to loop_top */
|
||||
@@ -1613,8 +1575,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
|
||||
/* For loop */
|
||||
if (strcmp(kind, "for") == 0) {
|
||||
int saved_vc = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
cJSON *init = cJSON_GetObjectItemCaseSensitive(stmt, "init");
|
||||
cJSON *cond = cJSON_GetObjectItemCaseSensitive(stmt, "test");
|
||||
cJSON *update = cJSON_GetObjectItemCaseSensitive(stmt, "update");
|
||||
@@ -1643,8 +1603,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
|
||||
/* Body — "statements" on a child "block"/"body", or directly on the for node */
|
||||
{
|
||||
int body_vc = cs->var_count;
|
||||
cs->scope_depth++;
|
||||
cJSON *stmts = body ? cJSON_GetObjectItemCaseSensitive(body, "statements") : NULL;
|
||||
if (!stmts) stmts = cJSON_GetObjectItemCaseSensitive(stmt, "statements");
|
||||
if (stmts && cJSON_IsArray(stmts)) {
|
||||
@@ -1654,10 +1612,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
} else if (body) {
|
||||
mach_compile_stmt(cs, body);
|
||||
}
|
||||
cs->scope_depth--;
|
||||
for (int i = body_vc; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = body_vc;
|
||||
}
|
||||
|
||||
/* Patch continue chain to update (or loop_top if no update) */
|
||||
@@ -1699,10 +1653,6 @@ static void mach_compile_stmt(MachCompState *cs, cJSON *stmt) {
|
||||
}
|
||||
cs->loop_break = old_break;
|
||||
cs->loop_continue = old_continue;
|
||||
cs->scope_depth--;
|
||||
for (int i = saved_vc; i < cs->var_count; i++)
|
||||
sys_free(cs->vars[i].name);
|
||||
cs->var_count = saved_vc;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user