Эх сурвалжийг харах

Namespace for code functions

Kajetan Johannes Hammerle 2 долоо хоног өмнө
parent
commit
1de3d81cda
4 өөрчлөгдсөн 38 нэмэгдсэн , 38 устгасан
  1. 19 19
      src/Code.c
  2. 9 9
      src/Code.h
  3. 7 7
      src/Compiler.c
  4. 3 3
      src/Main.c

+ 19 - 19
src/Code.c

@@ -6,12 +6,12 @@ static u8 code[MAX_CODE];
 static size_t codeIndex = 0;
 static size_t codeExecutionIndex = 0;
 
-void resetCode() {
+void codeReset() {
     codeIndex = 0;
     codeExecutionIndex = 0;
 }
 
-static bool pushU8(u8 u) {
+static bool codePushU8(u8 u) {
     if(codeIndex >= MAX_CODE) {
         return true;
     }
@@ -19,7 +19,7 @@ static bool pushU8(u8 u) {
     return false;
 }
 
-static bool push(const void* p, size_t n) {
+static bool codePush(const void* p, size_t n) {
     if(codeIndex + n >= MAX_CODE) {
         return true;
     }
@@ -28,48 +28,48 @@ static bool push(const void* p, size_t n) {
     return false;
 }
 
-bool pushInstruction(Instruction i) {
-    return pushU8(i);
+bool codePushInstruction(Instruction i) {
+    return codePushU8(i);
 }
 
-bool pushI64(i64 i) {
-    return push(&i, sizeof(i));
+bool codePushI64(i64 i) {
+    return codePush(&i, sizeof(i));
 }
 
-bool pushSize(size_t i) {
-    return push(&i, sizeof(i));
+bool codePushSize(size_t i) {
+    return codePush(&i, sizeof(i));
 }
 
-bool pushConstantString(const char* c) {
+bool codePushConstantString(const char* c) {
     size_t n = strlen(c) + 1;
-    return pushSize(n) || push(c, n);
+    return codePushSize(n) || codePush(c, n);
 }
 
-Instruction readInstruction() {
+Instruction codeReadInstruction() {
     return codeExecutionIndex < codeIndex ? code[codeExecutionIndex++] : STOP;
 }
 
-static void read(void* p, size_t n) {
+static void codeRead(void* p, size_t n) {
     if(codeExecutionIndex + n <= codeIndex) {
         memcpy(p, code + codeExecutionIndex, n);
         codeExecutionIndex += n;
     }
 }
 
-i64 readI64() {
+i64 codeReadI64() {
     i64 i = 0;
-    read(&i, sizeof(i));
+    codeRead(&i, sizeof(i));
     return i;
 }
 
-size_t readSize() {
+size_t codeReadSize() {
     size_t i = 0;
-    read(&i, sizeof(i));
+    codeRead(&i, sizeof(i));
     return i;
 }
 
-const char* readConstantString() {
-    size_t n = readSize();
+const char* codeReadConstantString() {
+    size_t n = codeReadSize();
     if(codeExecutionIndex + n <= codeIndex &&
        code[codeExecutionIndex + n - 1] == '\0') {
         const char* s = (char*)(code + codeExecutionIndex);

+ 9 - 9
src/Code.h

@@ -12,16 +12,16 @@ typedef enum : u8 {
     STOP
 } Instruction;
 
-void resetCode();
+void codeReset();
 
-[[nodiscard]] bool pushInstruction(Instruction i);
-[[nodiscard]] bool pushI64(i64 i);
-[[nodiscard]] bool pushSize(size_t i);
-[[nodiscard]] bool pushConstantString(const char* c);
+[[nodiscard]] bool codePushInstruction(Instruction i);
+[[nodiscard]] bool codePushI64(i64 i);
+[[nodiscard]] bool codePushSize(size_t i);
+[[nodiscard]] bool codePushConstantString(const char* c);
 
-Instruction readInstruction();
-i64 readI64();
-size_t readSize();
-const char* readConstantString();
+Instruction codeReadInstruction();
+i64 codeReadI64();
+size_t codeReadSize();
+const char* codeReadConstantString();
 
 #endif

+ 7 - 7
src/Compiler.c

@@ -267,11 +267,11 @@ static double readDouble() {
 static void compileExpression() {
     Token t = nextToken();
     if(t == STRING) {
-        CODE(pushInstruction(PUSH_CONSTANT_STRING));
-        CODE(pushConstantString(readString()));
+        CODE(codePushInstruction(PUSH_CONSTANT_STRING));
+        CODE(codePushConstantString(readString()));
     } else if(t == INT64) {
-        CODE(pushInstruction(PUSH_INT64));
-        CODE(pushI64(readInt64()));
+        CODE(codePushInstruction(PUSH_INT64));
+        CODE(codePushI64(readInt64()));
     } else {
         unexpectedToken(t);
     }
@@ -285,17 +285,17 @@ static void compileLine(Token t) {
     if(strcmp(s, "print") == 0) {
         while(peekToken() != NEWLINE) {
             compileExpression();
-            CODE(pushInstruction(PRINT));
+            CODE(codePushInstruction(PRINT));
         }
         nextToken();
-        CODE(pushInstruction(PRINT_NEWLINE));
+        CODE(codePushInstruction(PRINT_NEWLINE));
     } else {
         THROW_ERROR("Unexpected literal(%s) on line %zu", s, lineCounter);
     }
 }
 
 static void parseTokens() {
-    resetCode();
+    codeReset();
     while(true) {
         Token t = nextToken();
         if(t == END) {

+ 3 - 3
src/Main.c

@@ -17,11 +17,11 @@ static bool iAdd() {
 }
 
 static bool iPushConstantString() {
-    return pushValue(CSTRING_VALUE(readConstantString()));
+    return pushValue(CSTRING_VALUE(codeReadConstantString()));
 }
 
 static bool iPushInt() {
-    return pushValue(INT_VALUE(readI64()));
+    return pushValue(INT_VALUE(codeReadI64()));
 }
 
 static bool iPrint() {
@@ -89,7 +89,7 @@ int main(int argCount, const char** args) {
     //    (void)pushInstruction(PRINT);
     //    (void)pushInstruction(PRINT_NEWLINE);
     //}
-    while(!execute(readInstruction())) {}
+    while(!execute(codeReadInstruction())) {}
     // char line[256];
     // while(true) {
     //     fgets(line, sizeof(line), stdin);