From ae47e376ff47b091779c09c703a8d7baa41a9e0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 13 Jun 2021 19:50:54 +0200 Subject: [PATCH] :art: code cleaning --- include/compile.h | 6 ++++++ src/arg.c | 6 +++--- src/compile.c | 30 ++++++++++++++++++++++++++---- src/import/cleaner.c | 5 +++-- src/lib/lib_func.c | 3 +-- src/lib/object_op.c | 2 +- src/parse/did_you_mean.c | 4 +--- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/include/compile.h b/include/compile.h index f8865c19..a59c03de 100644 --- a/include/compile.h +++ b/include/compile.h @@ -4,4 +4,10 @@ m_uint compile_filename(struct Gwion_ *vm, const m_str filename); m_uint compile_string(struct Gwion_ *vm, const m_str filename, const m_str data); m_uint compile_file(struct Gwion_ *vm, const m_str filename, FILE *file); +m_uint compile_filename_xid(struct Gwion_ *vm, const m_str filename, + const m_uint xid); +m_uint compile_string_xid(struct Gwion_ *vm, const m_str filename, + const m_str data, const m_uint xid); +m_uint compile_file_xid(struct Gwion_ *vm, const m_str filename, FILE *file, + const m_uint xid); #endif diff --git a/src/arg.c b/src/arg.c index 85df3182..2157e670 100644 --- a/src/arg.c +++ b/src/arg.c @@ -195,9 +195,9 @@ ANN static void split_line(const m_str line, const Vector v) { } ANN static Vector get_config(const char *name) { - char * line = NULL; - size_t len = 0; - FILE * f = fopen(name, "r"); + char * line = NULL; + size_t len = 0; + FILE * f = fopen(name, "r"); CHECK_OO(f); const Vector v = (Vector)xmalloc(sizeof(struct Vector_)); vector_init(v); diff --git a/src/compile.c b/src/compile.c index e4120788..34744434 100644 --- a/src/compile.c +++ b/src/compile.c @@ -18,6 +18,7 @@ struct Compiler { FILE * file; Ast ast; struct Vector_ args; + VM_Shred shred; enum compile_type type; }; @@ -134,11 +135,11 @@ ANN static m_uint _compile(struct Gwion_ *gwion, struct Compiler *c) { if (compiler_open(c) < 0) return 0; if (_check(gwion, c) < 0) return 0; if (gwion->emit->info->code) { - const VM_Shred shred = new_vm_shred(gwion->mp, gwion->emit->info->code); - shred->info->args.ptr = c->args.ptr; - vm_add_shred(gwion->vm, shred); + c->shred = new_vm_shred(gwion->mp, gwion->emit->info->code); + c->shred->info->args.ptr = c->args.ptr; + vm_add_shred(gwion->vm, c->shred); gwion->emit->info->code = NULL; - return shred->tick->xid; + return c->shred->tick->xid; } return GW_OK; } @@ -168,3 +169,24 @@ ANN m_uint compile_file(struct Gwion_ *gwion, const m_str filename, struct Compiler c = {.base = filename, .type = COMPILE_FILE, .file = file}; return compile(gwion, &c); } + +ANN m_uint compile_filename_xid(struct Gwion_ *gwion, const m_str filename, + const m_uint xid) { + struct Compiler c = {.base = filename, .type = COMPILE_NAME}; + if (!compile(gwion, &c)) return 0; + return c.shred->tick->xid = xid; +} + +ANN m_uint compile_string_xid(struct Gwion_ *gwion, const m_str filename, + const m_str data, const m_uint xid) { + struct Compiler c = {.base = filename, .type = COMPILE_MSTR, .data = data}; + if (!compile(gwion, &c)) return 0; + return c.shred->tick->xid = xid; +} + +ANN m_uint compile_file_xid(struct Gwion_ *gwion, const m_str filename, + FILE *file, const m_uint xid) { + struct Compiler c = {.base = filename, .type = COMPILE_FILE, .file = file}; + if (!compile(gwion, &c)) return 0; + return c.shred->tick->xid = xid; +} diff --git a/src/import/cleaner.c b/src/import/cleaner.c index 7e5ba6f3..6b64baa3 100644 --- a/src/import/cleaner.c +++ b/src/import/cleaner.c @@ -35,11 +35,12 @@ ANN void ck_end(const Gwi gwi) { } typedef void (*cleaner)(MemPool, ImportCK *); -static cleaner cleaners[] = {NULL, ck_clean_udef, ck_clean_tdef, +static cleaner cleaners[] = {NULL, ck_clean_udef, ck_clean_tdef, NULL, // ck_clean_oper, ck_clean_item, ck_clean_fdef}; ANN void ck_clean(const Gwi gwi) { - cleaners[gwi->ck->type](gwi->gwion->mp, gwi->ck); + const cleaner clean = cleaners[gwi->ck->type]; + if (clean) clean(gwi->gwion->mp, gwi->ck); memset(gwi->ck, 0, sizeof(ImportCK)); } diff --git a/src/lib/lib_func.c b/src/lib/lib_func.c index 338b2e45..c7019f56 100644 --- a/src/lib/lib_func.c +++ b/src/lib/lib_func.c @@ -358,11 +358,10 @@ static void op_narg_err(const Env env, const Func_Def fdef, const loc_t loc) { gwerr_basic(_("invalid operator decay"), _("Decayed operators take two arguments"), NULL, env->name, loc, 0); - if (fdef) gwerr_secondary("declared here", env->name, fdef->base->pos); + if (fdef) defined_here(fdef->base->func->value_ref); env->context->error = true; } } - static m_bool op_call_narg(const Env env, Exp arg, const loc_t loc) { m_uint narg = 0; while (arg) { diff --git a/src/lib/object_op.c b/src/lib/object_op.c index f553a935..e49af714 100644 --- a/src/lib/object_op.c +++ b/src/lib/object_op.c @@ -194,7 +194,7 @@ OP_CHECK(opck_object_dot) { gwerr_basic("invalid variable access", "is private", NULL, env->name, exp_self(member)->pos, 0); env_error_footer(env); - gwerr_secondary("declared here", value->from->filename, value->from->loc); + defined_here(value); env->context->error = true; } else if (GET_FLAG(value, protect)) exp_setprot(exp_self(member), 1); diff --git a/src/parse/did_you_mean.c b/src/parse/did_you_mean.c index 0255ee96..dbce28b2 100644 --- a/src/parse/did_you_mean.c +++ b/src/parse/did_you_mean.c @@ -39,9 +39,7 @@ ANN static void ressembles(const Nspc nspc, const char *name, *done = true; gw_err("{-/}did you mean{0}:\n"); } - if (!vflag(value, vflag_builtin)) - gwerr_secondary("declared here", value->from->filename, - value->from->loc); + if (!vflag(value, vflag_builtin)) defined_here(value); } } } -- 2.43.0