Add scope types
This commit is contained in:
parent
a1fa96a626
commit
03ac8336c0
@ -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);
|
||||
|
@ -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
|
@ -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 {};
|
||||
|
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user