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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
#
# Lexical analyzer.
#
lexdebug : con 0;
#
# Import tokens from parser.
#
Land,
Lat,
Lbackq,
Lcaret,
Lcase,
Lcolon,
Lcolonmatch,
Lcons,
Ldefeq,
Lelse,
Leof,
Leq,
Leqeq,
Lerror,
Lfn,
Lfor,
Lgreat,
Lgreatgreat,
Lhd,
Lif,
Lin,
Llen,
Lless,
Llessgreat,
Lmatch,
Lmatched,
Lnot,
Lnoteq,
Loffcurly,
Loffparen,
Loncurly,
Lonparen,
Lpipe,
Lquote,
Lrescue,
Lsemi,
Ltl,
Lwhile,
Lword
: import Mashparse;
KWSIZE: con 31; # keyword hashtable size
NCTYPE: con 128; # character class array size
ALPHA,
NUMERIC,
ONE,
WS,
META
: con 1 << iota;
keywords := array[] of
{
("case", Lcase),
("else", Lelse),
("fn", Lfn),
("for", Lfor),
("hd", Lhd),
("if", Lif),
("in", Lin),
("len", Llen),
("rescue", Lrescue),
("tl", Ltl),
("while", Lwhile)
};
ctype := array[NCTYPE] of
{
0 or ' ' or '\t' or '\n' or '\r' or '\v' => WS,
':' or '#' or ';' or '&' or '|' or '^' or '$' or '=' or '@'
or '~' or '`'or '{' or '}' or '(' or ')' or '<' or '>' => ONE,
'a' to 'z' or 'A' to 'Z' or '_' => ALPHA,
'0' to '9' => NUMERIC,
'*' or '[' or ']' or '?' => META,
* => 0
};
keytab: ref HashTable;
#
# Initialize hashtable.
#
initlex()
{
keytab = hash->new(KWSIZE);
for (i := 0; i < len keywords; i++) {
(s, v) := keywords[i];
keytab.insert(s, HashVal(v, 0.0, nil));
}
}
#
# Keyword value, or -1.
#
keyval(i: ref Item): int
{
if (i.op != Iword)
return -1;
w := i.word;
if (w.flags & Wquoted)
return -1;
v := keytab.find(w.text);
if (v == nil)
return -1;
return v.i;
}
#
# Attach a source file to an environment.
#
Env.fopen(e: self ref Env, fd: ref Sys->FD, s: string)
{
in := bufio->fopen(fd, Bufio->OREAD);
if (in == nil)
e.error(sys->sprint("could not fopen %s: %r\n", s));
e.file = ref File(in, s, 1, 0);
}
#
# Attach a source string to an environment.
#
Env.sopen(e: self ref Env, s: string)
{
in := bufio->sopen(s);
if (in == nil)
e.error(sys->sprint("Bufio->sopen failed: %r\n"));
e.file = ref File(in, "<string>", 1, 0);
}
#
# Close source file.
#
fclose(e: ref Env, c: int)
{
if (c == Bufio->ERROR)
readerror(e, e.file);
e.file.in.close();
e.file = nil;
}
#
# Character class routines.
#
isalpha(c: int): int
{
return c >= NCTYPE || (c >= 0 && (ctype[c] & ALPHA) != 0);
}
isalnum(c: int): int
{
return c >= NCTYPE || (c >= 0 && (ctype[c] & (ALPHA | NUMERIC)) != 0);
}
isdigit(c: int): int
{
return c >= 0 && c < NCTYPE && (ctype[c] & NUMERIC) != 0;
}
isquote(c: int): int
{
return c < NCTYPE && (c < 0 || (ctype[c] & (ONE | WS | META)) != 0);
}
isspace(c: int): int
{
return c >= 0 && c < NCTYPE && (ctype[c] & WS) != 0;
}
isterm(c: int): int
{
return c < NCTYPE && (c < 0 || (ctype[c] & (ONE | WS)) != 0);
}
#
# Test for an identifier.
#
ident(s: string): int
{
if (s == nil || !isalpha(s[0]))
return 0;
n := len s;
for (x := 1; x < n; x++) {
if (!isalnum(s[x]))
return 0;
}
return 1;
}
#
# Quote text.
#
enquote(s: string): string
{
r := "'";
j := 1;
n := len s;
for (i := 0; i < n; i++) {
c := s[i];
if (c == '\'' || c == '\\')
r[j++] = '\\';
r[j++] = c;
}
r[j] = '\'';
return r;
}
#
# Quote text if needed.
#
quote(s: string): string
{
n := len s;
for (i := 0; i < n; i++) {
if (isquote(s[i]))
return enquote(s);
}
return s;
}
#
# Test for single word and identifier.
#
Item.sword(i: self ref Item, e: ref Env): ref Item
{
if (i.op == Iword && ident(i.word.text))
return i;
e.report("malformed identifier: " + i.text());
return nil;
}
readerror(e: ref Env, f: ref File)
{
sys->fprint(e.stderr, "error reading %s: %r\n", f.name);
}
where(e: ref Env): string
{
if ((e.flags & EInter) || e.file == nil)
return nil;
return e.file.name + ":" + string e.file.line + ": ";
}
#
# Suck input (on error).
#
Env.suck(e: self ref Env)
{
if (e.file == nil)
return;
in := e.file.in;
while ((c := in.getc()) >= 0 && c != '\n')
;
}
#
# Lexical analyzer.
#
Env.lex(e: self ref Env, yylval: ref Mashparse->YYSTYPE): int
{
i, r: ref Item;
reader:
for (;;) {
if (e.file == nil)
return -1;
f := e.file;
in := f.in;
while (isspace(c := in.getc())) {
if (c == '\n')
f.line++;
}
if (c < 0) {
fclose(e, c);
return Leof;
}
case c {
':' =>
if ((d := in.getc()) == ':')
return Lcons;
if (d == '=')
return Ldefeq;
if (d == '~')
return Lcolonmatch;
if (d >= 0)
in.ungetc();
return Lcolon;
'#' =>
for (;;) {
if ((c = in.getc()) < 0) {
fclose(e, c);
return Leof;
}
if (c == '\n') {
f.line++;
continue reader;
}
}
';' =>
return Lsemi;
'&' =>
return Land;
'|' =>
return Lpipe;
'^' =>
return Lcaret;
'@' =>
return Lat;
'!' =>
if ((d := in.getc()) == '=')
return Lnoteq;
if (d >= 0)
in.ungetc();
return Lnot;
'~' =>
return Lmatch;
'=' =>
if ((d := in.getc()) == '>')
return Lmatched;
if (d == '=')
return Leqeq;
if (d >= 0)
in.ungetc();
return Leq;
'`' =>
return Lbackq;
'"' =>
return Lquote;
'{' =>
return Loncurly;
'}' =>
return Loffcurly;
'(' =>
return Lonparen;
')' =>
return Loffparen;
'<' =>
if ((d := in.getc()) == '>')
return Llessgreat;
if (d >= 0)
in.ungetc();
return Lless;
'>' =>
if ((d := in.getc()) == '>')
return Lgreatgreat;
if (d >= 0)
in.ungetc();
return Lgreat;
'\\' =>
if ((d := in.getc()) == '\n') {
f.line++;
continue reader;
}
if (d >= 0)
in.ungetc();
}
# Loop over "carets for free".
for (;;) {
if (c == '$')
(i, c) = getdollar(f);
else
(i, c) = getword(e, f, c);
if (i == nil)
return Lerror;
if (isterm(c) && c != '$')
break;
if (r != nil)
r = ref Item(Iicaret, nil, r, i, nil, nil);
else
r = i;
}
if (c >= 0)
in.ungetc();
if (r != nil)
yylval.item = ref Item(Iicaret, nil, r, i, nil, nil);
else if ((c = keyval(i)) >= 0)
return c;
else
yylval.item = i;
return Lword;
}
}
#
# Get $n or $word.
#
getdollar(f: ref File): (ref Item, int)
{
s: string;
in := f.in;
l := f.line;
o := Idollar;
if (isdigit(c := in.getc())) {
s[0] = c;
n := 1;
while (isdigit(c = in.getc()))
s[n++] = c;
o = Imatch;
} else {
if (c == '"') {
o = Idollarq;
c = in.getc();
}
if (isalpha(c)) {
s[0] = c;
n := 1;
while (isalnum(c = in.getc()))
s[n++] = c;
} else {
if (o == Idollar)
s = "$";
else
s = "$\"";
o = Iword;
}
}
return (ref Item(o, ref Word(s, 0, Src(l, f.name)), nil, nil, nil, nil), c);
}
#
# Get word with quoting.
#
getword(e: ref Env, f: ref File, c: int): (ref Item, int)
{
s: string;
in := f.in;
l := f.line;
wf := 0;
n := 0;
if (c == '\'') {
wf = Wquoted;
collect:
while ((c = in.getc()) >= 0) {
case c {
'\'' =>
c = in.getc();
break collect;
'\\' =>
c = in.getc();
if (c != '\'' && c != '\\') {
if (c == '\n')
continue collect;
if (c >= 0)
in.ungetc();
c = '\\';
}
'\n' =>
f.line++;
e.report("newline in quoted word");
return (nil, 0);
}
s[n++] = c;
}
} else {
do {
case c {
'*' or '[' or '?' =>
wf |= Wexpand;
}
s[n++] = c;
} while (!isterm(c = in.getc()) && c != '\'');
}
if (lexdebug && s == "exit")
exit;
return (ref Item(Iword, ref Word(s, wf, Src(l, f.name)), nil, nil, nil, nil), c);
}
#
# Get a line, mapping escape newline to space newline.
#
getline(in: ref Bufio->Iobuf): string
{
if (inchan != nil) {
alt {
b := <-inchan =>
if (inchan == nil)
return nil;
s := string b;
n := len s;
if (n > 1) {
while (s[n - 2] == '\\' && s[n - 1] == '\n') {
s[n - 2] = ' ';
s[n - 1] = ' ';
prprompt(1);
b = <-inchan;
if (b == nil)
break;
s += string b;
n = len s;
}
}
return s;
b := <-servechan =>
s := string b;
sys->print("%s", s);
return s;
}
} else {
s := in.gets('\n');
if (s == nil)
return nil;
n := len s;
if (n > 1) {
while (s[n - 2] == '\\' && s[n - 1] == '\n') {
s[n - 2] = ' ';
s[n - 1] = ' ';
prprompt(1);
t := in.gets('\n');
if (t == nil)
break;
s += t;
n = len s;
}
}
return s;
}
}
#
# Interactive shell loop.
#
Env.interactive(e: self ref Env, fd: ref Sys->FD)
{
in := bufio->fopen(fd, Sys->OREAD);
if (in == nil)
e.error(sys->sprint("could not fopen stdin: %r\n"));
e.flags |= EInter;
for (;;) {
prprompt(0);
if (startserve)
e.serve();
if ((s := getline(in)) == nil)
exitmash();
e.sopen(s);
parse->parse(e);
if (histchan != nil)
histchan <-= array of byte s;
}
}
|