-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymtable.hpp
92 lines (66 loc) · 2.12 KB
/
symtable.hpp
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
90
91
92
#ifndef _SYMTABLE_HPP_
#define _SYMTABLE_HPP_
#include <memory>
#include <string>
#include <variant>
#include <unordered_map>
#include "llvm/IR/Function.h"
#include "llvm/IR/Value.h"
namespace pl0::symtable {
class SymbolEntry {
private:
SymbolEntry(llvm::Value* value, bool isConstant)
: m_data(value), m_isConstant(isConstant) {}
SymbolEntry(llvm::Function* value)
: m_data(value), m_isConstant(false) {}
public:
SymbolEntry() = default;
static inline auto constant(llvm::Value* value) -> SymbolEntry {
return SymbolEntry(value, true);
}
static inline auto variable(llvm::Value* value) -> SymbolEntry {
return SymbolEntry(value, false);
}
static inline auto procedure(llvm::Function* value) -> SymbolEntry {
return SymbolEntry(value);
}
constexpr auto isProcedure() const -> bool {
return std::holds_alternative<llvm::Function*>(m_data);
}
constexpr auto isConstant() const -> bool {
return m_isConstant;
}
constexpr auto isVariable() const -> bool {
return !isConstant() && !isProcedure();
}
constexpr auto procedure() const -> llvm::Function* {
return std::get<llvm::Function*>(m_data);
}
constexpr auto constant() -> llvm::Value* {
return std::get<llvm::Value*>(m_data);
}
constexpr auto variable() -> llvm::Value* {
return std::get<llvm::Value*>(m_data);
}
private:
std::variant<llvm::Value*, llvm::Function*> m_data;
bool m_isConstant;
};
class SymbolTable : public std::enable_shared_from_this<SymbolTable> {
public:
SymbolTable(std::shared_ptr<SymbolTable> parent = nullptr)
: m_parent(std::move(parent)) {}
auto lookup(const std::string& key) -> SymbolEntry*;
auto insert(const std::string& key, SymbolEntry info) -> bool;
inline auto hasParent() -> bool {
return m_parent != nullptr;
}
inline auto parent() -> std::shared_ptr<SymbolTable> {
return m_parent;
}
private:
std::shared_ptr<SymbolTable> m_parent;
std::unordered_map<std::string, SymbolEntry> m_symbols;
};
}
#endif