forked from adnanis78612/mana-script
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymbol_table.cpp
More file actions
60 lines (47 loc) · 1.49 KB
/
symbol_table.cpp
File metadata and controls
60 lines (47 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Adding symbol_table.cpp from Ayush-Debnath
#include "symbol_table.hpp"
namespace mana {
bool Scope::define(const std::string& name, Symbol::Kind kind, std::shared_ptr<Type> type) {
// Check if the symbol already exists in this scope
if (symbols.find(name) != symbols.end()) {
return false;
}
// Add the symbol to this scope
symbols.emplace(name, Symbol(name, kind, type));
return true;
}
Symbol* Scope::resolve(const std::string& name) {
// Look in the current scope
auto it = symbols.find(name);
if (it != symbols.end()) {
return &it->second;
}
// Look in the parent scope if it exists
if (parent) {
return parent->resolve(name);
}
return nullptr;
}
Symbol* Scope::resolveLocal(const std::string& name) {
auto it = symbols.find(name);
if (it != symbols.end()) {
return &it->second;
}
return nullptr;
}
void SymbolTable::enterScope() {
current_scope = std::make_shared<Scope>(current_scope);
}
void SymbolTable::exitScope() {
if (current_scope->getParent()) {
current_scope = current_scope->getParent();
}
}
bool SymbolTable::define(const std::string& name, Symbol::Kind kind, std::shared_ptr<Type> type) {
return current_scope->define(name, kind, type);
}
Symbol* SymbolTable::resolve(const std::string& name) {
return current_scope->resolve(name);
}
} // namespace mana// Adding symbol_table.cpp from Ayush-Debnath
// Adding symbol_table.cpp from Ayush-Debnath