Skip to content

Commit

Permalink
🎨 Operators as symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Jul 5, 2019
1 parent b5b0f2c commit 8fb8790
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 276 deletions.
2 changes: 1 addition & 1 deletion ast
Submodule ast updated from e44ed6 to 288d88
3 changes: 1 addition & 2 deletions include/gwi.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ typedef struct {
} DL_Func;

typedef struct {
Operator op;
Symbol op;
m_str ret, lhs, rhs;
Type (*ck)(Env, void*, m_bool*);
m_bool (*em)(Emitter, void*);
m_bool mut;
} DL_Oper;

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions include/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct Gwi_* Gwi;
#define ALLOC_PTR(p, a, b, c) b* a = (b*)_mp_calloc(p, sizeof(b)); *a = (b)c
#define _CHECK_OP(op, check, func)\
CHECK_BB(gwi_oper_add(gwi, opck_##check))\
CHECK_BB(gwi_oper_end(gwi, op_##op, func))
CHECK_BB(gwi_oper_end(gwi, op, func))
#define GWI_LOC new_loc(gwi->gwion->mp, __LINE__)


Expand Down Expand Up @@ -61,7 +61,7 @@ ANN m_int gwi_func_end(const Gwi gwi, const ae_flag flag);
ANN2(1) m_int gwi_oper_ini(const Gwi gwi, const m_str l, const m_str r, const m_str t);
ANN m_int gwi_oper_add(const Gwi gwi, opck);
ANN m_int gwi_oper_emi(const Gwi gwi, opem);
ANN2(1) m_int gwi_oper_end(const Gwi gwi, const Operator op, const f_instr f);
ANN2(1) m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f);

ANN Type_Decl* str2decl(const Env, const m_str, m_uint* depth);

Expand Down
2 changes: 1 addition & 1 deletion include/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Op_Import {
opem em;
uintptr_t data;
loc_t pos;
Operator op;
Symbol op;
};

struct Implicit {
Expand Down
10 changes: 5 additions & 5 deletions src/emit/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ ANN static m_bool emit_exp_binary(const Emitter emit, const Exp_Binary* bin) {
}

ANN static m_bool emit_exp_cast(const Emitter emit, const Exp_Cast* cast) {
struct Op_Import opi = { .op=op_cast, .lhs=cast->exp->type, .rhs=exp_self(cast)->type, .data=(uintptr_t)cast};
struct Op_Import opi = { .op=insert_symbol("$"), .lhs=cast->exp->type, .rhs=exp_self(cast)->type, .data=(uintptr_t)cast};
CHECK_BB(emit_exp(emit, cast->exp, 0))
(void)op_emit(emit, &opi);
return GW_OK;
Expand Down Expand Up @@ -955,7 +955,7 @@ ANN static m_bool spork_func(const Emitter emit, const Exp_Call* exp) {
}

ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary* unary) {
const m_bool is_spork = unary->op == op_spork;
const m_bool is_spork = unary->op == insert_symbol("spork");
const Func f = !unary->code ? unary->exp->d.exp_call.m_func : NULL;
if(!f) {
emit_add_instr(emit, RegPushImm);
Expand All @@ -969,7 +969,7 @@ ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary* unary) {
CHECK_BB(spork_func(emit, &unary->exp->d.exp_call))
}
const VM_Code code = finalyze(emit);
const Instr ini = emit_add_instr(emit, unary->op == op_spork ? SporkIni : ForkIni);
const Instr ini = emit_add_instr(emit, unary->op == insert_symbol("spork") ? SporkIni : ForkIni);
ini->m_val = (m_uint)code;
ini->m_val2 = is_spork;
if(!f) {
Expand Down Expand Up @@ -1004,7 +1004,7 @@ ANN m_bool emit_exp_spork(const Emitter emit, const Exp_Unary* unary) {

ANN static m_bool emit_exp_unary(const Emitter emit, const Exp_Unary* unary) {
struct Op_Import opi = { .op=unary->op, .data=(uintptr_t)unary };
if(unary->op != op_spork && unary->op != op_fork && unary->exp) {
if(unary->op != insert_symbol("spork") && unary->op != insert_symbol("fork") && unary->exp) {
CHECK_BB(emit_exp(emit, unary->exp, 1))
opi.rhs = unary->exp->type;
}
Expand All @@ -1014,7 +1014,7 @@ ANN static m_bool emit_exp_unary(const Emitter emit, const Exp_Unary* unary) {
ANN static m_bool emit_implicit_cast(const Emitter emit,
const restrict Exp from, const restrict Type to) {
const struct Implicit imp = { from, to };
struct Op_Import opi = { .op=op_impl, .lhs=from->type, .rhs=to, .data=(m_uint)&imp };
struct Op_Import opi = { .op=insert_symbol("@implicit"), .lhs=from->type, .rhs=to, .data=(m_uint)&imp };
return op_emit(emit, &opi);
}

Expand Down
6 changes: 3 additions & 3 deletions src/lib/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ GWION_IMPORT(array) {
CHECK_BB(gwi_class_end(gwi))
CHECK_BB(gwi_oper_ini(gwi, "@Array", (m_str)OP_ANY_TYPE, NULL))
CHECK_BB(gwi_oper_add(gwi, opck_array_at))
CHECK_BB(gwi_oper_end(gwi, op_ref, ObjectAssign))
CHECK_BB(gwi_oper_end(gwi, "@=>", ObjectAssign))
CHECK_BB(gwi_oper_add(gwi, opck_array_shift))
CHECK_BB(gwi_oper_emi(gwi, opem_array_shift))
CHECK_BB(gwi_oper_end(gwi, op_shl, NULL))
CHECK_BB(gwi_oper_end(gwi, "<<", NULL))
CHECK_BB(gwi_oper_ini(gwi, "@Array", "Array", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_array_cast))
CHECK_BB(gwi_oper_emi(gwi, opem_basic_cast))
CHECK_BB(gwi_oper_end(gwi, op_cast, NULL))
CHECK_BB(gwi_oper_end(gwi, "$", NULL))
register_freearg(gwi, ArrayAlloc, freearg_array);
return GW_OK;
}
Expand Down
36 changes: 18 additions & 18 deletions src/lib/complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,34 @@ GWION_IMPORT(complex) {
CHECK_BB(gwi_item_end(gwi, ae_flag_member, NULL))
CHECK_BB(gwi_class_end(gwi))
CHECK_BB(gwi_oper_ini(gwi, "complex", "complex", "complex"))
CHECK_BB(gwi_oper_end(gwi, op_add, ComplexAdd))
CHECK_BB(gwi_oper_end(gwi, op_sub, ComplexSub))
CHECK_BB(gwi_oper_end(gwi, op_mul, ComplexMul))
CHECK_BB(gwi_oper_end(gwi, op_div, ComplexDiv))
CHECK_BB(gwi_oper_end(gwi, "+", ComplexAdd))
CHECK_BB(gwi_oper_end(gwi, "-", ComplexSub))
CHECK_BB(gwi_oper_end(gwi, "*", ComplexMul))
CHECK_BB(gwi_oper_end(gwi, "/", ComplexDiv))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_chuck, ComplexRAssign))
CHECK_BB(gwi_oper_end(gwi, "=>", ComplexRAssign))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_radd, ComplexRAdd))
CHECK_BB(gwi_oper_end(gwi, "+=>", ComplexRAdd))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rsub, ComplexRSub))
CHECK_BB(gwi_oper_end(gwi, "-=>", ComplexRSub))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rmul, ComplexRMul))
CHECK_BB(gwi_oper_end(gwi, "*=>", ComplexRMul))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rdiv, ComplexRDiv))
CHECK_BB(gwi_oper_end(gwi, "/=>", ComplexRDiv))
CHECK_BB(gwi_oper_ini(gwi, "polar", "polar", "polar"))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_chuck, ComplexRAssign))
CHECK_BB(gwi_oper_end(gwi, op_add, PolarAdd))
CHECK_BB(gwi_oper_end(gwi, op_sub, PolarSub))
CHECK_BB(gwi_oper_end(gwi, op_mul, PolarMul))
CHECK_BB(gwi_oper_end(gwi, op_div, PolarDiv))
CHECK_BB(gwi_oper_end(gwi, "=>", ComplexRAssign))
CHECK_BB(gwi_oper_end(gwi, "+", PolarAdd))
CHECK_BB(gwi_oper_end(gwi, "-", PolarSub))
CHECK_BB(gwi_oper_end(gwi, "*", PolarMul))
CHECK_BB(gwi_oper_end(gwi, "/", PolarDiv))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_radd, PolarRAdd))
CHECK_BB(gwi_oper_end(gwi, "+=>", PolarRAdd))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rsub, PolarRSub))
CHECK_BB(gwi_oper_end(gwi, "-=>", PolarRSub))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rmul, PolarRMul))
CHECK_BB(gwi_oper_end(gwi, "*=>", PolarRMul))
CHECK_BB(gwi_oper_add(gwi, opck_rassign))
CHECK_BB(gwi_oper_end(gwi, op_rdiv, PolarRDiv))
CHECK_BB(gwi_oper_end(gwi, "/=>", PolarRDiv))
return GW_OK;
}
2 changes: 1 addition & 1 deletion src/lib/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ANN static m_bool import_core_libs(const Gwi gwi) {
CHECK_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL))
CHECK_BB(gwi_oper_add(gwi, opck_new))
CHECK_BB(gwi_oper_emi(gwi, opem_new))
CHECK_BB(gwi_oper_end(gwi, op_new, NULL))
CHECK_BB(gwi_oper_end(gwi, "new", NULL))
CHECK_BB(import_prim(gwi))
CHECK_BB(import_complex(gwi))
CHECK_BB(import_vec3(gwi))
Expand Down
2 changes: 1 addition & 1 deletion src/lib/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ GWION_IMPORT(event) {
CHECK_BB(gwi_func_end(gwi, 0))
CHECK_BB(gwi_class_end(gwi))
CHECK_BB(gwi_oper_ini(gwi, "Event", "@now", "int"))
_CHECK_OP(chuck, eventwait, EventWait)
_CHECK_OP("=>", eventwait, EventWait)
return GW_OK;
}
18 changes: 9 additions & 9 deletions src/lib/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,19 @@ ANN Type check_exp_unary_spork(const Env env, const Stmt code);

