From: NeilBrown Date: Sun, 26 May 2019 05:03:13 +0000 (+1000) Subject: parsergen: make it easier to test the simple 'calc' code. X-Git-Url: https://ocean-lang.org/code/?p=ocean;a=commitdiff_plain;h=1330688ecf4b573ec82fc21309113dc5c35be75c parsergen: make it easier to test the simple 'calc' code. Allow 'calc' to read a test code from parsergen.mdc. Signed-off-by: NeilBrown --- diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index 3c816fa..1d9d611 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -3009,6 +3009,9 @@ an error. ./parsergen --tag calc -o calc parsergen.mdc calc : calc.o libparser.o libscanner.o libmdcode.o libnumber.o $(CC) $(CFLAGS) -o calc calc.o libparser.o libscanner.o libmdcode.o libnumber.o -licuuc -lgmp + calctest : calc + ./calc parsergen.mdc + tests :: calctest # calc: header @@ -3029,6 +3032,7 @@ an error. #include #include #include + #include #include "mdcode.h" #include "scanner.h" #include "number.h" @@ -3042,12 +3046,19 @@ an error. free(n); } + static int text_is(struct text t, char *s) + { + return (strlen(s) == t.len && + strncmp(s, t.txt, t.len) == 0); + } + int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDONLY); int len = lseek(fd, 0, 2); char *file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0); - struct section *s = code_extract(file, file+len, NULL); + struct section *table = code_extract(file, file+len, NULL); + struct section *s; struct token_config config = { .ignored = (1 << TK_line_comment) | (1 << TK_block_comment) @@ -3057,12 +3068,14 @@ an error. .word_start = "", .word_cont = "", }; - parse_calc(s->code, &config, argc > 2 ? stderr : NULL); - while (s) { - struct section *t = s->next; - code_free(s->code); - free(s); - s = t; + for (s = table; s; s = s->next) + if (text_is(s->section, "example: input")) + parse_calc(s->code, &config, argc > 2 ? stderr : NULL); + while (table) { + struct section *t = table->next; + code_free(table->code); + free(table); + table = t; } exit(0); } @@ -3100,3 +3113,14 @@ an error. | Expression / Expression ${ mpq_init($0.val); mpq_div($0.val, $1.val, $3.val); }$ | NUMBER ${ if (number_parse($0.val, $0.tail, $1.txt) == 0) mpq_init($0.val); }$ | ( Expression ) ${ mpq_init($0.val); mpq_set($0.val, $2.val); }$ + +# example: input + + 355/113 + 3.1415926535 - 355/113 + 2 + 4 * 5 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 * 9 / 2 + 1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1 + + error