-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdent.cpp
More file actions
101 lines (80 loc) · 2.14 KB
/
Ident.cpp
File metadata and controls
101 lines (80 loc) · 2.14 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
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
#pragma once
#include <iostream>
#include <string>
#include "Lex.cpp"
using namespace std;
class Ident {
string name;
string value;
type_of_lex type;
int name_space; //пространсвто имен
public:
Ident(const Lex &lex = Lex(), int name_space = -1, type_of_lex type = LEX_NULL) {
this->name = lex.get_value();
this->name_space = name_space;
this->type = type;
this->value = "0";
}
string get_name() { return name; }
string get_value() { return value; }
void put_value(string value) { this->value = value; }
type_of_lex get_type() { return type; }
int get_namespace() { return name_space; }
};
//класс таблица идентефикаторов
class tabl_Ident {
Ident * p; //массив идентефикаторов
int size; //размер
int top; //текущая позиция
public:
tabl_Ident(int max_size = 256) {
p = new Ident[size = max_size];
top = 0;
}
~tabl_Ident() { delete[] p; }
Ident& operator[] (int k) { return p[k]; }
void put(const Lex &lex = Lex(), int name_space = -1, type_of_lex type = LEX_NULL) {
p[top] = Ident(lex, name_space, type);
++top;
}
int find(const Lex &lex, int name_space) {
string name = lex.get_value();
for(int i = top -1; i >= 0; i --) {
if(p[i].get_namespace() < name_space)
name_space--;
if(p[i].get_namespace() == name_space) {
if(p[i].get_name() == name)
return i;
}
}
return -1;
}
bool is_exist_name(const Lex &lex, int name_space) {
string name = lex.get_value();
for(int i = top -1; i >= 0; i --) {
if(p[i].get_namespace() < name_space)
return false;
if(p[i].get_namespace() == name_space) {
if(p[i].get_name() == name)
return true;
}
}
return false;
}
void clear_namespace(bool f) {
if(f) print();
int name_space = p[top-1].get_namespace();
for(int i = top - 1; i >= 0; i--) {
if(p[i].get_namespace() == name_space)
top--;
else return;
}
}
void print() {
cout << "TID : ";
for(int i = 0; i < top; i++) {
cout << "(" << p[i].get_name() << " ," << p[i].get_type() << " ," << p[i].get_namespace() << ") ";
}
cout << endl;
}
};