Skip to content

Commit

Permalink
refactor(melang): refactor operator !
Browse files Browse the repository at this point in the history
  • Loading branch information
Water-Melon committed Nov 7, 2023
1 parent 2fba6eb commit d0d520c
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 39 deletions.
7 changes: 4 additions & 3 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3785,9 +3785,10 @@ Their definitions can be found in melon/include/mln_types.h.
<7> >> <<
<8> + -
<9> * / %
<10> ++ -- (suffix)
<11> [] .
<12> - ~ ! & ++ -- $ ()
<10> !
<11> ++ -- (suffix)
<12> [] .
<13> - ~ & ++ -- $ ()

They are not supported by all data types.
If encountered unsupported operatior, error message would be logged.
Expand Down
2 changes: 2 additions & 0 deletions include/mln_lang.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ typedef enum {
M_LSNT_MOVE,
M_LSNT_ADDSUB,
M_LSNT_MULDIV,
M_LSNT_NOT,
M_LSNT_SUFFIX,
M_LSNT_LOCATE,
M_LSNT_SPEC,
Expand Down Expand Up @@ -167,6 +168,7 @@ struct mln_lang_stack_node_s {
mln_lang_move_t *move;
mln_lang_addsub_t *addsub;
mln_lang_muldiv_t *muldiv;
mln_lang_not_t *not;
mln_lang_suffix_t *suffix;
mln_lang_locate_t *locate;
mln_lang_spec_t *spec;
Expand Down
21 changes: 19 additions & 2 deletions include/mln_lang_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ typedef struct mln_lang_addsub_s mln_lang_addsub_t;
typedef struct mln_lang_addsub_tmp_s mln_lang_addsub_tmp_t;
typedef struct mln_lang_muldiv_s mln_lang_muldiv_t;
typedef struct mln_lang_muldiv_tmp_s mln_lang_muldiv_tmp_t;
typedef struct mln_lang_not_s mln_lang_not_t;
typedef struct mln_lang_suffix_s mln_lang_suffix_t;
typedef struct mln_lang_suffix_tmp_s mln_lang_suffix_tmp_t;
typedef struct mln_lang_locate_s mln_lang_locate_t;
Expand Down Expand Up @@ -344,7 +345,7 @@ typedef enum mln_lang_muldiv_op_e {
struct mln_lang_muldiv_s {
mln_string_t *file;
mln_u64_t line;
mln_lang_suffix_t *left;
mln_lang_not_t *left;
mln_lang_muldiv_op_t op;
mln_lang_muldiv_t *right;
void *jump;
Expand All @@ -356,6 +357,23 @@ struct mln_lang_muldiv_tmp_s {
mln_lang_muldiv_t *muldiv;
};

typedef enum mln_lang_not_op_e {
M_NOT_NONE = 0,
M_NOT_NOT
} mln_lang_not_op_t;

struct mln_lang_not_s {
mln_string_t *file;
mln_u64_t line;
mln_lang_not_op_t op;
union {
mln_lang_not_t *not;
mln_lang_suffix_t *suffix;
} right;
void *jump;
int type;
};

typedef enum mln_lang_suffix_op_e {
M_SUFFIX_NONE = 0,
M_SUFFIX_INC,
Expand Down Expand Up @@ -408,7 +426,6 @@ struct mln_lang_locate_tmp_s {
typedef enum mln_lang_spec_op_e {
M_SPEC_NEGATIVE = 0,
M_SPEC_REVERSE,
M_SPEC_NOT,
M_SPEC_REFER,
M_SPEC_INC,
M_SPEC_DEC,
Expand Down
16 changes: 16 additions & 0 deletions src/mln_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ void mln_alloc_destroy(mln_alloc_t *pool)

void *mln_alloc_m(mln_alloc_t *pool, mln_size_t size)
{
#ifdef __DEBUG__
return malloc(size);
#else
mln_alloc_blk_t *blk;
mln_alloc_mgr_t *am;
mln_alloc_chunk_t *ch;
Expand Down Expand Up @@ -325,6 +328,7 @@ void *mln_alloc_m(mln_alloc_t *pool, mln_size_t size)
blk->in_used = 1;
++(blk->chunk->refer);
return blk->data;
#endif
}

static inline mln_alloc_mgr_t *
Expand Down Expand Up @@ -356,14 +360,21 @@ mln_alloc_get_mgr_by_size(mln_alloc_mgr_t *tbl, mln_size_t size)

void *mln_alloc_c(mln_alloc_t *pool, mln_size_t size)
{
#ifdef __DEBUG__
return calloc(1, size);
#else
mln_u8ptr_t ptr = mln_alloc_m(pool, size);
if (ptr == NULL) return NULL;
memset(ptr, 0, size);
return ptr;
#endif
}

void *mln_alloc_re(mln_alloc_t *pool, void *ptr, mln_size_t size)
{
#ifdef __DEBUG__
return realloc(ptr, size);
#else
if (size == 0) {
mln_alloc_free(ptr);
return NULL;
Expand All @@ -380,13 +391,17 @@ void *mln_alloc_re(mln_alloc_t *pool, void *ptr, mln_size_t size)
mln_alloc_free(ptr);

return new_ptr;
#endif
}

void mln_alloc_free(void *ptr)
{
if (ptr == NULL) {
return;
}
#ifdef __DEBUG__
return free(ptr);
#else

mln_alloc_t *pool;
mln_alloc_chunk_t *ch;
Expand Down Expand Up @@ -442,6 +457,7 @@ void mln_alloc_free(void *ptr)
} else
free(ch);
}
#endif
}

static inline void *mln_alloc_shm_m(mln_alloc_t *pool, mln_size_t size)
Expand Down
123 changes: 111 additions & 12 deletions src/mln_lang.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ static void mln_lang_stack_handler_relativehigh(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_move(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_addsub(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_muldiv(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_not(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_suffix(mln_lang_ctx_t *ctx);
static void mln_lang_stack_handler_locate(mln_lang_ctx_t *ctx);
static inline int mln_lang_stack_handler_funccall_run(mln_lang_ctx_t *ctx, \
Expand Down Expand Up @@ -376,6 +377,10 @@ static void mln_lang_ctx_pipe_elem_destroy(mln_lang_ctx_pipe_elem_t *pe);
n->data.muldiv = (mln_lang_muldiv_t *)(_data);\
n->pos = n->data.muldiv;\
break;\
case M_LSNT_NOT:\
n->data.not = (mln_lang_not_t *)(_data);\
n->pos = n->data.not;\
break;\
case M_LSNT_SUFFIX:\
n->data.suffix = (mln_lang_suffix_t *)(_data);\
break;\
Expand Down Expand Up @@ -518,6 +523,7 @@ mln_lang_stack_handler mln_lang_stack_map[] = {
mln_lang_stack_handler_move,
mln_lang_stack_handler_addsub,
mln_lang_stack_handler_muldiv,
mln_lang_stack_handler_not,
mln_lang_stack_handler_suffix,
mln_lang_stack_handler_locate,
mln_lang_stack_handler_spec,
Expand Down Expand Up @@ -1992,15 +1998,19 @@ mln_lang_funcdef_args_get(mln_lang_ctx_t *ctx, mln_lang_exp_t *exp, mln_array_t
spec = spec->data.spec;
type = M_LANG_VAR_REFER;
}
if (spec->op != M_SPEC_FACTOR) return -1;
if (spec->op != M_SPEC_FACTOR) {
return -1;
}

factor = spec->data.factor;
} else if (scan->type == M_LSNT_FACTOR) {
factor = (mln_lang_factor_t *)(scan->jump);
} else {
return -1;
}
if (factor->type != M_FACTOR_ID) return -1;
if (factor->type != M_FACTOR_ID) {
return -1;
}

if ((v = (mln_lang_var_t **)mln_array_push(arr)) == NULL) {
return -1;
Expand Down Expand Up @@ -2999,6 +3009,10 @@ static void __mln_lang_errmsg(mln_lang_ctx_t *ctx, char *msg)
filename = node->data.muldiv->file;
line = node->data.muldiv->line;
break;
case M_LSNT_NOT:
filename = node->data.not->file;
line = node->data.not->line;
break;
case M_LSNT_SUFFIX:
filename = node->data.suffix->file;
line = node->data.suffix->line;
Expand Down Expand Up @@ -4007,14 +4021,34 @@ static inline void mln_lang_generate_jump_ptr(void *ptr, mln_lang_stack_node_typ
case M_LSNT_MULDIV:
{
mln_lang_muldiv_t *m = (mln_lang_muldiv_t *)ptr;
if (m->left->op != M_SUFFIX_NONE) {
m->jump = m->left;
m->type = M_LSNT_SUFFIX;
} else {
if (m->left->op == M_NOT_NONE) {
if (m->left->jump == NULL)
mln_lang_generate_jump_ptr(m->left, M_LSNT_SUFFIX);
mln_lang_generate_jump_ptr(m->left, M_LSNT_NOT);
m->jump = m->left->jump;
m->type = m->left->type;
} else {
m->jump = m->left;
m->type = M_LSNT_NOT;
}
break;
}
case M_LSNT_NOT:
{
mln_lang_not_t *n = (mln_lang_not_t *)ptr;
if (n->op != M_NOT_NONE) {
ASSERT(n->op == M_NOT_NOT);
n->jump = n->right.not;
n->type = M_LSNT_NOT;
} else {
if (n->right.suffix->op != M_SUFFIX_NONE) {
n->jump = n->right.suffix;
n->type = M_LSNT_SUFFIX;
} else {
if (n->right.suffix->jump == NULL)
mln_lang_generate_jump_ptr(n->right.suffix, M_LSNT_SUFFIX);
n->jump = n->right.suffix->jump;
n->type = n->right.suffix->type;
}
}
break;
}
Expand Down Expand Up @@ -4863,7 +4897,7 @@ static void mln_lang_stack_handler_muldiv(mln_lang_ctx_t *ctx)
goon2:
node->step = 3;
tmp = muldiv->right;
if ((node = mln_lang_stack_push(ctx, M_LSNT_SUFFIX, tmp->left)) == NULL) {
if ((node = mln_lang_stack_push(ctx, M_LSNT_NOT, tmp->left)) == NULL) {
__mln_lang_errmsg(ctx, "Stack is full.");
ctx->quit = 1;
return;
Expand Down Expand Up @@ -4939,6 +4973,75 @@ static void mln_lang_stack_handler_muldiv(mln_lang_ctx_t *ctx)
}
}

static void mln_lang_stack_handler_not(mln_lang_ctx_t *ctx)
{
mln_lang_var_t *res = NULL;
mln_lang_stack_node_t *node = mln_lang_stack_top(ctx);
mln_lang_not_t *not = node->data.not;

if (node->step == 0) {
mln_lang_stack_node_reset_ret_val(node);
mln_lang_ctx_reset_ret_var(ctx);
node->step = not->op == M_NOT_NONE? M_LANG_STEP_OUT: 1;
if (not->jump == NULL)
mln_lang_generate_jump_ptr(not, M_LSNT_NOT);
if ((node = mln_lang_stack_push(ctx, (mln_lang_stack_node_type_t)(not->type), not->jump)) == NULL) {
__mln_lang_errmsg(ctx, "Stack is full.");
ctx->quit = 1;
return;
}
return mln_lang_stack_map[node->type](ctx);
} else if (node->step == 1) {
node->step = 2;
mln_lang_op handler = NULL;
mln_lang_method_t *method;
method = mln_lang_methods[mln_lang_var_val_type_get(ctx->ret_var)];
if (method == NULL) {
__mln_lang_errmsg(ctx, "Operation NOT support.");
ctx->quit = 1;
return;
}
switch (not->op) {
case M_NOT_NOT:
handler = method->not_handler;
break;
default:
break;
}
if (handler != NULL) {
if (handler(ctx, &res, ctx->ret_var, NULL) < 0) {
ctx->quit = 1;
return;
}
__mln_lang_ctx_set_ret_var(ctx, res);
if (res->val->type == M_LANG_VAL_TYPE_CALL) {
node->call = 1;
again:
if (mln_lang_stack_handler_funccall_run(ctx, node, res->val->data.call) < 0) {
ctx->quit = 1;
return;
}
} else {
goto out;
}
}
} else if (node->step == 2) {
if (node->call) {
if (mln_lang_withdraw_until_func(ctx) < 0) {
ctx->quit = 1;
return;
}
res = ctx->ret_var;
if (ctx->ret_var->val->type == M_LANG_VAL_TYPE_CALL) goto again;
}
goto out;
} else {
out:
mln_lang_stack_node_free(mln_lang_stack_pop(ctx));
mln_lang_stack_popuntil(ctx);
}
}

static void mln_lang_stack_handler_suffix(mln_lang_ctx_t *ctx)
{
mln_lang_var_t *res = NULL;
Expand Down Expand Up @@ -5526,7 +5629,6 @@ static void mln_lang_stack_handler_spec(mln_lang_ctx_t *ctx)
switch (spec->op) {
case M_SPEC_NEGATIVE:
case M_SPEC_REVERSE:
case M_SPEC_NOT:
case M_SPEC_REFER:
case M_SPEC_INC:
case M_SPEC_DEC:
Expand Down Expand Up @@ -5600,9 +5702,6 @@ static void mln_lang_stack_handler_spec(mln_lang_ctx_t *ctx)
case M_SPEC_REVERSE:
handler = method->reverse_handler;
break;
case M_SPEC_NOT:
handler = method->not_handler;
break;
case M_SPEC_INC:
handler = method->pinc_handler;
break;
Expand Down
Loading

0 comments on commit d0d520c

Please sign in to comment.