From 70c76b708308c72956e83163b2819be69f725a7e Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 1 Aug 2018 02:18:41 +1200 Subject: Change emit to be general, add filename to output --- parser.c | 21 +++++++++------------ parser.h | 2 +- simulator.c | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/parser.c b/parser.c index 987f26d..618df1a 100644 --- a/parser.c +++ b/parser.c @@ -12,15 +12,11 @@ static FILE* fd; static struct token *cursor; +static const char *filename; -#ifdef emit_error -#warning "Remember to remove the global emit_error" -#undef emit_error -#endif /* ifdef emit_error */ - -#define emit_error(...) fprintf(stderr, "Error (%zd,%zd): ", cursor->loc.line, cursor->loc.column);\ - fprintf(stderr, __VA_ARGS__);\ - indicate_file_area(fd, cursor->loc.line, cursor->loc.column, cursor->span) +#define emit(...) 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->span) //static struct op_lookup uop_handlers[] = { @@ -49,7 +45,7 @@ expect(enum TOKEN_TYPE e) { if (cursor->type != e) { expected_desc = get_token_description(e); observed_desc = get_token_description(cursor->type); - emit_error("Expected %s, got %s\n", expected_desc, observed_desc); + emit("Error: Expected %s, got %s\n", expected_desc, observed_desc); return 1; } @@ -62,9 +58,10 @@ kerchunk() { } int -parse(FILE *f, struct token *t) { +parse(const char *fname, FILE *f, struct token *t) { char *token_desc = "(internal error)"; fd = f; + filename = fname; cursor = t; if (expect(TOK_MODULE)) { @@ -75,7 +72,7 @@ parse(FILE *f, struct token *t) { if (expect(TOK_IDENT)) { return 1; } - emit_error("Debug: module is named %s\n", cursor->value); + emit("Debug: module is named %s\n", cursor->value); kerchunk(); if (expect(TOK_EOL)) { @@ -91,7 +88,7 @@ parse(FILE *f, struct token *t) { /* FIXME implement everything */ default: token_desc = get_token_description(cursor->type); - emit_error("Unexpected %s\n", token_desc); + emit("Error: Unexpected %s\n", token_desc); return 1; } } diff --git a/parser.h b/parser.h index 1446650..6e28dbf 100644 --- a/parser.h +++ b/parser.h @@ -1 +1 @@ -int parse(FILE*, struct token *); +int parse(const char*, FILE*, struct token *); diff --git a/simulator.c b/simulator.c index bbea893..1b33aec 100644 --- a/simulator.c +++ b/simulator.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) { } struct token *tok = lex_file(fd); - int p = parse(fd, tok); + int p = parse(argv[1], fd, tok); // gate_set_input("a", LOGIC_LOW); // gate_set_input("b", LOGIC_LOW); -- cgit v1.1