From 2d01b09ee9701fe92f775a345ee4d76ce662ffe8 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 26 Apr 2025 09:45:46 -0400 Subject: [PATCH] Added scope structures. Not yet used in exec.c. --- src/include/scope.h | 19 +++++++++++++++++++ src/scope.c | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/include/scope.h create mode 100644 src/scope.c diff --git a/src/include/scope.h b/src/include/scope.h new file mode 100644 index 0000000..3531584 --- /dev/null +++ b/src/include/scope.h @@ -0,0 +1,19 @@ +#ifndef SCOPE_H +#define SCOPE_H + +#include "htab.h" + +// Represents the reverse linked tree of scope. +typedef struct SCOPE_T { + HTab* here; + struct SCOPE_T* inherit; +} Scope; + +// Create a new `Scope`. +Scope* scope_init(HTab* here, Scope* inherit); +// Destroy all linked `Scope`s this inherits from. +void scope_destroy(Scope* scope); +// Destroy the current `Scope` only. +void scope_destroy_psv(Scope *scope); + +#endif diff --git a/src/scope.c b/src/scope.c new file mode 100644 index 0000000..30ed71c --- /dev/null +++ b/src/scope.c @@ -0,0 +1,24 @@ +#include "include/scope.h" +#include "include/htab.h" +#include + +Scope* scope_init(HTab* here, Scope* inherit) { + Scope* scope = malloc(sizeof(Scope)); + + scope->here = here; + scope->inherit = inherit; + + return scope; +} + +void scope_destroy(Scope* scope) { + htab_destroy(scope->here); + if (scope->inherit != NULL) scope_destroy(scope->inherit); + free(scope); +} + +void scope_destroy_psv(Scope* scope) { + htab_destroy(scope->here); + scope->inherit = NULL; + free(scope); +}