Explorar el Código

Move code runner to own file

Kajetan Johannes Hammerle hace 2 semanas
padre
commit
8463900145
Se han modificado 4 ficheros con 56 adiciones y 77 borrados
  1. 1 0
      CMakeLists.txt
  2. 1 0
      src/Code.h
  3. 53 0
      src/CodeRunner.c
  4. 1 77
      src/Main.c

+ 1 - 0
CMakeLists.txt

@@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD 23)
 set(SRC
     "src/Main.c"
     "src/Code.c"
+    "src/CodeRunner.c"
     "src/Values.c"
     "src/Error.c"
     "src/Compiler.c"

+ 1 - 0
src/Code.h

@@ -13,6 +13,7 @@ typedef enum : u8 {
 } Instruction;
 
 void codeReset();
+void codeRun();
 
 [[nodiscard]] bool codePushInstruction(Instruction i);
 [[nodiscard]] bool codePushI64(i64 i);

+ 53 - 0
src/CodeRunner.c

@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+#include "Code.h"
+#include "Values.h"
+
+#define POP_VALUE(name) \
+    Value name;         \
+    if(popValue(&name)) \
+    return true
+
+static bool iAdd() {
+    POP_VALUE(a);
+    POP_VALUE(b);
+    return pushValue(INT_VALUE(a.intValue + b.intValue));
+}
+
+static bool iPushConstantString() {
+    return pushValue(CSTRING_VALUE(codeReadConstantString()));
+}
+
+static bool iPushInt() {
+    return pushValue(INT_VALUE(codeReadI64()));
+}
+
+static bool iPrint() {
+    POP_VALUE(a);
+    switch(a.type) {
+        case INT64: printf("%ld", a.intValue); break;
+        case CONSTANT_STRING: printf("%s", a.constantStringValue); break;
+    }
+    return false;
+}
+
+static bool iPrintNewline() {
+    putchar('\n');
+    return false;
+}
+
+static bool execute(Instruction command) {
+    switch(command) {
+        case ADD: return iAdd();
+        case PUSH_CONSTANT_STRING: return iPushConstantString();
+        case PUSH_INT64: return iPushInt();
+        case PRINT: return iPrint();
+        case PRINT_NEWLINE: return iPrintNewline();
+        case STOP: return true;
+    }
+    return false;
+}
+
+void codeRun() {
+    while(!execute(codeReadInstruction())) {}
+}

+ 1 - 77
src/Main.c

@@ -3,52 +3,6 @@
 
 #include "Code.h"
 #include "Compiler.h"
-#include "Values.h"
-
-#define POP_VALUE(name) \
-    Value name;         \
-    if(popValue(&name)) \
-    return true
-
-static bool iAdd() {
-    POP_VALUE(a);
-    POP_VALUE(b);
-    return pushValue(INT_VALUE(a.intValue + b.intValue));
-}
-
-static bool iPushConstantString() {
-    return pushValue(CSTRING_VALUE(codeReadConstantString()));
-}
-
-static bool iPushInt() {
-    return pushValue(INT_VALUE(codeReadI64()));
-}
-
-static bool iPrint() {
-    POP_VALUE(a);
-    switch(a.type) {
-        case INT64: printf("%ld", a.intValue); break;
-        case CONSTANT_STRING: printf("%s", a.constantStringValue); break;
-    }
-    return false;
-}
-
-static bool iPrintNewline() {
-    putchar('\n');
-    return false;
-}
-
-static bool execute(Instruction command) {
-    switch(command) {
-        case ADD: return iAdd();
-        case PUSH_CONSTANT_STRING: return iPushConstantString();
-        case PUSH_INT64: return iPushInt();
-        case PRINT: return iPrint();
-        case PRINT_NEWLINE: return iPrintNewline();
-        case STOP: return true;
-    }
-    return false;
-}
 
 int main(int argCount, const char** args) {
     if(argCount < 2) {
@@ -59,37 +13,7 @@ int main(int argCount, const char** args) {
         puts(e->text);
         return 0;
     }
-    // if(strcmp(args[1], "./test/Print.basic") == 0) {
-    //     (void)pushInstruction(PUSH_CONSTANT_STRING);
-    //     (void)pushConstantString("Hi");
-    //     (void)pushInstruction(PRINT);
-    //     (void)pushInstruction(PRINT_NEWLINE);
-
-    //    (void)pushInstruction(PUSH_INT);
-    //    (void)pushI64(6);
-    //    (void)pushInstruction(PRINT);
-    //    (void)pushInstruction(PRINT_NEWLINE);
-
-    //    (void)pushInstruction(PUSH_CONSTANT_STRING);
-    //    (void)pushConstantString("Hi there ");
-    //    (void)pushInstruction(PRINT);
-    //    (void)pushInstruction(PUSH_INT);
-    //    (void)pushI64(8);
-    //    (void)pushInstruction(PRINT);
-    //    (void)pushInstruction(PUSH_CONSTANT_STRING);
-    //    (void)pushConstantString(" great");
-    //    (void)pushInstruction(PRINT);
-    //    (void)pushInstruction(PRINT_NEWLINE);
-    //} else if(strcmp(args[1], "./test/Add.basic") == 0) {
-    //    (void)pushInstruction(PUSH_INT);
-    //    (void)pushI64(1);
-    //    (void)pushInstruction(PUSH_INT);
-    //    (void)pushI64(20);
-    //    (void)pushInstruction(ADD);
-    //    (void)pushInstruction(PRINT);
-    //    (void)pushInstruction(PRINT_NEWLINE);
-    //}
-    while(!execute(codeReadInstruction())) {}
+    codeRun();
     // char line[256];
     // while(true) {
     //     fgets(line, sizeof(line), stdin);