diff options
author | metamuffin <metamuffin@disroot.org> | 2025-04-12 16:02:00 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2025-04-12 16:02:00 +0200 |
commit | accdadbababdada12ac2f005c2faa9936d8101a6 (patch) | |
tree | f6d566f8d39fe0598f13184bdd361131d8ba6b52 | |
parent | de93325a308cd71e251c697b99b8fa2f27f6623c (diff) | |
download | attocc-accdadbababdada12ac2f005c2faa9936d8101a6.tar attocc-accdadbababdada12ac2f005c2faa9936d8101a6.tar.bz2 attocc-accdadbababdada12ac2f005c2faa9936d8101a6.tar.zst |
enum decls but broken
-rw-r--r-- | attocc.c | 68 | ||||
-rw-r--r-- | makefile | 10 |
2 files changed, 59 insertions, 19 deletions
@@ -1,6 +1,6 @@ /* attocc - A minimal C compiler. - Copyright (C) 2024 metamuffin + Copyright (C) 2025 metamuffin This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as @@ -627,10 +627,10 @@ int token_step(struct token_iter *iter, struct token *a) { } enum type_kind { - PRIMITIVE, - ENUM, - STRUCT, - UNION, + TY_PRIMITIVE, + TY_ENUM, + TY_STRUCT, + TY_UNION, }; struct struct_decl {}; @@ -642,20 +642,32 @@ struct type { }; struct constant { + char *name; struct type type; + int value; }; struct global_context { int num_constants; struct constant *constants; + int num_enums; + char **enums; }; int enum_decl(struct global_context *gcx, struct token_iter *iter, - struct token *a) { + struct token *a, char *name) { + + gcx->enums = + realloc_failsafe(gcx->enums, sizeof(char *) * (gcx->num_enums + 1)); + int enum_index = gcx->num_enums; + gcx->enums[enum_index] = name; + gcx->num_enums += 1; + int value = 0; while (1) { if (a->kind == TOK_IDENTIFIER) { - char *name = strdup_failsafe(a->data.identifier); + char *variant_name = strdup_failsafe(a->data.identifier); + if (token_step(iter, a)) return 1; @@ -677,6 +689,17 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter, return 1; } + struct constant con; + con.type.kind = TY_ENUM; + con.type.index = enum_index; + con.value = value; + con.name = variant_name; + + gcx->constants = realloc_failsafe( + gcx->constants, sizeof(struct constant) * (gcx->num_constants + 1)); + gcx->constants[gcx->num_constants] = con; + gcx->num_constants += 1; + value += 1; } else if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RCURLY) { if (token_step(iter, a)) @@ -689,10 +712,25 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter, } } +#ifdef DEBUG +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); + } +} +#endif + int toplevel(struct token_iter *iter) { struct token a = token_iter_next(iter); struct global_context gcx; + + char *type_name = NULL; + gcx.num_constants = 0; + gcx.constants = NULL; + gcx.num_enums = 0; + gcx.enums = NULL; if (a.kind == TOK_KEYWORD) { switch (a.data.keyword) { @@ -700,14 +738,14 @@ int toplevel(struct token_iter *iter) { if (token_step(iter, &a)) return 1; if (a.kind == TOK_IDENTIFIER) { - char *type_name = strdup_failsafe(a.data.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)) + if (enum_decl(&gcx, iter, &a, type_name)) return 1; } break; @@ -717,6 +755,10 @@ int toplevel(struct token_iter *iter) { return 1; } } + +#ifdef DEBUG + gcx_print(&gcx); +#endif return 0; } @@ -791,24 +833,20 @@ int main(int argc, char **argv) { struct linemap linemap; struct token_iter iter; - // struct token_buffer buf; struct token tok; linemap = linemap_new(source); iter = token_iter_new(source); - // buf = token_buffer_new(&iter); - // toplevel(&buf); - toplevel(&iter); + if (toplevel(&iter)) + fprintf(stderr, "compilation failed\n"); // while (1) { // tok = token_iter_next(&iter); // #ifdef DEBUG // token_print(tok); // #endif - // if (tok.kind == TOK_END) // break; - // token_free(tok); // }; @@ -1,5 +1,10 @@ .PHONY: all clean test -ALL = attocc attocc-small attocc-debug attocc-test + +attocc: attocc.c + cc -DDEBUG -O3 -o $@ $< + +ALL = attocc attocc-small attocc-debug attocc-test attocc-test-opt + all: $(ALL) clean: rm $(ALL) @@ -7,9 +12,6 @@ test: attocc-test attocc-test-opt ./attocc-test ./attocc-test-opt -attocc: attocc.c - cc -DDEBUG -O3 -o $@ $< - attocc-debug: attocc.c cc -DDEBUG -Wall -Wextra -Wpedantic -Og -g -o $@ $< |