Browse Source

repeated defines work correctly

Kajetan Johannes Hammerle 3 years ago
parent
commit
761f22926d
8 changed files with 122 additions and 76 deletions
  1. 1 1
      Compiler.c
  2. 50 0
      tests/pre/list
  3. 0 0
      tests/pre/list.out
  4. 23 0
      tests/pre/list_test
  5. 10 0
      tests/pre/list_test.out
  6. 0 53
      tests/test
  7. 0 5
      tests/test.out
  8. 38 17
      tokenizer/File.c

+ 1 - 1
Compiler.c

@@ -964,7 +964,7 @@ static void cStruct() {
     cConsumeToken(T_LITERAL);
     const char* name = cReadString();
     if(stsSearch(&structs, name) != NULL) {
-        cError("struct registered twice");
+        cError("struct '%s' registered twice", name);
     }
     Struct* st = stsAdd(&structs, name);
     DataType self = dtStruct(st);

+ 50 - 0
tests/pre/list

@@ -0,0 +1,50 @@
+#define LIST 
+    struct LISTNAME {
+        int size;
+        LISTTYPE* data;     
+    };
+
+    LISTNAME* LISTNEW() {
+        LISTNAME* list = new LISTNAME[1];
+        list->size = 0;
+        list->data = new LISTTYPE[4];
+        return list;
+    }
+
+    void listAdd(LISTNAME* list, LISTTYPE i) {
+        int l = length(list->data);
+        if(list->size >= l) {
+            LISTTYPE* newData = new LISTTYPE[l * 2];
+            for(int i = 0; i < l; i++) {
+                newData[i] = list->data[i];
+            }
+            delete list->data;
+            list->data = newData;
+        }
+        list->data[list->size] = i;
+        list->size++;
+    }
+
+    int listGetLength(LISTNAME* list) {
+        return list->size;
+    }
+
+    LISTTYPE listGetIndex(LISTNAME* list, int index) {
+        return list->data[index];
+    }
+
+    void listDelete(LISTNAME* list) {
+        delete list->data;
+        delete list;
+    }
+#end
+
+#define LISTNAME IntList #end
+#define LISTTYPE int #end
+#define LISTNEW listNewInt #end
+LIST
+
+#define LISTNAME FloatList #end
+#define LISTTYPE float #end
+#define LISTNEW listNewFloat #end
+LIST

+ 0 - 0
tests/pre/list.out


+ 23 - 0
tests/pre/list_test

@@ -0,0 +1,23 @@
+#include "list"
+
+void main() {
+    IntList* intList = listNewInt();
+    FloatList* floatList = listNewFloat();
+
+    for(int i = 0; i < 5; i++) {
+        listAdd(intList, i * 50 + 3);
+        listAdd(floatList, 3.0);
+    }
+    
+    for(int i = 0; i < listGetLength(intList); i++) {
+        print listGetIndex(intList, i);
+    }
+    
+    for(int i = 0; i < listGetLength(floatList); i++) {
+        print listGetIndex(floatList, i);
+    }
+    
+    listDelete(intList);
+    listDelete(floatList);
+}
+

+ 10 - 0
tests/pre/list_test.out

@@ -0,0 +1,10 @@
+3
+53
+103
+153
+203
+3.00
+3.00
+3.00
+3.00
+3.00

+ 0 - 53
tests/test

@@ -1,53 +0,0 @@
-struct IntList {
-    int size;
-    int* data;     
-};
-
-IntList* listNewInt() {
-    IntList* list = new IntList[1];
-    list->size = 0;
-    list->data = new int[4];
-    return list;
-}
-
-void listAdd(IntList* list, int i) {
-    int l = length(list->data);
-    if(list->size >= l) {
-        int* newData = new int[l * 2];
-        for(int i = 0; i < l; i++) {
-            newData[i] = list->data[i];
-        }
-        delete list->data;
-        list->data = newData;
-    }
-    list->data[list->size] = i;
-    list->size++;
-}
-
-int listGetLength(IntList* list) {
-    return list->size;
-}
-
-int listGetIndex(IntList* list, int index) {
-    return list->data[index];
-}
-
-void listDelete(IntList* list) {
-    delete list->data;
-    delete list;
-}
-
-void main() {
-    IntList* list = listNewInt();
-
-    for(int i = 0; i < 5; i++) {
-        listAdd(list, i * 50 + 3);
-    }
-    
-    for(int i = 0; i < listGetLength(list); i++) {
-        print listGetIndex(list, i);
-    }
-    
-    listDelete(list);
-}
-

+ 0 - 5
tests/test.out

@@ -1,5 +0,0 @@
-3
-53
-103
-153
-203

+ 38 - 17
tokenizer/File.c

@@ -113,28 +113,21 @@ static const char* fGetDefineName(Define* d) {
 }
 
 static int fStartDefine(OpenFile* of, bool* newLine) {
-    if(files.defineIndex >= MAX_INDEX) {
-        files.error("too many defines");
-    }
-    Define* d = files.defines + files.defineIndex++;
-
     fAddChar(of, '#');
     int end = fReserverInt(of);
+    int nameFileIndex = fReserverInt(of);
+    int nameIndex = fReserverInt(of);
+    int codeFileIndex = fReserverInt(of);
+    int codeIndex = fReserverInt(of);
 
     char command[64];
     *newLine = fReadCommandString(of, command, 64);
-    d->name.fileIndex = of->arrayIndex;
-    d->name.index = of->index;
+    fSetInt(of, nameFileIndex, of->arrayIndex);
+    fSetInt(of, nameIndex, of->index);
     fAdd(of, command, strlen(command) + 1);
 
-    for(int i = 0; i < files.defineIndex - 1; i++) {
-        if(strcmp(command, fGetDefineName(files.defines + i)) == 0) {
-            files.error("'%s' is already defined", command);
-        }
-    }
-
-    d->code.fileIndex = of->arrayIndex;
-    d->code.index = of->index;
+    fSetInt(of, codeFileIndex, of->arrayIndex);
+    fSetInt(of, codeIndex, of->index);
     return end;
 }
 
@@ -308,6 +301,35 @@ static bool fCheckForReplacement() {
     return false;
 }
 
+static void fReadDefine() {
+    int base = files.readIndex.index + 1;
+    int end = fReadInt(currentFile(), base);
+    int nameFileIndex = fReadInt(currentFile(), base + sizeof(int));
+    int nameIndex = fReadInt(currentFile(), base + sizeof(int) * 2);
+    int codeFileIndex = fReadInt(currentFile(), base + sizeof(int) * 3);
+    int codeIndex = fReadInt(currentFile(), base + sizeof(int) * 4);
+
+    const char* name = files.files[nameFileIndex].content + nameIndex;
+
+    if(files.defineIndex >= MAX_INDEX) {
+        files.error("too many defines");
+    }
+    Define* d = files.defines + files.defineIndex++;
+    for(int i = 0; i < files.defineIndex - 1; i++) {
+        if(strcmp(name, fGetDefineName(files.defines + i)) == 0) {
+            d = files.defines + i;
+            files.defineIndex--;
+            break;
+        }
+    }
+    d->name.fileIndex = nameFileIndex;
+    d->name.index = nameIndex;
+    d->code.fileIndex = codeFileIndex;
+    d->code.index = codeIndex;
+
+    files.readIndex.index = end;
+}
+
 static void fPrepareChar() {
     while(true) {
         int c = fReadChar();
@@ -319,8 +341,7 @@ static void fPrepareChar() {
         } else if(c == EOF && stackIndex > 0) {
             files.readIndex = stack[--stackIndex];
         } else if(c == '#') {
-            files.readIndex.index =
-                fReadInt(currentFile(), files.readIndex.index + 1);
+            fReadDefine();
         } else if(isLetter(c) && !isLetter(lastChar)) {
             if(fCheckForReplacement()) {
                 continue;