mirror of
https://github.com/stedolan/jq.git
synced 2024-05-11 05:55:39 +00:00
an addition operator, of sorts
This commit is contained in:
20
c/builtin.c
20
c/builtin.c
@@ -3,16 +3,32 @@
|
||||
#include <jansson.h>
|
||||
|
||||
|
||||
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])};
|
||||
|
14
c/execute.c
14
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user