static OP_CHECK(opck_spork) {
const Exp_Unary* unary = (Exp_Unary*)data;
if(unary->op == op_fork && !unary->fork_ok)
if(unary->op == insert_symbol("fork") && !unary->fork_ok)
ERR_O(exp_self(unary)->pos, _("forks must be stored in a value:\n"
"fork xxx @=> Fork f"))
if(unary->exp && unary->exp->exp_type == ae_exp_call)
return unary->op == op_spork ? t_shred : t_fork;
return unary->op == insert_symbol("spork") ? t_shred : t_fork;
else if(unary->code) {
++env->scope->depth;
nspc_push_value(env->gwion->mp, env->curr);
const m_bool ret = check_stmt(env, unary->code);
nspc_pop_value(env->gwion->mp, env->curr);
--env->scope->depth;
CHECK_BO(ret)
return unary->op == op_spork ? t_shred : t_fork;
return unary->op == insert_symbol("spork") ? t_shred : t_fork;
} else
ERR_O(exp_self(unary)->pos, _("only function calls can be sporked..."))
return NULL;
Expand All @@ -284,24 +284,24 @@ static FREEARG(freearg_dottmpl) {
GWION_IMPORT(func) {
CHECK_BB(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, "@function", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_func_call))
CHECK_BB(gwi_oper_end(gwi, op_chuck, NULL))
CHECK_BB(gwi_oper_end(gwi, "=>", NULL))
CHECK_BB(gwi_oper_ini(gwi, "@function", "@func_ptr", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_fptr_at))
CHECK_BB(gwi_oper_emi(gwi, opem_func_assign))
CHECK_BB(gwi_oper_end(gwi, op_ref, NULL /*FuncAssign*/))
CHECK_BB(gwi_oper_end(gwi, "@=>", NULL /*FuncAssign*/))
CHECK_BB(gwi_oper_add(gwi, opck_fptr_cast))
CHECK_BB(gwi_oper_emi(gwi, opem_fptr_cast))
CHECK_BB(gwi_oper_end(gwi, op_cast, NULL))
CHECK_BB(gwi_oper_end(gwi, "$", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_fptr_impl))
CHECK_BB(gwi_oper_emi(gwi, opem_fptr_impl))
CHECK_BB(gwi_oper_end(gwi, op_impl, NULL))
CHECK_BB(gwi_oper_end(gwi, "@implicit", NULL))
CHECK_BB(gwi_oper_ini(gwi, NULL, (m_str)OP_ANY_TYPE, NULL))
CHECK_BB(gwi_oper_add(gwi, opck_spork))
CHECK_BB(gwi_oper_emi(gwi, opem_spork))
CHECK_BB(gwi_oper_end(gwi, op_spork, NULL))
CHECK_BB(gwi_oper_end(gwi, "spork", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_spork))
CHECK_BB(gwi_oper_emi(gwi, opem_spork))
CHECK_BB(gwi_oper_end(gwi, op_fork, NULL))
CHECK_BB(gwi_oper_end(gwi, "fork", NULL))
register_freearg(gwi, SporkIni, freearg_xork);
register_freearg(gwi, ForkIni, freearg_xork);
register_freearg(gwi, DotTmpl, freearg_dottmpl);
Expand Down
5 changes: 2 additions & 3 deletions src/lib/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ ANN2(1,2) static int import_op(const Gwi gwi, const DL_Oper* op,

ANN2(1) m_int gwi_oper_ini(const Gwi gwi, const restrict m_str l,
const restrict m_str r, const restrict m_str t) {
gwi->oper.mut = 0;
gwi->oper.ret = t;
gwi->oper.rhs = r;
gwi->oper.lhs = l;
Expand All @@ -506,8 +505,8 @@ ANN m_int gwi_oper_emi(const Gwi gwi, m_bool (*em)(Emitter, void*)) {
return GW_OK;
}

ANN m_int gwi_oper_end(const Gwi gwi, const Operator op, const f_instr f) {
gwi->oper.op = op;
ANN m_int gwi_oper_end(const Gwi gwi, const m_str op, const f_instr f) {
gwi->oper.op = insert_symbol(gwi->gwion->st, op);
const m_bool ret = import_op(gwi, &gwi->oper, f);
gwi->oper.ck = NULL;
gwi->oper.em = NULL;
Expand Down
24 changes: 12 additions & 12 deletions src/lib/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,30 +154,30 @@ GWION_IMPORT(object) {
CHECK_BB(gwi_class_end(gwi))
CHECK_BB(gwi_oper_ini(gwi, "@null", "Object", "Object"))
CHECK_BB(gwi_oper_add(gwi, at_object))
CHECK_BB(gwi_oper_end(gwi, op_ref, ObjectAssign))
CHECK_BB(gwi_oper_end(gwi, "@=>", ObjectAssign))
CHECK_BB(gwi_oper_ini(gwi, "Object", "Object", NULL))
CHECK_BB(gwi_oper_add(gwi, at_object))
CHECK_BB(gwi_oper_end(gwi, op_ref, ObjectAssign))
CHECK_BB(gwi_oper_end(gwi, "@=>", ObjectAssign))
CHECK_BB(gwi_oper_ini(gwi, "Object", "Object", "int"))
CHECK_BB(gwi_oper_end(gwi, op_eq, EqObject))
CHECK_BB(gwi_oper_end(gwi, op_ne, NeqObject))
CHECK_BB(gwi_oper_end(gwi, "==", EqObject))
CHECK_BB(gwi_oper_end(gwi, "!=", NeqObject))
CHECK_BB(gwi_oper_add(gwi, opck_object_cast))
CHECK_BB(gwi_oper_emi(gwi, opem_basic_cast))
CHECK_BB(gwi_oper_end(gwi, op_cast, NULL))
CHECK_BB(gwi_oper_end(gwi, "$", NULL))
CHECK_BB(gwi_oper_ini(gwi, "@null", "Object", "int"))
CHECK_BB(gwi_oper_end(gwi, op_eq, EqObject))
CHECK_BB(gwi_oper_end(gwi, op_ne, NeqObject))
CHECK_BB(gwi_oper_end(gwi, "==", EqObject))
CHECK_BB(gwi_oper_end(gwi, "!=", NeqObject))
CHECK_BB(gwi_oper_add(gwi, opck_basic_cast))
CHECK_BB(gwi_oper_emi(gwi, opem_basic_cast))
CHECK_BB(gwi_oper_end(gwi, op_cast, NULL))
CHECK_BB(gwi_oper_end(gwi, "$", NULL))
CHECK_BB(gwi_oper_add(gwi, opck_implicit_null2obj))
CHECK_BB(gwi_oper_end(gwi, op_impl, NULL))
CHECK_BB(gwi_oper_end(gwi, "@implicit", NULL))
CHECK_BB(gwi_oper_ini(gwi, "Object", "@null", "int"))
CHECK_BB(gwi_oper_end(gwi, op_eq, EqObject))
CHECK_BB(gwi_oper_end(gwi, op_ne, NeqObject))
CHECK_BB(gwi_oper_end(gwi, "==", EqObject))
CHECK_BB(gwi_oper_end(gwi, "!=", NeqObject))
CHECK_BB(gwi_oper_ini(gwi, NULL, "Object", "int"))
CHECK_BB(gwi_oper_add(gwi, opck_unary_meta2))
CHECK_BB(gwi_oper_end(gwi, op_not, IntNot))
CHECK_BB(gwi_oper_end(gwi, "!", IntNot))
gwi_item_ini(gwi, "@null", "null");
gwi_item_end(gwi, 0, NULL);
gwi_reserve(gwi, "this");
Expand Down
8 changes: 4 additions & 4 deletions src/lib/opfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ OP_CHECK(opck_const_rhs) {
if(bin->rhs->meta != ae_meta_var)
ERR_N(bin->rhs->pos, _("cannot assign '%s' on types '%s' and '%s'.\n"
" ... (reason: --- right-side operand is %s.)"),
op2str(bin->op), bin->lhs->type->name, bin->rhs->type->name,
s_name(bin->op), bin->lhs->type->name, bin->rhs->type->name,
access(bin->rhs->meta))
return bin->rhs->type;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ OP_CHECK(opck_unary_meta2_uniq) {
CHECK_OO(opck_unary_meta2(env, data, mut))
if(unary->exp->next)
ERR_N(exp_self(unary)->pos,
_("'%s' must be applied to a unique expression"), op2str(unary->op))
_("'%s' must be applied to a unique expression"), s_name(unary->op))
return t_int;
}

Expand All @@ -76,7 +76,7 @@ OP_CHECK(opck_unary) {
if(unary->exp->meta != ae_meta_var)
ERR_N(unary->exp->pos,
_("unary operator '%s' cannot be used on %s data-types."),
op2str(unary->op), access(unary->exp->meta))
s_name(unary->op), access(unary->exp->meta))
unary->exp->emit_var = 1;
exp_self(unary)->meta = ae_meta_value;
return unary->exp->type;
Expand All @@ -86,7 +86,7 @@ OP_CHECK(opck_post) {
const Exp_Postfix* post = (Exp_Postfix*)data;
if(post->exp->meta != ae_meta_var)
ERR_N(post->exp->pos, _("post operator '%s' cannot be used on %s data-type."),
op2str(post->op), access(post->exp->meta))
s_name(post->op), access(post->exp->meta))
post->exp->emit_var = 1;
exp_self(post)->meta = ae_meta_value;
return post->exp->type;
Expand Down

0 comments on commit 8fb8790

Please sign in to comment.