From addd54dc31603dc204773d3108dba4e000cd7657 Mon Sep 17 00:00:00 2001
From: A404M <ahmadmahmoudiprogrammer@gmail.com>
Date: Tue, 8 Oct 2024 04:16:27 +0330
Subject: added fasm support added compiler options tried to compile to fasm
 first

---
 src/main.c | 240 ++++++++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 173 insertions(+), 67 deletions(-)

(limited to 'src/main.c')

diff --git a/src/main.c b/src/main.c
index 0764388..8f9b095 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,79 +1,26 @@
-#include <compiler/tree_parser/tree_parser.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 #include <utils/file.h>
-#include <utils/time.h>
-#include <utils/types.h>
 #include <vm/runner/runner.h>
 
 #include "compiler/source_code/source_code.h"
+#include "fasm/code_generator/code_generator.h"
+#include "fasm/lexer/lexer.h"
+#include "fasm/linker/linker.h"
+#include "fasm/runner/runner.h"
+#include "utils/types.h"
 
-Clock runWithPrint(SourceCode *code) {
-  Clock sum = 0;
-  Clock diff = 0;
-  fprintf(stderr, "----code:\n%s\n----\n", code->codes[0]->code);
-  Clock start = getTimeInNano();
-  const Nodes nodes = lexer(code, 0);
-  diff += getTimeInNano() - start;
-  sum += diff;
-  if (nodes.size != ERROR_SIZE) {
-    printNodes(nodes);
-    fprintf(stderr, "----lexing in %ldns\n", diff);
-    start = getTimeInNano();
-    ParsedNode *parsedNode = _parser(nodes, code);
-    diff = getTimeInNano() - start;
-    sum += diff;
-    if (parsedNode != NULL) {
-      printParsedNode(parsedNode);
-      fprintf(stderr, "----node parsing in %ldns\n", diff);
-      start = getTimeInNano();
-      ParsedTree *parsedTree = _treeParser(parsedNode, code);
-      diff = getTimeInNano() - start;
-      sum += diff;
-      if (parsedTree != NULL) {
-        printParsedTreeNode(parsedTree);
-        fprintf(stderr, "----tree parsing in %ldns\n", diff);
-        start = getTimeInNano();
-        Instructions instructions = _codeGenerator(parsedTree, code);
-        diff = getTimeInNano() - start;
-        sum += diff;
-        if (instructions.size != ERROR_SIZE) {
-          printInstructions(instructions);
-          fprintf(stderr, "----code_generator in %ldns\n", diff);
-          start = getTimeInNano();
-          bool ranSuccess = _runner(instructions);
-          diff = getTimeInNano() - start;
-          sum += diff;
-          fprintf(stderr, "----runner in %ldns\n", diff);
-          fprintf(stderr, "ran sucessfully = %s\n",
-                  ranSuccess ? "true" : "false");
-          fprintf(stderr, "----sum %ldns\n", sum);
-          deleteInstructions(instructions);
-        } else {
-          fprintf(stderr, "----returned error");
-        }
-        deleteParsedTree(parsedTree);
-      } else {
-        fprintf(stderr, "----returned error");
-      }
-      deleteParsedNode(parsedNode);
-    } else {
-      fprintf(stderr, "----returned error");
-    }
-  } else {
-    fprintf(stderr, "----returned error");
-  }
-  deleteNodes(nodes);
-  return sum;
-}
+typedef enum FelanMode {
+  FELAN_MODE_NONE,
+  FELAN_MODE_COMPILE_FASM,
+  FELAN_MODE_COMPILE_FELAN,
+  FELAN_MODE_RUN_FASM,
+  FELAN_MODE_RUN_FELAN,
+} FelanMode;
 
