Procházet zdrojové kódy

base for correct struct sizes

Kajetan Johannes Hammerle před 3 roky
rodič
revize
daec614031
9 změnil soubory, kde provedl 18 přidání a 17 odebrání
  1. 6 6
      Compiler.c
  2. 2 1
      DataType.c
  3. 1 1
      DataType.h
  4. 2 2
      utils/Functions.c
  5. 1 1
      utils/Functions.h
  6. 2 2
      utils/Variables.c
  7. 1 1
      utils/Variables.h
  8. 2 2
      vm/Arrays.c
  9. 1 1
      vm/Arrays.h

+ 6 - 6
Compiler.c

@@ -177,7 +177,7 @@ static DataType cExpression();
 static void cCallFunctionArguments(Function* f) {
     while(!cConsumeTokenIf(T_CLOSE_BRACKET)) {
         DataType dt = cExpression();
-        if(fAddArgument(f, dt)) {
+        if(fAddArgument(f, dt, &structs)) {
             cTooMuchArguments();
         }
         if(cConsumeTokenIf(T_COMMA) && tPeekToken() == T_CLOSE_BRACKET) {
@@ -595,7 +595,7 @@ static void cDeclareStruct(Struct* st) {
     if(v != NULL) {
         cDeclared(var);
     }
-    vAdd(&vars, var, dtStruct(st));
+    vAdd(&vars, var, dtStruct(st), &structs);
     int varLength = strlen(var);
     for(int i = 0; i < st->amount; i++) {
         int length = strlen(st->vars[i].name);
@@ -603,7 +603,7 @@ static void cDeclareStruct(Struct* st) {
         memcpy(fullName, var, varLength);
         fullName[varLength] = '.';
         memcpy(fullName + varLength + 1, st->vars[i].name, length + 1);
-        vAdd(&vars, fullName, st->vars[i].type);
+        vAdd(&vars, fullName, st->vars[i].type, &structs);
         free(fullName);
     }
 }
@@ -785,7 +785,7 @@ static void cDeclare(DataType dt) {
     if(v != NULL) {
         cDeclared(var);
     }
-    v = vAdd(&vars, var, dt);
+    v = vAdd(&vars, var, dt, &structs);
     cConsumeToken(T_SET);
     cStoreVariable(v, cExpression(), "=");
 }
@@ -908,8 +908,8 @@ static void cFunctionAddArgument(Function* f, DataType dt) {
     if(v != NULL) {
         cDeclared(name);
     }
-    vAdd(&vars, name, dt);
-    if(fAddArgument(f, dt)) {
+    vAdd(&vars, name, dt, &structs);
+    if(fAddArgument(f, dt, &structs)) {
         cTooMuchArguments();
     }
     cFunctionCommaOrEnd(f);

+ 2 - 1
DataType.c

@@ -42,7 +42,8 @@ const char* dtGetName(Structs* sts, DataType dt) {
     return typeName[typeNameSwap];
 }
 
-int dtGetSize(DataType dt) {
+int dtGetSize(DataType dt, Structs* sts) {
+    (void)sts;
     switch(dtAsInt(dt)) {
         case DT_INT: return sizeof(int);
         case DT_FLOAT: return sizeof(float);

+ 1 - 1
DataType.h

@@ -34,7 +34,7 @@ typedef struct {
     Struct* data;
 } Structs;
 
-int dtGetSize(DataType dt);
+int dtGetSize(DataType dt, Structs* sts);
 
 DataType dtInt();
 DataType dtFloat();

+ 2 - 2
utils/Functions.c

@@ -12,11 +12,11 @@ void fInit(Function* f, const char* name, int line) {
     f->line = line;
 }
 
-bool fAddArgument(Function* f, DataType type) {
+bool fAddArgument(Function* f, DataType type, Structs* sts) {
     if(f->arguments >= FUNCTION_ARGUMENTS) {
         return true;
     }
-    f->size += dtGetSize(type);
+    f->size += dtGetSize(type, sts);
     f->argumentTypes[f->arguments++] = type;
     return false;
 }

+ 1 - 1
utils/Functions.h

@@ -24,7 +24,7 @@ typedef struct {
 } Functions;
 
 void fInit(Function* f, const char* name, int line);
-bool fAddArgument(Function* f, DataType type);
+bool fAddArgument(Function* f, DataType type, Structs* sts);
 
 void fsInit(Functions* fs);
 void fsDelete(Functions* fs);

+ 2 - 2
utils/Variables.c

@@ -33,14 +33,14 @@ Variable* vSearchScope(Variables* v, const char* s) {
     return vSearchUntil(v, s, v->scope);
 }
 
-Variable* vAdd(Variables* v, const char* s, DataType type) {
+Variable* vAdd(Variables* v, const char* s, DataType type, Structs* sts) {
     if(v->entries >= v->capacity) {
         v->capacity *= 2;
         v->data = realloc(v->data, sizeof(Variable) * v->capacity);
     }
     int index = v->entries++;
     v->data[index] = (Variable){strdup(s), type, v->address};
-    v->address += dtGetSize(type);
+    v->address += dtGetSize(type, sts);
     if(v->address > v->maxAddress) {
         v->maxAddress = v->address;
     }

+ 1 - 1
utils/Variables.h

@@ -30,7 +30,7 @@ void vInit(Variables* v);
 void vDelete(Variables* v);
 Variable* vSearch(Variables* v, const char* s);
 Variable* vSearchScope(Variables* v, const char* s);
-Variable* vAdd(Variables* v, const char* s, DataType type);
+Variable* vAdd(Variables* v, const char* s, DataType type, Structs* sts);
 void vReset(Variables* v);
 void vEnterScope(Variables* v, Scope* s);
 void vLeaveScope(Variables* v, Scope* s);

+ 2 - 2
vm/Arrays.c

@@ -52,7 +52,7 @@ static void asEnsureCapacity(Arrays* as) {
     }
 }
 
-int asAllocate(Arrays* as, DataType type, int length) {
+int asAllocate(Arrays* as, DataType type, Structs* sts, int length) {
     asEnsureCapacity(as);
     int index = as->freeStart;
     Array* array = as->data + index;
@@ -70,7 +70,7 @@ int asAllocate(Arrays* as, DataType type, int length) {
 
     array->length = length;
     array->type = type;
-    array->data = malloc(sizeof(dtGetSize(type)) * length);
+    array->data = malloc(sizeof(dtGetSize(type, sts)) * length);
     return index;
 }
 

+ 1 - 1
vm/Arrays.h

@@ -23,7 +23,7 @@ typedef struct {
 
 void asInit(Arrays* as);
 void asDelete(Arrays* as);
-int asAllocate(Arrays* as, DataType type, int length);
+int asAllocate(Arrays* as, DataType type, Structs* sts, int length);
 Array* asGet(Arrays* as, int p);
 void aAddReference(Array* a);
 void aRemoveReference(Array* a);