From 79c9c418c6c6312c550b9253b62701f3f788dfa2 Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Fri, 28 Dec 2012 16:08:29 +0000 Subject: [PATCH] Path manipulation (path/getpath/setpath/delpath) and docs. del function should fix #37. --- builtin.c | 42 +++++++++++++++------ execute.c | 23 +++++++++++- jv.c | 3 +- jv_aux.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++--- jv_aux.h | 8 ++-- opcode_list.h | 1 + testdata | 44 ++++++++++++++++++++++ 7 files changed, 198 insertions(+), 25 deletions(-) diff --git a/builtin.c b/builtin.c index c431be0..de40be7 100644 --- a/builtin.c +++ b/builtin.c @@ -484,6 +484,9 @@ static struct cfunction function_list[] = { {(cfunction_ptr)f_tonumber, "tonumber", 1}, {(cfunction_ptr)f_tostring, "tostring", 1}, {(cfunction_ptr)f_keys, "keys", 1}, + {(cfunction_ptr)jv_setpath, "setpath", 3}, // FIXME typechecking + {(cfunction_ptr)jv_getpath, "getpath", 2}, + {(cfunction_ptr)jv_delpath, "delpath", 2}, {(cfunction_ptr)f_equal, "_equal", 3}, {(cfunction_ptr)f_notequal, "_notequal", 3}, {(cfunction_ptr)f_less, "_less", 3}, @@ -508,22 +511,36 @@ static struct cfunction function_list[] = { static struct symbol_table cbuiltins = {function_list, sizeof(function_list)/sizeof(function_list[0])}; -typedef block (*bytecoded_builtin)(); 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)}, - {"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()))} - }; block builtins = gen_noop(); - for (unsigned i=0; ii[1] + end < array->length); + assert(a->i[0] + end <= a->i[1]); jv_complex slice = *a; slice.i[0] += start; slice.i[1] = slice.i[0] + (end - start); diff --git a/jv_aux.c b/jv_aux.c index a4bfddd..83695f9 100644 --- a/jv_aux.c +++ b/jv_aux.c @@ -3,7 +3,7 @@ #include #include "jv_alloc.h" -jv jv_lookup(jv t, jv k) { +jv jv_get(jv t, jv k) { jv v; if (jv_get_kind(t) == JV_KIND_OBJECT && jv_get_kind(k) == JV_KIND_STRING) { v = jv_object_get(t, k); @@ -33,7 +33,12 @@ jv jv_lookup(jv t, jv k) { return v; } -jv jv_modify(jv t, jv k, jv v) { +jv jv_set(jv t, jv k, jv v) { + if (!jv_is_valid(v)) { + jv_free(t); + jv_free(k); + return v; + } int isnull = jv_get_kind(t) == JV_KIND_NULL; if (jv_get_kind(k) == JV_KIND_STRING && (jv_get_kind(t) == JV_KIND_OBJECT || isnull)) { @@ -55,15 +60,100 @@ jv jv_modify(jv t, jv k, jv v) { return t; } -jv jv_insert(jv root, jv value, jv* path, int pathlen) { - if (pathlen == 0) { +jv jv_del(jv t, jv k) { + if (jv_get_kind(t) == JV_KIND_NULL) { + jv_free(k); + } else if (jv_get_kind(t) == JV_KIND_ARRAY && + jv_get_kind(k) == JV_KIND_NUMBER) { + int idx = (int)jv_number_value(k); + jv_free(k); + int len = jv_array_length(jv_copy(t)); + if (idx >= 0 && idx < len) { + for (int i = idx+1; i < len; i++) { + t = jv_array_set(t, i-1, jv_array_get(jv_copy(t), i)); + } + t = jv_array_slice(t, 0, len-1); + } + } else if (jv_get_kind(t) == JV_KIND_OBJECT && + jv_get_kind(k) == JV_KIND_STRING) { + t = jv_object_delete(t, k); + } else { + jv err = jv_invalid_with_msg(jv_string_fmt("Cannot delete field at %s index of %s", + jv_kind_name(jv_get_kind(k)), + jv_kind_name(jv_get_kind(t)))); + jv_free(t); + jv_free(k); + t = err; + } + return t; +} + +jv jv_setpath(jv root, jv path, jv value) { + if (jv_get_kind(path) != JV_KIND_ARRAY) { + jv_free(value); + jv_free(root); + jv_free(path); + return jv_invalid_with_msg(jv_string("Path must be specified as an array")); + } + if (!jv_is_valid(root)){ + jv_free(value); + jv_free(path); + return root; + } + if (jv_array_length(jv_copy(path)) == 0) { + jv_free(path); jv_free(root); return value; } - return jv_modify(root, jv_copy(*path), - jv_insert(jv_lookup(jv_copy(root), jv_copy(*path)), value, path+1, pathlen-1)); + jv pathcurr = jv_array_get(jv_copy(path), 0); + jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path))); + return jv_set(root, pathcurr, + jv_setpath(jv_get(jv_copy(root), jv_copy(pathcurr)), pathrest, value)); +} + +jv jv_getpath(jv root, jv path) { + if (jv_get_kind(path) != JV_KIND_ARRAY) { + jv_free(root); + jv_free(path); + return jv_invalid_with_msg(jv_string("Path must be specified as an array")); + } + if (!jv_is_valid(root)) { + jv_free(path); + return root; + } + if (jv_array_length(jv_copy(path)) == 0) { + jv_free(path); + return root; + } + jv pathcurr = jv_array_get(jv_copy(path), 0); + jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path))); + return jv_getpath(jv_get(root, pathcurr), pathrest); } +jv jv_delpath(jv root, jv path) { + if (jv_get_kind(path) != JV_KIND_ARRAY) { + jv_free(root); + jv_free(path); + return jv_invalid_with_msg(jv_string("Path must be specified as an array")); + } + if (!jv_is_valid(root)) { + jv_free(path); + return root; + } + if (jv_array_length(jv_copy(path)) == 1) { + return jv_del(root, jv_array_get(path, 0)); + } + jv pathcurr = jv_array_get(jv_copy(path), 0); + jv pathrest = jv_array_slice(path, 1, jv_array_length(jv_copy(path))); + jv new_obj = jv_delpath(jv_get(jv_copy(root), jv_copy(pathcurr)), pathrest); + if (jv_get_kind(new_obj) == JV_KIND_NULL) { + jv_free(pathcurr); + jv_free(new_obj); + return root; + } else { + return jv_set(root, pathcurr, new_obj); + } +} static int string_cmp(const void* pa, const void* pb){ const jv* a = pa; diff --git a/jv_aux.h b/jv_aux.h index 36b0e5e..0b827d2 100644 --- a/jv_aux.h +++ b/jv_aux.h @@ -3,9 +3,11 @@ #include "jv.h" -jv jv_lookup(jv t, jv k); -jv jv_modify(jv t, jv k, jv v); -jv jv_insert(jv root, jv value, jv* path, int pathlen); +jv jv_get(jv t, jv k); +jv jv_set(jv t, jv k, jv v); +jv jv_setpath(jv root, jv path, jv value); +jv jv_getpath(jv root, jv path); +jv jv_delpath(jv root, jv path); jv jv_keys(jv /*object or array*/); int jv_cmp(jv, jv); diff --git a/opcode_list.h b/opcode_list.h index e6dece6..e460ffe 100644 --- a/opcode_list.h +++ b/opcode_list.h @@ -15,6 +15,7 @@ OP(BACKTRACK, NONE, 0, 0) OP(APPEND, NONE, 2, 1) OP(INSERT, NONE, 4, 2) +OP(GETPATH, NONE, 2, 1) OP(ASSIGN, VARIABLE, 3, 0) OP(CALL_BUILTIN, CFUNC, -1, 1) diff --git a/testdata b/testdata index bb8e677..36429f5 100644 --- a/testdata +++ b/testdata @@ -335,6 +335,50 @@ fold 0 as $s (.[] | $s + .) [1,2,4] 7 +# +# Paths +# + +path(.foo[0,1]) +null +["foo", 0] +["foo", 1] + +path(.[] | select(.>3)) +[1,5,3] +[1] + +path(.) +42 +[] + +["foo",1] as $p | getpath($p), setpath($p; 20), delpath($p) +{"bar": 42, "foo": ["a", "b", "c", "d"]} +"b" +{"bar": 42, "foo": ["a", 20, "c", "d"]} +{"bar": 42, "foo": ["a", "c", "d"]} + +map(getpath([2])), map(setpath([2]; 42)), map(delpath([2])) +[[0], [0,1], [0,1,2]] +[null, null, 2] +[[0,null,42], [0,1,42], [0,1,42]] +[[0], [0,1], [0,1]] + +map(delpath([0,"foo"])) +[[{"foo":2, "x":1}], [{"bar":2}]] +[[{"x":1}], [{"bar":2}]] + +["foo",1] as $p | getpath($p), setpath($p; 20), delpath($p) +{"bar":false} +null +{"bar":false, "foo": [null, 20]} +{"bar":false} + +delpath([-200]) +[1,2,3] +[1,2,3] + + # # Assignment # -- 2.40.0