-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtables.hpp
127 lines (103 loc) · 2.83 KB
/
tables.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* File: tables.hpp
* Author: student16
*
* Created on April 9, 2017, 5:50 PM
*/
#include <string>
#ifndef TABLES_HPP
#define TABLES_HPP
#include "Declaration.hpp"
#include <unordered_map>
#include <iostream>
class Scope_Symbol_Table {
// int Number_Of_Scope=0;
struct Stack {
struct Stack_Node{
std::unordered_map <std::string,Declaration*> Symbol_Table;
Stack_Node* prev;
Stack_Node(){};
};
Stack_Node* top=NULL;
Stack_Node* Tranverse=NULL;
Stack();
void Push();
std::unordered_map <std::string,Declaration*> Peek();
void Pop();
bool If_Empty();
void Print_Stack();
};
Stack* Symbol_Table_Stack;
public:
int Number_Of_Scope=0;
Scope_Symbol_Table();
void Insert_in_Scope();
void Add_Scope();
void Remove_Scope();
void Edit_Variable();
Declaration* Check_Variable(std::string var_name);
void Add_Count();
void Remove_Count();
int Is_Scope_Balanced();
};
#endif /* TABLES_HPP */
void Scope_Symbol_Table::Add_Count(){
Number_Of_Scope=Number_Of_Scope+1;
}
void Scope_Symbol_Table::Remove_Count(){
Number_Of_Scope=Number_Of_Scope-1;
}
int Scope_Symbol_Table::Is_Scope_Balanced(){
return Number_Of_Scope;
}
Scope_Symbol_Table::Scope_Symbol_Table(){};
void Scope_Symbol_Table::Add_Scope(){
Symbol_Table_Stack->Push();
}
void Scope_Symbol_Table::Remove_Scope(){
if (Symbol_Table_Stack->If_Empty()){
std::cout<<"Error no scope";
}
else {
Symbol_Table_Stack->Pop();}
}
Declaration* Scope_Symbol_Table::Check_Variable(std::string var_name){
Symbol_Table_Stack->Tranverse=Symbol_Table_Stack->top;
while(Symbol_Table_Stack->Tranverse!=NULL){
std::unordered_map <std::string,Declaration*>::iterator val;
val = Symbol_Table_Stack->Tranverse->Symbol_Table.find(var_name);
if (val != Symbol_Table_Stack->Tranverse->Symbol_Table.end()) {
return val->second;
}
Symbol_Table_Stack->Tranverse=Symbol_Table_Stack->top->prev;
}
if (Symbol_Table_Stack->Tranverse==NULL){
std::cout<< "Error no table checked";}
return NULL;
}
//
void Scope_Symbol_Table::Stack::Push(){
if (If_Empty()){
Stack_Node* temp= new Stack_Node();
temp->prev=NULL;
top=temp;
}
else{
Stack_Node* temp= new Stack_Node();
temp->prev=top;
top=temp;
}
};
std::unordered_map <std::string,Declaration*> Scope_Symbol_Table::Stack::Peek(){
return top->Symbol_Table;
};
void Scope_Symbol_Table::Stack::Pop(){
Stack_Node* temp;
temp=top;
top=top->prev;
delete temp;
};
bool Scope_Symbol_Table::Stack::If_Empty(){
if (top==NULL){return true;}
else{return false;}
};