From 7dbec8d29413d62967ed7eb5854dd56b1a2814ab Mon Sep 17 00:00:00 2001 From: c Date: Sat, 3 Feb 2024 17:34:25 -0500 Subject: Parser now parses blocks and simple calls. --- test/include/test.h | 12 +- test/parser.c | 464 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 464 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/include/test.h b/test/include/test.h index 7366aa2..74642b7 100644 --- a/test/include/test.h +++ b/test/include/test.h @@ -12,23 +12,19 @@ extern unsigned int TESTS_PASSED; #define TEST_INIT unsigned int TESTS_RUN = 0, TESTS_PASSED = 0; -#ifndef __func__ -#define __func__ "" -#endif - #define ASSERT(EXPR) \ TESTS_RUN++; \ if (EXPR && ++TESTS_PASSED) { \ - LOG_INF("%s:%s:%d: Assertion passed!", __FILE__, __func__, __LINE__); \ + LOG_INFF("%s:%s:%d: Assertion passed!", __FILE__, __func__, __LINE__); \ } else { \ - LOG_ERR("%s:%s:%d: Assertion failed:\n\t%s", __FILE__, __func__, __LINE__, #EXPR); \ + LOG_ERRF("Assertion failed:\n\t%s", #EXPR); \ } #define TEST_REPORT \ if (TESTS_RUN == TESTS_PASSED) { \ - LOG_YAY("%s: All %d tests passed!", __FILE__, TESTS_RUN); \ + LOG_YAYF("%s: All %d tests passed!", __FILE__, TESTS_RUN); \ } else { \ - LOG_ERR("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); \ + LOG_ERRF("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); \ } #endif diff --git a/test/parser.c b/test/parser.c index f2fe908..3bceab1 100644 --- a/test/parser.c +++ b/test/parser.c @@ -39,9 +39,11 @@ void empty() { /* Probably not cleaning up fully here. Whatever. */ token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void single_lint() { @@ -82,9 +84,11 @@ void single_lint() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void double_lint() { @@ -134,9 +138,11 @@ void double_lint() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void single_lstr() { @@ -163,7 +169,8 @@ void single_lstr() { tree = tree_init(TREE_TYPE_BLOCK); tree_t* tree0lstr = tree->data.block.val = tree_init(TREE_TYPE_LSTR); - tree0lstr->data.lstr.val = "Hello, World."; + char* str0 = tree0lstr->data.lstr.val = ecalloc(14, sizeof(char)); + strcpy(str0, "Hello, World."); tree0lstr->data.lstr.len = 13; tree->data.block.nxt = NULL; @@ -180,9 +187,11 @@ void single_lstr() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void double_lstr() { @@ -217,11 +226,13 @@ void double_lstr() { tree = tree_init(TREE_TYPE_BLOCK); tree_t* tree0lstr = tree->data.block.val = tree_init(TREE_TYPE_LSTR); - tree0lstr->data.lstr.val = "foo"; + char* str0 = tree0lstr->data.lstr.val = ecalloc(4, sizeof(char)); + strcpy(str0, "foo"); tree0lstr->data.lstr.len = 3; tree_t* tree0block = tree->data.block.nxt = tree_init(TREE_TYPE_BLOCK); tree_t* tree1lstr = tree0block->data.block.val = tree_init(TREE_TYPE_LSTR); - tree1lstr->data.lstr.val = "bar"; + char* str1 = tree1lstr->data.lstr.val = ecalloc(4, sizeof(char)); + strcpy(str1, "bar"); tree1lstr->data.lstr.len = 3; tree0block->data.block.nxt = NULL; @@ -238,9 +249,11 @@ void double_lstr() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void single_block() { @@ -288,9 +301,11 @@ void single_block() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); } void lint_and_block() { @@ -299,7 +314,7 @@ void lint_and_block() { lexer_t* lexer; parser_t* parser; - char src[] = "1;{1}"; + char src[] = "1{1}"; /* @@ -346,9 +361,443 @@ void lint_and_block() { token_destroy(lexer->tokenl); lexer_destroy(lexer); + free(pp->psrc); pp_destroy(pp); tree_destroy(parser->tree); parser_destroy(parser); + tree_destroy(tree); +} + +void double_block() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "{1}{2}"; + + /* + + [block] + val: + [block] + val: + [lint] + val: + 1 + nxt: + NULL + nxt: + [block] + val: + [block] + val: + [lint] + val: + 2 + nxt: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0block = tree->data.block.val = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0lint = tree0block->data.block.val = tree_init(TREE_TYPE_LINT); + tree0lint->data.lint.val = 1; + tree0block->data.block.nxt = NULL; + tree_t* tree1block = tree->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* tree2block = tree1block->data.block.val = tree_init(TREE_TYPE_BLOCK); + tree_t* tree1lint = tree2block->data.block.val = tree_init(TREE_TYPE_LINT); + tree1lint->data.lint.val = 2; + tree2block->data.block.nxt = NULL; + tree1block->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void nested_block() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "{1;2}"; + + /* + + [block] + val: + [block] + val: + [lint] + val: + 1 + nxt: + [block] + val: + [lint] + val: + 2 + nxt: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0block = tree->data.block.val = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0lint = tree0block->data.block.val = tree_init(TREE_TYPE_LINT); + tree0lint->data.lint.val = 1; + tree_t* tree1block = tree0block->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* tree1lint = tree1block->data.block.val = tree_init(TREE_TYPE_LINT); + tree1lint->data.lint.val = 2; + tree1block->data.block.nxt = NULL; + tree->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void superfluous_semicolons() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "{;;;;;\"Hello\";;;;;;;;;;;};;;\"World\";;;;;;;;"; + + /* + + [block] + val: + [block] + val: + [lstr] + val: + Hello + len: + 5 + nxt: + NULL + nxt: + [block] + val: + [lstr] + val: + World + len: + 5 + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0block = tree->data.block.val = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0lstr = tree0block->data.block.val = tree_init(TREE_TYPE_LSTR); + char* str0 = tree0lstr->data.lstr.val = ecalloc(6, sizeof(char)); + strcpy(str0, "Hello"); + tree0lstr->data.lstr.len = 5; + tree_t* tree1block = tree->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* tree1lstr = tree1block->data.block.val = tree_init(TREE_TYPE_LSTR); + char* str1 = tree1lstr->data.lstr.val = ecalloc(6, sizeof(char)); + strcpy(str1, "World"); + tree1lstr->data.lstr.len = 5; + tree1block->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void bare_call() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "f"; + + /* + + [block] + val: + [call] + target: + f + arg: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0call = tree->data.block.val = tree_init(TREE_TYPE_CALL); + char* target0 = tree0call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target0, "f"); + tree0call->data.call.arg = NULL; + tree->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void double_bare_call() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "f;g"; + + /* + + [block] + val: + [call] + target: + f + arg: + NULL + nxt: + [block] + val: + [call] + target: + g + arg: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0call = tree->data.block.val = tree_init(TREE_TYPE_CALL); + char* target0 = tree0call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target0, "f"); + tree0call->data.call.arg = NULL; + tree_t* tree1block = tree->data.block.nxt = tree_init(TREE_TYPE_BLOCK); + tree_t* tree1call = tree1block->data.block.val = tree_init(TREE_TYPE_CALL); + char* target1 = tree1call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target1, "g"); + tree1call->data.call.arg = NULL; + tree1block->data.block.nxt = NULL; + + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void cargumented_call_of_call() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "f.x"; + + /* + + [block] + val: + [call] + target: + f + arg: + [carg] + val: + [call] + target: + x + arg: + NULL + nxt: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0call = tree->data.block.val = tree_init(TREE_TYPE_CALL); + char* target0 = tree0call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target0, "f"); + tree_t* tree0carg = tree0call->data.call.arg = tree_init(TREE_TYPE_CARG); + tree_t* tree1call = tree0carg->data.carg.val = tree_init(TREE_TYPE_CALL); + char* target1 = tree1call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target1, "x"); + tree1call->data.call.arg = NULL; + tree0call->data.carg.nxt = NULL; + tree->data.block.nxt = NULL; + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + tree_print(tree, 0); + tree_print(parser->tree, 0); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); +} + +void cargumented_call_of_lint() { + tree_t* tree; + pp_t* pp; + lexer_t* lexer; + parser_t* parser; + + char src[] = "f.5"; + + /* + + [block] + val: + [call] + target: + f + arg: + [carg] + val: + [lint] + val: + 5 + nxt: + NULL + nxt: + NULL + + */ + + tree = tree_init(TREE_TYPE_BLOCK); + tree_t* tree0call = tree->data.block.val = tree_init(TREE_TYPE_CALL); + char* target0 = tree0call->data.call.target = ecalloc(2, sizeof(char)); + strcpy(target0, "f"); + tree_t* tree0carg = tree0call->data.call.arg = tree_init(TREE_TYPE_CARG); + tree_t* tree0lint = tree0carg->data.carg.val = tree_init(TREE_TYPE_LINT); + tree0lint->data.lint.val = 5; + tree0carg->data.carg.nxt = NULL; + tree->data.block.nxt = NULL; + + pp = pp_init(src); + pp_run(pp); + + lexer = lexer_init(pp->psrc); + lexer_run(lexer); + + parser = parser_init(lexer->tokenl); + parser_run(parser); + + ASSERT(tree_cmp(parser->tree, tree) == 1); + + token_destroy(lexer->tokenl); + lexer_destroy(lexer); + free(pp->psrc); + pp_destroy(pp); + tree_destroy(parser->tree); + parser_destroy(parser); + tree_destroy(tree); } int main() { @@ -359,6 +808,13 @@ int main() { double_lstr(); single_block(); lint_and_block(); + double_block(); + nested_block(); + superfluous_semicolons(); + bare_call(); + double_bare_call(); + cargumented_call_of_call(); + cargumented_call_of_lint(); TEST_REPORT; -- cgit v1.2.3