From f090d484ab9425be9d7e4e3f42ba4224dde35c2d Mon Sep 17 00:00:00 2001
From: A404M <ahmadmahmoudiprogrammer@gmail.com>
Date: Wed, 29 Jan 2025 03:06:16 +0330
Subject: move assembly to new file make constants to have a separate token

---
 src/compiler/ast-tree.c       | 26 +++++++++++++-------------
 src/compiler/ast-tree.h       |  2 +-
 src/compiler/code-generator.c | 16 +++++-----------
 3 files changed, 19 insertions(+), 25 deletions(-)

(limited to 'src')

diff --git a/src/compiler/ast-tree.c b/src/compiler/ast-tree.c
index dcffecc..2c034d1 100644
--- a/src/compiler/ast-tree.c
+++ b/src/compiler/ast-tree.c
@@ -123,7 +123,7 @@ void astTreePrint(const AstTree *tree, int indent) {
     printf("]");
   }
     goto RETURN_SUCCESS;
-  case AST_TREE_TOKEN_VARIABLE: {
+  case AST_TREE_TOKEN_CONSTANT: {
     AstTreeVariable *metadata = tree->metadata;
     printf(",variable.name=%.*s",
            (int)(metadata->name_end - metadata->name_begin),
@@ -201,7 +201,7 @@ void astTreeDestroy(AstTree tree) {
     free(metadata);
   }
     return;
-  case AST_TREE_TOKEN_VARIABLE: {
+  case AST_TREE_TOKEN_CONSTANT: {
     // AstTreeIdentifier *metadata = tree.metadata; // not needed
   }
     return;
@@ -255,8 +255,8 @@ AstTree *copyAstTree(AstTree *tree) {
   case AST_TREE_TOKEN_VALUE_U64:
     return newAstTree(AST_TREE_TOKEN_VALUE_U64,
                       (void *)(AstTreeU64)tree->metadata);
-  case AST_TREE_TOKEN_VARIABLE:
-    return newAstTree(AST_TREE_TOKEN_VARIABLE, tree->metadata);
+  case AST_TREE_TOKEN_CONSTANT:
+    return newAstTree(AST_TREE_TOKEN_CONSTANT, tree->metadata);
   case AST_TREE_TOKEN_FUNCTION:
   case AST_TREE_TOKEN_KEYWORD_PRINT_U64:
   case AST_TREE_TOKEN_TYPE_FUNCTION:
@@ -623,7 +623,7 @@ AstTree *astTreeParseFunctionCall(ParserNode *parserNode,
   if (function == NULL) {
     return NULL;
   }
-  if (function->token != AST_TREE_TOKEN_VARIABLE) {
+  if (function->token != AST_TREE_TOKEN_CONSTANT) {
     printLog("Not yet supported");
     return NULL;
   }
@@ -665,7 +665,7 @@ AstTree *astTreeParseIdentifier(ParserNode *parserNode,
     printLog("Variable not found");
     return NULL;
   }
-  return newAstTree(AST_TREE_TOKEN_VARIABLE, var);
+  return newAstTree(AST_TREE_TOKEN_CONSTANT, var);
 }
 
 AstTree *astTreeParsePrintU64(ParserNode *parserNode,
@@ -732,7 +732,7 @@ RETURN_ERROR:
 }
 
 bool hasTypeOf(AstTree *value, const AstTree *type) {
-  if (value->token == AST_TREE_TOKEN_VARIABLE) {
+  if (value->token == AST_TREE_TOKEN_CONSTANT) {
     AstTreeVariable *variable = value->metadata;
     return typeIsEqual(variable->type, type);
   }
@@ -748,7 +748,7 @@ bool hasTypeOf(AstTree *value, const AstTree *type) {
     case AST_TREE_TOKEN_FUNCTION:
     case AST_TREE_TOKEN_KEYWORD_PRINT_U64:
     case AST_TREE_TOKEN_FUNCTION_CALL:
-    case AST_TREE_TOKEN_VARIABLE:
+    case AST_TREE_TOKEN_CONSTANT:
     case AST_TREE_TOKEN_VALUE_U64:
     case AST_TREE_TOKEN_NONE:
       return false;
@@ -777,7 +777,7 @@ bool hasTypeOf(AstTree *value, const AstTree *type) {
     case AST_TREE_TOKEN_TYPE_VOID:
     case AST_TREE_TOKEN_TYPE_U64:
     case AST_TREE_TOKEN_FUNCTION_CALL:
-    case AST_TREE_TOKEN_VARIABLE:
+    case AST_TREE_TOKEN_CONSTANT:
       return false;
     case AST_TREE_TOKEN_NONE:
     }
@@ -787,7 +787,7 @@ bool hasTypeOf(AstTree *value, const AstTree *type) {
   case AST_TREE_TOKEN_FUNCTION:
   case AST_TREE_TOKEN_TYPE_VOID:
   case AST_TREE_TOKEN_FUNCTION_CALL:
-  case AST_TREE_TOKEN_VARIABLE:
+  case AST_TREE_TOKEN_CONSTANT:
   case AST_TREE_TOKEN_KEYWORD_PRINT_U64:
   case AST_TREE_TOKEN_VALUE_U64:
     return false;
@@ -828,7 +828,7 @@ AstTree *makeTypeOf(AstTree *value) {
   }
   case AST_TREE_TOKEN_VALUE_U64:
     return newAstTree(AST_TREE_TOKEN_TYPE_U64, NULL);
-  case AST_TREE_TOKEN_VARIABLE:
+  case AST_TREE_TOKEN_CONSTANT:
   case AST_TREE_TOKEN_KEYWORD_PRINT_U64:
   case AST_TREE_TOKEN_NONE:
   }
@@ -855,8 +855,8 @@ bool typeIsEqual(const AstTree *type0, const AstTree *type1) {
   case AST_TREE_TOKEN_FUNCTION_CALL:
     printLog("Not implemented yet");
     exit(1);
-  case AST_TREE_TOKEN_VARIABLE:
-    return type1->token == AST_TREE_TOKEN_VARIABLE &&
+  case AST_TREE_TOKEN_CONSTANT:
+    return type1->token == AST_TREE_TOKEN_CONSTANT &&
            type0->metadata == type1->metadata;
   case AST_TREE_TOKEN_NONE:
     break;
diff --git a/src/compiler/ast-tree.h b/src/compiler/ast-tree.h
index c5f8dd5..7590003 100644
--- a/src/compiler/ast-tree.h
+++ b/src/compiler/ast-tree.h
@@ -15,7 +15,7 @@ typedef enum AstTreeToken {
   AST_TREE_TOKEN_TYPE_U64,
 
   AST_TREE_TOKEN_FUNCTION_CALL,
-  AST_TREE_TOKEN_VARIABLE,
+  AST_TREE_TOKEN_CONSTANT,
   AST_TREE_TOKEN_VALUE_U64,
 
   AST_TREE_TOKEN_NONE,
diff --git a/src/compiler/code-generator.c b/src/compiler/code-generator.c
index 342c790..27a0fdc 100644
--- a/src/compiler/code-generator.c
+++ b/src/compiler/code-generator.c
@@ -81,7 +81,7 @@ CodeGeneratorCodes *codeGenerator(AstTreeRoot *astTreeRoot) {
     case AST_TREE_TOKEN_TYPE_FUNCTION:
     case AST_TREE_TOKEN_TYPE_VOID:
     case AST_TREE_TOKEN_VALUE_U64:
-    case AST_TREE_TOKEN_VARIABLE:
+    case AST_TREE_TOKEN_CONSTANT:
     case AST_TREE_TOKEN_KEYWORD_PRINT_U64:
     case AST_TREE_TOKEN_FUNCTION_CALL:
     case AST_TREE_TOKEN_TYPE_U64:
@@ -110,7 +110,7 @@ bool codeGeneratorAstTreeFunction(char *label_begin, char *label_end,
         printLog("Not implemented");
         exit(0);
       }
-      if (function->token != AST_TREE_TOKEN_VARIABLE) {
+      if (function->token != AST_TREE_TOKEN_CONSTANT) {
         printLog("Not implemented");
         exit(0);
       }
@@ -132,7 +132,7 @@ bool codeGeneratorAstTreeFunction(char *label_begin, char *label_end,
             codes, createGenerateCode(label_begin, label_end,
                                       CODE_GENERATOR_INSTRUCTION_PRINT_U64,
                                       (void *)value));
-      } else if (metadata->token == AST_TREE_TOKEN_VARIABLE) {
+      } else if (metadata->token == AST_TREE_TOKEN_CONSTANT) {
         AstTreeVariable *variable = metadata->metadata;
         CodeGeneratorOperandU64 value = (AstTreeU64)variable->value->metadata;
         generateCodePushCode(
@@ -146,7 +146,7 @@ bool codeGeneratorAstTreeFunction(char *label_begin, char *label_end,
     }
       goto OK;
     case AST_TREE_TOKEN_VALUE_U64:
-    case AST_TREE_TOKEN_VARIABLE:
+    case AST_TREE_TOKEN_CONSTANT:
     case AST_TREE_TOKEN_FUNCTION:
     case AST_TREE_TOKEN_TYPE_TYPE:
     case AST_TREE_TOKEN_TYPE_FUNCTION:
@@ -169,13 +169,7 @@ bool codeGeneratorAstTreeFunction(char *label_begin, char *label_end,
 }
 
 static const char TEMPLATE[] =
-    "format ELF64 executable 3\n\nSYS_exit = 60\nSYS_write = 1\nSTDOUT = "
-    "1\n\nsegment readable executable\nentry _start\n\n; rdi = the "
-    "number\nprint_u64:\nmov rcx, rsp\nmov rax, rdi\nmov rbx, 10\n\n.L:\nxor "
-    "rdx, rdx\ndiv rbx\nadd dl, '0'\ndec rcx\nmov [rcx],dl\ncmp rax, 0\njnz "
-    ".L\n\nmov rax, SYS_write\nmov rdi, STDOUT\nmov rsi, rcx\n\nmov rdx, "
-    "rsp\nsub rdx, rcx\n\nsyscall\nret\n\n_start:\ncall main\nmov rax, "
-    "SYS_exit\nxor rdi,rdi\nsyscall\n";
+    "include 'stdlib/main.asm'\n";
 
 static const size_t TEMPLATE_LEN =
     sizeof(TEMPLATE) / sizeof(*TEMPLATE) - sizeof(*TEMPLATE);
-- 
cgit v1.2.3