#include #include #include "utils/Variables.h" void vsInit(Variables* v) { v->capacity = 16; vsReset(v); v->data = (Variable*)malloc(sizeof(Variable) * (size_t)v->capacity); } void vsDelete(Variables* v) { free(v->data); } bool vsSearch(const Variables* vs, Variable* v, const char* s) { for(int i = vs->entries - 1; i >= 0; i--) { if(strcmp(s, vs->data[i].name) == 0) { *v = vs->data[i]; return false; } } return true; } bool vsInScope(const Variables* vs, const char* s) { for(int i = vs->entries - 1; i >= vs->scope; i--) { if(strcmp(s, vs->data[i].name) == 0) { return true; } } return false; } VariableError vSearchStruct(Variable* v, const Structs* sts, const Struct* st, const char* s) { v->address = 0; for(int i = 0; i < st->amount; i++) { if(strcmp(st->vars[i].name, s) == 0) { v->name = s; v->type = st->vars[i].type; return VE_OK; } int add; if(dtGetSize(st->vars[i].type, sts, &add)) { return VE_SIZE_ERROR; } v->address += add; } return VE_CANNOT_FIND; } VariableError vsAdd(Variables* vs, const char* s, DataType type, const Structs* sts, Variable** v) { if(vs->entries >= vs->capacity) { vs->capacity *= 2; vs->data = (Variable*)realloc(vs->data, sizeof(Variable) * (size_t)vs->capacity); } int size; if(dtGetSize(type, sts, &size)) { return VE_SIZE_ERROR; } int index = vs->entries++; vs->data[index] = (Variable){s, type, vs->address}; vs->address += size; if(vs->address > vs->maxAddress) { vs->maxAddress = vs->address; } *v = vs->data + index; return VE_OK; } void vsReset(Variables* v) { v->entries = 0; v->address = 0; v->scope = 0; v->maxAddress = 0; } void vsEnterScope(Variables* v, Scope* s) { s->address = v->address; s->entries = v->entries; s->scope = v->scope; v->scope = v->entries; } void vsLeaveScope(Variables* v, const Scope* s) { v->address = s->address; v->entries = s->entries; v->scope = s->scope; }