-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
104 lines (87 loc) · 2.71 KB
/
parser.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
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
// 编程语言中最直接可见的部分是语法syntax
// 解析器parser是一个程序 读取一段文本并生成一个反应该文本中包含的数据结构的数据结构
const {evaluate} = require('./eval')
const { topScope } = require('./scope')
/**
* 接受一个字符串作为输入并返回一个对象 该对象包含了字符串起始位置处表达式的数据结构与解析表达式后剩余的字符串
* @param {*} program
*
*/
function parseExpression(program) {
program = skipSpace(program);
let match, expr;
if (match = /^"([^"]*)"/.exec(program)) {
expr = { type: "value", value: match[1] };
} else if (match = /^\d+\b/.exec(program)) {
expr = { type: "value", value: Number(match[0]) };
} else if (match = /^[^\s(),#"]+/.exec(program)) {
expr = { type: "word", name: match[0] };
} else {
throw new SyntaxError("Unexpected syntax: " + program);
}
return parseApply(expr, program.slice(match[0].length));
}
parseExpression("+(a, 10)")
function skipSpace(string) {
let first = string.search(/\S/);
if (first == -1) return "";
return string.slice(first);
}
function parseApply(expr, program) {
program = skipSpace(program);
if (program[0] != "(") {
return { expr: expr, rest: program };
}
program = skipSpace(program.slice(1));
expr = { type: "apply", operator: expr, args: [] };
while (program[0] != ")") {
let arg = parseExpression(program);
expr.args.push(arg.expr);
program = skipSpace(arg.rest);
if (program[0] == ",") {
program = skipSpace(program.slice(1));
} else if (program[0] != ")") {
throw new SyntaxError("Expected ',' or ')'");
}
}
return parseApply(expr, program.slice(1));
}
function parse(program) {
let { expr, rest } = parseExpression(program);
if (skipSpace(rest).length > 0) {
throw new SyntaxError("Unexpected text after program");
}
return expr;
}
// console.log(parse("+(a, 10)"));
// → {type: "apply",
// operator: {type: "word", name: "+"},
// args: [{type: "word", name: "a"},
// {type: "value", value: 10}]}
// let prog = parse(`if(true, false, true)`);
// console.log(evaluate(prog, topScope));
// // → false
// function run(program) {
// return evaluate(parse(program), Object.create(topScope));
// }
// run(`
// do(define(total, 0),
// define(count, 1),
// while(<(count, 11),
// do(define(total, +(total, count)),
// define(count, +(count, 1)))),
// print(total))
// `);
// run(`
// do(define(plusOne, fun(a, +(a, 1))),
// print(plusOne(10)))
// `);
// // → 11
// run(`
// do(define(pow, fun(base, exp,
// if(==(exp, 0),
// 1,
// *(base, pow(base, -(exp, 1)))))),
// print(pow(2, 10)))
// `);
// → 1024