summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-06-12 13:05:55 +0200
committerkdx <kikoodx@paranoici.org>2023-06-12 13:05:55 +0200
commitc26a76a39a93656b547886acbc96147fe779cffc (patch)
tree557b48546ad02a4705cf1c035be8cf8c384c2bab /src
parentacb633cc836a3068b597c89d4f160c5ab6e22fa7 (diff)
downloadgolem-c26a76a39a93656b547886acbc96147fe779cffc.tar.gz
🗿
Diffstat (limited to 'src')
-rw-r--r--src/main.c38
1 files changed, 24 insertions, 14 deletions
diff --git a/src/main.c b/src/main.c
index 4c8ace0..9951685 100644
--- a/src/main.c
+++ b/src/main.c
@@ -32,8 +32,8 @@ struct Token {
};
static void
-error(const char *fmt, ...) {
- fprintf(stderr, "error: ");
+_error(const char *fmt, ...) {
+ fprintf(stderr, "error:");
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
@@ -42,6 +42,8 @@ error(const char *fmt, ...) {
exit(1);
}
+#define error(fmt, ...) _error("%d: " fmt, __LINE__, __VA_ARGS__);
+
static void
expect(const Token *tok, TokenType type)
{
@@ -76,7 +78,7 @@ new_token(TokenType type, char *start, char *end)
{
Token *tok = calloc(1, sizeof(Token));
if (tok == NULL)
- error("calloc failed");
+ error("calloc failed", 0);
tok->type = type;
tok->loc = start;
tok->len = end - start;
@@ -99,6 +101,12 @@ tokenize(char *p)
Token *cur = &head;
while (*p != '\0') {
+ if (*p == '/' && p[1] == '/') {
+ while (*p != '\0' && *p != '\n')
+ p++;
+ continue;
+ }
+
if (isspace(*p)) {
p++;
continue;
@@ -115,7 +123,7 @@ tokenize(char *p)
if (*p == '"') {
char *end = strchr(p + 1, '"');
if (end == NULL)
- error("unclosed double quotes");
+ error("unclosed double quotes", 0);
cur = cur->next = new_token(TOK_STRING, p + 1, end);
p = end + 1;
continue;
@@ -256,7 +264,7 @@ new_node(NodeType type)
{
Node *const node = calloc(1, sizeof(Node));
if (node == NULL)
- error("calloc failed");
+ error("calloc failed", 0);
node->type = type;
return node;
}
@@ -595,8 +603,10 @@ primary(Token **rest, Token *tok)
return node;
}
- printf("'%.*s'\n", tok->len, tok->loc);
- error("expected an expression");
+ fprintf(stderr, "'%.*s'\n", tok->len, tok->loc);
+ if (tok->next != NULL)
+ fprintf(stderr, "'%.*s'\n", tok->next->len, tok->next->loc);
+ error("expected an expression", 0);
return NULL;
}
@@ -628,7 +638,7 @@ stmt(Token **rest, Token *tok)
cur = cur->next;
}
if (cur->type == TOK_EOF)
- error("unexpected EOF");
+ error("unexpected EOF", 0);
if (equal(cur, "="))
return assign_stmt(rest, tok);
}
@@ -947,7 +957,7 @@ gen_stmt(Node *node, Node *fname, int break_lbl)
break;
case NOD_BREAK_STMT:
if (break_lbl == -1)
- error("break statement outside of loop");
+ error("break statement outside of loop", 0);
printf("\tJMP ,__lbl_%x\n", break_lbl);
break;
case NOD_SLP_STMT:
@@ -1104,15 +1114,15 @@ static char *
drain(FILE *fp)
{
if (fseek(fp, 0, SEEK_END) < 0)
- error("fseek failed");
+ error("fseek failed", 0);
const size_t size = ftell(fp);
if (fseek(fp, 0, SEEK_SET) < 0)
- error("fseek failed");
+ error("fseek failed", 0);
char *data = malloc(size + 1);
if (data == NULL)
- error("malloc failed");
+ error("malloc failed", 0);
if (fread(data, 1, size, fp) != size)
- error("fread failed");
+ error("fread failed", 0);
data[size] = '\0';
return data;
}
@@ -1125,7 +1135,7 @@ main(int argc, char **argv)
FILE *const fp = fopen(argv[1], "rb");
if (fp == NULL)
- error("fopen failed");
+ error("fopen failed", 0);
Token *const tok = tokenize(drain(fp));
fclose(fp);