commit e9015e47939be0e2606d416653abdab37fa7bc66
parent df67a9c8f9fc64c7cbcf2250a07fc7a95b1de740
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Thu, 9 Dec 2021 20:17:30 -0600
map tokens to strings, improve error message
Diffstat:
2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/main.c b/main.c
@@ -19,6 +19,27 @@
#include "lex.h"
#include "parse.h"
+const char const *tokenstr[] = {
+ [TOK_NONE] = "TOK_NONE",
+ [TOK_NAME] = "TOK_NAME",
+ [TOK_LPAREN] = "TOK_LPAREN",
+ [TOK_RPAREN] = "TOK_RPAREN",
+ [TOK_LCURLY] = "TOK_LCURLY",
+ [TOK_RCURLY] = "TOK_RCURLY",
+ [TOK_PLUS] = "TOK_PLUS",
+ [TOK_MINUS] = "TOK_MINUS",
+ [TOK_GREATER] = "TOK_GREATER",
+ [TOK_COMMA] = "TOK_COMMA",
+ [TOK_EQUAL] = "TOK_EQUAL",
+ [TOK_NUM] = "TOK_NUM",
+ [TOK_STRING] = "TOK_STRING",
+ [TOK_LET] = "TOK_LET",
+ [TOK_IF] = "TOK_IF",
+ [TOK_ELSE] = "TOK_ELSE",
+ [TOK_LOOP] = "TOK_LOOP",
+ [TOK_RETURN] = "TOK_RETURN",
+};
+
struct decls decls;
struct assgns assgns;
struct exprs exprs;
diff --git a/parse.c b/parse.c
@@ -9,6 +9,8 @@
#include "util.h"
#include "array.h"
+extern const char const *tokenstr[];
+
extern struct decls decls;
extern struct assgns assgns;
extern struct exprs exprs;
@@ -34,7 +36,7 @@ expect(enum tokentype type)
if (!tok)
error(tok->line, tok->col, "unexpected null token!");
if (tok->type != type) {
- error(tok->line, tok->col, "mismatch");
+ error(tok->line, tok->col, "expected %s but got %s", tokenstr[type], tokenstr[tok->type]);
}
}