summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-16 07:38:28 +0200
committerkdx <kikoodx@paranoici.org>2023-06-16 07:38:28 +0200
commita7076569d5935e493a5e04a0693a0180b047ad21 (patch)
treee2686d6f555a04874a89e1db735a443f51ea7f5d
parent6458a1a6c483f82c2499e867b553ec92efee26c4 (diff)
downloadgolem-a7076569d5935e493a5e04a0693a0180b047ad21.tar.gz
prettier error
-rw-r--r--src/main.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/main.c b/src/main.c
index 4aa53da..0197bbc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -91,7 +91,7 @@ error(const char *fmt, ...) {
}
static void
-error_loc(const char *loc, const char *fmt, ...) {
+error_loc(const char *loc, int len, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
verror(fmt, va);
@@ -113,7 +113,10 @@ error_loc(const char *loc, const char *fmt, ...) {
spaces += 1 + 7 * (line[i] == '\t');
fprintf(stderr, "%*s", spaces, "");
- fprintf(stderr, ANSI_RED "^" ANSI_RESET "\n");
+ fprintf(stderr, ANSI_RED "^");
+ for (int i = 1; i < len; i++)
+ fprintf(stderr, "~");
+ fprintf(stderr, ANSI_RESET "\n");
exit(1);
}
@@ -122,7 +125,7 @@ static void
expect(const Token *tok, TokenType type)
{
if (tok->type != type)
- error_loc(tok->loc, "expected a %s, got %s '%.*s'",
+ error_loc(tok->loc, tok->len, "expected a %s, got %s '%.*s'",
str_tok[type], str_tok[tok->type], tok->len, tok->loc);
}
@@ -136,7 +139,7 @@ static Token *
skip(Token *tok, char *s)
{
if (!equal(tok, s))
- error_loc(tok->loc, "expected '%s'; got '%.*s'", s, tok->len,
+ error_loc(tok->loc, tok->len, "expected '%s'; got '%.*s'", s, tok->len,
tok->loc);
return tok->next;
}
@@ -232,7 +235,7 @@ tokenize(char *p)
if (*p == '"') {
char *end = strchr(p + 1, '"');
if (end == NULL)
- error_loc(p, "unclosed double quotes");
+ error_loc(p, 1, "unclosed double quotes");
cur = cur->next = new_token(TOK_STRING, p, end + 1);
p = end + 1;
continue;
@@ -242,7 +245,7 @@ tokenize(char *p)
char *q = p;
const int c = escaped_char(p + 1, &p);
if (*p != '\'')
- error_loc(p, "unclosed single quotes");
+ error_loc(p, 1, "unclosed single quotes");
p += 1;
cur = cur->next = new_token(TOK_NUM, q, p);
cur->val = (unsigned char)c;
@@ -264,7 +267,7 @@ tokenize(char *p)
continue;
}
- error_loc(p, "invalid token '%c'", *p);
+ error_loc(p, 1, "invalid token '%c'", *p);
}
cur->next = new_token(TOK_EOF, p, p);
@@ -726,7 +729,7 @@ primary(Token **rest, Token *tok)
return node;
}
- error_loc(tok->loc, "expected an expression");
+ error_loc(tok->loc, tok->len, "expected an expression");
return NULL;
}
@@ -994,7 +997,7 @@ gen_expr(Node *node)
}
if (op == NULL)
- error_loc(node->loc, "invalid expression %d", node->type);
+ error_loc(node->loc, node->len, "invalid expression %d", node->type);
printf("\t%s\n", op);
}
@@ -1039,7 +1042,7 @@ get_define(Node *node)
const int define_idx = node_find(defines, node);
if (define_idx != -1)
return const_expr(node_get(defines, define_idx)->rhs);
- error_loc(node->loc, "define '%.*s' doesn't exist", node->len,
+ error_loc(node->loc, node->len, "define '%.*s' doesn't exist", node->len,
node->loc);
return 0;
}
@@ -1073,8 +1076,7 @@ const_expr(Node *node)
default: break;
}
- error_loc(node->loc, "const expr doesn't support %d expression",
- node->type);
+ error_loc(node->loc, node->len, "const expr doesn't support %d expression", node->type);
return 0;
}
@@ -1217,7 +1219,7 @@ gen_stmt(Node *node, Node *fname, int break_lbl)
break;
case NOD_BREAK_STMT:
if (break_lbl == -1)
- error_loc(node->loc, "break statement outside of loop");
+ error_loc(node->loc, node->len, "break statement outside of loop");
printf("\tJMP ,__lbl_%x\n", break_lbl);
break;
case NOD_SLP_STMT:
@@ -1270,7 +1272,7 @@ gen_stmt(Node *node, Node *fname, int break_lbl)
printf("@__lbl_%x\n", lbl + 1);
break;
default:
- error_loc(node->loc, "unexpected %d", node->type);
+ error_loc(node->loc, node->len, "unexpected %d", node->type);
}
}
@@ -1350,7 +1352,7 @@ codegen(Node *node)
char *q = strings->lhs->loc + k;
const int c = escaped_char(q, &q);
if (c == '"') /* TODO: handle this */
- error_loc(q, "can't escape double quotes", 0);
+ error_loc(q - 2, 2, "can't escape double quotes", 0);
printf("\t%04x\n", (unsigned)c);
k = q - strings->lhs->loc;
}