Browse Source

tokenizer shows correct error lines and path trace, compiler shows
correct error line and path

Kajetan Johannes Hammerle 1 year ago
parent
commit
c0b3291c19
8 changed files with 68 additions and 33 deletions
  1. 17 16
      Compiler.c
  2. 2 3
      Compiler.h
  3. 3 3
      Main.c
  4. 5 5
      Test.c
  5. 1 1
      tokenizer/FileTokens.c
  6. 2 0
      tokenizer/Token.c
  7. 2 0
      tokenizer/Token.h
  8. 36 5
      tokenizer/Tokenizer.c

+ 17 - 16
Compiler.c

@@ -11,10 +11,10 @@
 #include "utils/Variables.h"
 #include "vm/Operation.h"
 
-#define ERROR_LENGTH 256
-static char error[ERROR_LENGTH] = {'\0'};
+static Error* error = NULL;
 static ByteCode* code;
 static int16 line = 1;
+static const char* path = NULL;
 
 static jmp_buf errorJump;
 
@@ -75,7 +75,7 @@ TYPE_OP(RIGHT_SHIFT, NOTHING, NOTHING, ">>")
 static void cError(const char* format, ...) {
     va_list args;
     va_start(args, format);
-    vsnprintf(error, ERROR_LENGTH, format, args);
+    eInitErrorV(error, path, line, format, args);
     va_end(args);
     longjmp(errorJump, 0);
 }
