From 5b891011de8beaf22a854a184d9739ab63fcd7d2 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Thu, 2 Aug 2018 00:58:55 +1200 Subject: parse: Change emit to func, misc tidy --- parse.c | 50 +++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/parse.c b/parse.c index b8fcb92..5bd56e3 100644 --- a/parse.c +++ b/parse.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "lex.h" #include "common.h" @@ -9,11 +10,15 @@ #include "logic.h" #include "gate.h" +/** Static defs **************************************************************/ static FILE* fd; static struct token *cursor; static const char *filename; + +/** Helpers ******************************************************************/ + #define EXPECT_AND_DISCARD_CRITICAL(type)\ do { \ EXPECT_CRITICAL(type) \ @@ -27,35 +32,20 @@ static const char *filename; } \ } while (0); -/* FIXME change to varadic fuction */ -#define emit(...) \ - if (cursor) { \ - fprintf(stderr, "%s at (%zd,%zd): ", filename, cursor->loc.line, cursor->loc.column); \ - fprintf(stderr, __VA_ARGS__); \ - indicate_file_area(fd, cursor->loc.line, cursor->loc.column - cursor->loc.leading_whitespace_len, cursor->span); \ - } else { \ - fprintf(stderr, "%s: ", filename); \ - fprintf(stderr, __VA_ARGS__); \ - } - - -//static struct op_lookup uop_handlers[] = { -// {.str = "not", .handler = logic_nand}, -//}; -// -//static struct op_lookup bop_handlers[] = { -// {.str = "and", .handler = logic_and}, -// {.str = "or", .handler = logic_or}, -// {.str = "nand", .handler = logic_nand}, -// {.str = "nor", .handler = logic_nor}, -// {.str = "xor", .handler = logic_xor} -//}; -// -//static struct tok_lookup tok_handlers[] = { -// {.str = "input", .handler = parse_input}, -// {.str = "module", .handler = parse_module}, -// {.str = "expr", .handler = parse_expr} -//}; +void +emit(const char *fmt, ...) { + va_list args; + va_start(args, fmt); + if (cursor) { + fprintf(stderr, "%s at (%zd,%zd): ", filename, cursor->loc.line, cursor->loc.column); + vfprintf(stderr, fmt, args); + indicate_file_area(fd, cursor->loc.line, cursor->loc.column - cursor->loc.leading_whitespace_len, cursor->span); + } else { + fprintf(stderr, "%s: ", filename); + vfprintf(stderr, fmt, args); + } + va_end(args); +} static int expect(enum TOKEN_TYPE e) { @@ -83,6 +73,8 @@ kerchunk() { } } +/** Parsers ******************************************************************/ + int parse_expr(void) { /* FIXME write wrapper to exit on fail */ -- cgit v1.1