]> granicus.if.org Git - jq/commitdiff
an addition operator, of sorts
authorStephen <stephen@polya.(none)>
Thu, 16 Aug 2012 00:16:08 +0000 (01:16 +0100)
committerStephen <stephen@polya.(none)>
Thu, 16 Aug 2012 00:16:08 +0000 (01:16 +0100)
c/builtin.c
c/execute.c
c/opcode_list.h
c/parser.y

index eba81994ebe098fe558e85fd68448b34b5cfaa40..ef37c28c32f970ff0dbd296255ce73d8e5a676cb 100644 (file)
@@ -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])};
index c06ed124d8c445f6fe3ef18ce166cba6a0193e90..44aaab3a31b24dbcb4c42c78bcb2b560f4bc9892 100644 (file)
@@ -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;
+    }
       
       
     }
index d88381b79dc1c6ec31dd1e9faab7e0ac6e3c0fe7..2fe3f8a80f02a6dc9d2c79ef2174c5fd99e2fe73 100644 (file)
@@ -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)
index e8721a17d8f9842ac8ff68770aa0ec026c5cd3f1..393f7a8301d486522d21ef410484fd8b0c55df78 100644 (file)
@@ -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; 
 }