summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-01-19 21:55:45 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-01-19 21:55:45 +0330
commit1963e7dddfaeebb22e3657adc1a7ee86fda50d52 (patch)
tree33a1c771443bc743d32a83dd775337f59cbbc977 /src
parent6372d439068482a847fabcb9e97d9e240ccb8a47 (diff)
fix memory leaks
Diffstat (limited to 'src')
-rw-r--r--src/compiler/ast-tree.c3
-rw-r--r--src/compiler/code-generator.c12
-rw-r--r--src/compiler/code-generator.h2
-rw-r--r--src/main.c21
4 files changed, 26 insertions, 12 deletions
diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index f25fc51..52a41c9 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -59,6 +59,8 @@ void astTreeDestroy(AstTree tree) {
for (size_t i = 0; i < metadata->expressions_size; ++i) {
astTreeDestroy(metadata->expressions[i]);
}
+ free(metadata->expressions);
+ free(metadata);
case AST_TREE_TOKEN_KEYWORD_PRINT:
return;
case AST_TREE_TOKEN_NONE:
@@ -75,6 +77,7 @@ void astTreeDelete(AstTree *tree) {
void astTreeRootDelete(AstTreeRoot *root) {
for (size_t i = 0; i < root->variables.size; ++i) {
astTreeDelete(root->variables.data[i]->value);
+ free(root->variables.data[i]);
}
free(root->variables.data);
free(root);
diff --git a/src/compiler/code-generator.c b/src/compiler/code-generator.c
index 0de098c..67fba4c 100644
--- a/src/compiler/code-generator.c
+++ b/src/compiler/code-generator.c
@@ -6,6 +6,14 @@
#include <stdlib.h>
#include <string.h>
+void codeGeneratorDelete(CodeGeneratorCodes *code) {
+ for (size_t i = 0; i < code->codes_size; ++i) {
+ continue;
+ }
+ free(code->codes);
+ free(code);
+}
+
CodeGeneratorCode createGenerateCode(char *label_begin, char *label_end,
CodeGeneratorInstruction instruction) {
CodeGeneratorCode code = {
@@ -119,7 +127,6 @@ char *codeGeneratorToFlatASM(const CodeGeneratorCodes *codes) {
size_t fasm_size = TEMPLATE_LEN + 1;
char *fasm = a404m_malloc(fasm_size * sizeof(*fasm));
size_t fasm_inserted = 0;
- fasm[0] = '\0';
codeGeneratorAppendFlatASMCommand(&fasm, &fasm_size, &fasm_inserted, TEMPLATE,
TEMPLATE_LEN);
@@ -165,6 +172,7 @@ bool codeGeneratorFlatASMExec(const char *filePath, const char *fasm) {
FILE *file = fopen(asmFilePath, "w");
if (file == NULL) {
+ free(asmFilePath);
return false;
}
@@ -174,6 +182,7 @@ bool codeGeneratorFlatASMExec(const char *filePath, const char *fasm) {
char *command;
asprintf(&command, "fasm -m 102400 \"%s\" \"%s\"", asmFilePath, filePath);
+ free(asmFilePath);
if (system(command) != 0) {
free(command);
@@ -187,6 +196,7 @@ bool codeGeneratorFlatASMExec(const char *filePath, const char *fasm) {
free(command);
return false;
}
+ free(command);
return true;
}
diff --git a/src/compiler/code-generator.h b/src/compiler/code-generator.h
index 3232fb3..f9ac6c9 100644
--- a/src/compiler/code-generator.h
+++ b/src/compiler/code-generator.h
@@ -20,6 +20,8 @@ typedef struct CodeGeneratorCodes {
size_t codes_size;
} CodeGeneratorCodes;
+void codeGeneratorDelete(CodeGeneratorCodes *code);
+
CodeGeneratorCode createGenerateCode(char *label_begin, char *label_end,
CodeGeneratorInstruction instruction);
diff --git a/src/main.c b/src/main.c
index 68da20e..07728f6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,7 +7,7 @@
#include <stdio.h>
#include <stdlib.h>
-int runWithPrint(const char *filePath) {
+int runWithPrint(const char *filePath, const char *outFilePath) {
char *code = readWholeFile(filePath);
if (code == NULL) {
return 1;
@@ -49,11 +49,12 @@ int runWithPrint(const char *filePath) {
char *fasm = codeGeneratorToFlatASM(codes);
printf("%s", fasm);
- if (codeGeneratorFlatASMExec("build/test-file", fasm)) {
- system("./build/test-file");
+ if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
+ system(outFilePath);
}
free(fasm);
+ codeGeneratorDelete(codes);
astTreeRootDelete(astTree);
parserNodeDelete(parsedRoot);
lexerNodeArrayDestroy(lexed);
@@ -61,7 +62,7 @@ int runWithPrint(const char *filePath) {
return 0;
}
-int run(const char *filePath) {
+int run(const char *filePath, const char *outFilePath) {
char *code = readWholeFile(filePath);
if (code == NULL) {
return 1;
@@ -98,11 +99,12 @@ int run(const char *filePath) {
}
char *fasm = codeGeneratorToFlatASM(codes);
- if (codeGeneratorFlatASMExec("build/test-file", fasm)) {
- system("./build/test-file");
+ if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
+ system(outFilePath);
}
free(fasm);
+ codeGeneratorDelete(codes);
astTreeRootDelete(astTree);
parserNodeDelete(parsedRoot);
lexerNodeArrayDestroy(lexed);
@@ -111,13 +113,10 @@ int run(const char *filePath) {
}
int main(int argc, char *argv[]) {
- (void)argc;
- (void)argv;
-
- if (argc < 2) {
+ if (argc < 3) {
printLog("Too few args");
return 1;
}
- return runWithPrint(argv[1]);
+ return runWithPrint(argv[1], argv[2]);
}