Add memory features for functions

This commit is contained in:
ala89 2024-01-03 11:51:31 +01:00
parent 68b952d105
commit 13b067f803
3 changed files with 16 additions and 2 deletions

View File

@ -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);

View File

@ -212,6 +212,8 @@ struct Scope {
unordered_map<string, MemoryVar> vars;
int depth;
ScopeType type;
CodePosition entry_pos;
MemoryVar* fn;
};
#endif

View File

@ -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;