Skip to content

Commit

Permalink
fixed #235: _ is a normal variable outside func.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kray-G committed Mar 22, 2021
1 parent e02f454 commit cc91f85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/ast_analyzer.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct kxana_context_ {
int depth;
int in_catch;
int in_native;
int in_function;
int class_id;
int arg_index;
int anon_arg;
Expand Down Expand Up @@ -665,7 +666,7 @@ LOOP_HEAD:;
}
break;
}
if (is_anon_var(actx, node)) {
if (actx->in_function && is_anon_var(actx, node)) {
break;
}
int enumv = 0;
Expand Down Expand Up @@ -1604,6 +1605,8 @@ LOOP_HEAD:;
enum_map_t enval = kh_init(enum_value);
kv_push(enum_map_t, actx->enval, enval);

int in_function = actx->in_function;
actx->in_function = 1;
kx_object_t *class_node = actx->class_node;
actx->class_node = node;
int depth = actx->depth;
Expand Down Expand Up @@ -1671,6 +1674,7 @@ LOOP_HEAD:;
kv_remove_last(actx->symbols);
actx->depth = depth;
actx->class_node = class_node;
actx->in_function = in_function;

kh_destroy(enum_value, enval);
kv_pop(actx->enval);
Expand Down Expand Up @@ -1727,6 +1731,8 @@ LOOP_HEAD:;
enum_map_t enval = kh_init(enum_value);
kv_push(enum_map_t, actx->enval, enval);

int in_function = actx->in_function;
actx->in_function = 1;
if (node->type == KXST_NATIVE) {
actx->in_native = 1;
node->var_type = KX_NFNC_T;
Expand Down Expand Up @@ -1785,6 +1791,7 @@ LOOP_HEAD:;
kv_remove_last(actx->symbols);
actx->depth = depth;
actx->in_native = 0;
actx->in_function = in_function;

kh_destroy(enum_value, enval);
kv_pop(actx->enval);
Expand Down
9 changes: 8 additions & 1 deletion src/optimizer/opt_cfold.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ typedef struct folding_context_ {
int anon_check;
int anon_arg;
int exprlist_r2l;
int in_function;
int in_case_when;
} folding_context_t;

Expand Down Expand Up @@ -53,7 +54,7 @@ static void opt_ast_constant_folding_impl(kx_context_t *ctx, kx_object_t *node,
break;

case KXOP_VAR: {
if (cctx->anon_check) {
if (cctx->in_function && cctx->anon_check) {
const char *name = node->value.s;
if (!cctx->in_case_when && name && name[0] == '_' && name[1] == 0) {
node->lexical = 0;
Expand Down Expand Up @@ -438,22 +439,28 @@ static void opt_ast_constant_folding_impl(kx_context_t *ctx, kx_object_t *node,
break;
case KXST_SYSCLASS:
case KXST_CLASS: { /* s: name, lhs: arglist, rhs: block: ex: expr (inherit) */
int in_function = cctx->in_function;
cctx->in_function = 1;
int anon_arg = cctx->anon_arg;
cctx->anon_arg = 0;
opt_ast_constant_folding_impl(ctx, node->lhs, cctx);
opt_ast_constant_folding_impl(ctx, node->rhs, cctx);
opt_ast_constant_folding_impl(ctx, node->ex, cctx);
cctx->anon_arg = anon_arg;
cctx->in_function = in_function;
break;
}
case KXST_COROUTINE: /* s: name, lhs: arglist, rhs: block: optional: public/private/protected */
case KXST_FUNCTION: /* s: name, lhs: arglist, rhs: block: optional: public/private/protected */
case KXST_NATIVE: { /* s: name, lhs: arglist, rhs: block: ret_type: return type */
int in_function = cctx->in_function;
cctx->in_function = 1;
int anon_arg = cctx->anon_arg;
cctx->anon_arg = 0;
opt_ast_constant_folding_impl(ctx, node->lhs, cctx);
opt_ast_constant_folding_impl(ctx, node->rhs, cctx);
cctx->anon_arg = anon_arg;
cctx->in_function = in_function;
break;
}
default:
Expand Down

0 comments on commit cc91f85

Please sign in to comment.