summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-03-29 16:50:30 +0100
committermetamuffin <metamuffin@disroot.org>2025-03-29 16:50:30 +0100
commit44b4706ea9d7956e0efb9ac684b0184a3693baee (patch)
tree3f9993b93f10d2b6932841669cb4a72b406daac2
parentbcf69099f11a09882309b4590b55c0a3a7d5caba (diff)
downloadattocc-44b4706ea9d7956e0efb9ac684b0184a3693baee.tar
attocc-44b4706ea9d7956e0efb9ac684b0184a3693baee.tar.bz2
attocc-44b4706ea9d7956e0efb9ac684b0184a3693baee.tar.zst
store token positions
-rw-r--r--attocc.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/attocc.c b/attocc.c
index 2d38f95..c81db15 100644
--- a/attocc.c
+++ b/attocc.c
@@ -222,6 +222,7 @@ union token_data {
struct token {
enum token_kind kind;
union token_data data;
+ int position;
};
char is_numeric(char c) { return c >= '0' && c <= '9'; }
@@ -321,6 +322,7 @@ struct token *tokenize(char *source) {
struct token *tokens = NULL;
while (*p) {
unsigned long remaining = end - p;
+ unsigned long position = p - source;
//* whitespace
if (p[0] == ' ' || p[0] == '\t' || p[0] == '\n') {
@@ -358,6 +360,7 @@ struct token *tokenize(char *source) {
return NULL;
new_token->kind = TOK_SEPERATOR;
new_token->data.seperator = i;
+ new_token->position = position;
match = 1;
break;
}
@@ -376,6 +379,7 @@ struct token *tokenize(char *source) {
return NULL;
new_token->kind = TOK_OPERATOR;
new_token->data.operator= i;
+ new_token->position = position;
p += strlen(op);
match = 1;
@@ -397,6 +401,7 @@ struct token *tokenize(char *source) {
return NULL;
new_token->kind = TOK_KEYWORD;
new_token->data.keyword = i;
+ new_token->position = position;
p += strlen(kw);
match = 1;
@@ -436,6 +441,7 @@ struct token *tokenize(char *source) {
new_token->kind = TOK_CONSTANT;
new_token->data.constant_kind = CONST_INT;
new_token->data.constant_int_value = value;
+ new_token->position = position;
continue;
}
@@ -459,6 +465,7 @@ struct token *tokenize(char *source) {
new_token->kind = TOK_CONSTANT;
new_token->data.constant_kind = CONST_STR;
new_token->data.constant_str_value = str;
+ new_token->position = position;
continue;
}
@@ -481,6 +488,7 @@ struct token *tokenize(char *source) {
new_token->kind = TOK_CONSTANT;
new_token->data.constant_kind = CONST_CHAR;
new_token->data.constant_char_value = chr;
+ new_token->position = position;
continue;
}
@@ -499,7 +507,7 @@ struct token *tokenize(char *source) {
struct token *new_token = token_push(&tokens, &num_tokens);
new_token->kind = TOK_IDENTIFIER;
new_token->data.identifier = ident_str;
-
+ new_token->position = position;
continue;
}
@@ -509,7 +517,7 @@ struct token *tokenize(char *source) {
struct token *new_token = token_push(&tokens, &num_tokens);
new_token->kind = TOK_END;
-
+ new_token->position = 0;
return tokens;
}
@@ -537,6 +545,7 @@ void free_tokens(struct token *tokens) {
#ifdef DEBUG
void debug_tokens(struct token *tokens) {
for (int i = 0; tokens[i].kind != TOK_END; i++) {
+ printf("%4lu ", tokens[i].position);
switch (tokens[i].kind) {
case TOK_IDENTIFIER:
printf("TOK_IDENTIFIER:%s, ", tokens[i].data.identifier);