1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- #include <stdlib.h>
- #include <string.h>
- #include "utils/Variables.h"
- void vsInit(Variables* v) {
- v->capacity = 16;
- vsReset(v);
- v->data = malloc(sizeof(Variable) * 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 = realloc(v->data, sizeof(Variable) * 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;
- }
|