-int main(int argc, char *argv[]) {
-  if (argc < 2) {
-    fprintf(stderr, "no file");
-    return 1;
-  }
+int runFelan(const char *const filePath) {
   SourceCode sourceCode = makeSourceCode();
-  const char *const filePath = argv[1];
 
   Code *code;
   code = read_whole_file("std/builtins.felan");
@@ -99,3 +46,162 @@ RETURN_ERROR:
   deleteSourceCodeInners(sourceCode);
   return 1;
 }
+
+int compileFasm(const char *const filePath) {
+  SourceCode sourceCode = makeSourceCode();
+
+  Code *code = read_whole_file(filePath);
+  if (code == NULL) {
+    goto RETURN_ERROR;
+  }
+  pushToSourceCode(&sourceCode, code);
+
+  printf("----lexing:\n");
+  FasmLines *lines;
+  if ((lines = fasmLexer(&sourceCode)) == NULL) {
+    goto RETURN_ERROR;
+  }
+
+  for (size_t i = 0; i < sourceCode.size; ++i) {
+    fasmLinesPrint(lines[i]);
+  }
+
+  printf("----linking:\n");
+
+  FasmLinkedLines linkedLines = fasmLinker(lines, &sourceCode);
+
+  if (linkedLines.lines_size == ERROR_SIZE) {
+    goto RETURN_LINKED_ERROR;
+  }
+
+  fasmLinkedLinesPrint(linkedLines);
+
+  ByteCode bytecode = fasmCodeGenerator(&linkedLines);
+
+  deleteByteCodeInners(bytecode);
+
+  fasmLinkedLinesDeleteInner(linkedLines);
+
+  for (size_t i = 0; i < sourceCode.size; ++i) {
+    fasmLinesDeleteInner(lines[i]);
+  }
+
+  free(lines);
+  deleteSourceCodeInners(sourceCode);
+  return 0;
+
+RETURN_LINKED_ERROR:
+  for (size_t i = 0; i < sourceCode.size; ++i) {
+    fasmLinesDeleteInner(lines[i]);
+  }
+RETURN_ERROR:
+  deleteSourceCodeInners(sourceCode);
+  return 1;
+}
+
+int runFasm(const char *const filePath) {
+  SourceCode sourceCode = makeSourceCode();
+
+  Code *code = read_whole_file(filePath);
+  if (code == NULL) {
+    goto RETURN_ERROR;
+  }
+  pushToSourceCode(&sourceCode, code);
+
+  FasmLines *lines;
+  if ((lines = fasmLexer(&sourceCode)) == NULL) {
+    goto RETURN_ERROR;
+  }
+
+  FasmLinkedLines linkedLines = fasmLinker(lines, &sourceCode);
+
+  if (linkedLines.lines_size == ERROR_SIZE) {
+    goto RETURN_LINKED_ERROR;
+  }
+
+  ByteCode bytecode = fasmCodeGenerator(&linkedLines);
+
+  uint32_t result = fasmRunner(bytecode);
+
+  deleteByteCodeInners(bytecode);
+
+  fasmLinkedLinesDeleteInner(linkedLines);
+
+  for (size_t i = 0; i < sourceCode.size; ++i) {
+    fasmLinesDeleteInner(lines[i]);
+  }
+
+  free(lines);
+  deleteSourceCodeInners(sourceCode);
+  return result;
+
+RETURN_LINKED_ERROR:
+  for (size_t i = 0; i < sourceCode.size; ++i) {
+    fasmLinesDeleteInner(lines[i]);
+  }
+RETURN_ERROR:
+  deleteSourceCodeInners(sourceCode);
+  return 1;
+}
+
+int main(int argc, char *argv[]) {
+  FelanMode compileMode = FELAN_MODE_NONE;
+
+  char const *filePath = NULL;
+
+  for (int i = 1; i < argc; ++i) {
+    const char *const arg = argv[i];
+    if (strcmp(arg, "compile-fasm") == 0) {
+      if (compileMode == FELAN_MODE_NONE) {
+        compileMode = FELAN_MODE_COMPILE_FASM;
+      } else {
+        fprintf(stderr, "'%s' is not expected\n", arg);
+        return 1;
+      }
+    } else if (strcmp(arg, "compile-felan") == 0) {
+      if (compileMode == FELAN_MODE_NONE) {
+        compileMode = FELAN_MODE_COMPILE_FELAN;
+      } else {
+        fprintf(stderr, "'%s' is not expected\n", arg);
+        return 1;
+      }
+      fprintf(stderr, "'%s' is not yet supported\n", arg);
+      return 1;
+    } else if (strcmp(arg, "run-fasm") == 0) {
+      if (compileMode == FELAN_MODE_NONE) {
+        compileMode = FELAN_MODE_RUN_FASM;
+      } else {
+        fprintf(stderr, "'%s' is not expected\n", arg);
+        return 1;
+      }
+    } else if (strcmp(arg, "run-felan") == 0) {
+      if (compileMode == FELAN_MODE_NONE) {
+        compileMode = FELAN_MODE_RUN_FELAN;
+      } else {
+        fprintf(stderr, "'%s' is not expected\n", arg);
+        return 1;
+      }
+    } else if (filePath == NULL) {
+      filePath = arg;
+    } else {
+      fprintf(stderr, "'%s' is not expected\n", arg);
+      return 1;
+    }
+  }
+
+  if (filePath == NULL) {
+    fprintf(stderr, "Need a file path to operate\n");
+  }
+
+  switch (compileMode) {
+    case FELAN_MODE_COMPILE_FASM:
+      return compileFasm(filePath);
+    case FELAN_MODE_COMPILE_FELAN:
+      return 1;
+    case FELAN_MODE_RUN_FASM:
+      return runFasm(filePath);
+    case FELAN_MODE_NONE:
+    case FELAN_MODE_RUN_FELAN:
+      return runFelan(filePath);
+  }
+}
-- 
cgit v1.2.3