summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-01-23 19:57:55 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-01-23 19:57:55 +0330
commit76272a881916b466f42f78ba16c696a843628d08 (patch)
tree24ceba08e21eb69a312bdc71b374ee2d05c910f7
parent6f2a7cd1c662e30cd75b8cbcb246bdf9978c1290 (diff)
cleaning the code
-rw-r--r--src/main.c149
1 files changed, 54 insertions, 95 deletions
diff --git a/src/main.c b/src/main.c
index 07728f6..86d8858 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,109 +7,68 @@
#include <stdio.h>
#include <stdlib.h>
-int runWithPrint(const char *filePath, const char *outFilePath) {
+static int runWithPrint(const char *filePath, const char *outFilePath) {
+ int ret = 1;
char *code = readWholeFile(filePath);
- if (code == NULL) {
- return 1;
- }
-
- LexerNodeArray lexed = lexer(code);
- if (lexerNodeArrayIsError(lexed)) {
- free(code);
- return 1;
- }
- lexerNodeArrayPrint(lexed);
-
- ParserNode *parsedRoot = parser(lexed);
- if (parsedRoot == NULL) {
- lexerNodeArrayDestroy(lexed);
+ if (code != NULL) {
+ LexerNodeArray lexed = lexer(code);
+ if (!lexerNodeArrayIsError(lexed)) {
+ lexerNodeArrayPrint(lexed);
+ ParserNode *parsedRoot = parser(lexed);
+ if (parsedRoot != NULL) {
+ parserNodePrint(parsedRoot, 0);
+ AstTreeRoot *astTree = makeAstTree(parsedRoot);
+ if (astTree != NULL) {
+ astTreeRootPrint(astTree);
+ CodeGeneratorCodes *codes = codeGenerator(astTree);
+ if (codes != NULL) {
+ char *fasm = codeGeneratorToFlatASM(codes);
+ printf("%s", fasm);
+ if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
+ ret = system(outFilePath);
+ }
+ free(fasm);
+ codeGeneratorDelete(codes);
+ }
+ astTreeRootDelete(astTree);
+ }
+ parserNodeDelete(parsedRoot);
+ }
+ lexerNodeArrayDestroy(lexed);
+ }
free(code);
- return 1;
}
- parserNodePrint(parsedRoot, 0);
- printf("\n");
-
- AstTreeRoot *astTree = makeAstTree(parsedRoot);
- if (astTree == NULL) {
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 1;
- }
- astTreeRootPrint(astTree);
-
- CodeGeneratorCodes *codes = codeGenerator(astTree);
- if (codes == NULL) {
- astTreeRootDelete(astTree);
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 1;
- }
-
- char *fasm = codeGeneratorToFlatASM(codes);
- printf("%s", fasm);
- if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
- system(outFilePath);
- }
-
- free(fasm);
- codeGeneratorDelete(codes);
- astTreeRootDelete(astTree);
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 0;
+ return ret;
}
-int run(const char *filePath, const char *outFilePath) {
+static int run(const char *filePath, const char *outFilePath) {
+ int ret = 1;
char *code = readWholeFile(filePath);
- if (code == NULL) {
- return 1;
- }
-
- LexerNodeArray lexed = lexer(code);
- if (lexerNodeArrayIsError(lexed)) {
- free(code);
- return 1;
- }
-
- ParserNode *parsedRoot = parser(lexed);
- if (parsedRoot == NULL) {
- lexerNodeArrayDestroy(lexed);
+ if (code != NULL) {
+ LexerNodeArray lexed = lexer(code);
+ if (!lexerNodeArrayIsError(lexed)) {
+ ParserNode *parsedRoot = parser(lexed);
+ if (parsedRoot != NULL) {
+ AstTreeRoot *astTree = makeAstTree(parsedRoot);
+ if (astTree != NULL) {
+ CodeGeneratorCodes *codes = codeGenerator(astTree);
+ if (codes != NULL) {
+ char *fasm = codeGeneratorToFlatASM(codes);
+ if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
+ ret = system(outFilePath);
+ }
+ free(fasm);
+ codeGeneratorDelete(codes);
+ }
+ astTreeRootDelete(astTree);
+ }
+ parserNodeDelete(parsedRoot);
+ }
+ lexerNodeArrayDestroy(lexed);
+ }
free(code);
- return 1;
}
-
- AstTreeRoot *astTree = makeAstTree(parsedRoot);
- if (astTree == NULL) {
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 1;
- }
-
- CodeGeneratorCodes *codes = codeGenerator(astTree);
- if (codes == NULL) {
- astTreeRootDelete(astTree);
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 1;
- }
-
- char *fasm = codeGeneratorToFlatASM(codes);
- if (codeGeneratorFlatASMExec(outFilePath, fasm)) {
- system(outFilePath);
- }
-
- free(fasm);
- codeGeneratorDelete(codes);
- astTreeRootDelete(astTree);
- parserNodeDelete(parsedRoot);
- lexerNodeArrayDestroy(lexed);
- free(code);
- return 0;
+ return ret;
}
int main(int argc, char *argv[]) {