Commit dfe86a9
Browser demo reads Python, and grows to 95 blocks
Two things the demo could not do: it had a small handful of blocks, and it
could only be built by hand.
Reading Python in a browser means no ast module, so this adds a tokeniser
and a recursive descent parser written from scratch in JavaScript. It
handles indentation, strings including triple quoted and f-strings, joined
lines, and the usual precedence ladder. On top of that sits a mapper to
blocks that understands assignments, if/elif/else, while, for over range and
over lists, def with parameters and returns, break, continue, comparisons,
and/or/not, list and string methods and the built-ins people actually use.
Functions arrive as real custom blocks with a define hat and a call block.
Anything unrecognised is kept word for word, so no file is refused.
Press Import .py, paste a program, or drop a file anywhere on the page.
The block set goes from 26 to 95 across eight categories: lists, custom
blocks and a Python category join the original five, and the existing ones
fill out with the operators, text handling and control blocks the desktop
app has. Everything still runs in the page.
Six programs are tested three ways: run by real Python, imported into blocks
and run by the demo's interpreter, then turned back into Python and run
again. All three agree exactly, which turned up four real faults on the way:
numeric slots trimming the text they were given, print with three or more
arguments falling back to raw code, list literals never reaching the
interpreter, and len() of a plain variable doing the same.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>1 parent 443d7e0 commit dfe86a9
2 files changed
Lines changed: 2026 additions & 137 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| |||
346 | 347 | | |
347 | 348 | | |
348 | 349 | | |
349 | | - | |
| 350 | + | |
350 | 351 | | |
351 | 352 | | |
352 | | - | |
| 353 | + | |
| 354 | + | |
353 | 355 | | |
354 | 356 | | |
355 | 357 | | |
356 | 358 | | |
357 | 359 | | |
| 360 | + | |
358 | 361 | | |
359 | 362 | | |
360 | 363 | | |
361 | 364 | | |
362 | 365 | | |
363 | | - | |
364 | | - | |
365 | | - | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
366 | 386 | | |
367 | 387 | | |
368 | 388 | | |
| |||
0 commit comments