Add scope types

This commit is contained in:
ala89 2023-12-08 09:21:34 +01:00
parent a1fa96a626
commit 03ac8336c0
4 changed files with 8 additions and 4 deletions

View File

@ -9,7 +9,7 @@ class Memory {
public:
bool contains(string identifier);
bool contains_top(string identifier);
void add_scope(void);
void add_scope(ScopeType type);
void remove_scope(void);
EvalResult get(string identifier);

View File

@ -162,9 +162,12 @@ struct ParseReturn {
*/
using EvalResult = variant<monostate, int, double>;
enum class ScopeType { Block, Function, For };
struct Scope {
unordered_map<string, EvalResult> vars;
int depth;
ScopeType type;
};
#endif

View File

@ -84,7 +84,7 @@ EvalResult eval(Node &ast, Memory &memory) {
return {};
} break;
case NodeType::For: {
memory.add_scope();
memory.add_scope(ScopeType::For);
eval(node.children[0], memory);
@ -101,7 +101,7 @@ EvalResult eval(Node &ast, Memory &memory) {
return {};
} break;
case NodeType::Bloc: {
memory.add_scope();
memory.add_scope(ScopeType::Block);
eval(node.children[0], memory);
memory.remove_scope();
return {};

View File

@ -21,10 +21,11 @@ bool Memory::contains_top(string identifier) {
return top.vars.contains(identifier);
}
void Memory::add_scope(void) {
void Memory::add_scope(ScopeType type) {
Scope& top = scopes.back();
scopes.emplace_back();
scopes.back().depth = top.depth + 1;
scopes.back().type = type;
}
void Memory::remove_scope(void) {