1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include <stdlib.h>
- #include <string.h>
- #include "utils/Variables.h"
- void vInit(Variables* v) {
- v->capacity = 16;
- vReset(v);
- v->data = malloc(sizeof(Variable) * v->capacity);
- }
- void vDelete(Variables* v) {
- free(v->data);
- }
- static Variable* vSearchUntil(Variables* v, const char* s, int index) {
- for(int i = v->entries - 1; i >= index; i--) {
- if(strcmp(s, v->data[i].name) == 0) {
- return v->data + i;
- }
- }
- return NULL;
- }
- Variable* vSearch(Variables* v, const char* s) {
- return vSearchUntil(v, s, 0);
- }
- Variable* vSearchScope(Variables* v, const char* s) {
- return vSearchUntil(v, s, v->scope);
- }
- Variable* vAdd(Variables* v, const char* s, DataType type) {
- 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);
- if(v->address > v->maxAddress) {
- v->maxAddress = v->address;
- }
- return v->data + index;
- }
- void vReset(Variables* v) {
- v->entries = 0;
- v->address = 0;
- v->scope = 0;
- v->maxAddress = 0;
- }
- void vEnterScope(Variables* v, Scope* s) {
- s->address = v->address;
- s->entries = v->entries;
- s->scope = v->scope;
- v->scope = v->entries;
- }
- void vLeaveScope(Variables* v, Scope* s) {
- v->address = s->address;
- v->entries = s->entries;
- v->scope = s->scope;
- }
|