diff options
author | c | 2023-12-13 14:49:19 -0500 |
---|---|---|
committer | c | 2023-12-13 14:49:19 -0500 |
commit | 6a966d621c48118ab1a97db13473b78b2884ace1 (patch) | |
tree | f0607da8284f4081970325c37eb7ea409de68a9f /src | |
parent | d8fcdcc5491ae053ebf0586a8093cda6781af51a (diff) |
Added tests again.
Diffstat (limited to 'src')
-rw-r--r-- | src/doer.c | 66 | ||||
-rw-r--r-- | src/include/test.h | 26 | ||||
-rw-r--r-- | src/include/util.h | 2 | ||||
-rw-r--r-- | src/main.c | 6 | ||||
-rw-r--r-- | src/parser.c | 26 | ||||
-rw-r--r-- | src/util.c | 29 |
6 files changed, 134 insertions, 21 deletions
diff --git a/src/doer.c b/src/doer.c new file mode 100644 index 0000000..21a1045 --- /dev/null +++ b/src/doer.c @@ -0,0 +1,66 @@ +#include "include/doer.h" + + +/* Creates a new doer from a tree. */ +doer_t* doer_init(tree_t* tree) { + doer_t* doer; + + doer = emalloc(sizeof(doer_t)); + + doer->tree = tree; + + return doer; +} + +/* + Destroys a doer. + - Does not destroy the `tree`. +*/ +void doer_destroy(doer_t* doer) { + free(doer); +} + + +void doer_do_blin_print(char* s) { + +} + +void doer_do_block(tree_t* tree) { + tree->data.block.val +} + +void doer_do_expr(tree_t* tree) { + /* For now, assume the only expression that exists is a call to print(). */ + + + +} + +void doer_do_lint(tree_t* tree) { + +} + +void doer_do_lstr(tree_t* tree) { + +} + +void doer_do_tag(tree_t* tree) { + +} + +void doer_do_darg(tree_t* tree) { + +} + +void doer_do_carg(tree_t* tree) { + +} + +void doer_do_def(tree_t* tree) { + +} + +void doer_do_call(tree_t* tree) { + +} + diff --git a/src/include/test.h b/src/include/test.h new file mode 100644 index 0000000..0b60add --- /dev/null +++ b/src/include/test.h @@ -0,0 +1,26 @@ +#ifndef TEST_H +#define TEST_H + +#include "util.h" + +#ifdef TEST + +unsigned int TESTS_RUN = 0; +unsigned int TESTS_PASSED = 0; + +#define ASSERT(EXPR) \ + TESTS_RUN++; \ + (EXPR && ++TESTS_PASSED) ? \ + log_yay("Assertion passed!") : \ + log_err("%s:%d: Assertion failed:\n\t%s", __FILE__, __LINE__, #EXPR); + +#define TEST_REPORT \ + (TESTS_RUN == TESTS_PASSED) ? \ + log_yay("All %d tests passed!", TESTS_RUN) : \ + log_err("%d/%d tests failed.", TESTS_RUN - TESTS_PASSED, TESTS_RUN); + +#else +#define ASSERT(EXPR) NULL; +#endif + +#endif diff --git a/src/include/util.h b/src/include/util.h index cf59a44..15f862e 100644 --- a/src/include/util.h +++ b/src/include/util.h @@ -8,6 +8,8 @@ /* Log some debug information. */ void log_dbg(const char*, ...); +/* Log some congratulatory information. */ +void log_yay(const char*, ...); /* Log some information. */ void log_inf(const char*, ...); /* Log something with no formatting. */ @@ -1,6 +1,8 @@ #include <stdio.h> #include <stdlib.h> +#include "include/test.h" + #include "include/util.h" #include "include/source.h" #include "include/token.h" @@ -19,6 +21,8 @@ int main(int argc, char* argv[]) { log_dbg("source gotten"); log_inf("source: %s", src); + ASSERT(src); + /* create pre-processor */ pp = pp_init(src); log_dbg("preprocessor created"); @@ -52,5 +56,7 @@ int main(int argc, char* argv[]) { parser_destroy(parser); free(src); + TEST_REPORT; + return 0; } diff --git a/src/parser.c b/src/parser.c index ac4dfa6..09ce7bc 100644 --- a/src/parser.c +++ b/src/parser.c @@ -79,6 +79,7 @@ tree_t* parser_parse_expr(parser_t* parser) { expr->data.expr.val = parser_parse_block(parser); break; default: + expr->data.lstr.val = "???"; /* TODO: Add an "unknown" token type. */ return expr; } @@ -87,9 +88,9 @@ tree_t* parser_parse_expr(parser_t* parser) { tree_t* parser_parse_block(parser_t* parser) { if ( - !parser->token || - parser->token->type == TOKEN_EXPR_END || - parser->token->type == TOKEN_RBLOCK + ! parser->token || + parser->token->type == TOKEN_EXPR_END || + parser->token->type == TOKEN_RBLOCK ) { return NULL; } tree_t* block; @@ -97,10 +98,9 @@ tree_t* parser_parse_block(parser_t* parser) { block = tree_init(TREE_TYPE_BLOCK); block->data.block.val = parser_parse_expr(parser); - block->data.block.nxt = parser_nxt_token_match(parser, TOKEN_EXPR_END) && - parser_nxt_token(parser) ? - parser_parse_block(parser) : - NULL; + block->data.block.nxt = parser_nxt_token(parser) ? + parser_parse_block(parser) : + NULL; return block; } @@ -169,10 +169,10 @@ tree_t* parser_parse_carg(parser_t* parser) { carg->data.carg.val = parser_parse_expr(parser); carg->data.carg.nxt = ( parser_nxt_token_match(parser, TOKEN_LIST_DELIM) && - parser_nxt_token(parser) ? - parser_parse_carg(parser) : - NULL - ); + parser_nxt_token(parser) + ) ? + parser_parse_carg(parser) : + NULL; return carg; } @@ -184,10 +184,8 @@ tree_t* parser_parse_call(parser_t* parser) { call->data.call.target = parser->token->val; parser->token->val = NULL; - parser_nxt_token_match(parser, TOKEN_APPLY) || - (call->data.call.arg = NULL); call->data.call.arg = ( - parser_nxt_token(parser) ? + parser_nxt_token_match(parser, TOKEN_APPLY) && parser_nxt_token(parser) ? parser_parse_carg(parser) : NULL ); @@ -3,7 +3,8 @@ void log_dbg(const char* fmt, ...) { va_list ap; - fprintf(stdout, "\e[3m\e[37m[==] "); + fprintf(stdout, ""); + fprintf(stderr, "\x1b[37m[\x1b[95;1m==\x1b[0m\x1b[37m]\x1b[0m\x1b[35m "); va_start(ap, fmt); vfprintf(stdout, fmt, ap); @@ -12,10 +13,22 @@ void log_dbg(const char* fmt, ...) { fprintf(stdout, "\e[0m\n"); } +void log_yay(const char* fmt, ...) { + va_list ap; + + fprintf(stdout, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); + + va_start(ap, fmt); + vfprintf(stdout, fmt, ap); + va_end(ap); + + fprintf(stdout, "\x1b[0m\n"); +} + void log_inf(const char* fmt, ...) { va_list ap; - fprintf(stdout, "[\e[34m==\e[0m] "); + fprintf(stderr, "\x1b[37m[\x1b[94;1m==\x1b[0m\x1b[37m]\x1b[0m "); va_start(ap, fmt); vfprintf(stdout, fmt, ap); @@ -35,7 +48,7 @@ void log_raw(const char* fmt, ...) { void log_war(const char* fmt, ...) { va_list ap; - fprintf(stderr, "[\e[33m==\e[0m] WARNING "); + fprintf(stderr, "\x1b[37m[\x1b[93;1m==\x1b[0m\x1b[37m]\x1b[93;1m WARNING:\x1b[0m\x1b[33m "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); @@ -47,24 +60,26 @@ void log_war(const char* fmt, ...) { void log_err(const char* fmt, ...) { va_list ap; - fprintf(stderr, "[\e[31m==\e[0m] ERROR "); + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m ERROR:\x1b[0m\x1b[31m "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, "\n"); + fprintf(stderr, "\x1b[0m\n"); } void die(const char* fmt, ...) { va_list ap; - fprintf(stderr, "[\e[31;1m==\e[0m] FATAL ERROR "); + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m CAUSE OF DEATH:\x1b[0m\x1b[31m "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); - fprintf(stderr, "\n"); + fprintf(stderr, "\x1b[0m\n"); + + fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m Exiting...\x1b[0m\n"); exit(1); } |