@@ -1031,7 +1031,15 @@ static void cStruct() {
 }
 
 static void cGlobalScope(Token t) {
-    if(t == T_STRUCT) {
+    if(t == T_OPEN_PATH) {
+        path = cReadString();
+        // printf("OPEN PATH %s\n", path);
+        return;
+    } else if(t == T_CLOSE_PATH) {
+        path = cReadString();
+        // printf("CLOSE OLD PATH - OPEN PATH %s\n", path);
+        return;
+    } else if(t == T_STRUCT) {
         cStruct();
         return;
     }
@@ -1108,21 +1116,14 @@ static void cAllocAndCompile() {
     vsDelete(&vars);
 }
 
-ByteCode* cCompile() {
-    error[0] = '\0';
+ByteCode* cCompile(Error* e) {
+    error = e;
+    eInitSuccess(e);
     code = bcInit();
     cAllocAndCompile();
-    if(error[0] != '\0') {
+    if(eHasError(e)) {
         bcDelete(code);
         return NULL;
     }
     return code;
-}
-
-const char* cGetError() {
-    return error;
-}
-
-int cGetLine() {
-    return line;
-}
+}

+ 2 - 3
Compiler.h

@@ -5,11 +5,10 @@
 extern "C" {
 #endif
 
+#include "Error.h"
 #include "vm/ByteCode.h"
 
-ByteCode* cCompile();
-const char* cGetError();
-int cGetLine();
+ByteCode* cCompile(Error* e);
 
 #ifdef __cplusplus
 }

+ 3 - 3
Main.c

@@ -28,10 +28,10 @@ static void start(int argAmount, const char** args) {
             printf("path: %s\n", e.paths);
             return;
         }
-        ByteCode* code = cCompile();
+        ByteCode* code = cCompile(&e);
         if(code == NULL) {
-            puts(cGetError());
-            printf("line: %d\n", cGetLine());
+            puts(e.message);
+            printf("line: %d\n", e.line);
             printf("path: %s\n", e.paths);
             return;
         }

+ 5 - 5
Test.c

@@ -127,16 +127,16 @@ static void tsCheckFile() {
     Error e;
     tTokenize(path, &e);
     if(eHasError(&e)) {
-        puts(path);
         puts(e.message);
         printf("line: %d\n", e.line);
+        printf("path: %s\n", e.paths);
         return;
     }
-    ByteCode* bc = cCompile();
+    ByteCode* bc = cCompile(&e);
     if(bc == NULL) {
-        puts(path);
-        puts(cGetError());
-        printf("line: %d\n", cGetLine());
+        puts(e.message);
+        printf("line: %d\n", e.line);
+        printf("path: %s\n", e.paths);
         return;
     }
     Script* sc = sInit(bc);

+ 1 - 1
tokenizer/FileTokens.c

@@ -6,7 +6,7 @@
 #include "tokenizer/FileTokens.h"
 #include "utils/SnuviUtils.h"
 
-static Error* error;
+static Error* error = NULL;
 
 typedef char String[64];
 #define STRING_LENGTH ((int)sizeof(String))

+ 2 - 0
tokenizer/Token.c

@@ -61,6 +61,8 @@ const char* tGetName(Token token) {
         case T_CLOSE_SQUARE_BRACKET: return "]";
         case T_NEW: return "new";
         case T_LENGTH: return "length";
+        case T_OPEN_PATH: return "open path";
+        case T_CLOSE_PATH: return "close path";
         case T_END: return "end";
     }
     return "unknown";

+ 2 - 0
tokenizer/Token.h

@@ -65,6 +65,8 @@ typedef enum {
     T_CLOSE_SQUARE_BRACKET,
     T_NEW,
     T_LENGTH,
+    T_OPEN_PATH,
+    T_CLOSE_PATH,
     T_END
 } Token;
 

+ 36 - 5
tokenizer/Tokenizer.c

@@ -11,12 +11,19 @@
 #include "utils/SnuviUtils.h"
 
 #define TOKEN_BUFFER_LENGTH (1024 * 1024)
-#define ERROR_LENGTH 256
+#define INCLUDE_STACK_LENGTH 32
 
 static FileTokens fileTokens;
 static int fileTokenIndex;
 static jmp_buf errorJump;
-static Error* error;
+static Error* error = NULL;
+
+typedef struct {
+    const char* path;
+    int line;
+} IncludeEntry;
+static IncludeEntry includeStack[INCLUDE_STACK_LENGTH];
+static int includeStackIndex = 0;
 
 static char tokenBuffer[TOKEN_BUFFER_LENGTH];
 static int writeIndex = 0;
@@ -26,7 +33,13 @@ static int16 line = 1;
 static void tError(const char* format, ...) {
     va_list args;
     va_start(args, format);
-    eInitErrorV(error, "path not set", line, format, args);
+    const char* path = includeStackIndex > 0
+                           ? includeStack[includeStackIndex - 1].path
+                           : "path not set";
+    eInitErrorV(error, path, line, format, args);
+    for(int i = includeStackIndex - 2; i >= 0; i--) {
+        eAddPath(error, includeStack[i].path);
+    }
     va_end(args);
     longjmp(errorJump, 0);
 }
@@ -306,10 +319,27 @@ static void tAddToken4(int c, Token tce, Token tc, Token te, Token t) {
 static void tParseToken(FileToken* t) {
     switch(t->type) {
         case FT_PATH:
-            // TODO: do something useful with the path
+            if(includeStackIndex >= INCLUDE_STACK_LENGTH) {
+                tError("include stack overflow");
+            }
+            includeStack[includeStackIndex].path = t->literal;
+            includeStack[includeStackIndex].line = line;
+            includeStackIndex++;
+            line = 1;
+            tAddToken(T_OPEN_PATH);
+            tAdd(t->literal, strlen(t->literal) + 1);
             return;
         case FT_END_PATH:
-            // TODO: do something useful
+            if(includeStackIndex <= 0) {
+                tError("include stack underflow");
+            }
+            includeStackIndex--;
+            line = includeStack[includeStackIndex].line;
+            tAddToken(T_CLOSE_PATH);
+            const char* path = includeStackIndex > 0
+                                   ? includeStack[includeStackIndex - 1].path
+                                   : "path not set";
+            tAdd(path, strlen(path) + 1);
             return;
         case FT_NEWLINE: line++; return;
         case FT_LITERAL: {
@@ -393,6 +423,7 @@ void tTokenize(const char* path, Error* e) {
     if(eHasError(e)) {
         ftPrint(&fileTokens);
     }
+    includeStackIndex = 0;
     ftDelete(&fileTokens);
 }