From 4d182e1685e5ef8fbeb6ac94c09f222567f8e2ee Mon Sep 17 00:00:00 2001 From: Mid <245600-midn@users.noreply.gitlab.com> Date: Thu, 31 Aug 2023 21:24:46 +0300 Subject: [PATCH] Closer to OW-compliance + removed some leaks --- src/cg.c | 2 +- src/lexer.c | 7 +++++++ src/lexer.h | 1 + src/parse.c | 19 ++++++------------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/cg.c b/src/cg.c index 518528a..758e04f 100644 --- a/src/cg.c +++ b/src/cg.c @@ -6,7 +6,7 @@ #include #define REGS 4 -static const char *regs[REGS][3] = {{"al", "ax", "eax"}, {"bl", "bx", "ebx"}, {"cl", "cx", "ecx"}, {"dl", "dx", "edx"}, {"sil", "si", "esi"}, {"dil", "di", "edi"}}; +static const char *regs[REGS][3] = {{"al", "ax", "eax"}, {"bl", "bx", "ebx"}, {"cl", "cx", "ecx"}, {"dl", "dx", "edx"}}; static const char *BINOP_SIMPLE_INSTRS[] = {[BINOP_ADD] = "add", [BINOP_SUB] = "sub", [BINOP_BITWISE_AND] = "and", [BINOP_BITWISE_OR] = "or", [BINOP_BITWISE_XOR] = "xor"}; diff --git a/src/lexer.c b/src/lexer.c index ad55c90..9c906b8 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -318,3 +318,10 @@ Token *nct_lex(FILE *f) { return NULL; /* Doesn't reach here. */ } + +void nct_lex_free(Token *tokens) { + for(Token *t = tokens; t->type != TOKEN_EOF; t++) { + if(t->content) free(t->content); + } + free(tokens); +} diff --git a/src/lexer.h b/src/lexer.h index c54fcb3..dae5eae 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -50,5 +50,6 @@ typedef struct { Token nct_tokenize(FILE*); Token *nct_lex(FILE*); +void nct_lex_free(Token *); #endif diff --git a/src/parse.c b/src/parse.c index ff00bb8..b605036 100644 --- a/src/parse.c +++ b/src/parse.c @@ -16,7 +16,7 @@ typedef struct { typedef struct { Token *tokens; - ssize_t i; + intmax_t i; VarTable *scope; @@ -115,15 +115,11 @@ static ASTExprPrimitive *parse_prim(Parser *P) { static void newusedef(Parser *P, VarTableEntry *v, AST *expr) { for(size_t i = 0; i < v->data.var.reachingDefs->defCount; i++) { P->udsToAdd = realloc(P->udsToAdd, sizeof(*P->udsToAdd) * (++P->udsToAddCount)); - P->udsToAdd[P->udsToAddCount - 1] = (UseDefToAdd) { - .ud = { - .def = v->data.var.reachingDefs->defs[i], - .use = expr, - .stmt = NULL, // set by pushstmt - .next = NULL, - }, - .to = v - }; + P->udsToAdd[P->udsToAddCount - 1].ud.def = v->data.var.reachingDefs->defs[i]; + P->udsToAdd[P->udsToAddCount - 1].ud.use = expr; + P->udsToAdd[P->udsToAddCount - 1].ud.stmt = NULL; // set by pushstmt + P->udsToAdd[P->udsToAddCount - 1].ud.next = NULL; + P->udsToAdd[P->udsToAddCount - 1].to = v; } } @@ -773,9 +769,6 @@ void nct_parse_statement(Parser *P) { } ASTChunk *nct_parse_chunk(Parser *P, int isTopLevel, int varPrioritize) { - if(P->scope) { - } - AST *ret = malloc(sizeof(ASTChunk)); ret->nodeKind = AST_CHUNK; ret->chunk.statementFirst = ret->chunk.statementLast = NULL;