From a182b2f541b3a1dbc03bd9813dfd500c634416f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Astor?= Date: Sun, 23 Feb 2020 14:18:31 +0100 Subject: [PATCH] :art: Introduce interp --- ast | 2 +- include/opcode.h | 4 ++-- src/emit/emit.c | 36 ++++++++++++++++++++++++------------ src/parse/check.c | 5 +++++ src/parse/scan1.c | 4 ++++ src/parse/scan2.c | 4 ++++ src/vm/vm.c | 16 ++++++++++------ 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/ast b/ast index db1106db..15c11079 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit db1106db2e29341dff616e070530d0881a925c8f +Subproject commit 15c110793cba16dc12855082ebf14794c5f16ea0 diff --git a/include/opcode.h b/include/opcode.h index de85bdeb..0634386d 100644 --- a/include/opcode.h +++ b/include/opcode.h @@ -169,8 +169,8 @@ enum { eGcIni, eGcAdd, eGcEnd, - eGack, eGackEnd, + eGack, eNoOp, eDotTmplVal, eOP_MAX, @@ -345,8 +345,8 @@ enum { #define GcIni (f_instr)eGcIni #define GcAdd (f_instr)eGcAdd #define GcEnd (f_instr)eGcEnd -#define Gack (f_instr)eGack #define GackEnd (f_instr)eGackEnd +#define Gack (f_instr)eGack #define NoOp (f_instr)eNoOp #define DotTmplVal (f_instr)eDotTmplVal #define OP_MAX (f_instr)eOP_MAX diff --git a/src/emit/emit.c b/src/emit/emit.c index 708a5b11..070515d0 100644 --- a/src/emit/emit.c +++ b/src/emit/emit.c @@ -598,9 +598,19 @@ ANN static m_bool emit_prim_str(const Emitter emit, const m_str *str) { return GW_OK; } -ANN static m_bool emit_prim_hack(const Emitter emit, const Exp *exp) { +ANN static m_bool emit_prim_unpack(const Emitter emit NUSED, const Tuple *tuple) { + if(prim_exp(tuple)->meta == ae_meta_var) + return GW_OK; + ERR_B(prim_pos(tuple), _("unused Tuple unpack")) +} + +#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_interp(const Emitter emit, const Exp exp) { regpushi(emit, 0); - Exp e = *exp, next = NULL; + Exp e = exp, next = NULL; do { next = e->next; e->next = NULL; @@ -612,21 +622,16 @@ ANN static m_bool emit_prim_hack(const Emitter emit, const Exp *exp) { instr->m_val = (m_uint)e->type; instr->m_val2 = emit_code_offset(emit); } while((e = e->next = next)); - if(!(emit->env->func && emit->env->func->def->base->xid == insert_symbol("@gack"))) - emit_add_instr(emit, GackEnd); return GW_OK; } -ANN static m_bool emit_prim_unpack(const Emitter emit NUSED, const Tuple *tuple) { - if(prim_exp(tuple)->meta == ae_meta_var) - return GW_OK; - ERR_B(prim_pos(tuple), _("unused Tuple unpack")) +ANN static m_bool emit_prim_hack(const Emitter emit, const Exp *exp) { + CHECK_BB(emit_interp(emit, *exp)) + if(!(emit->env->func && emit->env->func->def->base->xid == insert_symbol("@gack"))) + emit_add_instr(emit, GackEnd); + return GW_OK; } -#define emit_prim_complex emit_prim_vec -#define emit_prim_polar emit_prim_vec -#define emit_prim_nil (void*)dummy_func - DECL_PRIM_FUNC(emit, m_bool , Emitter); ANN static m_bool emit_prim(const Emitter emit, Exp_Primary *const prim) { return emit_prim_func[prim->prim_type](emit, &prim->d); @@ -1239,6 +1244,13 @@ ANN static m_bool emit_exp_typeof(const Emitter emit, const Exp_Typeof *exp) { return GW_OK; } +ANN static m_bool emit_exp_interp(const Emitter emit, const Exp_Interp *exp) { + CHECK_BB(emit_interp(emit, exp->exp)) + const Instr instr = emit_add_instr(emit, GackEnd); + instr->m_val = 1; + return GW_OK; +} + DECL_EXP_FUNC(emit, m_bool, Emitter) ANN2(1) static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool ref) { diff --git a/src/parse/check.c b/src/parse/check.c index ff2a7812..57e093bd 100644 --- a/src/parse/check.c +++ b/src/parse/check.c @@ -939,6 +939,11 @@ ANN static Type check_exp_typeof(const Env env, const Exp_Typeof *exp) { return v->type; } +ANN static Type check_exp_interp(const Env env, const Exp_Interp* exp) { + CHECK_OO(check_exp(env, exp->exp)) + return env->gwion->type[et_string]; +} + DECL_EXP_FUNC(check, Type, Env) ANN Type check_exp(const Env env, const Exp exp) { diff --git a/src/parse/scan1.c b/src/parse/scan1.c index 12acfb42..d1db4f31 100644 --- a/src/parse/scan1.c +++ b/src/parse/scan1.c @@ -197,6 +197,10 @@ ANN static inline m_bool scan1_exp_typeof(const restrict Env env, const Exp_Type return scan1_exp(env, exp->exp); } +ANN static inline m_bool scan1_exp_interp(const restrict Env env, const Exp_Interp *exp) { + return scan1_exp(env, exp->exp); +} + #define scan1_exp_lambda dummy_func HANDLE_EXP_FUNC(scan1, m_bool, Env) diff --git a/src/parse/scan2.c b/src/parse/scan2.c index fbb76028..ba453323 100644 --- a/src/parse/scan2.c +++ b/src/parse/scan2.c @@ -189,6 +189,10 @@ ANN static inline m_bool scan2_exp_typeof(const restrict Env env, const Exp_Type return scan2_exp(env, exp->exp); } +ANN static inline m_bool scan2_exp_interp(const restrict Env env, const Exp_Interp *exp) { + return scan2_exp(env, exp->exp); +} + ANN static inline m_bool _scan2_stmt_match_case(const restrict Env env, const Stmt_Match stmt) { CHECK_BB(scan2_exp(env, stmt->cond)) if(stmt->when) diff --git a/src/vm/vm.c b/src/vm/vm.c index 7c8becf8..41169e68 100644 --- a/src/vm/vm.c +++ b/src/vm/vm.c @@ -340,7 +340,7 @@ ANN void vm_run(const VM* vm) { // lgtm [cpp/use-of-goto] &&staticint, &&staticfloat, &&staticother, &&dotfunc, &&dotstaticfunc, &&pushstaticcode, &&gcini, &&gcadd, &&gcend, - &&gack, &&gackend, &&noop, &®pushimm, &&other, &&eoc + &&gackend, &&gack, &&noop, &®pushimm, &&other, &&eoc }; const Shreduler s = vm->shreduler; register VM_Shred shred; @@ -849,16 +849,20 @@ gcend: while((a.obj = (M_Object)vector_pop(&shred->gc))) _release(a.obj, shred); DISPATCH() -gack: - VM_OUT - gack(shred, (Instr)VAL); - goto in; gackend: { m_str str = *(m_str*)(reg - SZ_INT); - gw_out("%s\n", str); + if(!VAL) + gw_out("%s\n", str); + else + *(M_Object*)(reg - SZ_INT)= new_string(vm->gwion->mp, shred, str); mp_free2(vm->gwion->mp, strlen(str), str); + DISPATCH(); } +gack: + VM_OUT + gack(shred, (Instr)VAL); + goto in; noop: DISPATCH(); other: -- 2.43.0