summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-07 21:14:11 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-07 21:14:11 +0330
commit6bd439f35c1184f04488ee4f0db21632e1301b51 (patch)
tree369766b11ef80c33dfca262deae0051e5343aaa5 /src/main.c
parentb89bec88a56b81d3524ed082db9796ef3169b060 (diff)
add plus
trying to add return
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/src/main.c b/src/main.c
index cd63515..999b41a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,12 +2,14 @@
#include "compiler/code-generator.h"
#include "compiler/lexer.h"
#include "compiler/parser.h"
+#include "runner/runner.h"
#include "utils/file.h"
#include "utils/log.h"
#include <stdio.h>
#include <stdlib.h>
-static int runWithPrint(const char *filePath, const char *outFilePath) {
+static int compileRun(const char *filePath, const char *outFilePath,
+ bool print) {
char *code = readWholeFile(filePath);
if (code == NULL) {
@@ -18,21 +20,24 @@ static int runWithPrint(const char *filePath, const char *outFilePath) {
if (lexerNodeArrayIsError(lexed)) {
goto RETURN_ERROR;
}
- lexerNodeArrayPrint(lexed);
+ if (print)
+ lexerNodeArrayPrint(lexed);
ParserNode *parsedRoot = parser(lexed);
lexerNodeArrayDestroy(lexed);
if (parsedRoot == NULL) {
goto RETURN_ERROR;
}
- parserNodePrint(parsedRoot, 0);
+ if (print)
+ parserNodePrint(parsedRoot, 0);
AstTreeRoot *astTree = makeAstTree(parsedRoot);
parserNodeDelete(parsedRoot);
if (astTree == NULL) {
goto RETURN_ERROR;
}
- astTreeRootPrint(astTree);
+ if (print)
+ astTreeRootPrint(astTree);
CodeGeneratorCodes *codes = codeGenerator(astTree);
astTreeRootDelete(astTree);
@@ -44,7 +49,8 @@ static int runWithPrint(const char *filePath, const char *outFilePath) {
codeGeneratorDelete(codes);
free(code);
- puts(fasm);
+ if (print)
+ puts(fasm);
if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
free(fasm);
@@ -58,7 +64,7 @@ RETURN_ERROR:
return 1;
}
-static int run(const char *filePath, const char *outFilePath) {
+static int run(const char *filePath, bool print) {
char *code = readWholeFile(filePath);
if (code == NULL) {
@@ -69,47 +75,45 @@ static int run(const char *filePath, const char *outFilePath) {
if (lexerNodeArrayIsError(lexed)) {
goto RETURN_ERROR;
}
+ if (print)
+ lexerNodeArrayPrint(lexed);
ParserNode *parsedRoot = parser(lexed);
lexerNodeArrayDestroy(lexed);
if (parsedRoot == NULL) {
goto RETURN_ERROR;
}
+ if (print)
+ parserNodePrint(parsedRoot, 0);
AstTreeRoot *astTree = makeAstTree(parsedRoot);
parserNodeDelete(parsedRoot);
if (astTree == NULL) {
goto RETURN_ERROR;
}
-
- CodeGeneratorCodes *codes = codeGenerator(astTree);
- astTreeRootDelete(astTree);
- if (codes == NULL) {
- goto RETURN_ERROR;
- }
-
- char *fasm = codeGeneratorToFlatASM(codes);
- codeGeneratorDelete(codes);
- free(code);
-
- if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
- free(fasm);
- return system(outFilePath);
+ if (print)
+ astTreeRootPrint(astTree);
+
+ if (runAstTree(astTree)) {
+ astTreeRootDelete(astTree);
+ return 0;
+ } else {
+ astTreeRootDelete(astTree);
+ return 1;
}
- return 1;
-
RETURN_ERROR:
free(code);
return 1;
}
int main(int argc, char *argv[]) {
- if (argc < 3) {
- run("test/main.felan", "build/out");
+ if (argc < 2) {
+ // compileRun("test/main.felan", "build/out", false);
+ run("test/main.felan", false);
printLog("Too few args");
return 1;
}
- return runWithPrint(argv[1], argv[2]);
+ return run(argv[1], true);
}