summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--ast.h1
-rw-r--r--eval.c11
-rw-r--r--lexer.c2
-rw-r--r--lexer.h2
-rw-r--r--parser.y1
5 files changed, 16 insertions, 1 deletions
diff --git a/ast.h b/ast.h
index 2090b33..f20be9c 100644
--- a/ast.h
+++ b/ast.h
@@ -77,6 +77,7 @@ struct ut_state {
uint8_t size;
uint8_t off;
} stack;
+ struct json_object *ctx;
};
struct ut_opcode *ut_new_op(struct ut_state *s, int type, struct json_object *val, ...);
diff --git a/eval.c b/eval.c
index a6e9dee..02ef0d6 100644
--- a/eval.c
+++ b/eval.c
@@ -823,6 +823,8 @@ ut_invoke(struct ut_state *state, struct ut_opcode *op, struct json_object *scop
for (arridx = 0; arg; arridx++, arg = arg->sibling)
ut_setval(s, arg->val, argvals ? json_object_array_get_idx(argvals, arridx) : NULL);
+ json_object_set_userdata(s, json_object_get(state->ctx), NULL);
+
rv = ut_execute_op_sequence(state, decl->operand[2]);
tag = json_object_get_userdata(rv);
@@ -849,6 +851,7 @@ ut_invoke(struct ut_state *state, struct ut_opcode *op, struct json_object *scop
if (!scope) {
state->stack.scope[--state->stack.off] = NULL;
+ json_object_put(json_object_get_userdata(s));
json_object_put(s);
}
@@ -1139,6 +1142,9 @@ ut_execute_op(struct ut_state *state, struct ut_opcode *op)
case T_STRING:
return json_object_get(op->val);
+ case T_THIS:
+ return json_object_get_userdata(ut_getscope(state, 0));
+
case T_FUNC:
if (op->operand[0])
ut_setval(ut_getscope(state, 0), op->operand[0]->val, op->val);
@@ -1158,11 +1164,13 @@ ut_execute_op(struct ut_state *state, struct ut_opcode *op)
case T_LABEL:
scope = ut_getref(state, op, &key);
+ state->ctx = scope;
return ut_getval(scope, key);
case T_DOT:
scope = ut_getref_required(state, op, &key);
+ state->ctx = scope;
return key ? ut_getval(scope, key) : scope;
@@ -1170,6 +1178,7 @@ ut_execute_op(struct ut_state *state, struct ut_opcode *op)
/* postfix access */
if (op->val) {
scope = ut_getref_required(state, op, &key);
+ state->ctx = scope;
return key ? ut_getval(scope, key) : scope;
}
@@ -1288,6 +1297,8 @@ ut_run(struct ut_state *state)
if (!json_object_is_type(scope, json_type_object))
return UT_ERROR_EXCEPTION;
+ state->ctx = scope;
+
ut_lib_init(state, scope);
args = json_object_new_array();
diff --git a/lexer.c b/lexer.c
index 67d390a..ed27c68 100644
--- a/lexer.c
+++ b/lexer.c
@@ -131,6 +131,7 @@ static const struct token reserved_words[] = {
{ T_BOOL, "false", 5, parse_bool },
{ T_BOOL, "true", 4, parse_bool },
{ T_ELSE, "else", 4 },
+ { T_THIS, "this", 4 },
{ T_NUMBER, "NaN", 3, parse_number },
{ T_FOR, "for", 3 },
{ T_IN, "in", 2 },
@@ -204,6 +205,7 @@ const char *tokennames[__T_MAX] = {
[T_RETURN] = "'return'",
[T_BREAK] = "'break'",
[T_CONTINUE] = "'continue'",
+ [T_THIS] = "'this'",
//[T_LSTM] = "'{%'",
//[T_RSTM] = "'%}'"
};
diff --git a/lexer.h b/lexer.h
index de722ec..27039f9 100644
--- a/lexer.h
+++ b/lexer.h
@@ -19,7 +19,7 @@
#include "ast.h"
-#define __T_MAX 69
+#define __T_MAX 70
#define T_EXCEPTION (__T_MAX + 0)
#define T_CFUNC (__T_MAX + 1)
#define T_RESSOURCE (__T_MAX + 2)
diff --git a/parser.y b/parser.y
index eab4583..204b091 100644
--- a/parser.y
+++ b/parser.y
@@ -260,6 +260,7 @@ primary_exp(A) ::= T_NUMBER(B). { A = B; }
primary_exp(A) ::= T_DOUBLE(B). { A = B; }
primary_exp(A) ::= T_STRING(B). { A = B; }
primary_exp(A) ::= T_LABEL(B). { A = B; }
+primary_exp(A) ::= T_THIS(B). { A = B; }
primary_exp(A) ::= array(B). { A = B; }
primary_exp(A) ::= object(B). { A = B; }
primary_exp(A) ::= T_LPAREN assign_exp(B) T_RPAREN. { A = B; }