From 961fd865724a1061c181d9854bbaf3886004fd9e Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Mon, 15 Aug 2022 18:56:04 +0200 Subject: [PATCH] Revert "Revert ":art: USe Stmt_List for Func_Def"" This reverts commit 5247ef085f11727a535a9a940b5c1d2f33ac8715. --- 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, 90 insertions(+), 50 deletions(-) diff --git a/ast b/ast index c405f31e..3ef5bb68 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit c405f31eb2ffaf5a26b4ae92165d14784b2f2287 +Subproject commit 3ef5bb688518c5b537eee6152f1cb81e8f477cdb diff --git a/include/parse.h b/include/parse.h index fd03f95e..bc2964aa 100644 --- a/include/parse.h +++ b/include/parse.h @@ -56,6 +56,7 @@ 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 66c94aa0..83ce694e 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(a, b->code); + clean_stmt_list(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(a, b->d.code); + clean_stmt_list(a, b->d.code); else b->d.code = NULL; --a->scope; diff --git a/src/emit/emit.c b/src/emit/emit.c index 96e41f82..422b8f07 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->d.stmt_code.stmt_list), struct Stmt_, 0); + const Stmt stmt = mp_vector_at((emit->locale->def->d.code), 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,11 +1665,12 @@ static void push_spork_code(const Emitter emit, const m_str prefix, } struct Sporker { - const Stmt code; + const Stmt_List 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; }; @@ -1678,12 +1679,13 @@ 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->code->pos); + sp->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 scoped_stmt(emit, sp->code); + return emit_stmt_list(emit, sp->code); } ANN static m_bool spork_prepare_func(const Emitter emit, @@ -1739,6 +1741,7 @@ 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))); @@ -2719,7 +2722,12 @@ 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) CHECK_BB(scoped_stmt(emit, 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); + } 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 e26a7366..b6c68634 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->d.stmt_code.stmt_list, struct Stmt_, 0); + const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code, 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->d.stmt_code.stmt_list, struct Stmt_, 0); + const Stmt stmt = mp_vector_at(bin->rhs->type->info->func->def->d.code, 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,13 +618,12 @@ 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 slist = new_mp_vector(env->gwion->mp, struct Stmt_, 1); - mp_vector_set(slist, struct Stmt_, 0, + 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 = 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 4ef0ac65..acda04cc 100644 --- a/src/lib/ctrl.c +++ b/src/lib/ctrl.c @@ -45,9 +45,20 @@ 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 = loop; + exp->d.exp_unary.code = code; 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 ce1cfe99..8f16c61e 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 = lhs->d.exp_unary.code; + Stmt stmt = mp_vector_at(lhs->d.exp_unary.code, struct Stmt_, 0); 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)); + CHECK_BN(traverse_exp(env, next)); // how do we free it? fst->d.stmt_exp.val = next; const Exp exp = exp_self(bin); exp->exp_type = lhs->exp_type; @@ -60,12 +60,21 @@ 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 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 = stmt + } + }, + .pos = func->pos + })); exp->exp_type = ae_exp_unary; exp->d.exp_unary.unary_type = unary_code; - exp->d.exp_unary.code = loop; + exp->d.exp_unary.code = code; 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 8a56ccfc..b65ed401 100644 --- a/src/lib/xork.c +++ b/src/lib/xork.c @@ -66,7 +66,8 @@ 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; - const m_bool ret = check_stmt(env, unary->code); +// scope depth? + const m_bool ret = check_stmt_list(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 857e7e21..4899b09c 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -17,7 +17,7 @@ #include "partial.h" #include "spread.h" -ANN static m_bool check_stmt_list(const Env env, Stmt_List list); +ANN 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,18 +1176,17 @@ 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 body = new_mp_vector(env->gwion->mp, struct Stmt_, 2); - mp_vector_set(body, struct Stmt_, 0, + Stmt_List code = new_mp_vector(env->gwion->mp, struct Stmt_, 2); + mp_vector_set(code, struct Stmt_, 0, ((struct Stmt_) { .stmt_type = ae_stmt_exp, .d = { .stmt_exp = { .val = when }}, .pos = when->pos })); - mp_vector_set(body, struct Stmt_, 1, + mp_vector_set(code, 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)); @@ -1210,7 +1209,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->d.stmt_code.stmt_list, struct Stmt_, 1, ret); + mp_vector_set(fdef->d.code, struct Stmt_, 1, ret); ret_id->type = tdef->type; } if (tflag(tdef->type, tflag_cdef)) @@ -1623,7 +1622,7 @@ ANN m_bool check_stmt(const Env env, const Stmt stmt) { return check_stmt_func[stmt->stmt_type](env, &stmt->d); } -ANN static m_bool check_stmt_list(const Env env, Stmt_List l) { +ANN 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)); @@ -1750,8 +1749,14 @@ 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->d.stmt_code.stmt_list) - CHECK_BB(check_stmt_code(env, &fdef->d.code->d.stmt_code)); + 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; + } return GW_OK; } diff --git a/src/parse/default_arg.c b/src/parse/default_arg.c index 17bcf279..3d973831 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 slist = strcmp("new", s_name(base->xid)) + Stmt_List code = 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, body); +// const Stmt body = new_stmt_code(p, slist, base->pos); + const Func_Def fdef = new_func_def(p, base, code); 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 00da3a3e..b4447da1 100644 --- a/src/parse/partial.c +++ b/src/parse/partial.c @@ -164,14 +164,18 @@ ANN static Func partial_match(const Env env, const Func up, const Exp args, cons return NULL; } -ANN static Stmt partial_code(const Env env, Arg_List args, const Exp efun, const Exp earg) { +ANN static Stmt_List 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 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); + 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); } ANN static uint32_t count_args_exp(Exp args) { @@ -218,7 +222,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 code = partial_code(env, f->def->base->args, call->func, call->args); + const Stmt_List 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 23c44409..99ef20d9 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 && fdef->d.code->d.stmt_code.stmt_list) - scan0_stmt_list(env, fdef->d.code->d.stmt_code.stmt_list); + if(!fdef->builtin && fdef->d.code) + scan0_stmt_list(env, fdef->d.code); if(env->context->extend) - fdef->d.code->d.stmt_code.stmt_list = spread_func(env, fdef->d.code->d.stmt_code.stmt_list); + fdef->d.code = spread_func(env, fdef->d.code); env->func = former; env->context->extend = old_extend; }} diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 8196dc96..adfab34b 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(env, unary->code); + return scan1_stmt_list(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 <= 2) env->func->memoize = 1; + if (env->scope->depth == 1) 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 && fdef->d.code->d.stmt_code.stmt_list) - CHECK_BB(scan1_stmt_list(env, fdef->d.code->d.stmt_code.stmt_list)); + if (!fdef->builtin && fdef->d.code) + CHECK_BB(scan1_stmt_list(env, fdef->d.code)); return GW_OK; } @@ -673,9 +673,7 @@ 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 4536842c..ab075974 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -419,9 +419,13 @@ 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; - CHECK_BB(scan2_stmt_code(env, &f->d.code->d.stmt_code)); + 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--; env->func = former; - return GW_OK; + return ret; } ANN static void scan2_func_def_flag(const Env env, const Func_Def f) { -- 2.43.0