Something went wrong. Try again.
the hito embeddable programming language
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657#include "scope.h"#include <stdlib.h>#include <stdio.h>#include "util.h"struct scope { intern_t *data; size_t size; size_t capacity;};
scope_t *scope_alloc(void) { scope_t *s = malloc(sizeof(struct scope)); if (s == NULL) die("Out of memory: cannot allocate scope struct"); s->size = 0; s->capacity = 8; s->data = malloc(s->capacity * sizeof(intern_t)); if (s->data == NULL) die("Out of memory: cannot allocate scope buffer"); return s;}
void scope_push(scope_t *scope, intern_t identifier) { if (scope->size == scope->capacity) { scope->capacity *= 2; scope->data = realloc(scope->data, scope->capacity * sizeof(intern_t)); if (scope->data == NULL) die("Out of memory: cannot enlarge scope array"); } scope->data[scope->size++] = identifier;}
void scope_pop(scope_t *scope, size_t amount) { if (scope->size >= amount) scope->size -= amount;}
void scope_free(scope_t *scope) { if (scope == NULL) return; free(scope->data); free(scope);}
de_bruijn_t scope_lookup(scope_t *scope, intern_t identifier) { for (size_t i = scope->size; i > 0; i--) if (scope->data[i - 1] == identifier) return (scope->size - i); /* caller should decide what happens for "not found" */ return (de_bruijn_t)-1;}
void scope_debug_dump(scope_t *scope) { for (size_t i = 0; i < scope->size; i++) printf("%s, ", scope->data[i]); printf("|-");}