-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
274 lines (241 loc) · 6.83 KB
/
parser.cpp
File metadata and controls
274 lines (241 loc) · 6.83 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include "parser.h"
#include <cctype>
#include <cassert>
#include <cstdlib>
const std::string repr[] = {"LD", "ST", "LLO", "LHI", "IN", "OUT",
"ADDS", "SUBS", "NAND", "SHL", "SHR",
"SHRA", "B", "CALL", "RET", "HALT"
};
size_t saltear_espacios(const std::string & str, size_t pos = 0)
{
if (pos >= str.size() || pos == std::string::npos)
return std::string::npos;
while (pos < str.size() && isspace(str[pos]))
pos++;
if (pos == str.size())
return std::string::npos;
return pos;
}
instruccion leer_instruccion(const std::string & str, std::string & instr, size_t & pos)
{
pos = std::string::npos;
size_t pos_inic = saltear_espacios(str);
if (pos_inic == std::string::npos)
return NINGUNA;
size_t pos_fin = str.find_first_of(" \t", pos_inic);
if (pos_fin == std::string::npos)
instr = str.substr(pos_inic, str.size() - pos_inic + 1);
else
instr = str.substr(pos_inic, pos_fin - pos_inic);
//CALL y B se manejan en forma separada porque el campo cnd es parte
//del nombre de la instruccion
if ((instr.size() == 2 && instr[0] == 'B') ||
(instr.size() == 5 && instr.substr(0, 4) == "CALL"))
switch (instr[instr.size() - 1]) {
case 'R':
case 'V':
case 'Z':
case 'C':
case 'S':
pos = pos_fin;
return instr[0] == 'B' ? B : CALL;
break;
default:
return NINGUNA;
break;
}
bool encontrada = false;
int i = LD;
while (!encontrada && i <= HALT) {
if (i != B && i != CALL) {
if (instr == repr[i])
encontrada = true;
}
if (!encontrada)
i++;
}
if (encontrada) {
pos = pos_fin;
return (instruccion)i;
}
return NINGUNA;
}
int leer_registro(const std::string & str, size_t & pos)
{
pos = saltear_espacios(str, pos);
if (pos == std::string::npos ||
str.size() - pos < 2 ||
str[pos] != 'R' ||
!isxdigit(str[pos+1])) {
pos = std::string::npos;
return -1;
}
int reg = strtol(str.substr(pos + 1, 1).c_str(), NULL, 16);
pos += 2;
if (pos >= str.size())
pos = std::string::npos;
return reg;
}
//lee el campo v(de 8 bits) para las instrucciones de tipo 3
int leer_campo_v(const std::string & str, size_t & pos)
{
pos = saltear_espacios(str, pos);
if (pos == std::string::npos ||
str.size() - pos < 2 ||
!isxdigit(str[pos]) || !isxdigit(str[pos + 1])) {
pos = std::string::npos;
return -1;
}
int v = strtol(str.substr(pos, 2).c_str(), NULL, 16);
pos += 2;
if (pos >= str.size())
pos = std::string::npos;
return v;
}
uint16_t parsear_instruccion_tipo0(instruccion i, const std::string & str, size_t pos)
{
if (saltear_espacios(str, pos) != std::string::npos)
return INSTR_INVALIDA;
return (uint16_t)i << 12;
}
uint16_t parsear_instruccion_tipo1(instruccion i, const std::string & str, size_t pos)
{
int reg = leer_registro(str, pos);
if (reg == -1 ||
(saltear_espacios(str, pos) != std::string::npos))
return INSTR_INVALIDA;
return (uint16_t)i << 12 | (reg << 8);
}
uint16_t parsear_instruccion_tipo2(instruccion i, char cnd, const std::string & str,
size_t pos)
{
if (saltear_espacios(str, pos) != std::string::npos)
return INSTR_INVALIDA;
uint16_t op = (uint16_t)i << 12;
switch (cnd) {
case 'R':
break;
case 'Z':
op |= 0x0100;
break;
case 'S':
op |= 0x0200;
break;
case 'C':
op |= 0x0300;
break;
case 'V':
op |= 0x0400;
break;
default:
op = INSTR_INVALIDA;
break;
}
return op;
}
uint16_t parsear_instruccion_tipo3(instruccion i, const std::string & str, size_t pos)
{
int v;
int reg;
switch (i) {
case LD:
case LLO:
case LHI:
case IN:
reg = leer_registro(str, pos);
if (reg == -1 || pos == std::string::npos)
return INSTR_INVALIDA;
else {
pos = saltear_espacios(str, pos);
if (pos == std::string::npos || str[pos] != ',')
return INSTR_INVALIDA;
else {
pos++;
v = leer_campo_v(str, pos);
if (v == -1 ||
(saltear_espacios(str, pos) != std::string::npos))
return INSTR_INVALIDA;
}
}
break;
case OUT:
case ST:
v = leer_campo_v(str, pos);
if (v == -1 || pos == std::string::npos)
return INSTR_INVALIDA;
else {
pos = saltear_espacios(str, pos);
if (pos == std::string::npos || str[pos] != ',')
return INSTR_INVALIDA;
else {
pos++;
reg = leer_registro(str, pos);
if (reg == -1 ||
(saltear_espacios(str, pos) != std::string::npos))
return INSTR_INVALIDA;
}
}
break;
default:
break;
}
return (uint16_t)i << 12 | reg << 8 | v;
}
uint16_t parsear_instruccion_tipo4(instruccion i, const std::string & str, size_t pos)
{
int r1 = leer_registro(str, pos);
if (r1 == -1)
return INSTR_INVALIDA;
pos = saltear_espacios(str, pos);
if (pos == std::string::npos || str[pos] != ',')
return INSTR_INVALIDA;
pos++;
int r2 = leer_registro(str, pos);
if (r2 == -1)
return INSTR_INVALIDA;
pos = saltear_espacios(str, pos);
if (pos == std::string::npos || str[pos] != ',')
return INSTR_INVALIDA;
pos++;
int r3 = leer_registro(str, pos);
if (r3 == -1 || saltear_espacios(str, pos) != std::string::npos)
return INSTR_INVALIDA;
return (uint16_t)i << 12 | r1 << 8 | r2 << 4 | r3;
}
uint16_t parsear_instruccion(const std::string & ins)
{
size_t pos = 0;
std::string nom_inst;
instruccion i = leer_instruccion(ins, nom_inst, pos);
switch (i) {
case RET:
case HALT:
return parsear_instruccion_tipo0(i, ins, pos);
break;
case SHR:
case SHRA:
case SHL:
return parsear_instruccion_tipo1(i, ins, pos);
break;
case B:
case CALL:
return parsear_instruccion_tipo2(i, nom_inst[nom_inst.size() - 1], ins, pos);
break;
case LD:
case ST:
case LLO:
case LHI:
case IN:
case OUT:
return parsear_instruccion_tipo3(i, ins, pos);
break;
case ADDS:
case SUBS:
case NAND:
return parsear_instruccion_tipo4(i, ins, pos);
break;
default:
return INSTR_INVALIDA;
break;
}
}