#include #include #include "DataType.h" #define ARRAY_NAME 20 #define ARRAY_FACTOR 10 static char arrayName[ARRAY_NAME]; static const char* dtGetBaseName(unsigned int type) { switch(type) { case DT_INT: return "int"; case DT_FLOAT: return "float"; case DT_BOOL: return "bool"; default: return ""; } } static const char* dtGetArrayName(DataType dt) { int dimension = dt.pointers; const char* name = dtGetBaseName(dt.type); if(name[0] == '\0' || dimension <= 0 || dimension > dtMaxDimensions()) { return "unknown"; } int index = 0; while(index < (ARRAY_NAME - 1) && name[index] != '\0') { arrayName[index] = name[index]; index++; } while(index < (ARRAY_NAME - 1) && dimension > 0) { arrayName[index] = '*'; index++; dimension--; } arrayName[index] = '\0'; return arrayName; } const char* dtGetName(Structs* sts, DataType dt) { if(dt.structId > 0) { return sts->data[dt.structId - 1].name; } else if(dt.pointers > 0) { return dtGetArrayName(dt); } return dtGetBaseName(dt.type); } int dtGetSize(DataType dt) { switch(dtAsInt(dt)) { case DT_INT: return sizeof(int); case DT_FLOAT: return sizeof(float); case DT_BOOL: return sizeof(bool); default: if(dt.structId > 0) { return 0; } return sizeof(int); } } DataType dtInt() { DataType dt = {DT_INT, 0, 0, 0}; return dt; } DataType dtFloat() { DataType dt = {DT_FLOAT, 0, 0, 0}; return dt; } DataType dtBool() { DataType dt = {DT_BOOL, 0, 0, 0}; return dt; } DataType dtVoid() { DataType dt = {DT_VOID, 0, 0, 0}; return dt; } DataType dtStruct(Struct* st) { DataType dt = {DT_STRUCT, 0, 0, st->id}; return dt; } DataType dtToReference(DataType dt) { dt.reference = 1; return dt; } DataType dtToArray(DataType dt, int dimension) { dt.pointers = dimension; return dt; } bool dtCompare(DataType a, DataType b) { return dtAsInt(a) == dtAsInt(b); } int dtMaxDimensions() { return 15; } unsigned int dtAsInt(DataType dt) { unsigned int i; memcpy(&i, &dt, sizeof(dt)); return i; } bool dtIsArray(DataType dt) { return dt.pointers > 0; } void stAddVariable(Struct* st, const char* name, DataType type) { int index = st->amount; st->amount++; st->vars = realloc(st->vars, sizeof(StructVariable) * st->amount); st->vars[index].name = name; st->vars[index].type = type; } void stsInit(Structs* sts) { sts->capacity = 4; sts->entries = 0; sts->data = malloc(sizeof(Struct) * sts->capacity); } void stsDelete(Structs* sts) { for(int i = 0; i < sts->entries; i++) { free(sts->data[i].vars); } free(sts->data); } Struct* stsSearch(Structs* sts, const char* name) { for(int i = 0; i < sts->entries; i++) { if(strcmp(sts->data[i].name, name) == 0) { return sts->data + i; } } return NULL; } Struct* stsAdd(Structs* sts, const char* name) { if(sts->entries >= sts->capacity) { sts->capacity *= 2; sts->data = realloc(sts->data, sizeof(Struct) * sts->capacity); } int index = sts->entries++; sts->data[index].id = index; sts->data[index].amount = 0; sts->data[index].name = name; sts->data[index].vars = NULL; return sts->data + index; }