-
Notifications
You must be signed in to change notification settings - Fork 1
/
s_expr.js
69 lines (59 loc) · 1.48 KB
/
s_expr.js
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
/*
* S-Expression parser
*
* Example:
* node s$expr.js <expression>
*/
"use strict";
// const lq = require("@loquat/simple");
const lq = require("../index");
const space = lq.oneOf("\r\n\t ").label("");
function lexeme(parser) {
return parser.skipMany(space);
}
// expr ::= atom | list
const expr = lq.lazy(() => lq.choice([
atom,
list,
])).label("expression");
// atom ::= letter atom_tail*
// atom_tail ::= letter | number
const atom = lexeme(lq.qo(function* () {
const x = yield lq.letter;
const xs = yield lq.alphaNum.manyChars();
return x + xs;
})).label("atom");
// list ::= "(" expr* ["." expr] ")"
const list = lq.qo(function* () {
yield lexeme(lq.char("("));
const xs = yield expr.many();
const x = yield lq.option(null, lexeme(lq.char(".")).and(expr));
yield lexeme(lq.char(")"));
return xs.reduceRight((ys, y) => ({ car: y, cdr: ys }), x);
}).label("list");
const parser = lq.spaces.and(expr).left(lq.eof);
function parse(src) {
const result = lq.parse(parser, "", src);
if (result.success) {
console.log(print(result.value));
} else {
console.error(result.error.toString());
}
}
// pretty printer
function print(x) {
if (typeof x === "object") {
const xs = [];
let y = x;
while (typeof y === "object" && y !== null) {
xs.push(print(y.car));
y = y.cdr;
}
return y === null
? `(${xs.join(" ")})`
: `(${xs.join(" ")} . ${print(y)})`;
} else {
return x.toString();
}
}
parse(process.argv[2]);