-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
253 lines (240 loc) · 6.68 KB
/
main.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <algorithm>
#include <atomic>
#include <mutex>
#include <thread>
#include <utility>
#include "lexer.h"
#include "symtable.h"
#include "parser.h"
//clase que procesa los tokens generados por el FSM.
//no es la versión final.
class TokenTester : public TokenPusher{
using Lock = std::lock_guard<std::mutex>;
std::mutex buffer_mux;
std::mutex termination_mux;
//Lista de tokens que se han reconocido
Vector<Token> list;
//linea actual
unsigned line = 1;
//Vector2<int> test;
public:
void String(const std::string& string, Context &tables){
auto it = tables.strs.find(string);
if(it == tables.strs.end()){
tables.strs[string] = tables.strs_i;
Lock lock(buffer_mux);
list.push_back(Token(Token::STRING,(long) tables.strs_i++,line));
}else{
Lock lock(buffer_mux);
list.push_back(Token(Token::STRING,(long) *(it.second),line));
}
}
void ID(const std::string& string, Context &tables){
auto it = tables.ids.find(string);
if(it == tables.ids.end()){
tables.ids[string] = tables.ids_i;
Lock lock(buffer_mux);
list.push_back(Token(Token::ID,(long) tables.ids_i++,line));
}else{
Lock lock(buffer_mux);
list.push_back(Token(Token::ID,(long) *(it.second),line));
}
}
void Language(unsigned type,long id){
Lock lock(buffer_mux);
list.push_back(Token(type,id,line));
}
void Integer(const std::string &str, int base = 10){
Lock lock(buffer_mux);
list.push_back(Token(Token::INT_LITERAL,std::stol(str,0,base),line) );
}
void Float(const std::string &str){
Lock lock(buffer_mux);
list.push_back(Token(Token::FLT_LITERAL,std::stod(str),line) );
}
void Special(unsigned type, unsigned subtype){
Lock lock(buffer_mux);
list.push_back(Token(type,(long) subtype,line));
}
void Endl(){
line++;
}
void Eof(){
buffer_mux.lock();
list.push_back(Token(Token::FEOF,0L,line));
buffer_mux.unlock();
//TODO: verificar dependencia
std::lock_guard<std::mutex> lockd (termination_mux);
}
TokenHandle handle(){
return TokenHandle{&list,&buffer_mux,&termination_mux};
}
TokenBuffer& result(){
return list;
}
TokenTester(){
//4 HORAS 4 HORAS!!!!!!
buffer_mux.lock();
termination_mux.lock();
buffer_mux.unlock();
termination_mux.unlock();
}
};
//Interface entre el buffer de tokens y el parser, que lee de uno por uno
class TokenAtomAdapter : public AtomSource{
using Lock = std::lock_guard<std::mutex>;
std::ostream* out = nullptr;
TokenHandle handle;
TokenBuffer local_buffer;
bool finished_work = false;
Token last;
size_t rbuffer_index = 0;
size_t nline = 1;
public:
//Cambia el bufer con que se sincroniza
void setHandle(TokenHandle hl){
handle = hl;
hl.rev_mux->lock();
}
//Linea actual
unsigned line() {
return nline;
}
void setOutput(std::ostream& stream){
out = &stream;
}
//Trae un token
Atom fetch(){
if(finished_work){
return last.atom();
}
while(local_buffer.size() == 0){
{//TODO: eliminar busy-wait mientras se llena el buffer (si eso eso realmente llega a pasar)
Lock lock(*handle.mux);
swap(*(handle.data),local_buffer);
}
rbuffer_index = 0;
//TODO: REMOVE IN FINAL VERSION
//La tabla de tokens es efímera
printTokens();
}
auto token = local_buffer[rbuffer_index++];
nline = token.pos;
last = token;
if(token.type == Token::FEOF){
handle.rev_mux->unlock();
finished_work = true;
}
if(rbuffer_index == local_buffer.size())
local_buffer.resize(0,false);
return token.atom();
}
//IMprime tabla
void printTokens(){
std::ostream& cout = *out;
cout << "#\tValor\tID\tClase\tIDAtomo\tAtomo\n";
for(unsigned i = 0;i < local_buffer.size();i++){
cout << i << '\t' << local_buffer[i] << '\t' << local_buffer[i].getID() <<'\t'<<
local_buffer[i].type << ':' << S_class_names[ local_buffer[i].type]<< '\t'<< local_buffer[i].atom()<< '\t' << S_atoms[local_buffer[i].atom()] << '\t' <<local_buffer[i].pos <<'\n' ;
}
cout << std::endl;
}
};
//Imprime cadenas guardadas
void printStrings(std::ostream& cout, Context& context) {
cout << "#\tValor\n";
for(auto elem : context.strs){
cout << *elem.second << '\t' << *elem.first << '\n';
}
cout << std::endl;
}
//Imprime identificadores guardados
void printIDs(std::ostream& cout, Context& context) {
cout << "#\tValor\n";
for(auto elem : context.ids){
cout << *elem.second << '\t' << *elem.first << '\n';
}
cout << std::endl;
}
#ifdef TEST_MODE
int main0 (int argc, char **argv, Context& context, TokenTester& default_out)
#else
int main (int argc, char **argv)
#endif
{
//Seccion de manejo de archivo
std::ostream* output;
std::istream* input;
std::ifstream file_in;
std::ofstream file_out;
switch(argc){
case 1:
input = &std::cin;
output = &std::cout;
break;
case 2:
file_in.open(argv[1],std::ios::in);
if(!file_in){
std::cerr << "No se puede abrir " << argv[1] << " para lectura" << std::endl;
return -1;
}
input = &file_in;
output = &std::cout;
break;
case 3:
file_in.open(argv[1]);
if(file_in.fail()){
std::cerr << "No se puede abrir " << argv[1] << " para lectura" << std::endl;
return -1;
}
file_out.open(argv[2]);
if(file_out.fail()){
std::cerr << "No se puede abrir " << argv[2] << " para escritura" << std::endl;
return -1;
}
input = &file_in;
output = &file_out;
break;
default:
std::cerr << "Uso: badpas [archivo_entrada] [archivo_salida]" << std::endl;
return -1;
}
//Tablas auxiliares
#ifndef TEST_MODE
Context context;
//Consumidor de tokens
TokenTester default_out;
#endif
//Lexer
TokenFilter Tmachine;
//Adaptador
TokenAtomAdapter pipe;
pipe.setOutput(*output);
//Parser
PascalParser Pmachine;
//Conectar adaptador con fuente de tokens
pipe.setHandle(default_out.handle());
//Conectar parser conn adaptador
Pmachine.setSource(pipe);
//Configurar fuente de tokens
Tmachine.setBuilder(default_out);
Tmachine.setContext(context);
//Conectar fuente de tokens con fuente de cadenas
Tmachine.setSource([input](CharBuffer &buffer){return streamTestFun(*input,buffer);});
//Iniciar threads
std::thread lexer(std::move(Tmachine));
std::thread parser(std::move(Pmachine));
parser.join();
lexer.join();
//Imprimir lista de tokens
//default_out.printTokens(*output);
//Imprimir las otras tablas
*output << "CADENAS\n";
printStrings(*output,context);
*output << "IDENTIFICADORES\n";
printIDs(*output,context);
}