1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include <stdlib.h>
- #include <string.h>
- #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;
- }
|