diff options
author | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-05-06 14:21:00 +0100 |
---|---|---|
committer | Stephen Dolan <mu@netsoc.tcd.ie> | 2013-05-06 14:21:00 +0100 |
commit | 5be97463ecd4f93fdfbaec457d9100beeb9dc603 (patch) | |
tree | c60380d11fa33623ceb683189b12bf411bd24244 /execute.c | |
parent | 48be23233ad63bf1bd218c259efedff3e52106c5 (diff) |
Add a --arg option to allow variables to be passed from the cmdline.
Closes #107
Diffstat (limited to 'execute.c')
-rw-r--r-- | execute.c | 15 |
1 files changed, 14 insertions, 1 deletions
@@ -506,13 +506,22 @@ void jq_teardown(jq_state **jq) { jv_mem_free(old_jq); } -struct bytecode* jq_compile(const char* str) { +struct bytecode* jq_compile_args(const char* str, jv args) { + assert(jv_get_kind(args) == JV_KIND_ARRAY); struct locfile locations; locfile_init(&locations, str, strlen(str)); block program; struct bytecode* bc = 0; int nerrors = jq_parse(&locations, &program); if (nerrors == 0) { + for (int i=0; i<jv_array_length(jv_copy(args)); i++) { + jv arg = jv_array_get(jv_copy(args), i); + jv name = jv_object_get(jv_copy(arg), jv_string("name")); + jv value = jv_object_get(arg, jv_string("value")); + program = gen_var_binding(gen_const(value), jv_string_value(name), program); + jv_free(name); + } + jv_free(args); program = builtins_bind(program); nerrors = block_compile(program, &locations, &bc); } @@ -522,3 +531,7 @@ struct bytecode* jq_compile(const char* str) { locfile_free(&locations); return bc; } + +struct bytecode* jq_compile(const char* str) { + return jq_compile_args(str, jv_array()); +} |