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

arrays addresses of structs are used correctly

Kajetan Johannes Hammerle 3 жил өмнө
parent
commit
31407c843f
8 өөрчлөгдсөн 83 нэмэгдсэн , 25 устгасан
  1. 6 5
      Compiler.c
  2. 3 2
      Main.c
  3. 53 0
      tests/test
  4. 5 0
      tests/test.out
  5. 1 1
      utils/ByteCodePrinter.c
  6. 9 9
      vm/Arrays.c
  7. 1 1
      vm/Arrays.h
  8. 5 7
      vm/Script.c

+ 6 - 5
Compiler.c

@@ -102,7 +102,7 @@ static void cTooMuchArguments() {
 }
 
 static void cUnexpectedToken(Token t) {
-    cError("unexpected token on line %d: %s", line, tGetName(t));
+    cError("unexpected token: %s", tGetName(t));
 }
 
 static void cAddOperation(Operation token) {
@@ -386,7 +386,7 @@ static DataType cStructAccess(DataType dt, int pointers) {
         cError("%s has no member %s", st->name, name);
     } else if(inner.address > 0) {
         cAddIntOperation(OP_PUSH_INT, inner.address);
-        cAddOperation(OP_ADD_REFERENCE);
+        cAddIntOperation(OP_ADD_REFERENCE, 1);
     }
     return dtToVariable(inner.type);
 }
@@ -410,8 +410,9 @@ static DataType cAccess() {
             }
             cArrayIndex("index");
             cConsumeToken(T_CLOSE_SQUARE_BRACKET);
-            cAddOperation(OP_ADD_REFERENCE);
-            dt = dtToVariable(dtReference(dt));
+            dt = dtReference(dt);
+            cAddIntOperation(OP_ADD_REFERENCE, cGetSize(dt));
+            dt = dtToVariable(dt);
         } else {
             return dt;
         }
@@ -1058,4 +1059,4 @@ const char* cGetError() {
 
 int cGetLine() {
     return line;
-}
+}

+ 3 - 2
Main.c

@@ -23,7 +23,8 @@ int main(int argAmount, const char** args) {
         }
         ByteCode* code = cCompile();
         if(code == NULL) {
-            puts(tGetError());
+            puts(cGetError());
+            printf("line: %d\n", cGetLine());
             return 0;
         }
         Script* sc = sInit(code);
@@ -34,4 +35,4 @@ int main(int argAmount, const char** args) {
         sDelete(sc);
     }
     return 0;
-}
+}

+ 53 - 0
tests/test

@@ -0,0 +1,53 @@
+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);
+}
+

+ 5 - 0
tests/test.out

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

+ 1 - 1
utils/ByteCodePrinter.c

@@ -170,7 +170,7 @@ static void btConsumeOperation() {
         PRINT_OP_INT(OP_DEREFERENCE_VAR);
         PRINT_OP(OP_REFERENCE);
         PRINT_OP(OP_DUPLICATE_REFERENCE);
-        PRINT_OP(OP_ADD_REFERENCE);
+        PRINT_OP_INT(OP_ADD_REFERENCE);
         PRINT_OP_INT(OP_NEW);
         PRINT_OP(OP_DELETE);
         PRINT_OP(OP_LENGTH);

+ 9 - 9
vm/Arrays.c

@@ -16,7 +16,7 @@ void asInit(Arrays* as) {
 
 void asDelete(Arrays* as) {
     for(int i = 0; i < as->capacity; i++) {
-        allocatedBytes -= as->data[i].length * as->data[i].typeSize;
+        allocatedBytes -= as->data[i].size;
         free(as->data[i].data);
     }
     allocatedBytes -= as->capacity * sizeof(Array);
@@ -24,8 +24,8 @@ void asDelete(Arrays* as) {
 }
 
 static void aInitArray(Array* a, int previous, int next) {
+    a->size = 0;
     a->length = 0;
-    a->typeSize = 0;
     a->next = next;
     a->previous = previous;
     a->data = NULL;
@@ -63,9 +63,9 @@ static void asPrintDebug(Arrays* as) {
     printf("Free: %d, Used: %d\n", as->freeStart, as->usedStart);
     for(int i = 0; i < as->capacity; i++) {
         Array* a = as->data + i;
-        printf("%d: %s, length: %d, next: %d, previous: %d, size: %d\n", i,
-               a->data == NULL ? "null" : "valid", a->length, a->next,
-               a->previous, a->typeSize);
+        printf("%d: %s, size: %d, next: %d, previous: %d\n", i,
+               a->data == NULL ? "null" : "valid", a->size, a->next,
+               a->previous);
     }
 }
 
@@ -96,14 +96,14 @@ int asAllocate(Arrays* as, int typeSize, int length) {
     }
     as->usedStart = index;
 
+    array->size = bytes;
     array->length = length;
-    array->typeSize = typeSize;
     array->data = malloc(bytes);
     return index;
 }
 
 Array* asGet(Arrays* as, int p) {
-    if(p < 0 || p >= as->capacity || as->data[p].typeSize == 0) {
+    if(p < 0 || p >= as->capacity || as->data[p].size == 0) {
         return NULL;
     }
     return as->data + p;
@@ -128,10 +128,10 @@ void asDeleteArray(Arrays* as, Array* a, int p) {
     }
     as->freeStart = p;
 
-    allocatedBytes -= a->typeSize * a->length;
+    allocatedBytes -= a->size;
     a->previous = -1;
+    a->size = 0;
     a->length = 0;
-    a->typeSize = 0;
     free(a->data);
     a->data = NULL;
 }

+ 1 - 1
vm/Arrays.h

@@ -6,8 +6,8 @@
 #include "DataType.h"
 
 typedef struct {
+    int size;
     int length;
-    int typeSize;
     int next;
     int previous;
     void* data;

+ 5 - 7
vm/Script.c

@@ -196,14 +196,11 @@ static void* sCheckAddress(Script* sc, Pointer* p, int length) {
         if(a == NULL) {
             sError(sc, "invalid heap pointer");
             return NULL;
-        } else if(p->offset < 0 || p->offset >= a->length) {
+        } else if(p->offset < 0 || p->offset >= a->size) {
             sError(sc, "address %d is out of array bounds", p->offset);
             return NULL;
-        } else if(length != a->typeSize) {
-            sError(sc, "sizes do not match: %d != %d", length, a->typeSize);
-            return NULL;
         }
-        return ((char*)a->data) + p->offset * a->typeSize;
+        return ((char*)a->data) + p->offset;
     }
     if(p->offset < 0 || p->offset + length > sc->stackIndex) {
         sError(sc, "address %d is out of stack bounds", p->offset);
@@ -378,10 +375,11 @@ static void sDuplicateReference(Script* sc) {
 }
 
 static void sAddReference(Script* sc) {
+    int size = 0;
     int add = 0;
     Pointer p;
-    if(sPopInt(sc, &add) && sPopPointer(sc, &p)) {
-        p.offset += add;
+    if(sReadInt(sc, &size) && sPopInt(sc, &add) && sPopPointer(sc, &p)) {
+        p.offset += add * size;
         sPushPointer(sc, &p);
     }
 }