summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kdx@42l.fr>2023-01-20 05:13:56 +0100
committerkdx <kdx@42l.fr>2023-01-20 05:14:14 +0100
commitcca86dc829074e51da2cebcf9e7bb75ef9d3e544 (patch)
tree39174001b501c344ad2698a76d644eeb2a0c8b4e
parent3ab936fdda8bd218560c0839229be05cd8c4673e (diff)
downloadgolem-cca86dc829074e51da2cebcf9e7bb75ef9d3e544.tar.gz
glorious error
-rw-r--r--Token.c13
-rw-r--r--Token.h2
-rw-r--r--lexer.c21
-rw-r--r--main.c6
-rw-r--r--parse.c5
5 files changed, 24 insertions, 23 deletions
diff --git a/Token.c b/Token.c
index 85d65cf..d23e885 100644
--- a/Token.c
+++ b/Token.c
@@ -4,21 +4,16 @@
void token_free(Token *tok)
{
- switch (tok->type) {
- case TOK_WORD:
- if (tok != NULL && tok->v.s != NULL)
- free(tok->v.s);
- default:
- break;
- }
+ if (tok != NULL && tok->s != NULL)
+ free(tok->s);
}
void token_print(const Token *tok)
{
printf("%u:%u\t", tok->line, tok->column);
switch (tok->type) {
- case TOK_WORD: printf("TOK_WORD %s\n", tok->v.s); break;
- case TOK_STRING: printf("TOK_STRING \"%s\"\n", tok->v.s); break;
+ case TOK_WORD: printf("TOK_WORD %s\n", tok->s); break;
+ case TOK_STRING: printf("TOK_STRING \"%s\"\n", tok->s); break;
case TOK_INTEGER: printf("TOK_INTEGER %d\n", tok->v.i); break;
case TOK_PAREN_OPEN: printf("TOK_PAREN_OPEN\n"); break;
case TOK_PAREN_CLOS: printf("TOK_PAREN_CLOS\n"); break;
diff --git a/Token.h b/Token.h
index 1ff528a..8f20b2e 100644
--- a/Token.h
+++ b/Token.h
@@ -31,7 +31,6 @@ enum {
};
union TokenValue {
- char *s;
char c;
int i;
double d;
@@ -43,6 +42,7 @@ typedef struct Token {
unsigned int line;
unsigned int column;
union TokenValue v;
+ char *s;
} Token;
void token_free(Token *tok);
diff --git a/lexer.c b/lexer.c
index c42ac3b..e55f97e 100644
--- a/lexer.c
+++ b/lexer.c
@@ -81,28 +81,35 @@ Token *lexer(const char *s)
return NULL;
}
size_t len = end - s - 1;
- toks[tok_i].v.s = calloc(1, len + 1);
- if (toks[tok_i].v.s == NULL) {
+ toks[tok_i].s = calloc(1, len + 1);
+ if (toks[tok_i].s == NULL) {
perror("lexer");
lexer_free(toks);
return NULL;
}
toks[tok_i].type = TOK_STRING;
- strncpy(toks[tok_i].v.s, s + 1, len);
+ strncpy(toks[tok_i].s, s + 1, len);
tok_i += 1;
s = end + 1;
} else if (isalpha(*s) || *s == '_') {
size_t len = 0;
while (isalnum(s[len]) || s[len] == '_')
len += 1;
- toks[tok_i].v.s = calloc(1, len + 1);
- if (toks[tok_i].v.s == NULL) {
+ toks[tok_i].s = calloc(1, len + 1);
+ if (toks[tok_i].s == NULL) {
perror("lexer");
lexer_free(toks);
return NULL;
}
- strncpy(toks[tok_i].v.s, s, len);
- toks[tok_i].type = TOK_WORD;
+ strncpy(toks[tok_i].s, s, len);
+ if (strcmp("fn", toks[tok_i].s) == 0)
+ toks[tok_i].type = TOK_KW_FN;
+ else if (strcmp("var", toks[tok_i].s) == 0)
+ toks[tok_i].type = TOK_KW_VAR;
+ else if (strcmp("const", toks[tok_i].s) == 0)
+ toks[tok_i].type = TOK_KW_CONST;
+ else
+ toks[tok_i].type = TOK_WORD;
tok_i += 1;
s += len;
} else if (isdigit(*s)) {
diff --git a/main.c b/main.c
index 8cca483..3d2cd3a 100644
--- a/main.c
+++ b/main.c
@@ -28,11 +28,7 @@ int main(int argc, char **argv)
fprintf(stderr, "lexer failed\n");
return 1;
}
- lexer_print(toks);
- if (parse(toks)) {
- fprintf(stderr, "parser failed\n");
- return 1;
- }
+ parse(toks);
lexer_free(toks);
return 0;
}
diff --git a/parse.c b/parse.c
index b1b5c93..609e8a6 100644
--- a/parse.c
+++ b/parse.c
@@ -10,8 +10,11 @@ int parse(const Token *tok)
case TOK_KW_CONST:
break;
default:
- fprintf(stderr, "unexpected %s at %u:%u\n",
+ fprintf(stderr, "unexpected %s %s%s%sat %u:%u\n",
token_type_str(tok->type),
+ (tok->s != NULL) ? "(" : "",
+ (tok->s != NULL) ? tok->s : "",
+ (tok->s != NULL) ? ") " : "",
tok->line, tok->column);
return 1;
}