-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmpw-shell-parser.cpp
508 lines (387 loc) · 9.76 KB
/
mpw-shell-parser.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "mpw-shell.h"
#include "fdset.h"
#include "value.h"
#include "error.h"
#include "mpw-regex.h"
#include <unistd.h>
#include <fcntl.h>
#include <algorithm>
/*
* I'm sick of fighting with lemon. Just generate it by hand.
*
*/
namespace ToolBox {
std::string MacToUnix(const std::string path);
std::string UnixToMac(const std::string path);
}
template<class T>
T pop(std::vector<T> &v) {
T t = std::move(v.back());
v.pop_back();
return t;
}
/*
### MPW Shell - Unable to open "nofile".
# File not found (OS error -43)
{status} = -4
*/
void open_error(const std::string &name) {
std::string error = "MPW Shell - Unable to open ";
error.push_back('"');
error.append(name);
error.push_back('"');
error.push_back('.');
throw mpw_error(-4, error);
}
int open(const std::string &name, int flags) {
// dup2 does not copy the O_CLOEXEC flag so it's safe to use.
std::string uname = ToolBox::MacToUnix(name);
int fd = ::open(uname.c_str(), flags | O_CLOEXEC, 0666);
if (fd < 0) {
open_error(name);
return -1;
}
return fd;
}
void parse_tokens(std::vector<token> &&tokens, process &p) {
fdset fds;
std::vector<std::string> argv;
std::reverse(tokens.begin(), tokens.end());
argv.reserve(tokens.size());
// first token is always treated as a string.
token t = pop(tokens);
argv.emplace_back(std::move(t.string));
while(!tokens.empty()) {
t = pop(tokens);
int flags;
unsigned fd_bits;
switch (t.type) {
// >, >> -- redirect stdout.
case '>':
flags = O_WRONLY | O_CREAT | O_TRUNC;
fd_bits = 1 << 1;
goto redir;
case '>>':
flags = O_WRONLY | O_CREAT | O_APPEND;
fd_bits = 1 << 1;
goto redir;
case '<':
flags = O_RDONLY;
fd_bits = 1 << 0;
goto redir;
// ∑, ∑∑ - redirect stdout & stderr
case 0xb7: // ∑
flags = O_WRONLY | O_CREAT | O_TRUNC;
fd_bits = (1 << 1) + (1 << 2);
goto redir;
case 0xb7b7: // ∑∑
flags = O_WRONLY | O_CREAT | O_APPEND;
fd_bits = (1 << 1) + (1 << 2);
goto redir;
// ≥, ≥≥ -- redirect stderr.
case 0xb3: // ≥
flags = O_WRONLY | O_CREAT | O_TRUNC;
fd_bits = 1 << 2;
goto redir;
case 0xb3b3: // ≥≥
flags = O_WRONLY | O_CREAT | O_APPEND;
fd_bits = 1 << 2;
goto redir;
redir:
{
if (tokens.empty()) {
throw mpw_error(-4, "MPW Shell - Missing file name.");
}
token name = pop(tokens);
int fd = open(name.string, flags);
// todo -- if multiple fd_bits (stdin+stderr, should dup the second fd?)
switch(fd_bits) {
case 1 << 0:
fds.set(0, fd);
break;
case 1 << 1:
fds.set(1, fd);
break;
case 1 << 2:
fds.set(2, fd);
break;
case (1 << 1) + (1 << 2):
{
int newfd = fcntl(fd, F_DUPFD_CLOEXEC);
if (newfd < 1) {
close(fd);
open_error(name);
}
fds.set(1, fd);
fds.set(2, newfd);
break;
}
default:
// ???
close(fd);
}
}
break;
default:
argv.emplace_back(std::move(t.string));
break;
}
}
p.arguments = std::move(argv);
p.fds = std::move(fds);
}
class expression_parser {
public:
expression_parser(Environment &e, const std::string &n, std::vector<token> &&t) :
environment(e), name(n), tokens(std::move(t))
{}
expression_parser(const expression_parser &) = delete;
expression_parser(expression_parser &&) = delete;
expression_parser& operator=(const expression_parser &) = delete;
expression_parser& operator=(expression_parser &&) = delete;
// returns integer value of the expression.
int32_t evaluate();
private:
value terminal();
value unary();
value binary();
value eval(int op, value &lhs, value &rhs);
value eval_regex(value &lhs, value &rhs);
[[noreturn]] void expect_binary_operator();
[[noreturn]] void end_of_expression();
[[noreturn]] void divide_by_zero();
int peek_type() const;
token next();
static int precedence(int);
void skip() {
if (!tokens.empty()) tokens.pop_back();
}
Environment &environment;
const std::string &name;
std::vector<token> tokens;
};
int expression_parser::peek_type() const {
if (tokens.empty()) return token::eof;
return tokens.back().type;
}
token expression_parser::next() {
if (tokens.empty()) return token("", token::eof); // error?
return pop(tokens);
}
void expression_parser::expect_binary_operator() {
token t = next();
std::string error;
error = name;
error += " - Expected a binary operator when \"";
error += t.string;
error += "\" was encountered.";
throw mpw_error(-5, error);
}
void expression_parser::end_of_expression() {
std::string error;
error = name + " - Unexpected end of expression.";
throw mpw_error(-5, error);
}
void expression_parser::divide_by_zero() {
std::string error;
error = name + " - Attempt to divide by zero.";
throw mpw_error(-5, error);
}
value expression_parser::binary() {
std::vector<value> output;
std::vector<std::pair<int, int>> operators;
value v = unary();
output.emplace_back(std::move(v));
for(;;) {
// check for an operator.
int type = peek_type();
if (type == token::eof) break;
if (type == ')') break;
int p = precedence(type);
if (!p) expect_binary_operator();
skip();
while (!operators.empty() && operators.back().second <= p) {
// reduce top ops.
int op = operators.back().first;
operators.pop_back();
value rhs = pop(output);
value lhs = pop(output);
output.emplace_back(eval(op, lhs, rhs));
}
operators.push_back(std::make_pair(type, p));
v = unary();
output.emplace_back(std::move(v));
}
// reduce...
while (!operators.empty()) {
int op = pop(operators).first;
value rhs = pop(output);
value lhs = pop(output);
output.emplace_back(eval(op, lhs, rhs));
}
if (output.size() != 1) throw std::runtime_error("binary stack error");
return pop(output);
}
int expression_parser::precedence(int op) {
switch (op) {
case '*':
case '%':
case '/':
return 3;
case '+':
case '-':
return 4;
case '>>':
case '<<':
return 5;
case '<':
case '<=':
case '>':
case '>=':
case 0xb2:
case 0xb3:
return 6;
case '==':
case '!=':
case '=~':
case '!~':
return 7;
case '&':
return 8;
case '^':
return 9;
case '|':
return 10;
case '&&':
return 11;
case '||':
return 12;
}
return 0;
//throw std::runtime_error("unimplemented op";);
}
value expression_parser::eval_regex(value &lhs, value &rhs) {
try {
mpw_regex re(rhs.string, true);
// todo -- need environment to store matches.
bool ok = re.match(lhs.string, environment);
return ok ? 1 : 0;
} catch (std::exception &ex) {
std::string error;
error = name;
if (rhs.string.empty() || rhs.string.front() != '/')
error += " - Missing /s around regular expression: ";
else
error += " - Invalid regular expression encountered: ";
error += rhs.string;
throw mpw_error(-5, error);
}
}
value expression_parser::eval(int op, value &lhs, value &rhs) {
switch (op) {
case '*':
return lhs.to_number() * rhs.to_number();
case '/':
if (!rhs.to_number()) divide_by_zero();
return lhs.to_number() / rhs.to_number();
case '%':
if (!rhs.to_number()) divide_by_zero();
return lhs.to_number() % rhs.to_number();
case '+':
return lhs.to_number() + rhs.to_number();
case '-':
return lhs.to_number() - rhs.to_number();
case '>':
return lhs.to_number() > rhs.to_number();
case '<':
return lhs.to_number() < rhs.to_number();
case '<=':
case 0xb2:
return lhs.to_number() <= rhs.to_number();
case '>=':
case 0xb3:
return lhs.to_number() >= rhs.to_number();
case '>>':
return lhs.to_number() >> rhs.to_number();
case '<<':
return lhs.to_number() << rhs.to_number();
// logical || . NaN ok
case '||':
return lhs.to_number(1) || rhs.to_number(1);
// logical && . NaN ok
case '&&':
return lhs.to_number(1) && rhs.to_number(1);
case '|':
return lhs.to_number() | rhs.to_number();
case '&':
return lhs.to_number() & rhs.to_number();
case '^':
return lhs.to_number() ^ rhs.to_number();
case '==':
// string ==. 0x00==0 -> 0
// as a special case, 0=="". go figure.
if (lhs.string == "" && rhs.string == "0") return 1;
if (lhs.string == "0" && rhs.string == "") return 1;
return lhs.string == rhs.string;
case '!=':
if (lhs.string == "" && rhs.string == "0") return 0;
if (lhs.string == "0" && rhs.string == "") return 0;
return lhs.string != rhs.string;
case '=~':
return eval_regex(lhs, rhs);
case '!~':
return !eval_regex(lhs, rhs).number;
}
// todo...
throw std::runtime_error("unimplemented op");
}
value expression_parser::unary() {
int type = peek_type();
switch (type) {
case '-':
case '+':
case '!':
case '~':
next();
value v = unary();
// + is a nop.. doesn't even check if it's a number.
if (type == '-') v = -v.to_number();
if (type == '~') v = ~v.to_number();
if (type == '!') v = !v.to_number(1); // logical !, NaN ok.
return v;
}
return terminal();
}
value expression_parser::terminal() {
int type = peek_type();
if (type == token::text) {
token t = next();
return value(std::move(t.string));
}
if (type == '(') {
next();
value v = binary();
type = peek_type();
if (type != ')') {
end_of_expression();
}
next();
return v;
}
// insert a fake token.
return value();
}
int32_t expression_parser::evaluate() {
if (tokens.empty()) return 0;
value v = binary();
if (!tokens.empty()) {
if (tokens.back().type == ')')
throw mpw_error(-3, "MPW Shell - Extra ) command.");
throw std::runtime_error("evaluation stack error."); // ?? should be caught above.
}
return v.to_number(1);
}
int32_t evaluate_expression(Environment &env, const std::string &name, std::vector<token> &&tokens) {
expression_parser p(env, name, std::move(tokens));
return p.evaluate();
}