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