2012-09-18 10:17:38 +01:00
|
|
|
#include <string.h>
|
2012-08-16 01:00:30 +01:00
|
|
|
#include "builtin.h"
|
2012-12-02 21:25:54 +00:00
|
|
|
#include "bytecode.h"
|
2012-09-17 20:59:34 +01:00
|
|
|
#include "compile.h"
|
2012-09-18 10:17:38 +01:00
|
|
|
#include "parser.h"
|
|
|
|
#include "locfile.h"
|
2012-11-30 20:27:16 +00:00
|
|
|
#include "jv_aux.h"
|
2012-09-18 10:17:38 +01:00
|
|
|
|
2012-08-16 01:16:08 +01:00
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
typedef jv (*func_1)(jv);
|
|
|
|
typedef jv (*func_2)(jv,jv);
|
|
|
|
typedef jv (*func_3)(jv,jv,jv);
|
|
|
|
typedef jv (*func_4)(jv,jv,jv,jv);
|
|
|
|
typedef jv (*func_5)(jv,jv,jv,jv,jv);
|
|
|
|
jv cfunction_invoke(struct cfunction* function, jv input[]) {
|
|
|
|
switch (function->nargs) {
|
|
|
|
case 1: return ((func_1)function->fptr)(input[0]);
|
|
|
|
case 2: return ((func_2)function->fptr)(input[0], input[1]);
|
|
|
|
case 3: return ((func_3)function->fptr)(input[0], input[1], input[2]);
|
|
|
|
case 4: return ((func_4)function->fptr)(input[0], input[1], input[2], input[3]);
|
|
|
|
case 5: return ((func_5)function->fptr)(input[0], input[1], input[2], input[3], input[4]);
|
|
|
|
default: return jv_invalid_with_msg(jv_string("Function takes too many arguments"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv type_error(jv bad, const char* msg) {
|
|
|
|
jv err = jv_invalid_with_msg(jv_string_fmt("%s %s",
|
|
|
|
jv_kind_name(jv_get_kind(bad)),
|
|
|
|
msg));
|
|
|
|
jv_free(bad);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv type_error2(jv bad1, jv bad2, const char* msg) {
|
|
|
|
jv err = jv_invalid_with_msg(jv_string_fmt("%s and %s %s",
|
|
|
|
jv_kind_name(jv_get_kind(bad1)),
|
|
|
|
jv_kind_name(jv_get_kind(bad2)),
|
|
|
|
msg));
|
|
|
|
jv_free(bad1);
|
|
|
|
jv_free(bad2);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_plus(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
2012-09-02 16:31:59 +01:00
|
|
|
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_number(jv_number_value(a) +
|
|
|
|
jv_number_value(b));
|
2012-09-10 16:16:39 +01:00
|
|
|
} else if (jv_get_kind(a) == JV_KIND_STRING && jv_get_kind(b) == JV_KIND_STRING) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_string_concat(a, b);
|
2012-09-02 16:31:59 +01:00
|
|
|
} else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_array_concat(a, b);
|
2012-09-09 19:17:07 +01:00
|
|
|
} else if (jv_get_kind(a) == JV_KIND_OBJECT && jv_get_kind(b) == JV_KIND_OBJECT) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_object_merge(a, b);
|
2012-08-16 01:16:08 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error2(a, b, "cannot be added");
|
2012-08-16 01:16:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_minus(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
2012-09-10 16:49:25 +01:00
|
|
|
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_number(jv_number_value(a) - jv_number_value(b));
|
2012-09-10 16:49:25 +01:00
|
|
|
} else if (jv_get_kind(a) == JV_KIND_ARRAY && jv_get_kind(b) == JV_KIND_ARRAY) {
|
|
|
|
jv out = jv_array();
|
|
|
|
for (int i=0; i<jv_array_length(jv_copy(a)); i++) {
|
|
|
|
jv x = jv_array_get(jv_copy(a), i);
|
|
|
|
int include = 1;
|
|
|
|
for (int j=0; j<jv_array_length(jv_copy(b)); j++) {
|
|
|
|
if (jv_equal(jv_copy(x), jv_array_get(jv_copy(b), j))) {
|
|
|
|
include = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (include)
|
|
|
|
out = jv_array_append(out, jv_copy(x));
|
|
|
|
jv_free(x);
|
|
|
|
}
|
|
|
|
jv_free(a);
|
|
|
|
jv_free(b);
|
2012-12-02 22:12:08 +00:00
|
|
|
return out;
|
2012-09-10 16:49:25 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error2(a, b, "cannot be subtracted");
|
2012-09-10 16:49:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_multiply(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
2012-09-10 16:57:17 +01:00
|
|
|
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_number(jv_number_value(a) * jv_number_value(b));
|
2012-09-10 16:57:17 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error2(a, b, "cannot be multiplied");
|
2012-09-10 16:57:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_divide(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
2012-09-10 16:57:17 +01:00
|
|
|
if (jv_get_kind(a) == JV_KIND_NUMBER && jv_get_kind(b) == JV_KIND_NUMBER) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_number(jv_number_value(a) / jv_number_value(b));
|
2012-09-10 16:57:17 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error2(a, b, "cannot be divided");
|
2012-09-10 16:57:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_add(jv array) {
|
2012-09-18 23:32:24 +01:00
|
|
|
if (jv_get_kind(array) != JV_KIND_ARRAY) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error(array, "cannot have its elements added");
|
2012-09-18 23:32:24 +01:00
|
|
|
} else if (jv_array_length(jv_copy(array)) == 0) {
|
2012-12-02 22:12:08 +00:00
|
|
|
jv_free(array);
|
|
|
|
return jv_null();
|
2012-09-18 23:32:24 +01:00
|
|
|
} else {
|
|
|
|
jv sum = jv_array_get(jv_copy(array), 0);
|
|
|
|
for (int i = 1; i < jv_array_length(jv_copy(array)); i++) {
|
2012-12-02 22:12:08 +00:00
|
|
|
sum = f_plus(jv_null(), sum, jv_array_get(jv_copy(array), i));
|
2012-09-18 23:32:24 +01:00
|
|
|
}
|
2012-12-02 22:12:08 +00:00
|
|
|
jv_free(array);
|
|
|
|
return sum;
|
2012-09-18 23:32:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_equal(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
|
|
|
return jv_bool(jv_equal(a, b));
|
2012-09-10 18:08:00 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_notequal(jv input, jv a, jv b) {
|
|
|
|
jv_free(input);
|
|
|
|
return jv_bool(!jv_equal(a, b));
|
2012-10-23 17:01:39 +02:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
enum cmp_op {
|
|
|
|
CMP_OP_LESS,
|
|
|
|
CMP_OP_GREATER,
|
|
|
|
CMP_OP_LESSEQ,
|
|
|
|
CMP_OP_GREATEREQ
|
|
|
|
};
|
|
|
|
|
|
|
|
static jv order_cmp(jv input, jv a, jv b, enum cmp_op op) {
|
|
|
|
jv_free(input);
|
|
|
|
int r = jv_cmp(a, b);
|
|
|
|
return jv_bool((op == CMP_OP_LESS && r < 0) ||
|
|
|
|
(op == CMP_OP_LESSEQ && r <= 0) ||
|
|
|
|
(op == CMP_OP_GREATEREQ && r >= 0) ||
|
|
|
|
(op == CMP_OP_GREATER && r > 0));
|
2012-10-07 22:34:12 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_less(jv input, jv a, jv b) {
|
|
|
|
return order_cmp(input, a, b, CMP_OP_LESS);
|
2012-10-07 22:34:12 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_greater(jv input, jv a, jv b) {
|
|
|
|
return order_cmp(input, a, b, CMP_OP_GREATER);
|
2012-10-07 22:34:12 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_lesseq(jv input, jv a, jv b) {
|
|
|
|
return order_cmp(input, a, b, CMP_OP_LESSEQ);
|
2012-10-07 22:34:12 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_greatereq(jv input, jv a, jv b) {
|
|
|
|
return order_cmp(input, a, b, CMP_OP_GREATEREQ);
|
2012-10-07 22:34:12 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:24:02 +00:00
|
|
|
static jv f_contains(jv a, jv b) {
|
2012-10-24 11:42:25 -07:00
|
|
|
jv_kind akind = jv_get_kind(a);
|
|
|
|
|
|
|
|
if (akind == jv_get_kind(b)) {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_bool(jv_contains(a, b));
|
|
|
|
} else {
|
|
|
|
return type_error2(a, b, "cannot have their containment checked");
|
2012-10-24 11:42:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_tonumber(jv input) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_NUMBER) {
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
if (jv_get_kind(input) == JV_KIND_STRING) {
|
|
|
|
jv parsed = jv_parse(jv_string_value(input));
|
|
|
|
if (!jv_is_valid(parsed) || jv_get_kind(parsed) == JV_KIND_NUMBER) {
|
|
|
|
jv_free(input);
|
|
|
|
return parsed;
|
2012-09-10 17:08:13 +01:00
|
|
|
}
|
|
|
|
}
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error(input, "cannot be parsed as a number");
|
2012-09-10 17:08:13 +01:00
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_length(jv input) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_ARRAY) {
|
|
|
|
return jv_number(jv_array_length(input));
|
|
|
|
} else if (jv_get_kind(input) == JV_KIND_OBJECT) {
|
|
|
|
return jv_number(jv_object_length(input));
|
|
|
|
} else if (jv_get_kind(input) == JV_KIND_STRING) {
|
|
|
|
return jv_number(jv_string_length(input));
|
2012-12-16 13:10:48 +00:00
|
|
|
} else if (jv_get_kind(input) == JV_KIND_NULL) {
|
|
|
|
jv_free(input);
|
|
|
|
return jv_number(0);
|
2012-09-16 11:08:42 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error(input, "has no length");
|
2012-09-16 11:08:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_tostring(jv input) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_STRING) {
|
|
|
|
return input;
|
2012-09-17 20:14:07 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return jv_dump_string(input, 0);
|
2012-09-17 20:14:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_keys(jv input) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_OBJECT || jv_get_kind(input) == JV_KIND_ARRAY) {
|
|
|
|
return jv_keys(input);
|
2012-10-20 00:26:37 +01:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error(input, "has no keys");
|
2012-10-20 00:26:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_sort(jv input){
|
|
|
|
if (jv_get_kind(input) == JV_KIND_ARRAY) {
|
|
|
|
return jv_sort(input, jv_copy(input));
|
2012-11-30 20:27:16 +00:00
|
|
|
} else {
|
2012-12-02 22:12:08 +00:00
|
|
|
return type_error(input, "cannot be sorted, as it is not an array");
|
2012-11-30 20:27:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-02 23:22:15 +00:00
|
|
|
static jv f_sort_by_impl(jv input, jv keys) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_ARRAY &&
|
|
|
|
jv_get_kind(keys) == JV_KIND_ARRAY &&
|
|
|
|
jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {
|
|
|
|
return jv_sort(input, keys);
|
|
|
|
} else {
|
|
|
|
return type_error2(input, keys, "cannot be sorted, as they are not both arrays");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_group_by_impl(jv input, jv keys) {
|
|
|
|
if (jv_get_kind(input) == JV_KIND_ARRAY &&
|
|
|
|
jv_get_kind(keys) == JV_KIND_ARRAY &&
|
|
|
|
jv_array_length(jv_copy(input)) == jv_array_length(jv_copy(keys))) {
|
|
|
|
return jv_group(input, keys);
|
|
|
|
} else {
|
|
|
|
return type_error2(input, keys, "cannot be sorted, as they are not both arrays");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-04 22:45:03 +00:00
|
|
|
static jv minmax_by(jv values, jv keys, int is_min) {
|
|
|
|
if (jv_get_kind(values) != JV_KIND_ARRAY)
|
|
|
|
return type_error2(values, keys, "cannot be iterated over");
|
|
|
|
if (jv_get_kind(keys) != JV_KIND_ARRAY)
|
|
|
|
return type_error2(values, keys, "cannot be iterated over");
|
|
|
|
if (jv_array_length(jv_copy(values)) != jv_array_length(jv_copy(keys)))
|
|
|
|
return type_error2(values, keys, "have wrong length");
|
|
|
|
|
|
|
|
if (jv_array_length(jv_copy(values)) == 0) {
|
|
|
|
jv_free(values);
|
|
|
|
jv_free(keys);
|
|
|
|
return jv_null();
|
|
|
|
}
|
|
|
|
jv ret = jv_array_get(jv_copy(values), 0);
|
|
|
|
jv retkey = jv_array_get(jv_copy(keys), 0);
|
|
|
|
for (int i=1; i<jv_array_length(jv_copy(values)); i++) {
|
|
|
|
jv item = jv_array_get(jv_copy(keys), i);
|
|
|
|
int cmp = jv_cmp(jv_copy(item), jv_copy(retkey));
|
|
|
|
if ((cmp < 0) == (is_min == 1)) {
|
|
|
|
jv_free(retkey);
|
|
|
|
retkey = item;
|
|
|
|
jv_free(ret);
|
|
|
|
ret = jv_array_get(jv_copy(values), i);
|
|
|
|
} else {
|
|
|
|
jv_free(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jv_free(values);
|
|
|
|
jv_free(keys);
|
|
|
|
jv_free(retkey);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_min(jv x) {
|
|
|
|
return minmax_by(x, jv_copy(x), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_max(jv x) {
|
|
|
|
return minmax_by(x, jv_copy(x), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_min_by_impl(jv x, jv y) {
|
|
|
|
return minmax_by(x, y, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jv f_max_by_impl(jv x, jv y) {
|
|
|
|
return minmax_by(x, y, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-02 22:12:08 +00:00
|
|
|
static jv f_type(jv input) {
|
|
|
|
jv out = jv_string(jv_kind_name(jv_get_kind(input)));
|
|
|
|
jv_free(input);
|
|
|
|
return out;
|
2012-09-17 23:06:48 +01:00
|
|
|
}
|
|
|
|
|
2012-12-10 22:30:09 +00:00
|
|
|
static jv f_error(jv input, jv msg) {
|
|
|
|
jv_free(input);
|
|
|
|
msg = f_tostring(msg);
|
|
|
|
return jv_invalid_with_msg(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-17 20:59:34 +01:00
|
|
|
static struct cfunction function_list[] = {
|
2012-12-02 22:12:08 +00:00
|
|
|
{(cfunction_ptr)f_plus, "_plus", 3},
|
|
|
|
{(cfunction_ptr)f_minus, "_minus", 3},
|
|
|
|
{(cfunction_ptr)f_multiply, "_multiply", 3},
|
|
|
|
{(cfunction_ptr)f_divide, "_divide", 3},
|
|
|
|
{(cfunction_ptr)f_tonumber, "tonumber", 1},
|
|
|
|
{(cfunction_ptr)f_tostring, "tostring", 1},
|
|
|
|
{(cfunction_ptr)f_keys, "keys", 1},
|
|
|
|
{(cfunction_ptr)f_equal, "_equal", 3},
|
|
|
|
{(cfunction_ptr)f_notequal, "_notequal", 3},
|
|
|
|
{(cfunction_ptr)f_less, "_less", 3},
|
|
|
|
{(cfunction_ptr)f_greater, "_greater", 3},
|
|
|
|
{(cfunction_ptr)f_lesseq, "_lesseq", 3},
|
|
|
|
{(cfunction_ptr)f_greatereq, "_greatereq", 3},
|
2012-12-02 22:24:02 +00:00
|
|
|
{(cfunction_ptr)f_contains, "contains", 2},
|
2012-12-02 22:12:08 +00:00
|
|
|
{(cfunction_ptr)f_length, "length", 1},
|
|
|
|
{(cfunction_ptr)f_type, "type", 1},
|
|
|
|
{(cfunction_ptr)f_add, "add", 1},
|
|
|
|
{(cfunction_ptr)f_sort, "sort", 1},
|
2012-12-02 23:22:15 +00:00
|
|
|
{(cfunction_ptr)f_sort_by_impl, "_sort_by_impl", 2},
|
|
|
|
{(cfunction_ptr)f_group_by_impl, "_group_by_impl", 2},
|
2012-12-04 22:45:03 +00:00
|
|
|
{(cfunction_ptr)f_min, "min", 1},
|
|
|
|
{(cfunction_ptr)f_max, "max", 1},
|
|
|
|
{(cfunction_ptr)f_min_by_impl, "_min_by_impl", 2},
|
|
|
|
{(cfunction_ptr)f_max_by_impl, "_max_by_impl", 2},
|
2012-12-10 22:30:09 +00:00
|
|
|
{(cfunction_ptr)f_error, "error", 2},
|
2012-08-16 01:00:30 +01:00
|
|
|
};
|
2012-09-17 20:59:34 +01:00
|
|
|
|
2012-12-04 22:45:03 +00:00
|
|
|
static struct symbol_table cbuiltins =
|
|
|
|
{function_list, sizeof(function_list)/sizeof(function_list[0])};
|
2012-09-16 17:08:56 +01:00
|
|
|
|
2012-09-17 20:59:34 +01:00
|
|
|
typedef block (*bytecoded_builtin)();
|
2012-11-26 18:53:47 +00:00
|
|
|
struct bytecoded_builtin { const char* name; block code; };
|
|
|
|
static block bind_bytecoded_builtins(block b) {
|
|
|
|
struct bytecoded_builtin builtin_defs[] = {
|
|
|
|
{"empty", gen_op_simple(BACKTRACK)},
|
2012-12-03 20:00:36 +00:00
|
|
|
{"false", gen_const(jv_false())},
|
|
|
|
{"true", gen_const(jv_true())},
|
|
|
|
{"null", gen_const(jv_null())},
|
|
|
|
{"not", gen_condbranch(gen_const(jv_false()),
|
|
|
|
gen_const(jv_true()))}
|
2012-11-26 18:53:47 +00:00
|
|
|
};
|
|
|
|
block builtins = gen_noop();
|
|
|
|
for (unsigned i=0; i<sizeof(builtin_defs)/sizeof(builtin_defs[0]); i++) {
|
2012-12-04 00:39:21 +00:00
|
|
|
builtins = BLOCK(builtins, gen_function(builtin_defs[i].name, gen_noop(),
|
|
|
|
builtin_defs[i].code));
|
2012-11-26 18:53:47 +00:00
|
|
|
}
|
|
|
|
return block_bind(builtins, b, OP_IS_CALL_PSEUDO);
|
|
|
|
}
|
2012-09-17 20:59:34 +01:00
|
|
|
|
2012-09-18 10:17:38 +01:00
|
|
|
static const char* jq_builtins[] = {
|
2012-09-18 12:58:39 +01:00
|
|
|
"def map(f): [.[] | f];",
|
|
|
|
"def select(f): if f then . else empty end;",
|
2012-12-02 23:22:15 +00:00
|
|
|
"def sort_by(f): _sort_by_impl(map([f]));",
|
|
|
|
"def group_by(f): _group_by_impl(map([f]));",
|
2012-12-03 02:02:12 +00:00
|
|
|
"def unique: group_by(.) | map(.[0]);",
|
2012-12-04 22:45:03 +00:00
|
|
|
"def max_by(f): _max_by_impl(map([f]));",
|
|
|
|
"def min_by(f): _min_by_impl(map([f]));",
|
2012-09-18 10:17:38 +01:00
|
|
|
};
|
|
|
|
|
2012-09-17 20:59:34 +01:00
|
|
|
|
2012-09-16 17:08:56 +01:00
|
|
|
block builtins_bind(block b) {
|
2012-09-18 12:58:39 +01:00
|
|
|
for (int i=(int)(sizeof(jq_builtins)/sizeof(jq_builtins[0]))-1; i>=0; i--) {
|
2012-09-18 10:17:38 +01:00
|
|
|
struct locfile src;
|
|
|
|
locfile_init(&src, jq_builtins[i], strlen(jq_builtins[i]));
|
|
|
|
block funcs;
|
|
|
|
int nerrors = jq_parse_library(&src, &funcs);
|
|
|
|
assert(!nerrors);
|
2012-09-18 12:58:39 +01:00
|
|
|
b = block_bind(funcs, b, OP_IS_CALL_PSEUDO);
|
2012-09-18 10:17:38 +01:00
|
|
|
locfile_free(&src);
|
2012-09-17 20:59:34 +01:00
|
|
|
}
|
2012-11-26 18:53:47 +00:00
|
|
|
b = bind_bytecoded_builtins(b);
|
2012-09-18 10:17:38 +01:00
|
|
|
return gen_cbinding(&cbuiltins, b);
|
2012-09-16 17:08:56 +01:00
|
|
|
}
|