From 5247ef085f11727a535a9a940b5c1d2f33ac8715 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 15 Aug 2022 18:55:30 +0200 Subject: [PATCH] Revert ":art: USe Stmt_List for Func_Def" This reverts commit 70db9f7efe903ee8e78e30a58e04bced2ae73b58. --- ast | 2 +- include/parse.h | 1 - src/clean.c | 4 ++-- src/emit/emit.c | 18 +++++------------- src/lib/closure.c | 9 +++++---- src/lib/ctrl.c | 13 +------------ src/lib/sift.c | 19 +++++-------------- src/lib/xork.c | 3 +-- src/parse/check.c | 23 +++++++++-------------- src/parse/default_arg.c | 6 +++--- src/parse/partial.c | 18 +++++++----------- src/parse/scan0.c | 6 +++--- src/parse/scan1.c | 10 ++++++---- src/parse/scan2.c | 8 ++------ 14 files changed, 50 insertions(+), 90 deletions(-) diff --git a/ast b/ast index 3ef5bb68..c405f31e 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 3ef5bb688518c5b537eee6152f1cb81e8f477cdb +Subproject commit c405f31eb2ffaf5a26b4ae92165d14784b2f2287 diff --git a/include/parse.h b/include/parse.h index bc2964aa..fd03f95e 100644 --- a/include/parse.h +++ b/include/parse.h @@ -56,7 +56,6 @@ ANN m_bool scan2_exp(const Env, Exp); } ANN m_bool check_stmt(const Env env, const Stmt stmt); -ANN m_bool check_stmt_list(const Env env, const Stmt_List); typedef m_bool (*_exp_func)(const void *, const void *); ANN m_bool scanx_body(const Env env, const Class_Def cdef, const _exp_func f, diff --git a/src/clean.c b/src/clean.c index 83ce694e..66c94aa0 100644 --- a/src/clean.c +++ b/src/clean.c @@ -78,7 +78,7 @@ ANN static void clean_exp_unary(Clean *a, Exp_Unary *b) { clean_exp(a, b->ctor.exp); break; case unary_code: - clean_stmt_list(a, b->code); + clean_stmt(a, b->code); break; } if(b->captures) clean_captures(a, b->captures); @@ -275,7 +275,7 @@ ANN static void clean_func_def(Clean *a, Func_Def b) { ++a->scope; if (!b->builtin && b->d.code && !(b->base->func && safe_vflag(b->base->func->value_ref, vflag_builtin))) - clean_stmt_list(a, b->d.code); + clean_stmt(a, b->d.code); else b->d.code = NULL; --a->scope; diff --git a/src/emit/emit.c b/src/emit/emit.c index 422b8f07..96e41f82 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -886,7 +886,7 @@ ANN m_bool emit_ensure_func(const Emitter emit, const Func f) { ANN static m_bool emit_prim_locale(const Emitter emit, const Symbol *id) { if(emit->locale->def->d.code) { - const Stmt stmt = mp_vector_at((emit->locale->def->d.code), struct Stmt_, 0); + const Stmt stmt = mp_vector_at((emit->locale->def->d.code->d.stmt_code.stmt_list), struct Stmt_, 0); const Func f = stmt->d.stmt_exp.val->d.exp_call.func->type->info->func; CHECK_OB(emit_ensure_func(emit, f)); } @@ -1665,12 +1665,11 @@ static void push_spork_code(const Emitter emit, const m_str prefix, } struct Sporker { - const Stmt_List code; + const Stmt code; const Exp exp; VM_Code vm_code; const Type type; const Capture_List captures; - const loc_t pos; const bool emit_var; const bool is_spork; }; @@ -1679,13 +1678,12 @@ ANN static m_bool spork_prepare_code(const Emitter emit, const struct Sporker *sp) { emit_add_instr(emit, RegPushImm); push_spork_code(emit, sp->is_spork ? SPORK_CODE_PREFIX : FORK_CODE_PREFIX, - sp->pos); + sp->code->pos); if (emit->env->class_def) stack_alloc(emit); if (emit->env->func && vflag(emit->env->func->value_ref, vflag_member)) stack_alloc(emit); if(sp->captures) emit->code->frame->curr_offset += captures_sz(sp->captures); -// return scoped_stmt(emit, sp->code); - return emit_stmt_list(emit, sp->code); + return scoped_stmt(emit, sp->code); } ANN static m_bool spork_prepare_func(const Emitter emit, @@ -1741,7 +1739,6 @@ ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary *unary) { .code = unary->unary_type == unary_code ? unary->code : NULL, .type = exp_self(unary)->type, .captures = unary->captures, - .pos = exp_self(unary)->pos, .is_spork = (unary->op == insert_symbol("spork")), .emit_var = exp_getvar(exp_self(unary))}; CHECK_OB((sporker.vm_code = spork_prepare(emit, &sporker))); @@ -2722,12 +2719,7 @@ ANN static m_bool emit_func_def_body(const Emitter emit, const Func_Def fdef) { if (fdef->base->xid == insert_symbol("@dtor")) emit_local(emit, emit->gwion->type[et_int]); if (fdef->base->args) emit_func_def_args(emit, fdef->base->args); if (fdef->d.code) { - if(!fdef->builtin) { - scoped_ini(emit); - const m_bool ret = emit_stmt_list(emit, fdef->d.code); - scoped_end(emit); - CHECK_BB(ret); - } + if(!fdef->builtin) CHECK_BB(scoped_stmt(emit, fdef->d.code)); else fdef->base->func->code = (VM_Code)vector_at(&fdef->base->func->value_ref->from->owner_class->nspc->vtable, fdef->vt_index); } emit_func_def_return(emit); diff --git a/src/lib/closure.c b/src/lib/closure.c index b6c68634..e26a7366 100644 --- a/src/lib/closure.c +++ b/src/lib/closure.c @@ -16,7 +16,7 @@ #include "tmp_resolve.h" ANN static Exp uncurry(const Env env, const Exp_Binary *bin) { - const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code, struct Stmt_, 0); + const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code->d.stmt_code.stmt_list, struct Stmt_, 0); const Exp ecall = stmt->d.stmt_exp.val; const Exp_Call *call = &ecall->d.exp_call; Exp args = call->args; @@ -60,7 +60,7 @@ ANN static Type mk_call(const Env env, const Exp e, const Exp func, const Exp ar static OP_CHECK(opck_func_call) { Exp_Binary *bin = (Exp_Binary *)data; if(!strncmp(bin->rhs->type->name, "partial:", 8)) { - const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code, struct Stmt_, 0); + const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code->d.stmt_code.stmt_list, struct Stmt_, 0); const Exp_Call *call = &stmt->d.stmt_exp.val->d.exp_call; DECL_ON(const Exp, args, = uncurry(env, bin)); return mk_call(env, exp_self(bin), call->func, args); @@ -618,12 +618,13 @@ static OP_CHECK(opck_op_impl) { new_prim_id(env->gwion->mp, larg1->var_decl.xid, impl->e->pos); const Exp bin = new_exp_binary(env->gwion->mp, lhs, impl->e->d.prim.d.var, rhs, impl->e->pos); - Stmt_List code = new_mp_vector(env->gwion->mp, struct Stmt_, 1); - mp_vector_set(code, struct Stmt_, 0, + Stmt_List slist = new_mp_vector(env->gwion->mp, struct Stmt_, 1); + mp_vector_set(slist, struct Stmt_, 0, ((struct Stmt_) { .stmt_type = ae_stmt_return, .d = { .stmt_exp = { .val = bin }}, .pos = impl->e->pos })); + const Stmt code = new_stmt_code(env->gwion->mp, slist, impl->e->pos); const Func_Def def = new_func_def(env->gwion->mp, base, code); def->base->xid = impl->e->d.prim.d.var; // use envset diff --git a/src/lib/ctrl.c b/src/lib/ctrl.c index acda04cc..4ef0ac65 100644 --- a/src/lib/ctrl.c +++ b/src/lib/ctrl.c @@ -45,20 +45,9 @@ static OP_CHECK(opck_ctrl) { check_exp(env, cond); const Stmt loop = new_stmt_flow(mp, ae_stmt_while, cond, stmt, false, func->pos); - const Stmt_List code = new_mp_vector(mp, struct Stmt_, 1); - mp_vector_set(code, struct Stmt_, 0, ((struct Stmt_) { - .stmt_type = ae_stmt_while, - .d = { - .stmt_flow = { - .cond = cond, - .body = loop - } - }, - .pos = func->pos - })); exp->exp_type = ae_exp_unary; exp->d.exp_unary.unary_type = unary_code; - exp->d.exp_unary.code = code; + exp->d.exp_unary.code = loop; exp->d.exp_unary.op = insert_symbol(env->gwion->st, "spork"); return env->gwion->type[et_shred]; } diff --git a/src/lib/sift.c b/src/lib/sift.c index 8f16c61e..ce1cfe99 100644 --- a/src/lib/sift.c +++ b/src/lib/sift.c @@ -13,11 +13,11 @@ static OP_CHECK(opck_sift) { Exp_Binary *bin = (Exp_Binary*)data; const Exp lhs = bin->lhs; - Stmt stmt = mp_vector_at(lhs->d.exp_unary.code, struct Stmt_, 0); + Stmt stmt = lhs->d.exp_unary.code; Stmt fst = mp_vector_at(stmt->d.stmt_flow.body->d.stmt_code.stmt_list, struct Stmt_, 0); const Symbol chuck = insert_symbol(env->gwion->st, "=>"); Exp next = new_exp_binary(env->gwion->mp, fst->d.stmt_exp.val, chuck, bin->rhs, bin->rhs->pos); - CHECK_BN(traverse_exp(env, next)); // how do we free it? + CHECK_BN(traverse_exp(env, next)); fst->d.stmt_exp.val = next; const Exp exp = exp_self(bin); exp->exp_type = lhs->exp_type; @@ -60,21 +60,12 @@ static OP_CHECK(opck_ctrl) { const Exp cond = new_prim_id(mp, insert_symbol(env->gwion->st, "true"), func->pos); check_exp(env, cond); - const Stmt_List code = new_mp_vector(mp, struct Stmt_, 1); - mp_vector_set(code, struct Stmt_, 0, ((struct Stmt_) { - .stmt_type = ae_stmt_while, - .d = { - .stmt_flow = { - .cond = cond, - .body = stmt - } - }, - .pos = func->pos - })); + const Stmt loop = new_stmt_flow(mp, ae_stmt_while, cond, stmt, false, func->pos); exp->exp_type = ae_exp_unary; exp->d.exp_unary.unary_type = unary_code; - exp->d.exp_unary.code = code; + exp->d.exp_unary.code = loop; exp->d.exp_unary.op = insert_symbol(env->gwion->st, "spork"); + return NULL; } diff --git a/src/lib/xork.c b/src/lib/xork.c index b65ed401..8a56ccfc 100644 --- a/src/lib/xork.c +++ b/src/lib/xork.c @@ -66,8 +66,7 @@ static OP_CHECK(opck_spork) { struct Func_Def_ fdef = { .base = &fbase}; struct Func_ func = { .name = "in spork", .def = &fdef, .value_ref = &value}; env->func = &func; -// scope depth? - const m_bool ret = check_stmt_list(env, unary->code); + const m_bool ret = check_stmt(env, unary->code); env->func = f; free_scope(env->gwion->mp, env->curr->info->value); env->curr->info->value = upvalues.values; diff --git a/src/parse/check.c b/src/parse/check.c index 4899b09c..857e7e21 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -17,7 +17,7 @@ #include "partial.h" #include "spread.h" -ANN m_bool check_stmt_list(const Env env, Stmt_List list); +ANN static m_bool check_stmt_list(const Env env, Stmt_List list); ANN m_bool check_class_def(const Env env, const Class_Def class_def); ANN static Type check_internal(const Env env, const Symbol sym, const Exp e, @@ -1176,17 +1176,18 @@ ANN m_bool check_type_def(const Env env, const Type_Def tdef) { const Exp when = tdef->when; tdef->when = NULL; when->next = helper; - Stmt_List code = new_mp_vector(env->gwion->mp, struct Stmt_, 2); - mp_vector_set(code, struct Stmt_, 0, + Stmt_List body = new_mp_vector(env->gwion->mp, struct Stmt_, 2); + mp_vector_set(body, struct Stmt_, 0, ((struct Stmt_) { .stmt_type = ae_stmt_exp, .d = { .stmt_exp = { .val = when }}, .pos = when->pos })); - mp_vector_set(code, struct Stmt_, 1, + mp_vector_set(body, struct Stmt_, 1, ((struct Stmt_) { .stmt_type = ae_stmt_exp, .pos = when->pos })); + const Stmt code = new_stmt_code(env->gwion->mp, body, when->pos); const Func_Def fdef = new_func_def(env->gwion->mp, fb, code); tdef->when_def = fdef; CHECK_BB(traverse_func_def(env, fdef)); @@ -1209,7 +1210,7 @@ ANN m_bool check_type_def(const Env env, const Type_Def tdef) { .stmt_type = ae_stmt_return, .d = { .stmt_exp = { .val = ret_id }}, .pos = when->pos }; - mp_vector_set(fdef->d.code, struct Stmt_, 1, ret); + mp_vector_set(fdef->d.code->d.stmt_code.stmt_list, struct Stmt_, 1, ret); ret_id->type = tdef->type; } if (tflag(tdef->type, tflag_cdef)) @@ -1622,7 +1623,7 @@ ANN m_bool check_stmt(const Env env, const Stmt stmt) { return check_stmt_func[stmt->stmt_type](env, &stmt->d); } -ANN m_bool check_stmt_list(const Env env, Stmt_List l) { +ANN static m_bool check_stmt_list(const Env env, Stmt_List l) { for(m_uint i = 0; i < l->len; i++) { const Stmt s = mp_vector_at(l, struct Stmt_, i); CHECK_BB(check_stmt(env, s)); @@ -1749,14 +1750,8 @@ ANN static m_bool check_func_def_override(const Env env, const Func_Def fdef, ANN m_bool check_fdef(const Env env, const Func_Def fdef) { if (fdef->base->args) CHECK_BB(check_func_args(env, fdef->base->args)); if(fdef->builtin) return GW_OK; - if (fdef->d.code && fdef->d.code) { - env->scope->depth++; - nspc_push_value(env->gwion->mp, env->curr); - const m_bool ret = check_stmt_list(env, fdef->d.code); - nspc_pop_value(env->gwion->mp, env->curr); - env->scope->depth--; - return ret; - } + if (fdef->d.code && fdef->d.code->d.stmt_code.stmt_list) + CHECK_BB(check_stmt_code(env, &fdef->d.code->d.stmt_code)); return GW_OK; } diff --git a/src/parse/default_arg.c b/src/parse/default_arg.c index 3d973831..17bcf279 100644 --- a/src/parse/default_arg.c +++ b/src/parse/default_arg.c @@ -64,11 +64,11 @@ ANN void default_args(const Env env, const Section *s, Ast *acc) { Arg *arg = mp_vector_at(args, Arg, args->len); if(!arg->exp) break; Func_Base *const base = cpy_func_base(p, base_fdef->base); - Stmt_List code = strcmp("new", s_name(base->xid)) + Stmt_List slist = strcmp("new", s_name(base->xid)) ? std_code(env->gwion->mp, base, args, len) : new_code(env, base, args, len); -// const Stmt body = new_stmt_code(p, slist, base->pos); - const Func_Def fdef = new_func_def(p, base, code); + const Stmt body = new_stmt_code(p, slist, base->pos); + const Func_Def fdef = new_func_def(p, base, body); scan1_func_def(env, fdef); scan2_func_def(env, fdef); Section section = MK_SECTION(func, func_def, fdef); diff --git a/src/parse/partial.c b/src/parse/partial.c index b4447da1..00da3a3e 100644 --- a/src/parse/partial.c +++ b/src/parse/partial.c @@ -164,18 +164,14 @@ ANN static Func partial_match(const Env env, const Func up, const Exp args, cons return NULL; } -ANN static Stmt_List partial_code(const Env env, Arg_List args, const Exp efun, const Exp earg) { +ANN static Stmt partial_code(const Env env, Arg_List args, const Exp efun, const Exp earg) { const Exp arg = partial_call(env, args, earg); const Exp exp = new_exp_call(env->gwion->mp, efun, arg, efun->pos); - Stmt_List code = new_mp_vector(env->gwion->mp, struct Stmt_, 1); - mp_vector_set(code, struct Stmt_, 0, ((struct Stmt_) { - .stmt_type = ae_stmt_return, - .d = { .stmt_exp = { .val = exp }} - })); -// stmt->stmt_type = ae_stmt_return; -// stmt->d.stmt_exp.val = exp; - return code; -// return new_stmt_code(env->gwion->mp, slist, efun->pos); + Stmt_List slist = new_mp_vector(env->gwion->mp, struct Stmt_, 1); + Stmt stmt = mp_vector_at(slist, struct Stmt_, 0); + stmt->stmt_type = ae_stmt_return; + stmt->d.stmt_exp.val = exp; + return new_stmt_code(env->gwion->mp, slist, efun->pos); } ANN static uint32_t count_args_exp(Exp args) { @@ -222,7 +218,7 @@ ANN Type partial_type(const Env env, Exp_Call *const call) { } nspc_push_value(env->gwion->mp, env->curr); Func_Base *const fbase = partial_base(env, f->def->base, call->args, call->func->pos); - const Stmt_List code = partial_code(env, f->def->base->args, call->func, call->args); + const Stmt code = partial_code(env, f->def->base->args, call->func, call->args); const Exp exp = exp_self(call); exp->d.exp_lambda.def = new_func_def(env->gwion->mp, fbase, code); exp->exp_type = ae_exp_lambda; diff --git a/src/parse/scan0.c b/src/parse/scan0.c index 99ef20d9..23c44409 100644 --- a/src/parse/scan0.c +++ b/src/parse/scan0.c @@ -436,10 +436,10 @@ ANN m_bool scan0_func_def(const Env env, const Func_Def fdef) { struct Func_ fake = {.name = s_name(fdef->base->xid), .def = fdef }, *const former = env->func; env->func = &fake; - if(!fdef->builtin && fdef->d.code) - scan0_stmt_list(env, fdef->d.code); + if(!fdef->builtin && fdef->d.code && fdef->d.code->d.stmt_code.stmt_list) + scan0_stmt_list(env, fdef->d.code->d.stmt_code.stmt_list); if(env->context->extend) - fdef->d.code = spread_func(env, fdef->d.code); + fdef->d.code->d.stmt_code.stmt_list = spread_func(env, fdef->d.code->d.stmt_code.stmt_list); env->func = former; env->context->extend = old_extend; }} diff --git a/src/parse/scan1.c b/src/parse/scan1.c index adfab34b..8196dc96 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -243,7 +243,7 @@ ANN static inline m_bool scan1_exp_unary(const restrict Env env, if(unary->unary_type == unary_exp) return scan1_exp(env, unary->exp); if (unary->unary_type == unary_code) - return scan1_stmt_list(env, unary->code); + return scan1_stmt(env, unary->code); return GW_OK; } @@ -524,7 +524,7 @@ ANN static m_bool scan1_stmt_return(const Env env, const Stmt_Exp stmt) { if (!env->func) ERR_B(stmt_self(stmt)->pos, _("'return' statement found outside function definition")) - if (env->scope->depth == 1) env->func->memoize = 1; + if (env->scope->depth <= 2) env->func->memoize = 1; if(stmt->val) scan1_exp(env, stmt->val); return GW_OK; } @@ -632,8 +632,8 @@ ANN m_bool scan1_fbody(const Env env, const Func_Def fdef) { CHECK_BB(scan1_fdef_args(env, fdef->base->args)); CHECK_BB(scan1_args(env, fdef->base->args)); } - if (!fdef->builtin && fdef->d.code) - CHECK_BB(scan1_stmt_list(env, fdef->d.code)); + if (!fdef->builtin && fdef->d.code && fdef->d.code->d.stmt_code.stmt_list) + CHECK_BB(scan1_stmt_list(env, fdef->d.code->d.stmt_code.stmt_list)); return GW_OK; } @@ -673,7 +673,9 @@ ANN static m_bool _scan1_func_def(const Env env, const Func_Def fdef) { struct Func_ fake = {.name = s_name(fdef->base->xid), .def = fdef }, *const former = env->func; env->func = &fake; + ++env->scope->depth; const m_bool ret = scanx_fdef(env, env, fdef, (_exp_func)scan1_fdef); + --env->scope->depth; env->func = former; if (global) env_pop(env, scope); if ((strcmp(s_name(fdef->base->xid), "@implicit") || fbflag(fdef->base, fbflag_internal)) && !fdef->builtin && fdef->base->ret_type && diff --git a/src/parse/scan2.c b/src/parse/scan2.c index ab075974..4536842c 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -419,13 +419,9 @@ ANN static m_bool scan2_func_def_op(const Env env, const Func_Def f) { ANN static m_bool scan2_func_def_code(const Env env, const Func_Def f) { const Func former = env->func; env->func = f->base->func; - env->scope->depth++; - nspc_push_value(env->gwion->mp, env->curr); - const m_bool ret = scan2_stmt_list(env, f->d.code); // scope depth? - nspc_pop_value(env->gwion->mp, env->curr); - env->scope->depth--; + CHECK_BB(scan2_stmt_code(env, &f->d.code->d.stmt_code)); env->func = former; - return ret; + return GW_OK; } ANN static void scan2_func_def_flag(const Env env, const Func_Def f) { -- 2.43.0