Define bytecoded builtins more concisely.
This commit is contained in:
1 parent
4c65847568
commit
334a79b704
1 file changed
+16
-32
@@ -251,27 +251,6 @@ static void f_type(jv input[], jv output[]) {
|
|||||||
jv_free(input[0]);
|
jv_free(input[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static block j_empty() {
|
|
||||||
return gen_function("empty", gen_op_simple(BACKTRACK));
|
|
||||||
}
|
|
||||||
|
|
||||||
static block j_false() {
|
|
||||||
return gen_function("false", gen_op_const(LOADK, jv_false()));
|
|
||||||
}
|
|
||||||
|
|
||||||
static block j_true() {
|
|
||||||
return gen_function("true", gen_op_const(LOADK, jv_true()));
|
|
||||||
}
|
|
||||||
|
|
||||||
static block j_null() {
|
|
||||||
return gen_function("null", gen_op_const(LOADK, jv_null()));
|
|
||||||
}
|
|
||||||
|
|
||||||
static block j_not() {
|
|
||||||
return gen_function("not", gen_condbranch(gen_op_const(LOADK, jv_false()),
|
|
||||||
gen_op_const(LOADK, jv_true())));
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct cfunction function_list[] = {
|
static struct cfunction function_list[] = {
|
||||||
{f_plus, "_plus", CALL_BUILTIN_3_1},
|
{f_plus, "_plus", CALL_BUILTIN_3_1},
|
||||||
{f_minus, "_minus", CALL_BUILTIN_3_1},
|
{f_minus, "_minus", CALL_BUILTIN_3_1},
|
||||||
@@ -294,13 +273,22 @@ static struct cfunction function_list[] = {
|
|||||||
static struct symbol_table cbuiltins = {function_list, sizeof(function_list)/sizeof(function_list[0])};
|
static struct symbol_table cbuiltins = {function_list, sizeof(function_list)/sizeof(function_list[0])};
|
||||||
|
|
||||||
typedef block (*bytecoded_builtin)();
|
typedef block (*bytecoded_builtin)();
|
||||||
static bytecoded_builtin bytecoded_builtins[] = {
|
struct bytecoded_builtin { const char* name; block code; };
|
||||||
j_empty,
|
static block bind_bytecoded_builtins(block b) {
|
||||||
j_false,
|
struct bytecoded_builtin builtin_defs[] = {
|
||||||
j_true,
|
{"empty", gen_op_simple(BACKTRACK)},
|
||||||
j_null,
|
{"false", gen_op_const(LOADK, jv_false())},
|
||||||
j_not,
|
{"true", gen_op_const(LOADK, jv_true())},
|
||||||
|
{"null", gen_op_const(LOADK, jv_null())},
|
||||||
|
{"not", gen_condbranch(gen_op_const(LOADK, jv_false()),
|
||||||
|
gen_op_const(LOADK, jv_true()))}
|
||||||
};
|
};
|
||||||
|
block builtins = gen_noop();
|
||||||
|
for (unsigned i=0; i<sizeof(builtin_defs)/sizeof(builtin_defs[0]); i++) {
|
||||||
|
builtins = BLOCK(builtins, gen_function(builtin_defs[i].name, builtin_defs[i].code));
|
||||||
|
}
|
||||||
|
return block_bind(builtins, b, OP_IS_CALL_PSEUDO);
|
||||||
|
}
|
||||||
|
|
||||||
static const char* jq_builtins[] = {
|
static const char* jq_builtins[] = {
|
||||||
"def map(f): [.[] | f];",
|
"def map(f): [.[] | f];",
|
||||||
@@ -318,10 +306,6 @@ block builtins_bind(block b) {
|
|||||||
b = block_bind(funcs, b, OP_IS_CALL_PSEUDO);
|
b = block_bind(funcs, b, OP_IS_CALL_PSEUDO);
|
||||||
locfile_free(&src);
|
locfile_free(&src);
|
||||||
}
|
}
|
||||||
block builtins = gen_noop();
|
b = bind_bytecoded_builtins(b);
|
||||||
for (unsigned i=0; i<sizeof(bytecoded_builtins)/sizeof(bytecoded_builtins[0]); i++) {
|
|
||||||
block_append(&builtins, bytecoded_builtins[i]());
|
|
||||||
}
|
|
||||||
b = block_bind(builtins, b, OP_IS_CALL_PSEUDO);
|
|
||||||
return gen_cbinding(&cbuiltins, b);
|
return gen_cbinding(&cbuiltins, b);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user