diff options
Diffstat (limited to 'attocc.c')
-rw-r--r-- | attocc.c | 13 |
1 files changed, 11 insertions, 2 deletions
@@ -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); |