From c6e13fa4585ae55c4071fbfe2ae4bdbcf9ee85eb Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Tue, 12 Nov 2019 13:25:52 +0100 Subject: [PATCH] :art: Pass macros --- ast | 2 +- include/parse.h | 61 ++++++++++------------------------------------- src/emit/emit.c | 46 ++++++++++++++++------------------- src/lib/string.c | 4 ++-- src/parse/check.c | 46 +++++++++++++---------------------- src/parse/scan1.c | 18 +++++++------- src/parse/scan2.c | 19 ++++++++------- 7 files changed, 71 insertions(+), 125 deletions(-) diff --git a/ast b/ast index 511f6150..a95e4eac 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 511f61502bebcfc02df1e651d9d742b74072e4ed +Subproject commit a95e4eac0089fcbb55cd477987f5dcd40116eb6f diff --git a/include/parse.h b/include/parse.h index 4cbf523b..d03d98bb 100644 --- a/include/parse.h +++ b/include/parse.h @@ -24,57 +24,19 @@ if(GET_FLAG(a, private)) \ else if(GET_FLAG(a, protect)) \ SET_FLAG(b, protect); -typedef m_bool (*_exp_func)(const void*, const void*); -static inline m_bool dummy_func(const void*a NUSED, - const void*b NUSED) { return 1; } - -#define DECL_PRIM_FUNC(prefix) \ -static const _exp_func prim_func[] = { \ - (_exp_func)prefix##__primary_id, (_exp_func)prefix##__primary_num, \ - (_exp_func)prefix##__primary_float, (_exp_func)prefix##__primary_str, \ - (_exp_func)prefix##__primary_array, (_exp_func)prefix##__primary_hack, \ - (_exp_func)prefix##__primary_complex, (_exp_func)prefix##__primary_polar, \ - (_exp_func)prefix##__primary_vec, (_exp_func)prefix##__primary_tuple, \ - (_exp_func)prefix##__primary_unpack, (_exp_func)prefix##__primary_char, \ - (_exp_func)prefix##__primary_nil \ -} - -#define DECL_EXP_FUNC(prefix) \ -static const _exp_func exp_func[] = { \ - (_exp_func)prefix##_exp_decl, (_exp_func)prefix##_exp_binary, (_exp_func)prefix##_exp_unary, \ - (_exp_func)prefix##_exp_primary, (_exp_func)prefix##_exp_cast, (_exp_func)prefix##_exp_post, \ - (_exp_func)prefix##_exp_call, (_exp_func)prefix##_exp_array, (_exp_func)prefix##_exp_if, \ - (_exp_func)prefix##_exp_dot, (_exp_func)prefix##_exp_lambda, (_exp_func)prefix##_exp_typeof \ -}; - -#define DECL_STMT_FUNC(prefix) \ -static const _exp_func stmt_func[] = { \ - (_exp_func)prefix##__stmt_exp, (_exp_func)prefix##__stmt_while, \ - (_exp_func)prefix##__stmt_until, (_exp_func)prefix##__stmt_for, \ - (_exp_func)prefix##__stmt_auto, (_exp_func)prefix##__stmt_loop, \ - (_exp_func)prefix##__stmt_if, (_exp_func)prefix##__stmt_code, \ - (_exp_func)prefix##__stmt_break, (_exp_func)prefix##__stmt_continue, \ - (_exp_func)prefix##__stmt_return, (_exp_func)prefix##__stmt_match, \ - (_exp_func)prefix##__stmt_jump, \ -}; - -#define DECL_SECTION_FUNC(prefix) \ -static const _exp_func section_func[] = { \ - (_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def, \ - (_exp_func)prefix##_enum_def, (_exp_func)prefix##_union_def, \ - (_exp_func)prefix##_fptr_def, (_exp_func)prefix##_type_def \ -}; \ -ANN static inline m_bool prefix##_section(const void* a, /* const */ Section* section) { \ - void* d = §ion->d.stmt_list; \ - return section_func[section->section_type](a, *(void**)d); \ +#define HANDLE_SECTION_FUNC(prefix, type, Arg) \ +DECL_SECTION_FUNC(prefix, type, Arg) \ +ANN static inline type prefix##_section(const Arg a, /* const */ Section* section) { \ + void* d = §ion->d.stmt_list; \ + return section_func[section->section_type](a, *(void**)d); \ } -#define HANDLE_EXP_FUNC(prefix, type, ret) \ -DECL_EXP_FUNC(prefix) \ -ANN type prefix##_exp(const Env env, Exp exp) { \ - do CHECK_BB(exp_func[exp->exp_type](env, &exp->d)) \ - while((exp = exp->next)); \ - return ret; \ +#define HANDLE_EXP_FUNC(prefix, type, Arg) \ +DECL_EXP_FUNC(prefix, type, Arg) \ +ANN type prefix##_exp(const Arg arg, Exp exp) { \ + do CHECK_BB(exp_func[exp->exp_type](arg, &exp->d)) \ + while((exp = exp->next)); \ + return GW_OK; \ } ANN m_bool scan1_exp(const Env, Exp); ANN m_bool scan2_exp(const Env, Exp); @@ -88,6 +50,7 @@ ANN m_uint union_push(const Env, const Union_Def); ANN void union_pop(const Env, const Union_Def, const m_uint); ANN m_bool check_stmt(const Env env, const Stmt stmt); +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, void* data); static inline ANN m_bool env_body(const Env env, const Class_Def cdef, const _exp_func f) { return scanx_body(env, cdef, f, env); diff --git a/src/emit/emit.c b/src/emit/emit.c index ca90a3df..a133530f 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -388,7 +388,7 @@ ANN static VM_Code finalyze(const Emitter emit, const f_instr exec) { return code; } -ANN static m_bool prim_array(const Emitter emit, const Exp_Primary * primary) { +ANN static m_bool emit_prim_array(const Emitter emit, const Exp_Primary * primary) { const Array_Sub array = primary->d.array; Exp e = array->exp; CHECK_BB(emit_exp(emit, e, 0)) @@ -510,7 +510,7 @@ ANN static m_bool emit_exp_array(const Emitter emit, const Exp_Array* array) { return _emit_indexes(emit, &info); } -ANN static m_bool prim_vec(const Emitter emit, const Exp_Primary * primary) { +ANN static m_bool emit_prim_vec(const Emitter emit, const Exp_Primary * primary) { const Vec * vec = &primary->d.vec; const ae_prim_t t = primary->primary_type; CHECK_BB(emit_exp(emit, vec->exp, 0)); @@ -529,38 +529,38 @@ ANN static m_bool prim_vec(const Emitter emit, const Exp_Primary * primary) { return GW_OK; } -ANN static m_bool prim_id(const Emitter emit, const Exp_Primary* prim) { +ANN static m_bool emit_prim_id(const Emitter emit, const Exp_Primary* prim) { struct SpecialId_ * spid = specialid_get(emit->gwion, prim->d.var); if(spid) return specialid_instr(emit, spid, prim) ? GW_OK : GW_ERROR; return emit_symbol(emit, prim); } -ANN static m_bool prim_tuple(const Emitter emit, const Exp_Primary * primary) { +ANN static m_bool emit_prim_tuple(const Emitter emit, const Exp_Primary * primary) { CHECK_BB(emit_exp(emit, primary->d.tuple.exp, 1)) const Instr instr = emit_add_instr(emit, TupleCtor); instr->m_val = (m_uint)exp_self(primary)->type; return GW_OK; } -ANN static m_bool prim_num(const Emitter emit, const Exp_Primary * primary) { +ANN static m_bool emit_prim_num(const Emitter emit, const Exp_Primary * primary) { regpushi(emit, primary->d.num); return GW_OK; } -ANN static m_bool prim_float(const Emitter emit, const Exp_Primary* primary) { +ANN static m_bool emit_prim_float(const Emitter emit, const Exp_Primary* primary) { const Instr instr = emit_add_instr(emit, RegPushImm2); instr->f = primary->d.fnum; return GW_OK; } -ANN static m_bool prim_char(const Emitter emit, const Exp_Primary* prim) { +ANN static m_bool emit_prim_char(const Emitter emit, const Exp_Primary* prim) { DECL_BB(const m_int, c, = str2char(emit, prim->d.chr, exp_self(prim)->pos)) regpushi(emit, c); return GW_OK; } -ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { +ANN static m_bool emit_prim_str(const Emitter emit, const Exp_Primary* prim) { char c[strlen(prim->d.str) + 1]; if(strlen(prim->d.str)) { strcpy(c, prim->d.str); @@ -575,7 +575,7 @@ ANN static m_bool prim_str(const Emitter emit, const Exp_Primary* prim) { return GW_OK; } -ANN static m_bool prim_gack(const Emitter emit, const Exp_Primary* primary) { +ANN static m_bool emit_prim_hack(const Emitter emit, const Exp_Primary* primary) { const Exp exp = primary->d.exp; Exp e = exp, next = NULL; do { @@ -594,20 +594,18 @@ ANN static m_bool prim_gack(const Emitter emit, const Exp_Primary* primary) { return GW_OK; } -ANN static m_bool prim_unpack(const Emitter emit NUSED, const Exp_Primary* primary) { +ANN static m_bool emit_prim_unpack(const Emitter emit NUSED, const Exp_Primary* primary) { if(exp_self(primary)->meta == ae_meta_var) return GW_OK; ERR_B(exp_self(primary)->pos, _("unused Tuple unpack")) } -static const _exp_func prim_func[] = { - (_exp_func)prim_id, (_exp_func)prim_num, (_exp_func)prim_float, (_exp_func)prim_str, - (_exp_func)prim_array, (_exp_func)prim_gack, (_exp_func)prim_vec, (_exp_func)prim_vec, - (_exp_func)prim_vec, (_exp_func)prim_tuple, (_exp_func)prim_unpack, - (_exp_func)prim_char, (_exp_func)dummy_func, -}; +#define emit_prim_complex emit_prim_vec +#define emit_prim_polar emit_prim_vec +#define emit_prim_nil (void*)dummy_func -ANN static m_bool emit_exp_primary(const Emitter emit, const Exp_Primary* prim) { +DECL_PRIM_FUNC(emit, m_bool , Emitter); +ANN static m_bool emit_exp_primary(const Emitter emit, Exp_Primary *const prim) { return prim_func[prim->primary_type](emit, prim); } @@ -1163,7 +1161,7 @@ ANN static m_bool emit_exp_typeof(const Emitter emit, const Exp_Typeof *exp) { return GW_OK; } -DECL_EXP_FUNC(emit) +DECL_EXP_FUNC(emit, m_bool, Emitter) ANN2(1) static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool ref) { do { @@ -1573,13 +1571,9 @@ ANN static m_bool emit_stmt_match(const Emitter emit, const struct Stmt_Match_* return ret; } -static const _exp_func stmt_func[] = { - (_exp_func)emit_stmt_exp, (_exp_func)emit_stmt_flow, (_exp_func)emit_stmt_flow, - (_exp_func)emit_stmt_for, (_exp_func)emit_stmt_auto, (_exp_func)emit_stmt_loop, - (_exp_func)emit_stmt_if, (_exp_func)emit_stmt_code, (_exp_func)emit_stmt_break, - (_exp_func)emit_stmt_continue, (_exp_func)emit_stmt_return, (_exp_func)emit_stmt_match, - (_exp_func)emit_stmt_jump, -}; +#define emit_stmt_while emit_stmt_flow +#define emit_stmt_until emit_stmt_flow +DECL_STMT_FUNC(emit, m_bool , Emitter) ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) { CHECK_BB(stmt_func[stmt->stmt_type](emit, &stmt->d)) @@ -1859,7 +1853,7 @@ ANN static m_bool emit_func_def(const Emitter emit, const Func_Def fdef) { } #define emit_fptr_def dummy_func -DECL_SECTION_FUNC(emit) +HANDLE_SECTION_FUNC(emit, m_bool, Emitter) ANN Code* emit_class_code(const Emitter emit, const m_str name) { const m_uint len = strlen(name) + 7; diff --git a/src/lib/string.c b/src/lib/string.c index c51a02ae..9312f197 100644 --- a/src/lib/string.c +++ b/src/lib/string.c @@ -170,12 +170,12 @@ static CTOR(string_ctor) { STRING(o) = ""; } -ANN Type prim_str(const Env, Exp_Primary *const); +ANN Type check_prim_str(const Env, Exp_Primary *const); ID_CHECK(check_funcpp) { ((Exp_Primary*)prim)->primary_type = ae_primary_str; ((Exp_Primary*)prim)->d.str = env->func ? env->func->name : env->class_def ? env->class_def->name : env->name; - return prim_str(env, (Exp_Primary * const)prim); + return check_prim_str(env, (Exp_Primary * const)prim); } static GACK(gack_string) { diff --git a/src/parse/check.c b/src/parse/check.c index 9922f79d..e87a95a0 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -193,7 +193,7 @@ ANN static inline Type prim_array_match(const Env env, Exp e) { return array_type(env, type->array_depth ? array_base(type) : type, type->array_depth + 1); } -ANN static Type prim_array(const Env env, const Exp_Primary* primary) { +ANN static Type check_prim_array(const Env env, const Exp_Primary* primary) { const Array_Sub array = primary->d.array; const Exp e = array->exp; if(!e) @@ -258,7 +258,7 @@ ANN static inline Value prim_str_value(const Env env, const Symbol sym) { return value; } -ANN Type prim_str(const Env env, Exp_Primary *const prim) { +ANN Type check_prim_str(const Env env, Exp_Primary *const prim) { if(!prim->value) { const m_str str = prim->d.str; char c[strlen(str) + 8]; @@ -268,7 +268,7 @@ ANN Type prim_str(const Env env, Exp_Primary *const prim) { return env->gwion->type[et_string];// prim->value } -ANN static Type prim_id(const Env env, Exp_Primary* primary) { +ANN static Type check_prim_id(const Env env, Exp_Primary* primary) { struct SpecialId_ * spid = specialid_get(env->gwion, primary->d.var); if(spid) return specialid_type(env, spid, primary); @@ -305,7 +305,7 @@ ANN static void vec_info(const Env env, const ae_prim_t t, struct VecInfo* v) { } } -ANN static Type prim_vec(const Env env, const Exp_Primary* primary) { +ANN static Type check_prim_vec(const Env env, const Exp_Primary* primary) { const Vec * vec = &primary->d.vec; const ae_prim_t t = primary->primary_type; struct VecInfo info = { .n=vec->dim }; @@ -316,14 +316,14 @@ ANN static Type prim_vec(const Env env, const Exp_Primary* primary) { return info.t; } -ANN static Type prim_gack(const Env env, const Exp_Primary * primary) { +ANN static Type check_prim_hack(const Env env, const Exp_Primary * primary) { if(env->func) UNSET_FLAG(env->func, pure); CHECK_OO((check_exp(env, primary->d.exp))) return env->gwion->type[et_gack]; } -ANN static Type prim_tuple(const Env env, const Exp_Primary * primary) { +ANN static Type check_prim_tuple(const Env env, const Exp_Primary * primary) { CHECK_OO(check_exp(env, primary->d.tuple.exp)) struct Vector_ v; vector_init(&v); @@ -336,7 +336,7 @@ ANN static Type prim_tuple(const Env env, const Exp_Primary * primary) { } #define describe_prim_xxx(name, type) \ -ANN static Type prim_##name(const Env env NUSED, const Exp_Primary * primary NUSED) {\ +ANN static Type check##_prim_##name(const Env env NUSED, const Exp_Primary * primary NUSED) {\ return type; \ } describe_prim_xxx(num, env->gwion->type[et_int]) @@ -344,15 +344,12 @@ describe_prim_xxx(float, env->gwion->type[et_float]) describe_prim_xxx(nil, env->gwion->type[et_void]) describe_prim_xxx(unpack, env->gwion->type[et_tuple]) -typedef Type (*_type_func)(const Env, const void*); -static const _type_func prim_func[] = { - (_type_func)prim_id, (_type_func)prim_num, (_type_func)prim_float, (_type_func)prim_str, - (_type_func)prim_array, (_type_func)prim_gack, (_type_func)prim_vec, (_type_func)prim_vec, - (_type_func)prim_vec, (_type_func)prim_tuple, (_type_func)prim_unpack, - (_type_func)prim_num, (_type_func)prim_nil, -}; +#define check_prim_complex check_prim_vec +#define check_prim_polar check_prim_vec +#define check_prim_char check_prim_num +DECL_PRIM_FUNC(check, Type, Env); -ANN static Type check_exp_primary(const Env env, const Exp_Primary* primary) { +ANN static Type check_exp_primary(const Env env, Exp_Primary *primary) { return exp_self(primary)->type = prim_func[primary->primary_type](env, primary); } @@ -936,12 +933,7 @@ ANN static Type check_exp_typeof(const Env env, const Exp_Typeof *exp) { return v->type; } -static const _type_func exp_func[] = { - (_type_func)check_exp_decl, (_type_func)check_exp_binary, (_type_func)check_exp_unary, - (_type_func)check_exp_primary, (_type_func)check_exp_cast, (_type_func)check_exp_post, - (_type_func)check_exp_call, (_type_func)check_exp_array, (_type_func)check_exp_if, - (_type_func)check_exp_dot, (_type_func)check_exp_lambda, (_type_func)check_exp_typeof -}; +DECL_EXP_FUNC(check, Type, Env) ANN static inline Type check_exp(const Env env, const Exp exp) { Exp curr = exp, next = NULL, prev = NULL; @@ -1233,13 +1225,9 @@ ANN static m_bool check_stmt_match(const Env env, const Stmt_Match stmt) { RET_NSPC(_check_stmt_match(env, stmt)) } -static const _exp_func stmt_func[] = { - (_exp_func)check_stmt_exp, (_exp_func)check_stmt_flow, (_exp_func)check_stmt_flow, - (_exp_func)check_stmt_for, (_exp_func)check_stmt_auto, (_exp_func)check_stmt_loop, - (_exp_func)check_stmt_if, (_exp_func)check_stmt_code, (_exp_func)check_stmt_break, - (_exp_func)check_stmt_continue, (_exp_func)check_stmt_return, (_exp_func)check_stmt_match, - (_exp_func)check_stmt_jump, -}; +#define check_stmt_while check_stmt_flow +#define check_stmt_until check_stmt_flow +DECL_STMT_FUNC(check, m_bool , Env) ANN m_bool check_stmt(const Env env, const Stmt stmt) { return stmt_func[stmt->stmt_type](env, &stmt->d); @@ -1384,7 +1372,7 @@ ANN m_bool check_func_def(const Env env, const Func_Def fdef) { } #define check_fptr_def dummy_func -DECL_SECTION_FUNC(check) +HANDLE_SECTION_FUNC(check, m_bool, Env) ANN static m_bool check_parent(const Env env, const Class_Def cdef) { const Type parent = cdef->base.type->e->parent; diff --git a/src/parse/scan1.c b/src/parse/scan1.c index c2d04e6a..d3491b62 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -184,7 +184,7 @@ ANN static inline m_bool scan1_exp_typeof(const restrict Env env, const Exp_Type } #define scan1_exp_lambda dummy_func -HANDLE_EXP_FUNC(scan1, m_bool, 1) +HANDLE_EXP_FUNC(scan1, m_bool, Env) ANN static inline m_bool _scan1_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { CHECK_BB(scan1_exp(env, stmt->cond)) @@ -314,13 +314,13 @@ ANN m_bool scan1_union_def(const Env env, const Union_Def udef) { return ret; } -static const _exp_func stmt_func[] = { - (_exp_func)scan1_stmt_exp, (_exp_func)scan1_stmt_flow, (_exp_func)scan1_stmt_flow, - (_exp_func)scan1_stmt_for, (_exp_func)scan1_stmt_auto, (_exp_func)scan1_stmt_loop, - (_exp_func)scan1_stmt_if, (_exp_func)scan1_stmt_code, (_exp_func)dummy_func, - (_exp_func)dummy_func, (_exp_func)scan1_stmt_exp, (_exp_func)scan1_stmt_match, - (_exp_func)dummy_func -}; +#define scan1_stmt_while scan1_stmt_flow +#define scan1_stmt_until scan1_stmt_flow +#define scan1_stmt_continue (void*)dummy_func +#define scan1_stmt_break (void*)dummy_func +#define scan1_stmt_jump (void*)dummy_func +#define scan1_stmt_return scan1_stmt_exp +DECL_STMT_FUNC(scan1, m_bool, Env) ANN static inline m_bool scan1_stmt(const Env env, const Stmt stmt) { return stmt_func[stmt->stmt_type](env, &stmt->d); @@ -413,7 +413,7 @@ ANN m_bool scan1_func_def(const Env env, const Func_Def fdef) { return ret; } -DECL_SECTION_FUNC(scan1) +HANDLE_SECTION_FUNC(scan1, m_bool, Env) ANN static Type scan1_get_parent(const Env env, const Type_Def tdef) { const Type parent = known_type(env, tdef->ext); diff --git a/src/parse/scan2.c b/src/parse/scan2.c index e6fe1bca..c90145f0 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -200,7 +200,7 @@ ANN static inline m_bool scan2_stmt_match(const restrict Env env, const Stmt_Mat } #define scan2_exp_lambda dummy_func -HANDLE_EXP_FUNC(scan2, m_bool, 1) +HANDLE_EXP_FUNC(scan2, m_bool, Env) #define scan2_stmt_func(name, type, prolog, exp) describe_stmt_func(scan2, name, type, prolog, exp) scan2_stmt_func(flow, Stmt_Flow,, !(scan2_exp(env, stmt->cond) < 0 || @@ -268,13 +268,14 @@ ANN m_bool scan2_union_def(const Env env, const Union_Def udef) { return ret; } -static const _exp_func stmt_func[] = { - (_exp_func)scan2_stmt_exp, (_exp_func)scan2_stmt_flow, (_exp_func)scan2_stmt_flow, - (_exp_func)scan2_stmt_for, (_exp_func)scan2_stmt_auto, (_exp_func)scan2_stmt_loop, - (_exp_func)scan2_stmt_if, (_exp_func)scan2_stmt_code, (_exp_func)dummy_func, - (_exp_func)dummy_func, (_exp_func)scan2_stmt_exp, (_exp_func)scan2_stmt_match, - (_exp_func)scan2_stmt_jump -}; + +#define scan2_stmt_while scan2_stmt_flow +#define scan2_stmt_until scan2_stmt_flow +#define scan2_stmt_continue (void*)dummy_func +#define scan2_stmt_break (void*)dummy_func +#define scan2_stmt_return scan2_stmt_exp + +DECL_STMT_FUNC(scan2, m_bool, Env) ANN static m_bool scan2_stmt(const Env env, const Stmt stmt) { return stmt_func[stmt->stmt_type](env, &stmt->d); @@ -518,7 +519,7 @@ ANN m_bool scan2_func_def(const Env env, const Func_Def f) { } #define scan2_enum_def dummy_func -DECL_SECTION_FUNC(scan2) +HANDLE_SECTION_FUNC(scan2, m_bool, Env) ANN static m_bool scan2_parent(const Env env, const Class_Def cdef) { const Type parent = cdef->base.type->e->parent; -- 2.43.0