-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
307 lines (294 loc) · 9.23 KB
/
index.html
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lispy in JS</title>
<style>
:root {
--bg-color: #1e1e2e;
--text-color: #cdd6f4;
--input-bg: #313244;
--highlight-color: #89b4fa;
--secondary-color: #f5c2e7;
--error-color: #f38ba8;
--links-color: #15e0bf;
}
body {
font-family: "Fira Code", monospace;
background-color: var(--bg-color);
color: var(--text-color);
max-width: 1200px;
margin: 0 auto;
padding: 20px;
display: flex;
flex-direction: column;
height: 100vh;
}
h1,
h3 {
text-align: center;
color: var(--highlight-color);
margin-bottom: 20px;
}
#main-container {
display: grid;
grid-template-columns: 200px 1fr 300px;
gap: 20px;
height: calc(100vh - 100px);
}
#credits {
background-color: var(--input-bg);
border-radius: 5px;
padding: 10px;
font-size: 0.9em;
}
a {
color: var(--links-color);
}
#repl-container {
display: flex;
flex-direction: column;
}
#output {
flex: 1;
border: 1px solid var(--highlight-color);
border-radius: 5px;
padding: 10px;
overflow-y: auto;
background-color: rgba(49, 50, 68, 0.7);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
#sidebar {
display: flex;
flex-direction: column;
gap: 20px;
overflow-y: auto;
}
#examples,
#infobox,
#env-view {
background-color: var(--input-bg);
border-radius: 5px;
padding: 10px;
}
.example-category {
margin-bottom: 15px;
}
.example {
cursor: pointer;
margin-bottom: 5px;
padding: 5px;
border-radius: 3px;
transition: background-color 0.3s ease;
}
.example:hover {
background-color: rgba(137, 180, 250, 0.2);
}
#input-container {
display: flex;
}
#input {
flex-grow: 1;
padding: 10px;
border: none;
border-radius: 5px 0 0 5px;
background-color: var(--input-bg);
color: var(--text-color);
font-family: inherit;
}
#evaluate-btn {
padding: 10px 20px;
background-color: var(--highlight-color);
color: var(--bg-color);
border: none;
border-radius: 0 5px 5px 0;
cursor: pointer;
transition: background-color 0.3s ease;
}
#evaluate-btn:hover {
background-color: var(--secondary-color);
}
.input-line {
color: var(--highlight-color);
margin-bottom: 5px;
}
.error-line {
color: var(--error-color);
}
.output-line {
margin-bottom: 10px;
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
</style>
</head>
<body>
<h1>Lispy (but Lisp-jsy instead)</h1>
<div id="main-container">
<div id="credits">
<h3>Credits</h3>
<p>This Lispy implementation is inspired by:</p>
<ul>
<li>
<a href="https://norvig.com/lispy.html" target="_blank"
>Peter Norvig's Lispy</a
>
</li>
<li>
<a href="https://craftinginterpreters.com/" target="_blank"
>"Crafting Interpreters" by Robert Nystrom</a
>
</li>
</ul>
</div>
<div id="repl-container">
<div id="input-container">
<input
type="text"
id="input"
placeholder="Enter Lispy expression..."
/>
<button id="evaluate-btn">Evaluate</button>
</div>
<div id="output"></div>
</div>
<div id="sidebar">
<div id="examples">
<h3>Examples:</h3>
<div class="example-category">
<h4>Basic Operations</h4>
<div class="example">(+ 1 2)</div>
<div class="example">(* 4 5)</div>
<div class="example">(/ 10 2)</div>
</div>
<div class="example-category">
<h4>Variables and Functions</h4>
<div class="example">(define x 10)</div>
<div class="example">(define square (lambda (x) (* x x)))</div>
<div class="example">(square 5)</div>
</div>
<div class="example-category">
<h4>Conditionals</h4>
<div class="example">(if (> 5 3) (quote yes) (quote no))</div>
<div class="example">(if (< 5 3) (quote yes) (quote no))</div>
</div>
<div class="example-category">
<h4>Lists and List Operations</h4>
<div class="example">(define my-list (list 1 2 3 4 5))</div>
<div class="example">(car my-list)</div>
<div class="example">(cdr my-list)</div>
</div>
<div class="example-category">
<h4>Higher-Order Functions and map2 Implementation</h4>
<div class="example">(define square (lambda (x) (* x x)))</div>
<div class="example">(define inc (lambda (x) (+ x 1)))</div>
<div class="example">(define numbers (list 1 2 3 4 5))</div>
<div class="example">(map square numbers)</div>
<div class="example">
(define map2 (lambda (f items) (if (null? items) (list) (cons (f
(car items)) (map2 f (cdr items))))))
</div>
<div class="example">(map2 square numbers)</div>
<div class="example">(map2 inc numbers)</div>
<div class="example">
(define compose (lambda (f g) (lambda (x) (f (g x)))))
</div>
<div class="example">
(define square-then-inc (compose inc square))
</div>
<div class="example">(map2 square-then-inc numbers)</div>
</div>
<div class="example-category">
<h4>Recursion (Looping)</h4>
<div class="example">
(define factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n
1))))))
</div>
<div class="example">(factorial 5)</div>
</div>
</div>
<div id="infobox">
<h3>Standard Environment</h3>
<p>
The standard environment includes basic arithmetic operations (+, -,
*, /), comparison operators (>, <, >=, <=, =), and common
mathematical functions (sqrt, exp, log, sin, cos, etc.).
</p>
<p>
It also includes list operations (car, cdr, cons, list) and other
useful functions (map, apply, begin).
</p>
<p>Control flow is handled with 'if' statements and recursion.</p>
</div>
<div id="env-view">
<h3>Current Environment</h3>
<pre id="env-content"></pre>
</div>
</div>
</div>
<script src="lispy.js"></script>
<script>
const outputElement = document.getElementById("output");
const inputElement = document.getElementById("input");
const envViewElement = document.getElementById("env-content");
const env = standardEnv();
function updateEnvView() {
envViewElement.textContent = displayEnv(env);
}
function _evaluate() {
const input = inputElement.value.trim();
if (input === "") return;
const inputLine = document.createElement("div");
inputLine.className = "input-line";
inputLine.textContent = `> ${input}`;
outputElement.appendChild(inputLine);
try {
const result = evaluate(parse(input), env);
const outputLine = document.createElement("div");
outputLine.className = "output-line";
outputLine.textContent = schemeString(result);
outputElement.appendChild(outputLine);
} catch (error) {
const errorLine = document.createElement("div");
errorLine.className = "error-line";
errorLine.textContent = `Error: ${error.message}`;
outputElement.appendChild(errorLine);
}
inputElement.value = "";
outputElement.scrollTop = outputElement.scrollHeight;
const newElements = outputElement.querySelectorAll(
"div:nth-last-child(-n+2)"
);
newElements.forEach((el) => {
el.style.animation = "fadeIn 0.5s ease";
});
updateEnvView();
}
inputElement.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
_evaluate();
}
});
document
.getElementById("evaluate-btn")
.addEventListener("click", _evaluate);
document.querySelectorAll(".example").forEach((example) => {
example.addEventListener("click", () => {
inputElement.value = example.textContent;
_evaluate();
});
});
outputElement.innerHTML =
'<div class="output-line">Try out the examples or enter your own expressions.</div>';
updateEnvView();
</script>
</body>
</html>