summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-06-15 13:36:09 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-15 13:36:09 +0200
commitab8ec0f137b17be65369e05383f3471f901bb7c3 (patch)
treed38da84ad6e576b84cecc4ee3f1655d1537fc583
parente6483e3bec7630ad5c27dbbe04681e61b0791fe4 (diff)
downloadattocc-ab8ec0f137b17be65369e05383f3471f901bb7c3.tar
attocc-ab8ec0f137b17be65369e05383f3471f901bb7c3.tar.bz2
attocc-ab8ec0f137b17be65369e05383f3471f901bb7c3.tar.zst
bug with idents
-rw-r--r--attocc.c136
-rw-r--r--makefile6
2 files changed, 86 insertions, 56 deletions
diff --git a/attocc.c b/attocc.c
index 49ca7a2..b9a1ff9 100644
--- a/attocc.c
+++ b/attocc.c
@@ -242,11 +242,23 @@ char map_escape(char c) {
return '\'';
case '"':
return '"';
+ case '0':
+ return '\0';
default:
return '\0';
}
}
+struct token *token_push(struct token **tokens, unsigned long *num_tokens) {
+ *num_tokens += 1;
+ *tokens = realloc(*tokens, sizeof(struct token) * *num_tokens);
+ if (!tokens) {
+ fprintf(stderr, "realloc failed\n");
+ return NULL;
+ }
+ return &(*tokens)[*num_tokens - 1];
+}
+
struct token *tokenize(char *source) {
char *end = source + strlen(source);
char *p = source;
@@ -286,13 +298,9 @@ struct token *tokenize(char *source) {
for (int i = 0; SEPERATORS[i]; i++) {
if (p[0] == SEPERATORS[i]) {
p++;
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_SEPERATOR;
new_token->data.seperator = i;
match = 1;
@@ -308,13 +316,9 @@ struct token *tokenize(char *source) {
char *op = OPERATORS[i];
if (remaining >= strlen(op)) {
if (strncmp(op, p, strlen(op)) == 0) {
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_OPERATOR;
new_token->data.operator= i;
@@ -333,13 +337,9 @@ struct token *tokenize(char *source) {
char *kw = KEYWORDS[i];
if (remaining >= strlen(kw)) {
if (strncmp(kw, p, strlen(kw)) == 0) {
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_KEYWORD;
new_token->data.keyword = i;
@@ -375,13 +375,9 @@ struct token *tokenize(char *source) {
}
p--;
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_CONSTANT;
new_token->data.constant_kind = CONST_INT;
new_token->data.constant_int_value = value;
@@ -404,13 +400,9 @@ struct token *tokenize(char *source) {
str[str_len - 1] = c;
}
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_IDENTIFIER;
new_token->data.constant_kind = CONST_STR;
new_token->data.constant_str_value = str;
@@ -432,13 +424,10 @@ struct token *tokenize(char *source) {
if (*p++ != '\'')
return fprintf(stderr, "expected '\n"), NULL;
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens)
- return fprintf(stderr, "realloc failed\n"), NULL;
-
- struct token *new_token = &tokens[num_tokens - 1];
- new_token->kind = TOK_IDENTIFIER;
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
+ return NULL;
+ new_token->kind = TOK_CONSTANT;
new_token->data.constant_kind = CONST_STR;
new_token->data.constant_char_value = chr;
continue;
@@ -449,7 +438,6 @@ struct token *tokenize(char *source) {
char *ident_start = p;
for (char c; (c = *p++) && is_ident(c);)
;
- p--;
int ident_len = p - ident_start - 1;
char *ident_str = malloc(ident_len + 1);
if (!ident_str) {
@@ -460,13 +448,9 @@ struct token *tokenize(char *source) {
ident_str[i] = ident_start[i];
ident_str[ident_len] = '\0';
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_IDENTIFIER;
new_token->data.identifier = ident_str;
@@ -477,25 +461,44 @@ struct token *tokenize(char *source) {
return NULL;
}
- num_tokens += 1;
- tokens = realloc(tokens, sizeof(struct token) * num_tokens);
- if (!tokens) {
- fprintf(stderr, "realloc failed\n");
+ struct token *new_token = token_push(&tokens, &num_tokens);
+ if (!new_token)
return NULL;
- }
- struct token *new_token = &tokens[num_tokens - 1];
new_token->kind = TOK_END;
return tokens;
}
+void free_tokens(struct token *tokens) {
+ for (int i = 0; tokens[i].kind != TOK_END; i++) {
+ switch (tokens[i].kind) {
+ case TOK_IDENTIFIER:
+ free(tokens[i].data.identifier);
+ break;
+ case TOK_CONSTANT:
+ switch (tokens[i].data.constant_kind) {
+ case CONST_STR:
+ free(tokens[i].data.constant_str_value);
+ break;
+ default:
+ break;
+ }
+ default:
+ break;
+ }
+ }
+ free(tokens);
+}
+
#ifdef DEBUG
void debug_tokens(struct token *tokens) {
for (int i = 0; tokens[i].kind != TOK_END; i++) {
switch (tokens[i].kind) {
case TOK_IDENTIFIER:
- // printf("TOK_IDENTIFIER:%s, ", tokens[i].data.identifier);
- printf("TOK_IDENTIFIER, ");
+ if (tokens[i].data.identifier < 100)
+ printf("TOK_IDENTIFIER:%p, ", tokens[i].data.identifier);
+ else
+ printf("TOK_IDENTIFIER:%s, ", tokens[i].data.identifier);
break;
case TOK_KEYWORD:
printf("TOK_KEYWORD:%s, ", KEYWORD_NAMES[tokens[i].data.keyword]);
@@ -525,11 +528,38 @@ void debug_tokens(struct token *tokens) {
case TOK_END:
break;
}
+ printf("\n");
}
printf("TOK_END\n");
}
#endif
+enum expr_kind {
+ NO_BINOP,
+ NO_UNOP,
+ NO_MEMBER_ACCESS,
+};
+
+union expr_data {
+ struct {
+ struct expr *lhs;
+ struct expr *rhs;
+ } binop;
+};
+
+struct expr {
+ enum expr_kind kind;
+ union expr_data data;
+};
+
+enum decl_kind { DECL_STRUCT, DECL_UNION, DECL_ENUM, DECL_FUNCTION };
+
+struct decl {
+ enum decl_kind kind;
+};
+
+struct decl *parse(struct token *tokens) { return NULL; }
+
int main(int argc, char **argv) {
if (argc < 3) {
fprintf(stderr, "USAGE:\n\tattocc <input> <output>\n");
@@ -581,7 +611,7 @@ int main(int argc, char **argv) {
debug_tokens(tokens);
#endif
- free(tokens);
+ free_tokens(tokens);
return 0;
}
diff --git a/makefile b/makefile
index ffd9c32..7b19833 100644
--- a/makefile
+++ b/makefile
@@ -5,11 +5,11 @@ clean:
rm $(ALL)
attocc: attocc.c
- gcc -O3 -o $@ $<
+ cc -O3 -o $@ $<
attocc-debug: attocc.c
- gcc -DDEBUG -Wall -Wextra -Wpedantic -o $@ $<
+ cc -DDEBUG -Wall -Wextra -Wpedantic -o $@ $<
attocc-small: attocc.c
- gcc -Wl,--gc-sections -flto -ffunction-sections -fdata-sections -fPIC -s -Os -o $@ $<
+ cc -Wl,--gc-sections -flto -ffunction-sections -fdata-sections -fPIC -s -Os -o $@ $<
strip -x $@