summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile12
-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
-rw-r--r--test/main.felan5
6 files changed, 38 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index 0dc0bf7..4e1eb13 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,5 @@
+PROJECT_NAME := felan
+
CC := gcc
# CC := tcc
@@ -16,11 +18,11 @@ NC := \033[0m
INC_DIRS := $(SRC_DIR)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
-CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -O3
+# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -O3
# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -Oz
-# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -g
+CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -g
-EXEC_FILE := $(BUILD_DIR)/felan
+EXEC_FILE := $(BUILD_DIR)/$(PROJECT_NAME)
all: $(EXEC_FILE)
@@ -49,10 +51,10 @@ gdb-run: $(EXEC_FILE)
.PHONY: test
test: $(EXEC_FILE)
- $(EXEC_FILE) test/main.felan
+ $(EXEC_FILE) test/main.felan build/out
val-test: $(EXEC_FILE)
- valgrind --log-file="val.log" --leak-check=full --track-origins=yes --show-leak-kinds=all -s $(EXEC_FILE) test/main.felan
+ valgrind --log-file="val.log" --leak-check=full --track-origins=yes --show-leak-kinds=all -s $(EXEC_FILE) test/main.felan build/out
# $@ = left hand of :
# $< = right hand of : first one of them
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]);
}
diff --git a/test/main.felan b/test/main.felan
index eef127e..19120f5 100644
--- a/test/main.felan
+++ b/test/main.felan
@@ -1,3 +1,8 @@
main :: () -> void {
print;
};
+
+a :: () -> void {
+ print;
+ print;
+};