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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#include <stdio.h>
#include <limits.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define _(x) x
void exception(int, char *);
#endif
#include "exceptions.h"
#include "parser.h"
typedef unsigned char op_t;
typedef struct operator_t {
op_t op;
int pri, func;
} operator_t;
static op_t pile_operators[PILEOP_MAX];
static int pile_nestedcall[PILECALL_MAX];
static int pileop_pos = 0, pilecall_pos = 0;
static operator_t operators[] = {
{',', 0, OP_NEST},
{'+', 2, OP_PLUS},
{'-', 2, OP_MOINS},
{'*', 3, OP_MUL},
{'/', 3, OP_DIV},
{'+' + 128, 4, OP_PLUS_UNARY},
{'-' + 128, 4, OP_MOINS_UNARY},
{'(', 5, OP_FUNC_CALL},
{'(' + 128, 5, OP_LPAREN},
{'[', 5, OP_DECAL},
{'[' + 128, 5, OP_DIRECT},
{255, -1, -1}
};
static operator_t get_op(op_t op)
{
int i;
for (i = 0; operators[i].op != 255; i++) {
if (operators[i].op == op)
return operators[i];
}
return operators[i];
}
static int get_pri(op_t op)
{
return get_op(op).pri;
}
static int get_func(op_t op)
{
return get_op(op).func;
}
static op_t get_last_op(void)
{
if (pileop_pos)
return pile_operators[pileop_pos - 1];
else
return -1;
}
static op_t pop_op(void)
{
if (pileop_pos)
return pile_operators[--pileop_pos];
return -1;
}
static void push_op(op_t op)
{
if (pileop_pos != PILEOP_MAX)
pile_operators[pileop_pos++] = op;
else
exception(-1, _("Too many nested operators in expression.\n"));
}
static int pop_call(void)
{
if (pilecall_pos)
return pile_nestedcall[--pilecall_pos];
return -1;
}
static void increment_call(void) {
if (pilecall_pos) {
if (pile_nestedcall[pilecall_pos - 1] != -1)
pile_nestedcall[pilecall_pos - 1]++;
}
}
static int get_last_call(void) {
if (pilecall_pos)
return pile_nestedcall[pilecall_pos - 1];
return -1;
}
static void push_call(int call)
{
if (pilecall_pos != PILECALL_MAX)
pile_nestedcall[pilecall_pos++] = call;
else
exception(-1, _("Too many nested functions calls in expression.\n"));
}
static char *getword(char *line, char *p)
{
char o = 0, *d = line, instring = 0, gotbslash = 0;
do {
if (instring) {
o = *(p++) = *line;
if (!gotbslash) {
switch (instring) {
case 1:
if (*line == '\'') {
instring = 0;
}
break;
case 2:
if (*line == '"') {
instring = 0;
}
break;
}
if (*line == '\\') gotbslash = 1;
} else {
gotbslash = 0;
}
} else {
if (*(line) == '\'') {
o = *(p++) = *line;
instring = 1;
} else if (*(line) == '"') {
o = *(p++) = *line;
instring = 2;
} else {
if (*(line) != ' ') {
o = *(p++) = *line;
} else if (d != line) {
*p = '\0';
return line;
}
}
}
line++;
} while (((*line) && (*line != ')') && (*line != ']') && (*line != ';') && (get_func(*line) == -1)
&& (get_func(o) == -1)) || (instring));
*p = '\0';
return line;
}
void parse_line(char *line)
{
char buffer[BUFSIZ], imm[BUFSIZ];
op_t op;
int got_unary = 128, nbrargs;
while (*line) {
line = getword(line, buffer);
if (get_func(buffer[0]) != -1) {
buffer[0] += got_unary;
if (got_unary) {
}
if (get_pri(buffer[0]) == -1) {
if (got_unary) {
exception(-1, _("Invalid unary operator"));
} else {
exception(-1, _("Invalid binary operator"));
}
}
while (get_pri(get_last_op()) >= get_pri(buffer[0]) && (((get_last_op() & 127) != '(') && ((get_last_op() & 127) != '['))) {
act_pile(get_func(pop_op()));
got_unary = 0;
}
if (buffer[0] == '(') {
push_call(0);
}
if (buffer[0] == ',') {
increment_call();
} else push_op(buffer[0]);
got_unary = 128;
} else if ((buffer[0] == ';') || (buffer[0] == ')') || (buffer[0] == ']')) {
switch (buffer[0]) {
case ';':
while (pileop_pos) {
op = pop_op();
if (op == '(')
exception(-1, _("Parse error: too much left parenthesis"));
act_pile(get_func(op));
}
break;
case ')':
while (1) {
if (!pileop_pos)
exception(-1, _("Parse error: too much right parenthesis"));
op = pop_op();
if (((op & 127) == '('))
break;
if (((op & 127) == '['))
exception(-1, _("Parse error: enclosure mismatch"));
act_pile(get_func(op));
}
if (op == '(') {
nbrargs = pop_call();
sprintf(imm, "%i", nbrargs);
push_pile(imm);
act_pile(get_func(op));
}
got_unary = 0;
break;
case ']':
while (1) {
if (!pileop_pos)
exception(-1, _("Parse error: too much right parenthesis"));
op = pop_op();
if (((op & 127) == '['))
break;
if (((op & 127) == '('))
exception(-1, _("Parse error: enclosure mismatch"));
act_pile(get_func(op));
}
act_pile(get_func(op));
got_unary = 0;
break;
}
} else if (((buffer[0] >= 'A') && (buffer[0] <= 'Z')) || ((buffer[0] >= 'a') && (buffer[0] <= 'z'))
|| ((buffer[0] >= '0') && (buffer[0] <= '9')) || (buffer[0] == '_') || (buffer[0] == '"') || (buffer[0] == '\'')) {
push_pile(buffer);
got_unary = 0;
if (!get_last_call()) increment_call();
} else if (buffer[0]) {
exception(-1, _("Invalid character"));
}
}
}
#ifndef HAVE_CONFIG_H
void exception(int level, char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(level);
}
void push_pile(char *word)
{
printf("%s ", word);
}
void act_pile(int func)
{
char op;
switch (func) {
case OP_PLUS:
op = '+';
break;
case OP_MOINS:
op = '-';
break;
case OP_DIV:
op = '/';
break;
case OP_MUL:
op = '*';
break;
case OP_PLUS_UNARY:
op = '\0';
break;
case OP_MOINS_UNARY:
op = '|';
break;
case OP_FUNC_CALL:
op = '@';
break;
case OP_DECAL:
op = '[';
break;
case OP_DIRECT:
op = '{';
break;
}
printf("%c ", op);
}
void main(void)
{
parse_line("ADD R18, R20, 39;");
printf("\n\n"); fflush(stdout);
parse_line("MOV R31, Bidule[48 + R12];");
printf("\n\n"); fflush(stdout);
parse_line("MOV R12, [R3];");
printf("\n\n"); fflush(stdout);
parse_line("MOV R22, Truc[(3+2)*8];");
printf("\n\n"); fflush(stdout);
parse_line("Trucmuche DB \"Test de chaîne complète avec des opérateurs comme le + et le - ...\"");
printf("\n\n"); fflush(stdout);
}
#endif
|