forked from feather-lang/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
649 lines (562 loc) · 15.7 KB
/
index.html
File metadata and controls
649 lines (562 loc) · 15.7 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>feather TCL Interpreter</title>
<style>
:root {
--bg: #0a1f10;
--surface: #0f2e18;
--primary: #0b5628;
--accent: #7b8c11;
--text: #eee;
--text-dim: #8a9a7a;
--success: #4a721e;
--error: #c44536;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 20px;
background: var(--bg);
color: var(--text);
font-family:
system-ui,
-apple-system,
sans-serif;
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
}
h1 {
margin: 0 0 10px 0;
color: var(--accent);
font-size: 2rem;
}
.subtitle {
color: var(--text-dim);
margin-bottom: 20px;
}
.editor-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin-bottom: 20px;
}
@media (max-width: 768px) {
.editor-container {
grid-template-columns: 1fr;
}
}
.panel {
background: var(--surface);
border-radius: 8px;
overflow: hidden;
}
.panel-header {
background: var(--primary);
padding: 10px 15px;
font-weight: 600;
display: flex;
justify-content: space-between;
align-items: center;
}
.panel-body {
padding: 0;
}
textarea {
width: 100%;
height: 300px;
background: var(--surface);
color: var(--text);
border: none;
padding: 15px;
font-family: "JetBrains Mono", "Fira Code", monospace;
font-size: 14px;
resize: vertical;
outline: none;
}
textarea::placeholder {
color: var(--text-dim);
}
.output {
padding: 15px;
font-family: "JetBrains Mono", "Fira Code", monospace;
font-size: 14px;
min-height: 300px;
max-height: 400px;
overflow-y: auto;
white-space: pre-wrap;
word-break: break-all;
}
.output-line {
margin: 2px 0;
}
.output-line.error {
color: var(--error);
}
.output-line.result {
color: var(--success);
}
.output-line.info {
color: var(--text-dim);
}
.canvas-panel {
margin-top: 20px;
}
.canvas-panel canvas {
width: 100%;
height: 300px;
background: #111;
border-radius: 0 0 8px 8px;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
button {
background: var(--accent);
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
font-size: 14px;
font-weight: 600;
cursor: pointer;
transition: opacity 0.2s;
}
button:hover {
opacity: 0.9;
}
button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
button.secondary {
background: var(--primary);
}
.examples {
margin-top: 20px;
}
.examples h3 {
margin: 0 0 10px 0;
color: var(--text-dim);
font-size: 14px;
text-transform: uppercase;
letter-spacing: 1px;
}
.example-buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.example-btn {
background: var(--primary);
padding: 6px 12px;
font-size: 12px;
}
.status {
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--text-dim);
}
.status-dot.ready {
background: var(--success);
}
.status-dot.loading {
background: var(--accent);
animation: pulse 1s infinite;
}
@keyframes pulse {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
</style>
</head>
<body>
<div class="container">
<h1>
<img
src="/logo.png"
alt="feather"
style="height: 1.5em; vertical-align: middle; margin-right: 8px"
/>feather
</h1>
<p class="subtitle">
A lightweight TCL interpreter compiled to WebAssembly
</p>
<div class="controls">
<button id="runBtn" disabled>▶ Run</button>
<button id="clearBtn" class="secondary">Clear Output</button>
<div class="status">
<div class="status-dot" id="statusDot"></div>
<span id="statusText">Loading...</span>
</div>
</div>
<div class="editor-container">
<div class="panel">
<div class="panel-header">Script</div>
<div class="panel-body">
<textarea id="editor" placeholder="Enter TCL code here...">
# Welcome to feather!
# Try some TCL commands:
set greeting "Hello, World!"
puts $greeting
proc factorial {n} {
if {$n <= 1} {
return 1
}
expr {$n * [factorial [expr {$n - 1}]]}
}
puts "5! = [factorial 5]"
foreach fruit {apple banana cherry} {
puts "I like $fruit"
}
</textarea
>
</div>
</div>
<div class="panel">
<div class="panel-header">Output</div>
<div class="panel-body">
<div class="output" id="output"></div>
</div>
</div>
</div>
<div class="examples">
<h3>Examples</h3>
<div class="example-buttons">
<button class="example-btn" data-example="hello">Hello World</button>
<button class="example-btn" data-example="math">Math</button>
<button class="example-btn" data-example="lists">Lists</button>
<button class="example-btn" data-example="procs">Procedures</button>
<button class="example-btn" data-example="loops">Loops</button>
<button class="example-btn" data-example="dict">Dictionaries</button>
<button class="example-btn" data-example="canvas">Canvas</button>
</div>
</div>
<div class="panel canvas-panel">
<div class="panel-header">Canvas</div>
<div class="panel-body">
<canvas id="canvas" width="800" height="300"></canvas>
</div>
</div>
</div>
<script type="module">
import { createFeather } from "./feather.js";
const examples = {
hello: `puts "Hello, World!"
set name "feather"
puts "Welcome to $name!"`,
math: `# Basic arithmetic
puts [expr {2 + 3 * 4}]
puts [expr {(2 + 3) * 4}]
# Variables in expressions
set x 10
set y 5
puts "$x + $y = [expr {$x + $y}]"
puts "$x * $y = [expr {$x * $y}]"
# Comparisons
puts "10 > 5? [expr {10 > 5}]"
puts "10 == 10? [expr {10 == 10}]"`,
lists: `# Create a list
set colors {red green blue}
puts "Colors: $colors"
puts "Length: [llength $colors]"
# Access elements
puts "First: [lindex $colors 0]"
puts "Last: [lindex $colors end]"
# Modify lists
lappend colors yellow
puts "After append: $colors"
# List operations
set nums {3 1 4 1 5 9 2 6}
puts "Sorted: [lsort $nums]"
puts "Range: [lrange $nums 2 5]"`,
procs: `# Define a procedure
proc greet {name} {
return "Hello, $name!"
}
puts [greet "World"]
# Procedure with default argument
proc add {a {b 10}} {
expr {$a + $b}
}
puts "add 5: [add 5]"
puts "add 5 3: [add 5 3]"
# Recursive procedure
proc fib {n} {
if {$n <= 1} {
return $n
}
expr {[fib [expr {$n-1}]] + [fib [expr {$n-2}]]}
}
puts "fib(10) = [fib 10]"`,
loops: `# foreach loop
foreach item {a b c d e} {
puts "Item: $item"
}
# for loop
for {set i 0} {$i < 5} {incr i} {
puts "i = $i"
}
# while loop
set count 3
while {$count > 0} {
puts "Countdown: $count"
incr count -1
}
puts "Blast off!"`,
dict: `# Create a dictionary
set person [dict create name "Alice" age 30 city "NYC"]
puts "Person: $person"
# Access values
puts "Name: [dict get $person name]"
puts "Age: [dict get $person age]"
# Modify dictionary
dict set person job "Engineer"
puts "Updated: $person"
# Check existence
puts "Has name? [dict exists $person name]"
puts "Has salary? [dict exists $person salary]"
# Get all keys
puts "Keys: [dict keys $person]"`,
canvas: `# Canvas drawing demo
canvas clear
# Draw some rectangles
canvas fill #e94560
canvas rect 50 50 100 80
canvas fill #4ade80
canvas rect 200 100 120 60
# Draw circles
canvas fill #60a5fa
canvas circle 450 100 50
canvas fill #fbbf24
canvas circle 550 180 30
# Draw lines
canvas stroke #fff
canvas width 2
canvas line 50 200 200 250
canvas line 200 250 350 200
# Draw a polygon (triangle)
canvas fill #a78bfa
canvas polygon 600 50 700 150 500 150
# Add some text
canvas fill #fff
canvas font "16px sans-serif"
canvas text 50 280 "Hello from feather!"
puts "Canvas demo complete!"`,
};
const editor = document.getElementById("editor");
const output = document.getElementById("output");
const runBtn = document.getElementById("runBtn");
const clearBtn = document.getElementById("clearBtn");
const statusDot = document.getElementById("statusDot");
const statusText = document.getElementById("statusText");
let feather, interp;
function log(text, type = "") {
const line = document.createElement("div");
line.className = `output-line ${type}`;
line.textContent = text;
output.appendChild(line);
output.scrollTop = output.scrollHeight;
}
async function init() {
statusDot.className = "status-dot loading";
statusText.textContent = "Loading WASM...";
try {
feather = await createFeather("./feather.wasm");
interp = feather.create();
feather.register(interp, "puts", (args) => {
log(args.join(" "));
return "";
});
feather.register(interp, "clock", (args) => {
if (args[0] === "seconds") return Math.floor(Date.now() / 1000);
if (args[0] === "milliseconds") return Date.now();
throw new Error(`unknown clock subcommand "${args[0]}"`);
});
// Canvas command with drawing subcommands
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let fillColor = "#fff";
let strokeColor = "#fff";
let lineWidth = 1;
feather.register(interp, "canvas", (args) => {
const sub = args[0];
switch (sub) {
case "clear":
ctx.clearRect(0, 0, canvas.width, canvas.height);
break;
case "fill":
fillColor = args[1];
ctx.fillStyle = fillColor;
break;
case "stroke":
strokeColor = args[1];
ctx.strokeStyle = strokeColor;
break;
case "width":
lineWidth = parseFloat(args[1]);
ctx.lineWidth = lineWidth;
break;
case "rect":
ctx.fillRect(
parseFloat(args[1]),
parseFloat(args[2]),
parseFloat(args[3]),
parseFloat(args[4]),
);
break;
case "strokerect":
ctx.strokeRect(
parseFloat(args[1]),
parseFloat(args[2]),
parseFloat(args[3]),
parseFloat(args[4]),
);
break;
case "circle":
ctx.beginPath();
ctx.arc(
parseFloat(args[1]),
parseFloat(args[2]),
parseFloat(args[3]),
0,
Math.PI * 2,
);
ctx.fill();
break;
case "strokecircle":
ctx.beginPath();
ctx.arc(
parseFloat(args[1]),
parseFloat(args[2]),
parseFloat(args[3]),
0,
Math.PI * 2,
);
ctx.stroke();
break;
case "line":
ctx.beginPath();
ctx.moveTo(parseFloat(args[1]), parseFloat(args[2]));
ctx.lineTo(parseFloat(args[3]), parseFloat(args[4]));
ctx.stroke();
break;
case "polygon": {
const points = args.slice(1).map(parseFloat);
if (points.length >= 4) {
ctx.beginPath();
ctx.moveTo(points[0], points[1]);
for (let i = 2; i < points.length; i += 2) {
ctx.lineTo(points[i], points[i + 1]);
}
ctx.closePath();
ctx.fill();
}
break;
}
case "strokepolygon": {
const points = args.slice(1).map(parseFloat);
if (points.length >= 4) {
ctx.beginPath();
ctx.moveTo(points[0], points[1]);
for (let i = 2; i < points.length; i += 2) {
ctx.lineTo(points[i], points[i + 1]);
}
ctx.closePath();
ctx.stroke();
}
break;
}
case "font":
ctx.font = args[1];
break;
case "text":
ctx.fillText(
args.slice(3).join(" "),
parseFloat(args[1]),
parseFloat(args[2]),
);
break;
case "arc":
ctx.beginPath();
ctx.arc(
parseFloat(args[1]),
parseFloat(args[2]),
parseFloat(args[3]),
parseFloat(args[4]),
parseFloat(args[5]),
);
ctx.stroke();
break;
default:
throw new Error(`unknown canvas subcommand "${sub}"`);
}
return "";
});
statusDot.className = "status-dot ready";
statusText.textContent = "Ready";
runBtn.disabled = false;
log("feather interpreter ready", "info");
} catch (e) {
statusDot.className = "status-dot";
statusText.textContent = "Failed to load";
log(`Failed to load WASM: ${e.message}`, "error");
console.error(e);
}
}
runBtn.addEventListener("click", () => {
const script = editor.value;
if (!script.trim()) return;
log("", "info");
log("--- Running script ---", "info");
try {
const result = feather.eval(interp, script);
if (result) {
log(`=> ${result}`, "result");
}
} catch (e) {
log(`Error: ${e.message}`, "error");
}
});
clearBtn.addEventListener("click", () => {
output.innerHTML = "";
});
editor.addEventListener("keydown", (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
e.preventDefault();
runBtn.click();
}
});
document.querySelectorAll(".example-btn").forEach((btn) => {
btn.addEventListener("click", () => {
const example = examples[btn.dataset.example];
if (example) {
editor.value = example;
}
});
});
init();
</script>
</body>
</html>