Skip to content

Commit 5ce43db

Browse files
committed
Implement WebAssembly backends via ChatGPT
1 parent fb61318 commit 5ce43db

23 files changed

Lines changed: 836 additions & 1185 deletions

assets/apps/ast_parser.py

Lines changed: 47 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,27 @@
1010
import math
1111
import sys
1212
import time
13-
import types
1413
from random import random, choice
1514

16-
document = window = None
17-
create_proxy = lambda fn: fn
15+
import appdev
16+
import events
17+
from displaydev.wasmdisplay import WasmDisplay
18+
import pygraphics
1819

19-
from displaydev.auto import AutoDisplay
20+
21+
def _color(value):
22+
value = value.lstrip("#")
23+
red, green, blue = int(value[:2], 16), int(value[2:4], 16), int(value[4:], 16)
24+
return (red & 0xF8) << 8 | (green & 0xFC) << 3 | blue >> 3
25+
26+
27+
def _text(display, value, x, y, color, align="left"):
28+
value = str(value)
29+
if align == "right":
30+
x -= len(value) * 8
31+
elif align == "center":
32+
x -= len(value) * 4
33+
pygraphics.text8(display, value, int(x), int(y) - 8, _color(color))
2034

2135

2236
C_TOKENS = [
@@ -42,10 +56,8 @@ def __init__(self, canvas_id="hero_canvas", size=240):
4256
self.w = size
4357
self.h = size
4458

45-
bc = types.ModuleType("board_config")
46-
bc.display_drv = AutoDisplay(width=size, height=size, canvas_id=canvas_id)
47-
sys.modules["board_config"] = bc
48-
self.drv = bc.display_drv
59+
self.drv = WasmDisplay(width=size, height=size, canvas_id=canvas_id)
60+
self.app = appdev.App(displays=(self.drv,), host_read=self.drv.get_events)
4961

5062
self.tokens_parsed = 4820
5163
self.schemas_gen = 142
@@ -60,30 +72,21 @@ def __init__(self, canvas_id="hero_canvas", size=240):
6072
self.draw()
6173
self._bind_events()
6274

63-
self._tick_proxy = create_proxy(self._js_tick_cb) if window else None
64-
self._tick_interval = window.setInterval(self._tick_proxy, 33) if window else None
75+
self._tick_subscription = self.app.every(33, self._timer_tick)
6576

66-
def _js_tick_cb(self):
77+
def _timer_tick(self, _timer):
6778
self.tick()
6879

6980
def _bind_events(self):
70-
if not document:
71-
return
72-
canvas = document.getElementById(self.canvas_id)
73-
if not canvas:
74-
return
75-
76-
def on_pointer_down(event):
77-
event.preventDefault()
81+
def on_pointer_down(_event):
7882
self.tokens_parsed += 42
7983
self.schemas_gen += 1
8084
tok, col = choice(C_TOKENS)
8185
self.log_lines.pop(0)
8286
self.log_lines.append((f"EMIT {tok}", col))
8387
self.draw()
8488

85-
self._p_down = create_proxy(on_pointer_down)
86-
canvas.addEventListener("pointerdown", self._p_down)
89+
self.app.on(events.MOUSEBUTTONDOWN, on_pointer_down)
8790

8891
def tick(self):
8992
self.scan_line_y += 1.8
@@ -97,115 +100,54 @@ def tick(self):
97100
self.draw()
98101

99102
def draw(self):
100-
if not hasattr(self.drv, "_buf_ctx") or not self.drv._buf_ctx:
101-
return
102-
ctx = self.drv._buf_ctx
103+
display = self.drv
103104
w, h = self.w, self.h
104105

105106
# 1. Dark Blueprint Bezel Background
106-
ctx.fillStyle = "#070B12"
107-
ctx.fillRect(0, 0, w, h)
107+
display.fill(_color("#070B12"))
108108

109109
# Subtle Matrix Grid
110-
ctx.strokeStyle = "rgba(30, 41, 59, 0.4)"
111-
ctx.lineWidth = 1
112110
for gx in range(16, w, 20):
113-
ctx.beginPath()
114-
ctx.moveTo(gx, 0)
115-
ctx.lineTo(gx, h)
116-
ctx.stroke()
111+
pygraphics.vline(display, gx, 0, h, _color("#111827"))
117112
for gy in range(16, h, 20):
118-
ctx.beginPath()
119-
ctx.moveTo(0, gy)
120-
ctx.lineTo(w, gy)
121-
ctx.stroke()
113+
pygraphics.hline(display, 0, gy, w, _color("#111827"))
122114

123115
# 2. Header Bar
124-
ctx.fillStyle = "rgba(15, 23, 42, 0.92)"
125-
ctx.fillRect(0, 0, w, 26)
126-
ctx.strokeStyle = "#1E293B"
127-
ctx.lineWidth = 1
128-
ctx.beginPath()
129-
ctx.moveTo(0, 26)
130-
ctx.lineTo(w, 26)
131-
ctx.stroke()
132-
133-
ctx.fillStyle = "#38BDF8"
134-
ctx.font = "bold 9px system-ui, monospace"
135-
ctx.textAlign = "left"
136-
ctx.fillText("⚡ LVGL BINDINGS GEN", 10, 17)
137-
138-
ctx.fillStyle = "#10B981"
139-
ctx.textAlign = "right"
140-
ctx.fillText(f"AST: {self.schemas_gen}", w - 10, 17)
116+
display.fill_rect(0, 0, w, 26, _color("#0F172A"))
117+
pygraphics.hline(display, 0, 26, w, _color("#1E293B"))
118+
_text(display, "LVGL BINDINGS GEN", 10, 17, "#38BDF8")
119+
_text(display, f"AST: {self.schemas_gen}", w - 10, 17, "#10B981", "right")
141120

142121
# 3. Token Matrix Window
143122
matrix_y = 32
144123
for i, (text, color) in enumerate(self.log_lines):
145124
row_y = matrix_y + i * 22
146125
# Terminal prompt pill
147-
ctx.fillStyle = "rgba(15, 23, 42, 0.75)"
148-
ctx.beginPath()
149-
ctx.roundRect(10, row_y, w - 20, 18, 4)
150-
ctx.fill()
151-
ctx.strokeStyle = "rgba(56, 189, 248, 0.2)"
152-
ctx.stroke()
153-
154-
ctx.fillStyle = "#64748B"
155-
ctx.font = "8px monospace"
156-
ctx.textAlign = "left"
157-
ctx.fillText("›", 16, row_y + 12)
158-
159-
ctx.fillStyle = color
160-
ctx.font = "bold 9px monospace"
161-
ctx.fillText(text, 28, row_y + 12)
126+
pygraphics.round_rect(display, 10, row_y, w - 20, 18, 4, _color("#0F172A"), True)
127+
pygraphics.round_rect(display, 10, row_y, w - 20, 18, 4, _color("#164E63"))
128+
_text(display, ">", 16, row_y + 12, "#64748B")
129+
_text(display, text, 28, row_y + 12, color)
162130

163131
# 4. Scanning Parse Beam
164-
ctx.strokeStyle = "rgba(56, 189, 248, 0.85)"
165-
ctx.lineWidth = 1.5
166-
ctx.beginPath()
167-
ctx.moveTo(12, self.scan_line_y)
168-
ctx.lineTo(w - 12, self.scan_line_y)
169-
ctx.stroke()
132+
pygraphics.hline(display, 12, int(self.scan_line_y), w - 24, _color("#38BDF8"))
170133

171134
# 5. Bottom Generation Telemetry Box
172135
bot_y = 135
173-
ctx.fillStyle = "#0F172A"
174-
ctx.beginPath()
175-
ctx.roundRect(10, bot_y, w - 20, 94, 8)
176-
ctx.fill()
177-
ctx.strokeStyle = "#1E293B"
178-
ctx.lineWidth = 1
179-
ctx.stroke()
136+
pygraphics.round_rect(display, 10, bot_y, w - 20, 94, 8, _color("#0F172A"), True)
137+
pygraphics.round_rect(display, 10, bot_y, w - 20, 94, 8, _color("#1E293B"))
180138

181139
# Telemetry Labels
182-
ctx.fillStyle = "#94A3B8"
183-
ctx.font = "8px system-ui, sans-serif"
184-
ctx.textAlign = "left"
185-
ctx.fillText("TOKENS PARSED", 18, bot_y + 16)
186-
ctx.fillText("TARGETS", 18, bot_y + 40)
187-
ctx.fillText("HEADER SOT", 18, bot_y + 64)
188-
189-
ctx.fillStyle = "#F8FAFC"
190-
ctx.font = "bold 11px system-ui, monospace"
191-
ctx.textAlign = "right"
192-
ctx.fillText(f"{self.tokens_parsed:,}", w - 18, bot_y + 16)
193-
ctx.fillStyle = "#818CF8"
194-
ctx.font = "bold 9px system-ui, monospace"
195-
ctx.fillText("CPython · MP · CP", w - 18, bot_y + 40)
196-
ctx.fillStyle = "#34D399"
197-
ctx.fillText("LVGL v9.2.2 C", w - 18, bot_y + 64)
140+
_text(display, "TOKENS PARSED", 18, bot_y + 16, "#94A3B8")
141+
_text(display, "TARGETS", 18, bot_y + 40, "#94A3B8")
142+
_text(display, "HEADER SOT", 18, bot_y + 64, "#94A3B8")
143+
_text(display, f"{self.tokens_parsed:,}", w - 18, bot_y + 16, "#F8FAFC", "right")
144+
_text(display, "CPython / MP / CP", w - 18, bot_y + 40, "#818CF8", "right")
145+
_text(display, "LVGL v9.2.2 C", w - 18, bot_y + 64, "#34D399", "right")
198146

199147
# Progress bar
200-
ctx.fillStyle = "#1E293B"
201-
ctx.beginPath()
202-
ctx.roundRect(18, bot_y + 76, w - 36, 6, 3)
203-
ctx.fill()
204-
ctx.fillStyle = "#38BDF8"
148+
pygraphics.round_rect(display, 18, bot_y + 76, w - 36, 6, 3, _color("#1E293B"), True)
205149
prog_w = int((w - 36) * ((self.tokens_parsed % 500) / 500.0))
206-
ctx.beginPath()
207-
ctx.roundRect(18, bot_y + 76, max(6, prog_w), 6, 3)
208-
ctx.fill()
150+
pygraphics.round_rect(display, 18, bot_y + 76, max(6, prog_w), 6, 3, _color("#38BDF8"), True)
209151

210152
if hasattr(self.drv, "show"):
211153
self.drv.show()

0 commit comments

Comments
 (0)