diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-22 16:32:17 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-22 16:32:17 +0200 |
commit | d1f5732d0088855be1857bf49ff75e2ca0a6317f (patch) | |
tree | 916ff2a653bc7c3c4c57918b4859367229c7d917 | |
parent | ea1b206e6987cfd19edc4ff77d8c110d29106680 (diff) | |
download | attocc-d1f5732d0088855be1857bf49ff75e2ca0a6317f.tar attocc-d1f5732d0088855be1857bf49ff75e2ca0a6317f.tar.bz2 attocc-d1f5732d0088855be1857bf49ff75e2ca0a6317f.tar.zst |
fix enum parse and start on functions
-rw-r--r-- | attocc.c | 44 |
1 files changed, 38 insertions, 6 deletions
@@ -623,6 +623,7 @@ void token_print(struct token tok) { int token_step(struct token_iter *iter, struct token *a) { token_free(*a); *a = token_iter_next(iter); + token_print(*a); return a->kind == TOK_END || a->kind == TOK_ERROR; } @@ -680,15 +681,19 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter, return 1; if (a->kind == TOK_CONSTANT && a->data.constant_kind == CONST_INT) { value = a->data.constant_int_value; + if (token_step(iter, a)) + return 1; } else { fprintf(stderr, "expected int const after enum assign\n"); return 1; } - } else if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_COMMA) { + } + + if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_COMMA) { if (token_step(iter, a)) return 1; } else { - fprintf(stderr, "expected assign or comma after enum ident\n"); + fprintf(stderr, "expected comma after variant\n"); return 1; } @@ -715,6 +720,23 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter, } } +int function_decl(struct global_context *gcx, struct token_iter *iter, + struct token *a, char *fun_name) { + if (token_step(iter, a)) // lparen + return 1; + while (1) { + if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RPAREN) + break; + } + if (token_step(iter, a)) // rparen + return 1; + if (token_step(iter, a)) // lcurly + return 1; + + token_step(iter, a); // rcurly + return 0; +} + #ifdef DEBUG void gcx_print(struct global_context *gcx) { printf("Constants:\n"); @@ -739,7 +761,7 @@ int toplevel(struct token_iter *iter) { struct token a = token_iter_next(iter); struct global_context gcx; - char *type_name = NULL; + char *name = NULL; gcx.num_constants = 0; gcx.constants = NULL; @@ -749,20 +771,22 @@ int toplevel(struct token_iter *iter) { struct type type; while (1) { - if (a.kind == TOK_KEYWORD) { + if (a.kind == TOK_END) { + break; + } else 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); + 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)) + if (enum_decl(&gcx, iter, &a, name)) return 1; if (a.kind != TOK_SEPERATOR && a.data.seperator != SEP_SEMICOLON) { fprintf(stderr, "expected semicolon after enum decl\n"); @@ -784,8 +808,16 @@ int toplevel(struct token_iter *iter) { return 1; } } else if (a.kind == TOK_IDENTIFIER) { + name = strdup_failsafe(a.data.identifier); if (token_step(iter, &a)) return 1; + if (a.kind == TOK_OPERATOR && a.data.operator== OP_ASSIGN) { + fprintf(stderr, "todo: handle global var\n"); + return 1; + } else if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LPAREN) { + if (function_decl(&gcx, iter, &a, name)) + return 1; + } } else { fprintf(stderr, "undhandled toplevel token: "); token_print(a); |