From 13b067f803b73f5a50eb1dad18e1773b66dabfbb Mon Sep 17 00:00:00 2001 From: ala89 Date: Wed, 3 Jan 2024 11:51:31 +0100 Subject: [PATCH] Add memory features for functions --- src/include/memory.h | 3 ++- src/include/types.h | 2 ++ src/memory.cpp | 13 ++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/include/memory.h b/src/include/memory.h index 0c000d7..0f56a75 100644 --- a/src/include/memory.h +++ b/src/include/memory.h @@ -12,10 +12,11 @@ class Memory { public: bool contains(string identifier); bool contains_top(string identifier); - void add_scope(ScopeType type); + void add_scope(ScopeType type, CodePosition entry_pos = { }, MemoryVar* fn = NULL); void remove_scope(void); MemoryVar& get(string identifier); + Scope& get_function_scope(void); void declare(string identifier, Type type, CodePosition pos); void update(string identifier, EvalResult value); diff --git a/src/include/types.h b/src/include/types.h index c704405..b2fba99 100644 --- a/src/include/types.h +++ b/src/include/types.h @@ -212,6 +212,8 @@ struct Scope { unordered_map vars; int depth; ScopeType type; + CodePosition entry_pos; + MemoryVar* fn; }; #endif \ No newline at end of file diff --git a/src/memory.cpp b/src/memory.cpp index 59adedc..2374fa1 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -21,11 +21,13 @@ bool Memory::contains_top(string identifier) { return top.vars.contains(identifier); } -void Memory::add_scope(ScopeType type) { +void Memory::add_scope(ScopeType type, CodePosition entry_pos, MemoryVar* fn) { Scope& top = scopes.back(); scopes.emplace_back(); scopes.back().depth = top.depth + 1; scopes.back().type = type; + scopes.back().entry_pos = entry_pos; + scopes.back().fn = fn; } void Memory::remove_scope(void) { @@ -41,6 +43,15 @@ MemoryVar& Memory::get(string identifier) { throw exception(); } +Scope& Memory::get_function_scope(void) { + for (auto rit = scopes.rbegin(); rit != scopes.rend(); ++rit) { + Scope& scope = *rit; + if (scope.type == ScopeType::Function) return scope; + } + + throw exception(); +} + void Memory::declare(string identifier, Type type, CodePosition pos) { Scope& top = scopes.back(); top.vars[identifier].type = type;