From fd738bfaced523da61875ca3e6cf80651247572c Mon Sep 17 00:00:00 2001 From: Stephen Date: Thu, 16 Aug 2012 01:16:08 +0100 Subject: [PATCH] an addition operator, of sorts --- c/builtin.c | 20 ++++++++++++++++++-- c/execute.c | 14 ++++++++++++++ c/opcode_list.h | 1 + c/parser.y | 7 +++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/c/builtin.c b/c/builtin.c index eba8199..ef37c28 100644 --- a/c/builtin.c +++ b/c/builtin.c @@ -3,16 +3,32 @@ #include -void f_false(json_t* input[], json_t* output[]) { +static void f_false(json_t* input[], json_t* output[]) { output[0] = json_false(); } -void f_true(json_t* input[], json_t* output[]) { +static void f_true(json_t* input[], json_t* output[]) { output[0] = json_true(); } + +static void f_plus(json_t* input[], json_t* output[]) { + json_t* a = input[2]; + json_t* b = input[1]; + if (json_is_number(a) && json_is_number(b)) { + output[0] = json_real(json_number_value(a) + + json_number_value(b)); + } else if (json_is_array(a) && json_is_array(b)) { + output[0] = json_copy(a); + json_array_extend(output[0], b); + } else { + output[0] = json_string("wtf gaize"); + } +} + struct cfunction function_list[] = { {f_true, "true", CALL_BUILTIN_1_1}, {f_false, "false", CALL_BUILTIN_1_1}, + {f_plus, "_plus", CALL_BUILTIN_3_1}, }; struct symbol_table builtins = {function_list, sizeof(function_list)/sizeof(function_list[0])}; diff --git a/c/execute.c b/c/execute.c index c06ed12..44aaab3 100644 --- a/c/execute.c +++ b/c/execute.c @@ -340,6 +340,20 @@ json_t* jq_next() { stack_push(stackval_replace(top, cfunc_output[0])); break; } + + case CALL_BUILTIN_3_1: { + stackval top = stack_pop(); + json_t* a = stack_pop().value; + json_t* b = stack_pop().value; + cfunc_input[0] = top.value; + cfunc_input[1] = a; + cfunc_input[2] = b; + struct cfunction* func = &bc->globals->cfunctions[*pc++]; + printf(" call %s\n", func->name); + func->fptr(cfunc_input, cfunc_output); + stack_push(stackval_replace(top, cfunc_output[0])); + break; + } } diff --git a/c/opcode_list.h b/c/opcode_list.h index d88381b..2fe3f8a 100644 --- a/c/opcode_list.h +++ b/c/opcode_list.h @@ -15,3 +15,4 @@ OP(APPEND, NONE, 2, 1) OP(INSERT, NONE, 4, 2) OP(CALL_BUILTIN_1_1, CFUNC, 1, 1) +OP(CALL_BUILTIN_3_1, CFUNC, 3, 1) diff --git a/c/parser.y b/c/parser.y index e8721a1..393f7a8 100644 --- a/c/parser.y +++ b/c/parser.y @@ -71,6 +71,13 @@ Exp ',' Exp { $$ = gen_both($1, $3); } | +Exp '+' Exp { + $$ = gen_noop(); + block_append(&$$, gen_subexp($1)); + block_append(&$$, gen_subexp($3)); + block_append(&$$, gen_op_symbol(CALL_BUILTIN_3_1, "_plus")); +} | + Term { $$ = $1; } -- 2.49.0