-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.h
111 lines (90 loc) · 1.94 KB
/
entry.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by 23207 on 2020/10/22.
//
#ifndef COMPILER_ENTRY_H
#define COMPILER_ENTRY_H
#include <string>
#include <vector>
#include <map>
#include "word_parsing.h"
using namespace std;
// type :
// 0 -> void
// 1 -> int
// 2 -> char
// v_or_f_or_c:
// 1 -> var
// 2 -> func
// 3 -> const
string to_lower(string cc) {
string ret = cc;
for (char & i : ret) {
if ('A' <= i && i <= 'Z') {
i = i + 32;
}
}
return ret;
}
class Entry {
public:
Entry(int v_or_f_or_c, string name, int type, int line_n, int value=0) {
this->v_or_f_or_c = v_or_f_or_c;
this->name = to_lower(name);
this->type = type;
this->line_n = line_n;
this->value = value;
}
string get_name() {
return this->name;
}
int get_value() const {
return this->value;
}
void add_arg(int t) {
this->args_tab.push_back(t);
}
vector<int> get_arg() {
return this->args_tab;
}
void add_shape(int x) {
this->shape.push_back(x);
}
int get_type() const {
return this->type;
}
bool is_const() const {
return this->v_or_f_or_c == 3;
}
vector<int> get_shape() {
return this->shape;
}
int v_or_f_or_c;
private:
string name;
int type;
int line_n;
int value;
// func
vector<int> args_tab;
// array
vector<int> shape;
};
class Symbol_tab {
public:
void append_sym(Entry e) {
string name11 = e.get_name();
if (this->has_name(name11)) { this->tab.erase(name11); }
(this->tab).insert(pair<string, Entry>(e.get_name(), e));
}
bool has_name(const string& n) {
return this->tab.count(to_lower(n)) > 0;
}
Entry get(const string& n) {
map<string, Entry>::iterator iter;
iter = this->tab.find(to_lower(n));
return iter->second;
}
private:
map<string, Entry> tab;
};
#endif //COMPILER_ENTRY_H