Skip to content

Commit 5bccbbb

Browse files
committed
formatValue
1 parent 58b3421 commit 5bccbbb

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

TODO.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# readback
2+
3+
remove `readback`
4+
15
# lazy evaluation
26

37
we still can bring back lazy evaluation by lazy + box,
@@ -34,7 +38,3 @@ why top level wrap need a eta?
3438
```
3539

3640
can adding lazy evaluation help fix this?
37-
38-
# readback
39-
40-
be able to readback ackermann function -- maybe use occur check for all the sub-expressions

src/lang/format/formatValue.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { type Neutral, type Value } from "../value/index.ts"
2+
import { formatExp } from "./formatExp.ts"
3+
4+
export function formatValue(value: Value): string {
5+
switch (value.kind) {
6+
case "NotYet": {
7+
return formatNeutral(value.neutral)
8+
}
9+
10+
case "Lambda": {
11+
if (value.definedName === undefined) {
12+
return `(lambda (${value.name}) ${value.ret})`
13+
} else {
14+
return value.definedName
15+
}
16+
}
17+
18+
case "Lazy": {
19+
return formatExp(value.exp)
20+
}
21+
22+
case "DelayedApply": {
23+
const target = formatValue(value.target)
24+
const arg = formatValue(value.arg)
25+
return `(${target} ${arg})`
26+
}
27+
}
28+
}
29+
30+
function formatNeutral(neutral: Neutral): string {
31+
switch (neutral.kind) {
32+
case "Var": {
33+
return neutral.name
34+
}
35+
36+
case "Apply": {
37+
const target = formatNeutral(neutral.target)
38+
const arg = formatValue(neutral.arg)
39+
return `(${target} ${arg})`
40+
}
41+
}
42+
}

src/lang/format/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./formatExp.ts"
2+
export * from "./formatValue.ts"

0 commit comments

Comments
 (0)