From 15382fe85e399df3cd8fa5173bc98418dada81ce Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Tue, 22 Dec 2020 13:42:28 +0100 Subject: [PATCH] :art: Use vector for match->cond --- include/match.h | 20 ++++++++++---------- src/emit/emit.c | 6 +++--- src/parse/check.c | 18 ++++++++---------- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/include/match.h b/include/match.h index 5b1d4b9b..4a092db9 100644 --- a/include/match.h +++ b/include/match.h @@ -1,30 +1,30 @@ #ifndef __MATCH #define __MATCH struct Match_ { - struct Map_ map; + struct Vector_ cond; struct Vector_ vec; }; ANN static inline void match_map(struct Match_ * const match, Exp e) { - const Map map = &match->map; - map_init(map); + const Vector cond = &match->cond; + vector_init(cond); Exp next; do { next = e->next; e->next = NULL; - map_set(map, (vtype)e, 0); + vector_add(cond, (vtype)e); } while((e = next)); } ANN static inline void match_unmap(struct Match_ * const match) { - const Map map = &match->map; - const vtype sz = map_size(map); - for(m_uint i = 0; i < sz; ++i) { - const Exp e = (Exp)VKEY(map, i), - next = (i < (sz-2)) ? (Exp)VKEY(map, i + 1) : NULL; + const Vector cond = &match->cond; + const vtype sz = vector_size(cond); + for(m_uint i = 0; i < sz -1; ++i) { + const Exp e = (Exp)vector_at(cond, i), + next = (Exp)vector_at(cond, i + 1); e->next = next; } - map_release(map); + vector_release(cond); } #define MATCH_INI(scope) \ diff --git a/src/emit/emit.c b/src/emit/emit.c index 14afb8f2..2e26303f 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -1852,9 +1852,9 @@ ANN static Symbol case_op(const Emitter emit, const Exp base, const Exp e, const ANN static m_bool _emit_stmt_match_case(const Emitter emit, const struct Stmt_Match_* stmt, const Vector v) { Exp e = stmt->cond; - const Map map = &emit->env->scope->match->map; - for(m_uint i = 0; i < map_size(map) && e; e = e->next, ++i) { - const Exp base = (Exp)VKEY(map, i); + const Vector cond = &emit->env->scope->match->cond; + for(m_uint i = 0; i < vector_size(cond) && e; e = e->next, ++i) { + const Exp base = (Exp)vector_at(cond, i); const Symbol op = case_op(emit, base, e, v, 0); if(op != CASE_PASS) CHECK_BB(emit_case_head(emit, base, e, op, v)) diff --git a/src/parse/check.c b/src/parse/check.c index c6f78665..f4985edc 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -1025,22 +1025,21 @@ ANN static m_bool check_stmt_exp(const Env env, const Stmt_Exp stmt) { return stmt->val ? check_exp(env, stmt->val) ? 1 : -1 : 1; } -ANN static Value match_value(const Env env, const Type base, const Exp_Primary* prim, const m_uint i) { +ANN static Value match_value(const Env env, const Type base, const Exp_Primary* prim) { const Symbol sym = prim->d.var; const Value v = new_value(env->gwion->mp, base, s_name(sym)); set_vflag(v, vflag_valid); nspc_add_value(env->curr, sym, v); - VVAL(&env->scope->match->map, i) = (vtype)v; return v; } -ANN static Symbol case_op(const Env env, const Type base, const Exp e, const m_uint i) { +ANN static Symbol case_op(const Env env, const Type base, const Exp e) { if(e->exp_type == ae_exp_primary) { if(e->d.prim.prim_type == ae_prim_id) { if(e->d.prim.d.var == insert_symbol("_")) return NULL; if(!nspc_lookup_value1(env->curr, e->d.prim.d.var)) { - e->d.prim.value = match_value(env, base, &e->d.prim, i); + e->d.prim.value = match_value(env, base, &e->d.prim); return NULL; } } @@ -1049,9 +1048,8 @@ ANN static Symbol case_op(const Env env, const Type base, const Exp e, const m_u if(func->d.prim.prim_type == ae_prim_id) { const Value v= find_value(actual_type(env->gwion, base), func->d.prim.d.var); if(v) { - if(!i) - e->type = v->type; - case_op(env, v->type, e->d.exp_call.args, i); + e->type = v->type; + case_op(env, v->type, e->d.exp_call.args); e->d.exp_call.args->type = v->type; return NULL; } @@ -1062,12 +1060,12 @@ ANN static Symbol case_op(const Env env, const Type base, const Exp e, const m_u ANN static m_bool match_case_exp(const Env env, Exp e) { Exp last = e; - for(m_uint i = 0; i < map_size(&env->scope->match->map); e = e->next, ++i) { + for(m_uint i = 0; i < vector_size(&env->scope->match->cond); e = e->next, ++i) { if(!e) ERR_B(last->pos, _("no enough to match")) last = e; - const Exp base = (Exp)VKEY(&env->scope->match->map, i); - const Symbol op = case_op(env, base->type, e, i); + const Exp base = (Exp)vector_at(&env->scope->match->cond, i); + const Symbol op = case_op(env, base->type, e); if(op) { const Exp next = e->next; e->next = NULL; -- 2.43.0