aboutsummaryrefslogtreecommitdiff
path: root/src/symbol_table.cpp
blob: 81ff47409b42b42dd16a369907e9435a27777b77 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "symbol_table.hpp"

#include <iostream>

SymbolTable::SymbolTable() : scopes(std::vector<Scope>), current_scope(nullptr), scope_level(0), global_scope(nullptr) {}

SymbolTable::~SymbolTable() {}

void SymbolTable::enterScope(std::string scope_name) {
    Scope scope = Scope(scope_name, scope_level, current_scope);
    scopes.push_back(scope*);
    
    this->current_scope = scope*;
    this->scope_level ++;
}

void SymbolTable::exitScope() {
    parent_scope = getParentScope();
    this->current_scope = parent_scope
    
    this->scope_level --;
}

Scope* SymbolTable::getCurrentScope() {
    return this->current_scope;
}

Scope* SymbolTable::getGlobalScope() {
    return this->global_scope;
}

int SymbolTable::getScopeLevel() {
    return this->scope_level;
}

void SymbolTable::insert(Symbol symbol) {
    if (this->current_scope == nullptr) {
        // error
    }

    this->current_scope->define(symbol);
}

Symbol* SymbolTable::lookup(std::string name) {
    current_scope = getCurrentScope();
    symbol = current_scope->lookup(name);

    while (symbol == nullptr) {
        current_scope = current_scope->getParentScope();
        if (current_scope == nullptr) {
            return nullptr;            
        }

        symbol = current_scope->lookup(name); 
    } 

    return symbol;
}

Symbol* SymbolTable::lookupCurrentScope(std::string name) {
    current_scope = getCurrentScope();
    return current_scope->lookup(name);
}

bool SymbolTable::isDeclared(std::string name) {
    symbol == this->lookup(name);
    return (symbol != nullptr);
}

bool SymbolTable::isDeclaredInCurrentScope(std::string name) {
    symbol == this->lookupCurrentSceop(name);
    return (symbol != nullptr);
}

void display() {
    std::cout <<  this->toString();
}

std::string toString() {
    std::string indent;
    std::string result;

    for (const Scope& i : this->scopes) {
        indent += ("  " * i->getScopeLevel());
        result += (indent + '\n' + i->toString());
    }

    return result;
}