-
Notifications
You must be signed in to change notification settings - Fork 2
/
lambda-js.dom.test.ts
191 lines (158 loc) · 3.72 KB
/
lambda-js.dom.test.ts
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
// Places textareas into a browser that allow for writing and running lambda
// calculus code. This automatically injects the S, K, and I functions, and
// compiles to a lambda calculus expression rather than a number, pair, or
// other representation.
import * as L from "./lambda-js.js"
function run() {
try {
let parsed = L.parse(code.value)
let output =
(isCompact() ? parsed.toCompactString() : parsed.toString()) + "\n"
const next = parsed.ski()
if (next.toString() != parsed.toString()) {
if (willShowSteps()) {
const nextOutput = isCompact()
? next.toCompactString()
: next.toString()
if (nextOutput.length > 40) {
output += nextOutput + "\n\n"
} else {
output += nextOutput + "\n"
}
}
parsed = next
}
for (let i = 0; i < iterCount(); i++) {
const next = parsed.eval()
if (next.toString() == parsed.toString()) {
break
}
if (willShowSteps()) {
const nextOutput = isCompact()
? next.toCompactString()
: next.toString()
if (nextOutput.length > 40) {
output += nextOutput + "\n\n"
} else {
output += nextOutput + "\n"
}
}
parsed = next
}
if (willShowSteps()) {
outputEl.value = output
return
}
if (isCompact()) {
outputEl.value = parsed.toCompactString()
} else {
outputEl.value = parsed.toString()
}
} catch (error) {
outputEl.value = error instanceof Error ? error.message : String(error)
}
}
function createControl<T>({
label,
type,
value,
get,
set,
}: {
label: string
type: "checkbox" | "number" | "text"
value: T
get: (field: HTMLInputElement) => T
set: (field: HTMLInputElement, value: T) => void
}): [HTMLLabelElement, () => T] {
const labelEl = document.createElement("label")
labelEl.textContent = label
const fieldEl = document.createElement("input")
fieldEl.type = type
set(fieldEl, value)
fieldEl.addEventListener("input", () => {
value = get(fieldEl)
run()
})
labelEl.append(fieldEl)
return [labelEl, () => value]
}
const [iterCountEl, iterCount] = createControl({
get(field) {
return field.valueAsNumber
},
set(field, value) {
field.valueAsNumber = value
},
label: "Number of Steps:",
type: "number",
value: 1000,
})
const [isCompactEl, isCompact] = createControl({
get(field) {
return field.checked
},
set(field, value) {
field.checked = value
},
label: "Compact Output?",
type: "checkbox",
value: true,
})
const [willShowStepsEl, willShowSteps] = createControl({
get(field) {
return field.checked
},
set(field, value) {
field.checked = value
},
label: "Show Steps?",
type: "checkbox",
value: false,
})
const code = document.createElement("textarea")
code.placeholder = "zero = \\f \\x x;\nsucc = \\n \\f \\x f (n f x);\nsucc zero"
code.addEventListener("input", run)
const outputEl = document.createElement("textarea")
outputEl.readOnly = true
const styleEl = document.createElement("style")
styleEl.textContent = `
*, ::before, ::after {
box-sizing: border-box;
}
body {
height: 100vh;
margin: 0;
padding: 8px;
display: flex;
flex-direction: column;
gap: 8px;
font-family: ui-system, sans-serif;
}
label {
display: flex;
gap: 8px;
width: 100%;
align-items: center;
}
input {
margin: 0;
}
div {
display: flex;
gap: 8px;
}
div:last-child {
flex: 1;
}
textarea {
display: block;
flex: 1;
resize: none;
}
`
const div1 = document.createElement("div")
div1.append(iterCountEl, isCompactEl, willShowStepsEl)
const div2 = document.createElement("div")
div2.append(code, outputEl)
document.body.append(styleEl, div1, div2)