From fbb4db921ec1cb59c9264aab78d4dd3a7ef6d030 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Thu, 19 Jun 2025 14:40:30 +0200 Subject: parse func decl head --- attocc.c | 36 +++++++++++++++++++++++++++++++----- makefile | 2 +- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/attocc.c b/attocc.c index 5a575fa..82ea8d8 100644 --- a/attocc.c +++ b/attocc.c @@ -624,7 +624,9 @@ void token_print(struct token tok) { int token_step(struct token_iter *iter, struct token *a) { token_free(*a); *a = token_iter_next(iter); +#ifdef DEBUG token_print(*a); +#endif return a->kind == TOK_END || a->kind == TOK_ERROR; } @@ -774,6 +776,12 @@ int read_type(struct global_context *gcx, struct token_iter *iter, if (token_step(iter, a)) return 1; return 0; + } else if (a->data.keyword == KW_CHAR) { + ty->kind = TY_PRIMITIVE; + ty->index = 1; + if (token_step(iter, a)) + return 1; + return 0; } else { return 1; } @@ -786,13 +794,30 @@ int function_decl(struct global_context *gcx, struct token_iter *iter, while (1) { if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RPAREN) break; + struct type type; + if (read_type(gcx, iter, a, &type)) + return 1; + if (a->kind != TOK_IDENTIFIER) { + fprintf(stderr, "expected ident after arg type\n"); + return 1; + } + char *name = strdup_failsafe(a->data.identifier); + if (token_step(iter, a)) + return 1; } if (token_step(iter, a)) // rparen return 1; + if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_SEMICOLON) { + fprintf(stderr, "forward decl not supported yet\n"); + return 1; + } + if (a->kind != TOK_SEPERATOR || a->data.seperator != SEP_LCURLY) { + fprintf(stderr, "expected lcurly after function head\n"); + return 1; + } if (token_step(iter, a)) // lcurly return 1; - token_step(iter, a); // rcurly return 0; } @@ -839,19 +864,20 @@ int toplevel(struct token_iter *iter) { if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_SEMICOLON) { // was a type decl, continue if (token_step(iter, &a)) - return 1; + break; } else if (a.kind == TOK_IDENTIFIER) { // either var or func char *name = strdup_failsafe(a.data.identifier); if (token_step(iter, &a)) return 1; - if (a.kind == TOK_OPERATOR && a.data.operator == OP_ASSIGN) { // variable / constant TRAP } else if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LPAREN) { - // function decl - TRAP + if (function_decl(&gcx, iter, &a, name)) + return 1; + if (token_step(iter, &a)) + break; } else { fprintf(stderr, "unknown type-ident started toplevel statement\n"); return 1; diff --git a/makefile b/makefile index 07dcb29..436062c 100644 --- a/makefile +++ b/makefile @@ -1,7 +1,7 @@ .PHONY: all clean test attocc: attocc.c - cc -DDEBUG -O3 -o $@ $< + cc -O3 -o $@ $< ALL = attocc attocc-small attocc-debug attocc-test attocc-test-opt -- cgit v1.2.3-70-g09d2