2012-08-16 01:00:30 +01:00
|
|
|
#include <stdio.h>
|
2012-09-11 12:24:54 +01:00
|
|
|
#include <string.h>
|
2012-08-16 01:00:30 +01:00
|
|
|
#include "compile.h"
|
|
|
|
#include "parser.tab.h"
|
|
|
|
#include "builtin.h"
|
2012-09-02 16:31:59 +01:00
|
|
|
#include "jv.h"
|
2012-09-11 12:24:54 +01:00
|
|
|
#include "locfile.h"
|
2012-08-16 01:00:30 +01:00
|
|
|
|
2012-09-11 12:24:54 +01:00
|
|
|
int jq_parse(struct locfile* source, block* answer);
|
2012-08-16 01:00:30 +01:00
|
|
|
|
2012-09-02 17:20:13 +01:00
|
|
|
void jq_init(struct bytecode* bc, jv value);
|
|
|
|
jv jq_next();
|
2012-09-02 21:45:27 +01:00
|
|
|
void jq_teardown();
|
2012-08-16 01:00:30 +01:00
|
|
|
|
2012-09-11 12:24:54 +01:00
|
|
|
struct bytecode* jq_compile(const char* str) {
|
|
|
|
struct locfile locations;
|
|
|
|
locfile_init(&locations, str, strlen(str));
|
|
|
|
block program;
|
|
|
|
struct bytecode* bc = 0;
|
|
|
|
int nerrors = jq_parse(&locations, &program);
|
|
|
|
if (nerrors == 0) {
|
|
|
|
block_append(&program, block_join(gen_op_simple(YIELD), gen_op_simple(BACKTRACK)));
|
|
|
|
program = gen_cbinding(&builtins, program);
|
|
|
|
nerrors = block_compile(program, &locations, &bc);
|
|
|
|
block_free(program);
|
|
|
|
}
|
|
|
|
if (nerrors) {
|
|
|
|
fprintf(stderr, "%d compile %s\n", nerrors, nerrors > 1 ? "errors" : "error");
|
|
|
|
}
|
|
|
|
locfile_free(&locations);
|
|
|
|
return bc;
|
|
|
|
}
|
|
|
|
|
2012-09-11 00:04:47 +01:00
|
|
|
|
|
|
|
void run_program(struct bytecode* bc) {
|
|
|
|
#if JQ_DEBUG
|
|
|
|
dump_disassembly(0, bc);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
char buf[409600];
|
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
jv value = jv_parse(buf);
|
|
|
|
if (!jv_is_valid(value)) {
|
|
|
|
assert(0 && "couldn't parse input"); //FIXME
|
|
|
|
}
|
|
|
|
jq_init(bc, value);
|
|
|
|
jv result;
|
|
|
|
while (jv_is_valid(result = jq_next())) {
|
|
|
|
jv_dump(result);
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
jv_free(result);
|
|
|
|
#if JQ_DEBUG
|
|
|
|
printf("end of results\n");
|
|
|
|
#endif
|
|
|
|
jq_teardown();
|
|
|
|
}
|
2012-08-16 01:00:30 +01:00
|
|
|
|
|
|
|
int skipline(const char* buf) {
|
|
|
|
int p = 0;
|
|
|
|
while (buf[p] == ' ' || buf[p] == '\t') p++;
|
|
|
|
if (buf[p] == '#' || buf[p] == '\n' || buf[p] == 0) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_tests() {
|
|
|
|
FILE* testdata = fopen("testdata","r");
|
|
|
|
char buf[4096];
|
|
|
|
int tests = 0, passed = 0;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!fgets(buf, sizeof(buf), testdata)) break;
|
|
|
|
if (skipline(buf)) continue;
|
|
|
|
printf("Testing %s\n", buf);
|
|
|
|
int pass = 1;
|
2012-09-11 12:24:54 +01:00
|
|
|
struct bytecode* bc = jq_compile(buf);
|
|
|
|
assert(bc);
|
2012-08-21 18:14:13 +01:00
|
|
|
printf("Disassembly:\n");
|
|
|
|
dump_disassembly(2, bc);
|
|
|
|
printf("\n");
|
2012-08-16 01:00:30 +01:00
|
|
|
fgets(buf, sizeof(buf), testdata);
|
2012-09-02 16:31:59 +01:00
|
|
|
jv input = jv_parse(buf);
|
2012-09-03 17:26:52 +01:00
|
|
|
assert(jv_is_valid(input));
|
2012-09-02 17:20:13 +01:00
|
|
|
jq_init(bc, input);
|
2012-08-16 01:00:30 +01:00
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), testdata)) {
|
|
|
|
if (skipline(buf)) break;
|
2012-09-02 16:31:59 +01:00
|
|
|
jv expected = jv_parse(buf);
|
2012-09-03 17:26:52 +01:00
|
|
|
assert(jv_is_valid(expected));
|
2012-09-02 17:20:13 +01:00
|
|
|
jv actual = jq_next();
|
2012-09-03 16:16:14 +01:00
|
|
|
if (!jv_is_valid(actual)) {
|
2012-09-10 15:04:19 +01:00
|
|
|
jv_free(actual);
|
2012-08-16 01:00:30 +01:00
|
|
|
printf("Insufficient results\n");
|
|
|
|
pass = 0;
|
|
|
|
break;
|
2012-09-04 20:56:44 +01:00
|
|
|
} else if (!jv_equal(jv_copy(expected), jv_copy(actual))) {
|
2012-08-16 01:00:30 +01:00
|
|
|
printf("Expected ");
|
2012-09-04 20:56:44 +01:00
|
|
|
jv_dump(jv_copy(expected));
|
2012-08-16 01:00:30 +01:00
|
|
|
printf(", but got ");
|
2012-09-04 20:56:44 +01:00
|
|
|
jv_dump(jv_copy(actual));
|
2012-08-16 01:00:30 +01:00
|
|
|
printf("\n");
|
|
|
|
pass = 0;
|
|
|
|
}
|
2012-09-04 20:56:44 +01:00
|
|
|
jv_free(expected);
|
|
|
|
jv_free(actual);
|
2012-08-16 01:00:30 +01:00
|
|
|
}
|
2012-09-03 16:16:14 +01:00
|
|
|
if (pass) {
|
2012-09-02 16:31:59 +01:00
|
|
|
jv extra = jq_next();
|
2012-09-03 16:16:14 +01:00
|
|
|
if (jv_is_valid(extra)) {
|
2012-08-16 01:00:30 +01:00
|
|
|
printf("Superfluous result: ");
|
2012-09-03 16:16:14 +01:00
|
|
|
jv_dump(extra);
|
2012-08-16 01:00:30 +01:00
|
|
|
printf("\n");
|
|
|
|
pass = 0;
|
2012-09-10 15:04:19 +01:00
|
|
|
} else {
|
|
|
|
jv_free(extra);
|
2012-09-03 16:16:14 +01:00
|
|
|
}
|
2012-08-16 01:00:30 +01:00
|
|
|
}
|
2012-09-02 21:45:27 +01:00
|
|
|
jq_teardown();
|
2012-09-02 16:31:59 +01:00
|
|
|
bytecode_free(bc);
|
2012-08-16 01:00:30 +01:00
|
|
|
tests++;
|
|
|
|
passed+=pass;
|
|
|
|
}
|
|
|
|
fclose(testdata);
|
|
|
|
printf("%d of %d tests passed\n", passed,tests);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
if (argc == 1) { run_tests(); return 0; }
|
2012-09-11 12:24:54 +01:00
|
|
|
struct bytecode* bc = jq_compile(argv[1]);
|
|
|
|
if (!bc) return 1;
|
2012-09-02 17:20:13 +01:00
|
|
|
run_program(bc);
|
2012-09-02 22:08:36 +01:00
|
|
|
bytecode_free(bc);
|
2012-09-11 00:04:47 +01:00
|
|
|
return 0;
|
2012-08-16 01:00:30 +01:00
|
|
|
}
|