forked from t123yh/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol_table.h
58 lines (43 loc) · 1.23 KB
/
symbol_table.h
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
//
// Created by t123yh on 2020/10/29.
//
#ifndef COMPILER_SYMBOL_TABLE_H
#define COMPILER_SYMBOL_TABLE_H
#include "base_defs.h"
#include <utility>
#include <vector>
#include <map>
#include <memory>
#include "intermediate.h"
struct symbol {
virtual ~symbol() = default;
virtual std::string get_name() const = 0;
};
struct symbol_table_entry {
std::shared_ptr<symbol> item;
int ptr_next_same_name;
int layer;
};
struct variable_symbol : public symbol {
var_def definition;
std::shared_ptr<intermediate_variable> var;
std::string get_name() const override;
explicit variable_symbol(var_def def) : definition(std::move(def)) {}
};
struct function_symbol : public symbol {
function_signature sign;
std::string get_name() const override;
explicit function_symbol(function_signature sign) : sign(std::move(sign)) {}
};
struct symbol_table
{
std::map<std::string, int> lookup;
std::vector<symbol_table_entry> symbols;
int current_layer;
void add_symbol(std::shared_ptr<symbol> item);
void enter_layer();
void pop_layer();
symbol* find_symbol(const std::string& name);
bool check_duplicate(const std::string& name);
};
#endif //COMPILER_SYMBOL_TABLE_H