123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "DataType.h"
- #define ARRAY_NAME 256
- static int typeNameIndex = 0;
- static int typeNameSwap = 0;
- static char typeName[2][ARRAY_NAME];
- static bool useGlobals = false;
- static Structs globalStructs;
- static void dtAppend(const char* s) {
- int index = 0;
- while(typeNameIndex < (ARRAY_NAME - 1) && s[index] != '\0') {
- typeName[typeNameSwap][typeNameIndex] = s[index];
- index++;
- typeNameIndex++;
- }
- typeName[typeNameSwap][typeNameIndex] = '\0';
- }
- const char* dtGetName(const Structs* sts, DataType dt) {
- typeNameSwap = !typeNameSwap;
- typeNameIndex = 0;
- switch(dt.type) {
- case DT_INT: dtAppend("int"); break;
- case DT_FLOAT: dtAppend("float"); break;
- case DT_STRUCT: dtAppend(dtGetStruct(sts, dt)->name); break;
- case DT_VOID: dtAppend("void"); break;
- default: dtAppend("unknown");
- }
- if(dt.array != 0) {
- dtAppend("[]");
- }
- if(dt.pointer != 0) {
- dtAppend("*");
- }
- return typeName[typeNameSwap];
- }
- int dtGetSize(DataType dt, const Structs* sts) {
- if(dt.pointer != 0) {
- return 1;
- }
- switch(dt.type) {
- case DT_INT: return 1;
- case DT_FLOAT: return 1;
- case DT_STRUCT: {
- int size = 0;
- Struct* st = dtGetStruct(sts, dt);
- for(int i = 0; i < st->amount; i++) {
- size += dtGetSize(st->vars[i].type, sts);
- }
- return size;
- }
- default: return 0;
- }
- }
- DataType dtInt() {
- DataType dt = {DT_INT, 0, 0, 0};
- return dt;
- }
- DataType dtFloat() {
- DataType dt = {DT_FLOAT, 0, 0, 0};
- return dt;
- }
- DataType dtVoid() {
- DataType dt = {DT_VOID, 0, 0, 0};
- return dt;
- }
- DataType dtStruct(const Struct* st) {
- DataType dt = {DT_STRUCT, 0, 0, st->id};
- return dt;
- }
- DataType dtText() {
- DataType dt = {DT_INT, 0, 1, 0};
- return dt;
- }
- DataType dtToArray(DataType dt) {
- dt.array = 1;
- return dt;
- }
- DataType dtRemoveArray(DataType dt) {
- dt.array = 0;
- return dt;
- }
- DataType dtToPointer(DataType dt) {
- dt.pointer = 1;
- return dt;
- }
- bool dtRemovePointer(DataType* dt) {
- if(dt->pointer != 0) {
- dt->pointer = 0;
- return true;
- }
- return false;
- }
- bool dtCompare(DataType a, DataType b) {
- return a.array == b.array && a.structId == b.structId && a.type == b.type &&
- a.pointer == b.pointer;
- }
- bool dtIsInt(DataType dt) {
- return dtCompare(dt, dtInt());
- }
- bool dtIsFloat(DataType dt) {
- return dtCompare(dt, dtFloat());
- }
- bool dtIsVoid(DataType dt) {
- return dtCompare(dt, dtVoid());
- }
- bool dtIsArray(DataType dt) {
- return dt.array != 0;
- }
- Struct* dtGetStruct(const Structs* sts, DataType dt) {
- if(dt.type != DT_STRUCT) {
- return NULL;
- }
- if(dt.structId & 1) {
- if(!useGlobals) {
- return NULL;
- }
- return globalStructs.data + (dt.structId >> 1);
- }
- return sts->data + (dt.structId >> 1);
- }
- 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);
- }
- static Struct* stsInternSearch(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* stsSearch(Structs* sts, const char* name) {
- if(useGlobals) {
- Struct* st = stsInternSearch(&globalStructs, name);
- if(st != NULL) {
- return st;
- }
- }
- return stsInternSearch(sts, name);
- }
- 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 << 1;
- sts->data[index].amount = 0;
- sts->data[index].name = name;
- sts->data[index].vars = NULL;
- return sts->data + index;
- }
- void gstsInit() {
- stsInit(&globalStructs);
- useGlobals = true;
- }
- void gstsDelete() {
- stsDelete(&globalStructs);
- useGlobals = false;
- }
- Structs* gstsGet() {
- return &globalStructs;
- }
- Struct* gstsAdd(const char* name) {
- Struct* st = stsAdd(&globalStructs, name);
- st->id |= 1;
- return st;
- }
|