Skip to content

Commit 8f05dc0

Browse files
committed
测试
1 parent 03df64d commit 8f05dc0

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

parse-lisp-expression/index.ts

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,58 @@ function parseAdd(expression: string): Expression {
22
const content = expression.slice("(add ".length, -1);
33
const list = parseList("(" + content + ")");
44
console.log(list);
5-
throw Error("Not implemented");
5+
if (Array.isArray(list) && list.length !== 2) {
6+
throw Error("Invalid expression");
7+
}
8+
return buildExpression(["add", ...list]);
69
}
710
function parseLet(expression: string): Expression {
811
const content = expression.slice("(let ".length, -1);
912
const list = parseList("(" + content + ")");
1013
console.log(list);
11-
throw Error("Not implemented");
14+
if (Array.isArray(list) && list.length == 0) {
15+
throw Error("Invalid expression");
16+
}
17+
return buildExpression(["let", ...list]);
1218
}
1319

1420
function parseMult(expression: string): Expression {
1521
const content = expression.slice("(mult ".length, -1);
1622
const list = parseList("(" + content + ")");
1723
console.log(list);
24+
if (Array.isArray(list) && list.length !== 2) {
25+
throw Error("Invalid expression");
26+
}
27+
return buildExpression(["mult", ...list]);
28+
}
29+
function buildExpression(list: string | number | ListArray): Expression {
30+
console.log(list);
31+
if (typeof list === "string") {
32+
return { type: "Identifier", name: list };
33+
}
34+
if (typeof list === "number") {
35+
return parseNumeric(list);
36+
}
37+
38+
if (Array.isArray(list) && list.length == 0) {
39+
throw Error("Invalid expression");
40+
}
41+
if (Array.isArray(list) && list[0] === "mult") {
42+
return {
43+
type: "MultExpression",
44+
45+
left: buildExpression(list[1]),
46+
right: buildExpression(list[2]),
47+
};
48+
}
49+
if (Array.isArray(list) && list[0] === "add") {
50+
return {
51+
type: "AddExpression",
52+
53+
left: buildExpression(list[1]),
54+
right: buildExpression(list[2]),
55+
};
56+
}
1857
throw Error("Not implemented");
1958
}
2059
export type ListArray = Array<ListArray | string | number>;
@@ -28,11 +67,12 @@ function parseList(expression: string): ListArray {
2867
);
2968
}
3069

31-
function parseNumeric(expression: string): Expression {
70+
function parseNumeric(expression: string | number): Expression {
3271
return { type: "NumericLiteral", value: Number(expression) };
3372
}
3473

3574
function evaluate(expression: string): number {
75+
console.log(expression);
3676
const ast = parse(expression);
3777
console.log(ast);
3878
return calculate(ast, new ScopeList());

parse-lisp-expression/test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ Deno.test("parse-lisp-expression", () => {
55
assertEquals(9, evaluate("9"));
66
assertEquals(1119, evaluate("1119"));
77
});
8+
Deno.test("parse-lisp-expression", () => {
9+
assertEquals(87912, evaluate("(mult 88 999)"));
10+
});
11+
Deno.test("parse-lisp-expression", () => {
12+
assertEquals(2, evaluate("(let x 3 x 2 x)"));
13+
});
14+
Deno.test("parse-lisp-expression", () => {
15+
assertEquals(1007, evaluate("(add 8 999)"));
16+
});
17+
Deno.test("parse-lisp-expression", () => {
18+
assertEquals(14, evaluate("(let x 2 (mult x (let x 3 y 4 (add x y))))"));
19+
});
20+
Deno.test("parse-lisp-expression", () => {
21+
assertEquals(5, evaluate("(let x 1 y 2 x (add x y) (add x y))"));
22+
});

0 commit comments

Comments
 (0)