From 76272a881916b466f42f78ba16c696a843628d08 Mon Sep 17 00:00:00 2001
From: A404M <ahmadmahmoudiprogrammer@gmail.com>
Date: Thu, 23 Jan 2025 19:57:55 +0330
Subject: cleaning the code

---
 src/main.c | 149 ++++++++++++++++++++++---------------------------------------
 1 file changed, 54 insertions(+), 95 deletions(-)

(limited to 'src/main.c')

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[]) {
-- 
cgit v1.2.3