#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; } bool vSearchStruct(Variable* v, Structs* sts, 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 false; } v->address += dtGetSize(st->vars[i].type, sts); } return true; } Variable* vsAdd(Variables* v, const char* s, DataType type, Structs* sts) { if(v->entries >= v->capacity) { v->capacity *= 2; v->data = (Variable*)realloc(v->data, sizeof(Variable) * (size_t)v->capacity); } int index = v->entries++; v->data[index] = (Variable){s, type, v->address}; v->address += dtGetSize(type, sts); if(v->address > v->maxAddress) { v->maxAddress = v->address; } return v->data + index; } 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, Scope* s) { v->address = s->address; v->entries = s->entries; v->scope = s->scope; }