From 6bd439f35c1184f04488ee4f0db21632e1301b51 Mon Sep 17 00:00:00 2001 From: A404M Date: Fri, 7 Feb 2025 21:14:11 +0330 Subject: add plus trying to add return --- src/main.c | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'src/main.c') 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 #include -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); } -- cgit v1.2.3