diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-11-19 19:11:52 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-11-19 19:51:49 +0100 |
commit | a162cf7aadacd08eb11af072bcb9dd041866b579 (patch) | |
tree | e0a829f63600c869268d61d6fecd1663bd65c5ea /eval.c | |
parent | ff441aa908ad48ab5d197f4efd7d6e879fe185a2 (diff) |
treewide: rebrand to ucode
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'eval.c')
-rw-r--r-- | eval.c | 666 |
1 files changed, 333 insertions, 333 deletions
@@ -27,9 +27,9 @@ #include <regex.h> bool -ut_val_is_truish(struct json_object *val) +uc_val_is_truish(struct json_object *val) { - struct ut_op *tag = json_object_get_userdata(val); + struct uc_op *tag = json_object_get_userdata(val); double d; switch (tag ? tag->type : 0) { @@ -63,7 +63,7 @@ ut_val_is_truish(struct json_object *val) } enum json_type -ut_cast_number(struct json_object *v, int64_t *n, double *d) +uc_cast_number(struct json_object *v, int64_t *n, double *d) { bool is_double = false; const char *s; @@ -134,25 +134,25 @@ ut_cast_number(struct json_object *v, int64_t *n, double *d) } static struct json_object * -ut_execute_op(struct ut_state *state, uint32_t off); +uc_execute_op(struct uc_state *state, uint32_t off); static struct json_object * -ut_execute_op_sequence(struct ut_state *state, uint32_t off); +uc_execute_op_sequence(struct uc_state *state, uint32_t off); static struct json_object * -ut_execute_list(struct ut_state *state, uint32_t off); +uc_execute_list(struct uc_state *state, uint32_t off); static char * -ut_ref_to_str(struct ut_state *state, uint32_t off) +uc_ref_to_str(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); - struct ut_op *op2 = ut_get_child(state, off, 1); + struct uc_op *op = uc_get_op(state, off); + struct uc_op *op2 = uc_get_child(state, off, 1); const char *l; char *s, *p; switch (op ? op->type : 0) { case T_DOT: - s = ut_ref_to_str(state, op->tree.operand[0]); + s = uc_ref_to_str(state, op->tree.operand[0]); l = ((op2 ? op2->type : 0) == T_LABEL) ? json_object_get_string(op2->val) : "???"; if (asprintf(&p, "%s.%s", s ? s : "(...)", l) == -1) @@ -169,7 +169,7 @@ ut_ref_to_str(struct ut_state *state, uint32_t off) /* fall through */ case T_LPAREN: - s = ut_ref_to_str(state, op->tree.operand[0]); + s = uc_ref_to_str(state, op->tree.operand[0]); switch (op2 ? op2->type : 0) { case T_STRING: @@ -204,12 +204,12 @@ ut_ref_to_str(struct ut_state *state, uint32_t off) } static struct json_object * -ut_getref(struct ut_state *state, uint32_t off, struct json_object **key) +uc_getref(struct uc_state *state, uint32_t off, struct json_object **key) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); uint32_t off1 = op ? op->tree.operand[0] : 0; uint32_t off2 = op ? op->tree.operand[1] : 0; - struct ut_scope *sc, *next; + struct uc_scope *sc, *next; struct json_object *val; if (key) @@ -217,21 +217,21 @@ ut_getref(struct ut_state *state, uint32_t off, struct json_object **key) if (op && op->type == T_DOT) { if (key) - *key = off2 ? json_object_get(ut_get_op(state, off2)->val) : NULL; + *key = off2 ? json_object_get(uc_get_op(state, off2)->val) : NULL; - return ut_execute_op_sequence(state, off1); + return uc_execute_op_sequence(state, off1); } else if (op && op->type == T_LBRACK && op->is_postfix) { if (key) { - val = off2 ? ut_execute_op_sequence(state, off2) : NULL; + val = off2 ? uc_execute_op_sequence(state, off2) : NULL; - if (ut_is_type(val, T_EXCEPTION)) + if (uc_is_type(val, T_EXCEPTION)) return val; *key = val; } - return ut_execute_op_sequence(state, off1); + return uc_execute_op_sequence(state, off1); } else if (op && op->type == T_LABEL) { sc = state->scope; @@ -240,11 +240,11 @@ ut_getref(struct ut_state *state, uint32_t off, struct json_object **key) if (json_object_object_get_ex(sc->scope, json_object_get_string(op->val), NULL)) break; - next = ut_parent_scope(sc); + next = uc_parent_scope(sc); if (!next) { if (state->strict_declarations) { - return ut_new_exception(state, op->off, + return uc_new_exception(state, op->off, "Reference error: access to undeclared variable %s", json_object_get_string(op->val)); } @@ -269,26 +269,26 @@ ut_getref(struct ut_state *state, uint32_t off, struct json_object **key) } static struct json_object * -ut_getref_required(struct ut_state *state, uint32_t off, struct json_object **key) +uc_getref_required(struct uc_state *state, uint32_t off, struct json_object **key) { - struct ut_op *op1 = ut_get_child(state, off, 0); + struct uc_op *op1 = uc_get_child(state, off, 0); struct json_object *scope, *skey, *rv; char *lhs; - scope = ut_getref(state, off, &skey); + scope = uc_getref(state, off, &skey); if (!json_object_is_type(scope, json_type_array) && !json_object_is_type(scope, json_type_object)) { - if (!ut_is_type(scope, T_EXCEPTION)) { - lhs = op1 ? ut_ref_to_str(state, ut_get_off(state, op1)) : NULL; + if (!uc_is_type(scope, T_EXCEPTION)) { + lhs = op1 ? uc_ref_to_str(state, uc_get_off(state, op1)) : NULL; if (lhs) { - rv = ut_new_exception(state, op1->off, "Type error: `%s` is %s", + rv = uc_new_exception(state, op1->off, "Type error: `%s` is %s", lhs, scope ? "not an array or object" : "null"); free(lhs); } else { - rv = ut_new_exception(state, op1->off, "Type error: left-hand side is not an array or object"); + rv = uc_new_exception(state, op1->off, "Type error: left-hand side is not an array or object"); } json_object_put(scope); @@ -308,9 +308,9 @@ ut_getref_required(struct ut_state *state, uint32_t off, struct json_object **ke } static struct json_object * -ut_getproto(struct json_object *obj) +uc_getproto(struct json_object *obj) { - struct ut_op *op = json_object_get_userdata(obj); + struct uc_op *op = json_object_get_userdata(obj); if (!op || (op->type != T_LBRACE && op->type <= __T_MAX) || !op->val) return NULL; @@ -319,7 +319,7 @@ ut_getproto(struct json_object *obj) } static struct json_object * -ut_getval(struct json_object *scope, struct json_object *key) +uc_getval(struct json_object *scope, struct json_object *key) { struct json_object *o, *v; int64_t idx; @@ -349,7 +349,7 @@ ut_getval(struct json_object *scope, struct json_object *key) return json_object_get(json_object_array_get_idx(scope, idx)); } - for (o = scope; o; o = ut_getproto(o)) { + for (o = scope; o; o = uc_getproto(o)) { if (!json_object_is_type(o, json_type_object)) continue; @@ -361,7 +361,7 @@ ut_getval(struct json_object *scope, struct json_object *key) } static struct json_object * -ut_setval(struct json_object *scope, struct json_object *key, struct json_object *val) +uc_setval(struct json_object *scope, struct json_object *key, struct json_object *val) { int64_t idx; @@ -388,22 +388,22 @@ ut_setval(struct json_object *scope, struct json_object *key, struct json_object } static struct json_object * -ut_execute_assign(struct ut_state *state, uint32_t off) +uc_execute_assign(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); uint32_t label = op ? op->tree.operand[0] : 0; uint32_t value = op ? op->tree.operand[1] : 0; struct json_object *scope, *key, *val; - scope = ut_getref_required(state, label, &key); + scope = uc_getref_required(state, label, &key); if (!key) return scope; - val = ut_execute_op_sequence(state, value); + val = uc_execute_op_sequence(state, value); - if (!ut_is_type(val, T_EXCEPTION)) - ut_setval(scope, key, val); + if (!uc_is_type(val, T_EXCEPTION)) + uc_setval(scope, key, val); json_object_put(scope); json_object_put(key); @@ -412,22 +412,22 @@ ut_execute_assign(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_local(struct ut_state *state, uint32_t off) +uc_execute_local(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off), *asop, *alop; + struct uc_op *op = uc_get_op(state, off), *asop, *alop; uint32_t as = op ? op->tree.operand[0] : 0; struct json_object *val, *rv = NULL; while (as) { - asop = ut_get_op(state, as); + asop = uc_get_op(state, as); as = asop ? asop->tree.next : 0; switch (asop ? asop->type : 0) { case T_ASSIGN: - alop = ut_get_op(state, asop->tree.operand[0]); - val = ut_execute_op_sequence(state, asop->tree.operand[1]); + alop = uc_get_op(state, asop->tree.operand[0]); + val = uc_execute_op_sequence(state, asop->tree.operand[1]); - if (ut_is_type(val, T_EXCEPTION)) + if (uc_is_type(val, T_EXCEPTION)) return val; break; @@ -443,7 +443,7 @@ ut_execute_local(struct ut_state *state, uint32_t off) if (alop) { json_object_put(rv); - rv = ut_setval(state->scope->scope, alop->val, val); + rv = uc_setval(state->scope->scope, alop->val, val); } } @@ -451,13 +451,13 @@ ut_execute_local(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_op_sequence(struct ut_state *state, uint32_t off); +uc_execute_op_sequence(struct uc_state *state, uint32_t off); static bool -ut_test_condition(struct ut_state *state, uint32_t off) +uc_test_condition(struct uc_state *state, uint32_t off) { - struct json_object *val = ut_execute_op_sequence(state, off); - bool istrue = ut_val_is_truish(val); + struct json_object *val = uc_execute_op_sequence(state, off); + bool istrue = uc_val_is_truish(val); json_object_put(val); @@ -465,34 +465,34 @@ ut_test_condition(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_if(struct ut_state *state, uint32_t off) +uc_execute_if(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); uint32_t cond = op ? op->tree.operand[0] : 0; uint32_t Then = op ? op->tree.operand[1] : 0; uint32_t Else = op ? op->tree.operand[2] : 0; - bool res = ut_test_condition(state, cond); + bool res = uc_test_condition(state, cond); if (state->exception) return json_object_get(state->exception); else if (res) - return ut_execute_op_sequence(state, Then); + return uc_execute_op_sequence(state, Then); else if (Else) - return ut_execute_op_sequence(state, Else); + return uc_execute_op_sequence(state, Else); return NULL; } static struct json_object * -ut_execute_for(struct ut_state *state, uint32_t off) +uc_execute_for(struct uc_state *state, uint32_t off) { struct json_object *kscope, *vscope, *val, *item, *ik, *iv = NULL, *rv = NULL; - struct ut_op *loop = ut_get_op(state, off); - struct ut_op *init = ut_get_child(state, off, 0); + struct uc_op *loop = uc_get_op(state, off); + struct uc_op *init = uc_get_child(state, off, 0); uint32_t test = loop ? loop->tree.operand[1] : 0; uint32_t incr = loop ? loop->tree.operand[2] : 0; uint32_t body = loop ? loop->tree.operand[3] : 0; - struct ut_op *ikvar, *ivvar, *tag; + struct uc_op *ikvar, *ivvar, *tag; size_t arridx, arrlen; bool local = false; @@ -500,28 +500,28 @@ ut_execute_for(struct ut_state *state, uint32_t off) if (loop->is_for_in) { if (init->type == T_LOCAL) { local = true; - init = ut_get_op(state, init->tree.operand[0]); + init = uc_get_op(state, init->tree.operand[0]); } - ikvar = ut_get_op(state, init->tree.operand[0]); + ikvar = uc_get_op(state, init->tree.operand[0]); ik = ikvar->val; - kscope = local ? state->scope->scope : ut_getref(state, ut_get_off(state, ikvar), NULL); + kscope = local ? state->scope->scope : uc_getref(state, uc_get_off(state, ikvar), NULL); - if (ut_is_type(kscope, T_EXCEPTION)) + if (uc_is_type(kscope, T_EXCEPTION)) return kscope; if (ikvar->tree.next) { - ivvar = ut_get_op(state, ikvar->tree.next); + ivvar = uc_get_op(state, ikvar->tree.next); iv = ivvar->val; - vscope = local ? kscope : ut_getref(state, ut_get_off(state, ivvar), NULL); + vscope = local ? kscope : uc_getref(state, uc_get_off(state, ivvar), NULL); - if (ut_is_type(vscope, T_EXCEPTION)) + if (uc_is_type(vscope, T_EXCEPTION)) return vscope; } - val = ut_execute_op_sequence(state, init->tree.operand[1]); + val = uc_execute_op_sequence(state, init->tree.operand[1]); - if (ut_is_type(val, T_EXCEPTION)) + if (uc_is_type(val, T_EXCEPTION)) return val; if (json_object_is_type(val, json_type_array)) { @@ -530,16 +530,16 @@ ut_execute_for(struct ut_state *state, uint32_t off) item = json_object_array_get_idx(val, arridx); if (iv) { - ut_setval(kscope, ik, xjs_new_int64(arridx)); - ut_setval(vscope, iv, item); + uc_setval(kscope, ik, xjs_new_int64(arridx)); + uc_setval(vscope, iv, item); } else { - ut_setval(kscope, ik, item); + uc_setval(kscope, ik, item); } json_object_put(rv); - rv = ut_execute_op_sequence(state, body); + rv = uc_execute_op_sequence(state, body); tag = json_object_get_userdata(rv); switch (tag ? tag->type : 0) { @@ -559,14 +559,14 @@ ut_execute_for(struct ut_state *state, uint32_t off) } else if (json_object_is_type(val, json_type_object)) { json_object_object_foreach(val, key, item) { - json_object_put(ut_setval(kscope, ik, xjs_new_string(key))); + json_object_put(uc_setval(kscope, ik, xjs_new_string(key))); if (iv) - ut_setval(vscope, iv, item); + uc_setval(vscope, iv, item); json_object_put(rv); - rv = ut_execute_op_sequence(state, body); + rv = uc_execute_op_sequence(state, body); tag = json_object_get_userdata(rv); switch (tag ? tag->type : 0) { @@ -592,18 +592,18 @@ ut_execute_for(struct ut_state *state, uint32_t off) } if (init) { - val = ut_execute_op_sequence(state, ut_get_off(state, init)); + val = uc_execute_op_sequence(state, uc_get_off(state, init)); - if (ut_is_type(val, T_EXCEPTION)) + if (uc_is_type(val, T_EXCEPTION)) return val; json_object_put(val); } - while (test ? ut_test_condition(state, test) : true) { + while (test ? uc_test_condition(state, test) : true) { json_object_put(rv); - rv = ut_execute_op_sequence(state, body); + rv = uc_execute_op_sequence(state, body); tag = json_object_get_userdata(rv); switch (tag ? tag->type : 0) { @@ -618,9 +618,9 @@ ut_execute_for(struct ut_state *state, uint32_t off) } if (incr) { - val = ut_execute_op_sequence(state, incr); + val = uc_execute_op_sequence(state, incr); - if (ut_is_type(val, T_EXCEPTION)) { + if (uc_is_type(val, T_EXCEPTION)) { json_object_put(rv); return val; @@ -636,22 +636,22 @@ ut_execute_for(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_while(struct ut_state *state, uint32_t off) +uc_execute_while(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); uint32_t test = op ? op->tree.operand[0] : 0; uint32_t body = op ? op->tree.operand[1] : 0; struct json_object *v, *rv = NULL; - struct ut_op *tag = NULL; + struct uc_op *tag = NULL; bool cond; while (1) { json_object_put(rv); - v = test ? ut_execute_op_sequence(state, test) : NULL; - cond = test ? ut_val_is_truish(v) : true; + v = test ? uc_execute_op_sequence(state, test) : NULL; + cond = test ? uc_val_is_truish(v) : true; - if (ut_is_type(v, T_EXCEPTION)) + if (uc_is_type(v, T_EXCEPTION)) return v; json_object_put(v); @@ -659,7 +659,7 @@ ut_execute_while(struct ut_state *state, uint32_t off) if (!cond) return NULL; - rv = ut_execute_op_sequence(state, body); + rv = uc_execute_op_sequence(state, body); tag = json_object_get_userdata(rv); switch (tag ? tag->type : 0) { @@ -680,21 +680,21 @@ ut_execute_while(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_and_or(struct ut_state *state, uint32_t off) +uc_execute_and_or(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *val = NULL; int i; for (i = 0; i < ARRAY_SIZE(op->tree.operand) && op->tree.operand[i]; i++) { json_object_put(val); - val = ut_execute_op_sequence(state, op->tree.operand[i]); + val = uc_execute_op_sequence(state, op->tree.operand[i]); - if (ut_is_type(val, T_EXCEPTION)) + if (uc_is_type(val, T_EXCEPTION)) break; - if (ut_val_is_truish(val) == (op->type == T_OR)) + if (uc_val_is_truish(val) == (op->type == T_OR)) break; } @@ -702,7 +702,7 @@ ut_execute_and_or(struct ut_state *state, uint32_t off) } bool -ut_cmp(int how, struct json_object *v1, struct json_object *v2) +uc_cmp(int how, struct json_object *v1, struct json_object *v2) { enum json_type t1 = json_object_get_type(v1); enum json_type t2 = json_object_get_type(v2); @@ -718,8 +718,8 @@ ut_cmp(int how, struct json_object *v1, struct json_object *v2) delta = (void *)v1 - (void *)v2; } else { - t1 = ut_cast_number(v1, &n1, &d1); - t2 = ut_cast_number(v2, &n2, &d2); + t1 = uc_cast_number(v1, &n1, &d1); + t2 = uc_cast_number(v2, &n2, &d2); if (t1 == json_type_double || t2 == json_type_double) { d1 = (t1 == json_type_double) ? d1 : (double)n1; @@ -763,26 +763,26 @@ ut_cmp(int how, struct json_object *v1, struct json_object *v2) } static struct json_object * -_ut_get_operands(struct ut_state *state, struct ut_op *op, size_t n, struct json_object **v) +_uc_get_operands(struct uc_state *state, struct uc_op *op, size_t n, struct json_object **v) { struct json_object *ctx = NULL; - struct ut_op *child; + struct uc_op *child; size_t i, j; for (i = 0; i < n; i++) { - child = op ? ut_get_op(state, op->tree.operand[i]) : NULL; + child = op ? uc_get_op(state, op->tree.operand[i]) : NULL; if (child && child->is_list) - v[i] = ut_execute_list(state, ut_get_off(state, child)); + v[i] = uc_execute_list(state, uc_get_off(state, child)); else if (child) - v[i] = ut_execute_op_sequence(state, ut_get_off(state, child)); + v[i] = uc_execute_op_sequence(state, uc_get_off(state, child)); else v[i] = NULL; if (i == 0) ctx = json_object_get(state->ctx); - if (ut_is_type(v[i], T_EXCEPTION)) { + if (uc_is_type(v[i], T_EXCEPTION)) { json_object_put(ctx); for (j = 0; j < i; j++) @@ -798,21 +798,21 @@ _ut_get_operands(struct ut_state *state, struct ut_op *op, size_t n, struct json return NULL; } -#define ut_get_operands(state, op, vals) \ +#define uc_get_operands(state, op, vals) \ do { \ - struct json_object *ex = _ut_get_operands(state, op, ARRAY_SIZE(vals), vals); \ + struct json_object *ex = _uc_get_operands(state, op, ARRAY_SIZE(vals), vals); \ if (ex) return ex; \ } while(0) static struct json_object * -ut_execute_rel(struct ut_state *state, uint32_t off) +uc_execute_rel(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[2], *rv; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); - rv = xjs_new_boolean(ut_cmp(op->type, v[0], v[1])); + rv = xjs_new_boolean(uc_cmp(op->type, v[0], v[1])); json_object_put(v[0]); json_object_put(v[1]); @@ -821,10 +821,10 @@ ut_execute_rel(struct ut_state *state, uint32_t off) } static bool -ut_eq(struct json_object *v1, struct json_object *v2) +uc_eq(struct json_object *v1, struct json_object *v2) { - struct ut_op *tag1 = json_object_get_userdata(v1); - struct ut_op *tag2 = json_object_get_userdata(v2); + struct uc_op *tag1 = json_object_get_userdata(v1); + struct uc_op *tag2 = json_object_get_userdata(v2); enum json_type t1 = json_object_get_type(v1); enum json_type t2 = json_object_get_type(v2); @@ -862,15 +862,15 @@ ut_eq(struct json_object *v1, struct json_object *v2) } static struct json_object * -ut_execute_equality(struct ut_state *state, uint32_t off) +uc_execute_equality(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[2], *rv; bool equal = false; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); - equal = ut_eq(v[0], v[1]); + equal = uc_eq(v[0], v[1]); rv = xjs_new_boolean((op->type == T_EQS) ? equal : !equal); json_object_put(v[0]); @@ -880,22 +880,22 @@ ut_execute_equality(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_in(struct ut_state *state, uint32_t off) +uc_execute_in(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[2], *item; size_t arrlen, arridx; bool found = false; const char *key; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); if (json_object_is_type(v[1], json_type_array)) { for (arridx = 0, arrlen = json_object_array_length(v[1]); arridx < arrlen; arridx++) { item = json_object_array_get_idx(v[1], arridx); - if (ut_cmp(T_EQ, v[0], item)) { + if (uc_cmp(T_EQ, v[0], item)) { found = true; break; } @@ -913,30 +913,30 @@ ut_execute_in(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_inc_dec(struct ut_state *state, uint32_t off) +uc_execute_inc_dec(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *val, *nval, *scope, *key; uint32_t label = op ? op->tree.operand[0] : 0; int64_t n; double d; - scope = ut_getref_required(state, label, &key); + scope = uc_getref_required(state, label, &key); if (!key) return scope; - val = ut_getval(scope, key); + val = uc_getval(scope, key); json_object_put(scope); json_object_put(key); - if (ut_cast_number(val, &n, &d) == json_type_double) - nval = ut_new_double(d + (op->type == T_INC ? 1.0 : -1.0)); + if (uc_cast_number(val, &n, &d) == json_type_double) + nval = uc_new_double(d + (op->type == T_INC ? 1.0 : -1.0)); else nval = xjs_new_int64(n + (op->type == T_INC ? 1 : -1)); - json_object_put(ut_setval(scope, key, nval)); + json_object_put(uc_setval(scope, key, nval)); /* postfix inc/dec, return old val */ if (op->is_postfix) @@ -948,16 +948,16 @@ ut_execute_inc_dec(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_list(struct ut_state *state, uint32_t off) +uc_execute_list(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *ex, *val, *arr = xjs_new_array(); size_t i; while (op) { - val = ut_execute_op(state, ut_get_off(state, op)); + val = uc_execute_op(state, uc_get_off(state, op)); - if (ut_is_type(val, T_EXCEPTION)) { + if (uc_is_type(val, T_EXCEPTION)) { json_object_put(arr); return val; @@ -965,7 +965,7 @@ ut_execute_list(struct ut_state *state, uint32_t off) if (op->is_ellip) { if (!json_object_is_type(val, json_type_array)) { - ex = ut_new_exception(state, op->off, "Type error: (%s) is not iterable", + ex = uc_new_exception(state, op->off, "Type error: (%s) is not iterable", json_object_get_string(val)); json_object_put(arr); @@ -983,24 +983,24 @@ ut_execute_list(struct ut_state *state, uint32_t off) json_object_array_add(arr, val); } - op = ut_get_op(state, op->tree.next); + op = uc_get_op(state, op->tree.next); } return arr; } static struct json_object * -ut_execute_object(struct ut_state *state, uint32_t off) +uc_execute_object(struct uc_state *state, uint32_t off) { - struct json_object *ex, *v, *obj = ut_new_object(NULL); - struct ut_op *key; + struct json_object *ex, *v, *obj = uc_new_object(NULL); + struct uc_op *key; char *istr; size_t i; - for (key = ut_get_child(state, off, 0); key; key = ut_get_op(state, key->tree.next)) { - v = ut_execute_op_sequence(state, key->tree.operand[0]); + for (key = uc_get_child(state, off, 0); key; key = uc_get_op(state, key->tree.next)) { + v = uc_execute_op_sequence(state, key->tree.operand[0]); - if (ut_is_type(v, T_EXCEPTION)) { + if (uc_is_type(v, T_EXCEPTION)) { json_object_put(obj); return v; @@ -1029,7 +1029,7 @@ ut_execute_object(struct ut_state *state, uint32_t off) break; default: - ex = ut_new_exception(state, key->off, "Type error: (%s) is not iterable", + ex = uc_new_exception(state, key->off, "Type error: (%s) is not iterable", json_object_get_string(v)); json_object_put(obj); @@ -1047,25 +1047,25 @@ ut_execute_object(struct ut_state *state, uint32_t off) } struct json_object * -ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, +uc_invoke(struct uc_state *state, uint32_t off, struct json_object *this, struct json_object *func, struct json_object *argvals) { - struct ut_op *op, *tag = json_object_get_userdata(func); + struct uc_op *op, *tag = json_object_get_userdata(func); struct json_object *arr, *rv = NULL; - struct ut_callstack callstack = {}; - struct ut_function *fn, *prev_fn; + struct uc_callstack callstack = {}; + struct uc_function *fn, *prev_fn; size_t arridx, arglen; - struct ut_scope *sc; - ut_c_fn *fptr; + struct uc_scope *sc; + uc_c_fn *fptr; bool rest; if (!tag) return NULL; - op = ut_get_op(state, off); + op = uc_get_op(state, off); if (state->calldepth >= 1000) - return ut_new_exception(state, op->off, "Runtime error: Too much recursion"); + return uc_new_exception(state, op->off, "Runtime error: Too much recursion"); callstack.next = state->callstack; callstack.function = state->function; @@ -1086,16 +1086,16 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, /* is native function */ if (tag->type == T_CFUNC) { - fptr = (ut_c_fn *)fn->cfn; + fptr = (uc_c_fn *)fn->cfn; rv = fptr ? fptr(state, off, argvals) : NULL; } - /* is utpl function */ + /* is ucode function */ else { - callstack.scope = ut_new_scope(state, fn->parent_scope); + callstack.scope = uc_new_scope(state, fn->parent_scope); sc = state->scope; - state->scope = ut_acquire_scope(callstack.scope); + state->scope = uc_acquire_scope(callstack.scope); if (fn->args) { arglen = json_object_array_length(fn->args); @@ -1106,7 +1106,7 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, if (rest && arridx == arglen - 2) { arr = xjs_new_array(); - ut_setval(callstack.scope->scope, + uc_setval(callstack.scope->scope, json_object_array_get_idx(fn->args, arridx), arr); @@ -1116,21 +1116,21 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, break; } - ut_setval(callstack.scope->scope, json_object_array_get_idx(fn->args, arridx), + uc_setval(callstack.scope->scope, json_object_array_get_idx(fn->args, arridx), argvals ? json_object_array_get_idx(argvals, arridx) : NULL); } } - rv = ut_execute_op_sequence(state, fn->entry); + rv = uc_execute_op_sequence(state, fn->entry); tag = json_object_get_userdata(rv); switch (tag ? tag->type : 0) { case T_BREAK: case T_CONTINUE: json_object_put(rv); - rv = ut_new_exception(state, ut_get_off(state, tag), + rv = uc_new_exception(state, uc_get_off(state, tag), "Syntax error: %s statement must be inside loop", - ut_get_tokenname(tag->type)); + uc_get_tokenname(tag->type)); break; case T_RETURN: @@ -1140,11 +1140,11 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, } /* we left the function, pop the function scope... */ - ut_release_scope(state->scope); + uc_release_scope(state->scope); state->scope = sc; /* ... and release it */ - ut_release_scope(callstack.scope); + uc_release_scope(callstack.scope); } @@ -1158,21 +1158,21 @@ ut_invoke(struct ut_state *state, uint32_t off, struct json_object *this, } static struct json_object * -ut_execute_call(struct ut_state *state, uint32_t off) +uc_execute_call(struct uc_state *state, uint32_t off) { - struct ut_op *decl, *op = ut_get_op(state, off); - struct ut_op *op1 = ut_get_child(state, off, 0); + struct uc_op *decl, *op = uc_get_op(state, off); + struct uc_op *op1 = uc_get_child(state, off, 0); struct json_object *v[2], *rv; char *lhs; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); decl = json_object_get_userdata(v[0]); if (!decl || (decl->type != T_FUNC && decl->type != T_CFUNC)) { - lhs = ut_ref_to_str(state, ut_get_off(state, op1)); + lhs = uc_ref_to_str(state, uc_get_off(state, op1)); - rv = ut_new_exception(state, op1->off, "Type error: %s is not a function", + rv = uc_new_exception(state, op1->off, "Type error: %s is not a function", lhs ? lhs : "left-hand side expression"); free(lhs); @@ -1181,7 +1181,7 @@ ut_execute_call(struct ut_state *state, uint32_t off) if (v[1] == NULL) v[1] = xjs_new_array(); - rv = ut_invoke(state, off, NULL, v[0], v[1]); + rv = uc_invoke(state, off, NULL, v[0], v[1]); } json_object_put(v[0]); @@ -1191,7 +1191,7 @@ ut_execute_call(struct ut_state *state, uint32_t off) } static void -ut_write_str(struct json_object *v) +uc_write_str(struct json_object *v) { const char *p; size_t len; @@ -1222,11 +1222,11 @@ ut_write_str(struct json_object *v) } static struct json_object * -ut_execute_exp(struct ut_state *state, uint32_t off) +uc_execute_exp(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); - struct json_object *val = ut_execute_op_sequence(state, op ? op->tree.operand[0] : 0); - struct ut_op *tag = val ? json_object_get_userdata(val) : NULL; + struct uc_op *op = uc_get_op(state, off); + struct json_object *val = uc_execute_op_sequence(state, op ? op->tree.operand[0] : 0); + struct uc_op *tag = val ? json_object_get_userdata(val) : NULL; switch (tag ? tag->type : 0) { case T_EXCEPTION: @@ -1234,7 +1234,7 @@ ut_execute_exp(struct ut_state *state, uint32_t off) break; default: - ut_write_str(val); + uc_write_str(val); break; } @@ -1244,18 +1244,18 @@ ut_execute_exp(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_unary_plus_minus(struct ut_state *state, uint32_t off) +uc_execute_unary_plus_minus(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); - struct ut_op *op1 = ut_get_child(state, off, 0); + struct uc_op *op = uc_get_op(state, off); + struct uc_op *op1 = uc_get_child(state, off, 0); struct json_object *v[1]; enum json_type t; int64_t n; double d; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); - t = ut_cast_number(v[0], &n, &d); + t = uc_cast_number(v[0], &n, &d); json_object_put(v[0]); @@ -1267,14 +1267,14 @@ ut_execute_unary_plus_minus(struct ut_state *state, uint32_t off) return xjs_new_int64((op->type == T_SUB) ? -n : n); default: - return ut_new_double((op->type == T_SUB) ? -d : d); + return uc_new_double((op->type == T_SUB) ? -d : d); } } static struct json_object * -ut_execute_arith(struct ut_state *state, uint32_t off) +uc_execute_arith(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[2], *rv; enum json_type t1, t2; const char *s1, *s2; @@ -1284,9 +1284,9 @@ ut_execute_arith(struct ut_state *state, uint32_t off) char *s; if (!op->tree.operand[1]) - return ut_execute_unary_plus_minus(state, off); + return uc_execute_unary_plus_minus(state, off); - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); if (op->type == T_ADD && (json_object_is_type(v[0], json_type_string) || @@ -1308,8 +1308,8 @@ ut_execute_arith(struct ut_state *state, uint32_t off) return rv; } - t1 = ut_cast_number(v[0], &n1, &d1); - t2 = ut_cast_number(v[1], &n2, &d2); + t1 = uc_cast_number(v[0], &n1, &d1); + t2 = uc_cast_number(v[1], &n2, &d2); json_object_put(v[0]); json_object_put(v[1]); @@ -1320,26 +1320,26 @@ ut_execute_arith(struct ut_state *state, uint32_t off) switch (op->type) { case T_ADD: - return ut_new_double(d1 + d2); + return uc_new_double(d1 + d2); case T_SUB: - return ut_new_double(d1 - d2); + return uc_new_double(d1 - d2); case T_MUL: - return ut_new_double(d1 * d2); + return uc_new_double(d1 * d2); case T_DIV: if (d2 == 0.0) - return ut_new_double(INFINITY); + return uc_new_double(INFINITY); else if (isnan(d2)) - return ut_new_double(NAN); + return uc_new_double(NAN); else if (!isfinite(d2)) - return ut_new_double(isfinite(d1) ? 0.0 : NAN); + return uc_new_double(isfinite(d1) ? 0.0 : NAN); - return ut_new_double(d1 / d2); + return uc_new_double(d1 / d2); case T_MOD: - return ut_new_double(NAN); + return uc_new_double(NAN); } } @@ -1355,7 +1355,7 @@ ut_execute_arith(struct ut_state *state, uint32_t off) case T_DIV: if (n2 == 0) - return ut_new_double(INFINITY); + return uc_new_double(INFINITY); return xjs_new_int64(n1 / n2); @@ -1363,23 +1363,23 @@ ut_execute_arith(struct ut_state *state, uint32_t off) return xjs_new_int64(n1 % n2); } - return ut_new_double(NAN); + return uc_new_double(NAN); } static struct json_object * -ut_execute_bitop(struct ut_state *state, uint32_t off) +uc_execute_bitop(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[2]; int64_t n1, n2; double d; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); - if (ut_cast_number(v[0], &n1, &d) == json_type_double) + if (uc_cast_number(v[0], &n1, &d) == json_type_double) n1 = isnan(d) ? 0 : (int64_t)d; - if (ut_cast_number(v[1], &n2, &d) == json_type_double) + if (uc_cast_number(v[1], &n2, &d) == json_type_double) n2 = isnan(d) ? 0 : (int64_t)d; json_object_put(v[0]); @@ -1407,24 +1407,24 @@ ut_execute_bitop(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_not(struct ut_state *state, uint32_t off) +uc_execute_not(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); - return xjs_new_boolean(!ut_test_condition(state, op ? op->tree.operand[0] : 0)); + return xjs_new_boolean(!uc_test_condition(state, op ? op->tree.operand[0] : 0)); } static struct json_object * -ut_execute_compl(struct ut_state *state, uint32_t off) +uc_execute_compl(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[1]; int64_t n; double d; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); - if (ut_cast_number(v[0], &n, &d) == json_type_double) + if (uc_cast_number(v[0], &n, &d) == json_type_double) n = isnan(d) ? 0 : (int64_t)d; json_object_put(v[0]); @@ -1433,12 +1433,12 @@ ut_execute_compl(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_return(struct ut_state *state, uint32_t off) +uc_execute_return(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *v[1], *rv; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); json_object_put(state->rval); state->rval = v[0]; @@ -1451,9 +1451,9 @@ ut_execute_return(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_break_cont(struct ut_state *state, uint32_t off) +uc_execute_break_cont(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *rv = xjs_new_int64(0); json_object_set_userdata(rv, op, NULL); @@ -1462,40 +1462,40 @@ ut_execute_break_cont(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_function(struct ut_state *state, uint32_t off) +uc_execute_function(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); - struct ut_op *op1 = ut_get_child(state, off, 0); - struct json_object *obj = ut_new_func(state, op, state->scope); + struct uc_op *op = uc_get_op(state, off); + struct uc_op *op1 = uc_get_child(state, off, 0); + struct json_object *obj = uc_new_func(state, op, state->scope); if (op1) - ut_setval(state->scope->scope, op1->val, obj); + uc_setval(state->scope->scope, op1->val, obj); return obj; } static struct json_object * -ut_execute_this(struct ut_state *state, uint32_t off) +uc_execute_this(struct uc_state *state, uint32_t off) { return json_object_get(state->callstack->ctx); } static struct json_object * -ut_execute_try_catch(struct ut_state *state, uint32_t off) +uc_execute_try_catch(struct uc_state *state, uint32_t off) { - struct ut_op *tag, *op = ut_get_op(state, off); + struct uc_op *tag, *op = uc_get_op(state, off); struct json_object *rv; - rv = ut_execute_op_sequence(state, op->tree.operand[0]); + rv = uc_execute_op_sequence(state, op->tree.operand[0]); - if (ut_is_type(rv, T_EXCEPTION)) { + if (uc_is_type(rv, T_EXCEPTION)) { if (op->tree.operand[1]) { /* remove the T_EXCEPTION type from the object to avoid handling * it as a new exception in the catch block */ tag = json_object_get_userdata(rv); tag->type = T_LBRACE; - json_object_put(ut_setval(state->scope->scope, ut_get_child(state, off, 1)->val, + json_object_put(uc_setval(state->scope->scope, uc_get_child(state, off, 1)->val, json_object_get(rv))); } @@ -1503,42 +1503,42 @@ ut_execute_try_catch(struct ut_state *state, uint32_t off) state->exception = NULL; json_object_put(rv); - rv = ut_execute_op_sequence(state, op->tree.operand[2]); + rv = uc_execute_op_sequence(state, op->tree.operand[2]); } return rv; } static bool -ut_match_case(struct ut_state *state, struct json_object *v, struct ut_op *Case) +uc_match_case(struct uc_state *state, struct json_object *v, struct uc_op *Case) { - struct json_object *caseval = ut_execute_op_sequence(state, Case->tree.operand[0]); - bool rv = ut_eq(v, caseval); + struct json_object *caseval = uc_execute_op_sequence(state, Case->tree.operand[0]); + bool rv = uc_eq(v, caseval); json_object_put(caseval); return rv; } static struct json_object * -ut_execute_switch_case(struct ut_state *state, uint32_t off) +uc_execute_switch_case(struct uc_state *state, uint32_t off) { - struct ut_op *Default = NULL, *Case = NULL, *jmp = NULL; - struct ut_op *op = ut_get_op(state, off); + struct uc_op *Default = NULL, *Case = NULL, *jmp = NULL; + struct uc_op *op = uc_get_op(state, off); struct json_object *v[1], *rv = NULL; - ut_get_operands(state, op, v); + uc_get_operands(state, op, v); /* First try to find matching case... */ - for (Case = ut_get_child(state, off, 1); + for (Case = uc_get_child(state, off, 1); Case != NULL; - Case = ut_get_op(state, Case->tree.next)) + Case = uc_get_op(state, Case->tree.next)) { /* remember default case and throw on dupes */ if (Case->type == T_DEFAULT) { if (Default) { json_object_put(v[0]); - return ut_new_exception(state, Case->off, + return uc_new_exception(state, Case->off, "Syntax error: more than one switch default case"); } @@ -1547,7 +1547,7 @@ ut_execute_switch_case(struct ut_state *state, uint32_t off) } /* Found a matching case, remember jump offset */ - if (ut_match_case(state, v[0], Case)) { + if (uc_match_case(state, v[0], Case)) { jmp = Case; break; } @@ -1556,21 +1556,21 @@ ut_execute_switch_case(struct ut_state *state, uint32_t off) /* jump to matching case (or default) and continue until break */ for (Case = jmp ? jmp : Default; Case != NULL; - Case = ut_get_op(state, Case->tree.next)) + Case = uc_get_op(state, Case->tree.next)) { json_object_put(rv); if (Case == Default) - rv = ut_execute_op_sequence(state, Default->tree.operand[0]); + rv = uc_execute_op_sequence(state, Default->tree.operand[0]); else - rv = ut_execute_op_sequence(state, Case->tree.operand[1]); + rv = uc_execute_op_sequence(state, Case->tree.operand[1]); - if (ut_is_type(rv, T_BREAK)) { + if (uc_is_type(rv, T_BREAK)) { json_object_put(rv); rv = NULL; break; } - else if (ut_is_type(rv, T_RETURN) || ut_is_type(rv, T_EXCEPTION) || ut_is_type(rv, T_CONTINUE)) { + else if (uc_is_type(rv, T_RETURN) || uc_is_type(rv, T_EXCEPTION) || uc_is_type(rv, T_CONTINUE)) { break; } } @@ -1581,37 +1581,37 @@ ut_execute_switch_case(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_atom(struct ut_state *state, uint32_t off) +uc_execute_atom(struct uc_state *state, uint32_t off) { - return json_object_get(ut_get_op(state, off)->val); + return json_object_get(uc_get_op(state, off)->val); } static struct json_object * -ut_execute_text(struct ut_state *state, uint32_t off) +uc_execute_text(struct uc_state *state, uint32_t off) { - printf("%s", json_object_get_string(ut_get_op(state, off)->val)); + printf("%s", json_object_get_string(uc_get_op(state, off)->val)); return NULL; } static struct json_object * -ut_execute_label(struct ut_state *state, uint32_t off) +uc_execute_label(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); struct json_object *scope, *key, *val; - scope = ut_getref(state, off, &key); + scope = uc_getref(state, off, &key); json_object_put(state->ctx); state->ctx = NULL; if (state->strict_declarations && scope == NULL) { - return ut_new_exception(state, op->off, + return uc_new_exception(state, op->off, "Reference error: %s is not defined", json_object_get_string(op->val)); } - val = ut_getval(scope, key); + val = uc_getval(scope, key); json_object_put(scope); json_object_put(key); @@ -1619,11 +1619,11 @@ ut_execute_label(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_dot(struct ut_state *state, uint32_t off) +uc_execute_dot(struct uc_state *state, uint32_t off) { struct json_object *scope, *key, *val; - scope = ut_getref_required(state, off, &key); + scope = uc_getref_required(state, off, &key); json_object_put(state->ctx); state->ctx = json_object_get(scope); @@ -1631,7 +1631,7 @@ ut_execute_dot(struct ut_state *state, uint32_t off) if (!key) return scope; - val = ut_getval(scope, key); + val = uc_getval(scope, key); json_object_put(scope); json_object_put(key); @@ -1639,104 +1639,104 @@ ut_execute_dot(struct ut_state *state, uint32_t off) } static struct json_object * -ut_execute_lbrack(struct ut_state *state, uint32_t off) +uc_execute_lbrack(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); /* postfix access */ if (op->is_postfix) - return ut_execute_dot(state, off); + return uc_execute_dot(state, off); - return ut_execute_list(state, op->tree.operand[0]); + return uc_execute_list(state, op->tree.operand[0]); } static struct json_object * -ut_execute_exp_list(struct ut_state *state, uint32_t off) +uc_execute_exp_list(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); - return ut_execute_op_sequence(state, op->tree.operand[0]); + return uc_execute_op_sequence(state, op->tree.operand[0]); } -static struct json_object *(*fns[__T_MAX])(struct ut_state *, uint32_t) = { - [T_NUMBER] = ut_execute_atom, - [T_DOUBLE] = ut_execute_atom, - [T_STRING] = ut_execute_atom, - [T_REGEXP] = ut_execute_atom, - [T_BOOL] = ut_execute_atom, - [T_NULL] = ut_execute_atom, - [T_THIS] = ut_execute_this, - [T_FUNC] = ut_execute_function, - [T_ARROW] = ut_execute_function, - [T_TEXT] = ut_execute_text, - [T_ASSIGN] = ut_execute_assign, - [T_LOCAL] = ut_execute_local, - [T_LABEL] = ut_execute_label, - [T_DOT] = ut_execute_dot, - [T_LBRACK] = ut_execute_lbrack, - [T_LBRACE] = ut_execute_object, - [T_IF] = ut_execute_if, - [T_ELIF] = ut_execute_if, - [T_QMARK] = ut_execute_if, - [T_FOR] = ut_execute_for, - [T_WHILE] = ut_execute_while, - [T_AND] = ut_execute_and_or, - [T_OR] = ut_execute_and_or, - [T_LT] = ut_execute_rel, - [T_LE] = ut_execute_rel, - [T_GT] = ut_execute_rel, - [T_GE] = ut_execute_rel, - [T_EQ] = ut_execute_rel, - [T_NE] = ut_execute_rel, - [T_EQS] = ut_execute_equality, - [T_NES] = ut_execute_equality, - [T_IN] = ut_execute_in, - [T_INC] = ut_execute_inc_dec, - [T_DEC] = ut_execute_inc_dec, - [T_LPAREN] = ut_execute_call, - [T_LEXP] = ut_execute_exp, - [T_ADD] = ut_execute_arith, - [T_SUB] = ut_execute_arith, - [T_MUL] = ut_execute_arith, - [T_DIV] = ut_execute_arith, - [T_MOD] = ut_execute_arith, - [T_LSHIFT] = ut_execute_bitop, - [T_RSHIFT] = ut_execute_bitop, - [T_BAND] = ut_execute_bitop, - [T_BXOR] = ut_execute_bitop, - [T_BOR] = ut_execute_bitop, - [T_COMPL] = ut_execute_compl, - [T_NOT] = ut_execute_not, - [T_RETURN] = ut_execute_return, - [T_BREAK] = ut_execute_break_cont, - [T_CONTINUE] = ut_execute_break_cont, - [T_TRY] = ut_execute_try_catch, - [T_SWITCH] = ut_execute_switch_case, - [T_COMMA] = ut_execute_exp_list, +static struct json_object *(*fns[__T_MAX])(struct uc_state *, uint32_t) = { + [T_NUMBER] = uc_execute_atom, + [T_DOUBLE] = uc_execute_atom, + [T_STRING] = uc_execute_atom, + [T_REGEXP] = uc_execute_atom, + [T_BOOL] = uc_execute_atom, + [T_NULL] = uc_execute_atom, + [T_THIS] = uc_execute_this, + [T_FUNC] = uc_execute_function, + [T_ARROW] = uc_execute_function, + [T_TEXT] = uc_execute_text, + [T_ASSIGN] = uc_execute_assign, + [T_LOCAL] = uc_execute_local, + [T_LABEL] = uc_execute_label, + [T_DOT] = uc_execute_dot, + [T_LBRACK] = uc_execute_lbrack, + [T_LBRACE] = uc_execute_object, + [T_IF] = uc_execute_if, + [T_ELIF] = uc_execute_if, + [T_QMARK] = uc_execute_if, + [T_FOR] = uc_execute_for, + [T_WHILE] = uc_execute_while, + [T_AND] = uc_execute_and_or, + [T_OR] = uc_execute_and_or, + [T_LT] = uc_execute_rel, + [T_LE] = uc_execute_rel, + [T_GT] = uc_execute_rel, + [T_GE] = uc_execute_rel, + [T_EQ] = uc_execute_rel, + [T_NE] = uc_execute_rel, + [T_EQS] = uc_execute_equality, + [T_NES] = uc_execute_equality, + [T_IN] = uc_execute_in, + [T_INC] = uc_execute_inc_dec, + [T_DEC] = uc_execute_inc_dec, + [T_LPAREN] = uc_execute_call, + [T_LEXP] = uc_execute_exp, + [T_ADD] = uc_execute_arith, + [T_SUB] = uc_execute_arith, + [T_MUL] = uc_execute_arith, + [T_DIV] = uc_execute_arith, + [T_MOD] = uc_execute_arith, + [T_LSHIFT] = uc_execute_bitop, + [T_RSHIFT] = uc_execute_bitop, + [T_BAND] = uc_execute_bitop, + [T_BXOR] = uc_execute_bitop, + [T_BOR] = uc_execute_bitop, + [T_COMPL] = uc_execute_compl, + [T_NOT] = uc_execute_not, + [T_RETURN] = uc_execute_return, + [T_BREAK] = uc_execute_break_cont, + [T_CONTINUE] = uc_execute_break_cont, + [T_TRY] = uc_execute_try_catch, + [T_SWITCH] = uc_execute_switch_case, + [T_COMMA] = uc_execute_exp_list, }; static struct json_object * -ut_execute_op(struct ut_state *state, uint32_t off) +uc_execute_op(struct uc_state *state, uint32_t off) { - struct ut_op *op = ut_get_op(state, off); + struct uc_op *op = uc_get_op(state, off); if (!fns[op->type]) - return ut_new_exception(state, op->off, "Runtime error: Unrecognized opcode %d", op->type); + return uc_new_exception(state, op->off, "Runtime error: Unrecognized opcode %d", op->type); return fns[op->type](state, off); } static struct json_object * -ut_execute_op_sequence(struct ut_state *state, uint32_t off) +uc_execute_op_sequence(struct uc_state *state, uint32_t off) { struct json_object *v = NULL; - struct ut_op *tag = NULL; - struct ut_op *op = NULL; + struct uc_op *tag = NULL; + struct uc_op *op = NULL; while (off) { json_object_put(v); - v = ut_execute_op(state, off); + v = uc_execute_op(state, off); tag = v ? json_object_get_userdata(v) : NULL; switch (tag ? tag->type : 0) { @@ -1747,7 +1747,7 @@ ut_execute_op_sequence(struct ut_state *state, uint32_t off) return v; } - op = ut_get_op(state, off); + op = uc_get_op(state, off); off = op ? op->tree.next : 0; } @@ -1755,7 +1755,7 @@ ut_execute_op_sequence(struct ut_state *state, uint32_t off) } static void -ut_globals_init(struct ut_state *state, struct json_object *scope) +uc_globals_init(struct uc_state *state, struct json_object *scope) { struct json_object *arr = xjs_new_array(); const char *p, *last; @@ -1775,7 +1775,7 @@ ut_globals_init(struct ut_state *state, struct json_object *scope) } static void -ut_register_variable(struct json_object *scope, const char *key, struct json_object *val) +uc_register_variable(struct json_object *scope, const char *key, struct json_object *val) { char *name = strdup(key); char *p; @@ -1792,13 +1792,13 @@ ut_register_variable(struct json_object *scope, const char *key, struct json_obj } struct json_object * -ut_run(struct ut_state *state, struct json_object *env, struct json_object *modules) +uc_run(struct uc_state *state, struct json_object *env, struct json_object *modules) { struct json_object *args, *rv; - struct ut_function fn = {}; + struct uc_function fn = {}; size_t i; - state->scope = ut_new_scope(state, NULL); + state->scope = uc_new_scope(state, NULL); state->ctx = NULL; fn.source = state->source; @@ -1806,11 +1806,11 @@ ut_run(struct ut_state *state, struct json_object *env, struct json_object *modu if (env) { json_object_object_foreach(env, key, val) - ut_register_variable(state->scope->scope, key, json_object_get(val)); + uc_register_variable(state->scope->scope, key, json_object_get(val)); } - ut_globals_init(state, state->scope->scope); - ut_lib_init(state, state->scope->scope); + uc_globals_init(state, state->scope->scope); + uc_lib_init(state, state->scope->scope); if (modules) { args = xjs_new_array(); @@ -1818,14 +1818,14 @@ ut_run(struct ut_state *state, struct json_object *env, struct json_object *modu for (i = 0; i < json_object_array_length(modules); i++) { json_object_array_put_idx(args, 0, json_object_get(json_object_array_get_idx(modules, i))); - rv = ut_invoke(state, 0, NULL, + rv = uc_invoke(state, 0, NULL, json_object_object_get(state->scope->scope, "require"), args); - if (ut_is_type(rv, T_EXCEPTION)) + if (uc_is_type(rv, T_EXCEPTION)) goto out; - ut_register_variable(state->scope->scope, + uc_register_variable(state->scope->scope, json_object_get_string(json_object_array_get_idx(modules, i)), rv); } @@ -1833,10 +1833,10 @@ ut_run(struct ut_state *state, struct json_object *env, struct json_object *modu json_object_put(args); } - rv = ut_execute_source(state, state->source, state->scope); + rv = uc_execute_source(state, state->source, state->scope); out: - ut_release_scope(state->scope); + uc_release_scope(state->scope); return rv; } |