summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-22 16:16:15 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-22 16:16:15 +0200
commitea1b206e6987cfd19edc4ff77d8c110d29106680 (patch)
treefdba847cb0de72c1379e3e1773dedae4aceddf0d
parentaccdadbababdada12ac2f005c2faa9936d8101a6 (diff)
downloadattocc-ea1b206e6987cfd19edc4ff77d8c110d29106680.tar
attocc-ea1b206e6987cfd19edc4ff77d8c110d29106680.tar.bz2
attocc-ea1b206e6987cfd19edc4ff77d8c110d29106680.tar.zst
new parser
-rw-r--r--attocc.c70
1 files changed, 54 insertions, 16 deletions
diff --git a/attocc.c b/attocc.c
index 3f7cd0b..467a4e5 100644
--- a/attocc.c
+++ b/attocc.c
@@ -636,6 +636,10 @@ enum type_kind {
struct struct_decl {};
struct union_decl {};
+enum primitive_type {
+ PRIMTY_INT,
+};
+
struct type {
enum type_kind kind;
int index;
@@ -683,7 +687,6 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
} else if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_COMMA) {
if (token_step(iter, a))
return 1;
- continue;
} else {
fprintf(stderr, "expected assign or comma after enum ident\n");
return 1;
@@ -716,7 +719,18 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
void gcx_print(struct global_context *gcx) {
printf("Constants:\n");
for (int i = 0; i < gcx->num_constants; i++) {
- printf("\t%s = %i\n", gcx->constants[i].name, gcx->constants[i].value);
+ struct constant *con = &gcx->constants[i];
+ if (con->type.kind == TY_ENUM)
+ printf("\tenum %s %s = %i\n", gcx->enums[con->type.index], con->name,
+ con->value);
+ else {
+ fprintf(stderr, "todo const type\n");
+ exit(1);
+ }
+ }
+ printf("Enums:\n");
+ for (int i = 0; i < gcx->num_enums; i++) {
+ printf("\t%s\n", gcx->enums[i]);
}
}
#endif
@@ -732,25 +746,48 @@ int toplevel(struct token_iter *iter) {
gcx.num_enums = 0;
gcx.enums = NULL;
- if (a.kind == TOK_KEYWORD) {
- switch (a.data.keyword) {
- case KW_ENUM:
- if (token_step(iter, &a))
- return 1;
- if (a.kind == TOK_IDENTIFIER) {
- type_name = strdup_failsafe(a.data.identifier);
+ struct type type;
+
+ while (1) {
+ if (a.kind == TOK_KEYWORD) {
+ switch (a.data.keyword) {
+ case KW_ENUM:
if (token_step(iter, &a))
return 1;
- }
- if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LCURLY) {
+ if (a.kind == TOK_IDENTIFIER) {
+ type_name = strdup_failsafe(a.data.identifier);
+ if (token_step(iter, &a))
+ return 1;
+ }
+ if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LCURLY) {
+ if (token_step(iter, &a))
+ return 1;
+ if (enum_decl(&gcx, iter, &a, type_name))
+ return 1;
+ if (a.kind != TOK_SEPERATOR && a.data.seperator != SEP_SEMICOLON) {
+ fprintf(stderr, "expected semicolon after enum decl\n");
+ return 1;
+ }
+ if (token_step(iter, &a))
+ return 1;
+ }
+ break;
+ case KW_INT:
+ type.kind = TY_PRIMITIVE;
+ type.index = PRIMTY_INT;
if (token_step(iter, &a))
return 1;
- if (enum_decl(&gcx, iter, &a, type_name))
- return 1;
+ break;
+ default:
+ fprintf(stderr, "unhandled keyword: ");
+ token_print(a);
+ return 1;
}
- break;
- default:
- printf("unhandled keyword: ");
+ } else if (a.kind == TOK_IDENTIFIER) {
+ if (token_step(iter, &a))
+ return 1;
+ } else {
+ fprintf(stderr, "undhandled toplevel token: ");
token_print(a);
return 1;
}
@@ -759,6 +796,7 @@ int toplevel(struct token_iter *iter) {
#ifdef DEBUG
gcx_print(&gcx);
#endif
+
return 0;
}