Skip to content

Update examples and fix ssa bug in serveEmbedded#55

Merged
cs01 merged 3 commits intomainfrom
update-examples
Feb 24, 2026
Merged

Update examples and fix ssa bug in serveEmbedded#55
cs01 merged 3 commits intomainfrom
update-examples

Conversation

@cs01
Copy link
Copy Markdown
Owner

@cs01 cs01 commented Feb 24, 2026

Update examples and fix serveEmbedded codegen bug

Summary

  • Improve all examples with better console output and full URLs
  • Use ArgumentParser from src/argparse.ts for all examples that parse arguments
  • Add ripgrep-style colorized output to string-search
  • Overhaul the websocket example into a real chat app with embedded HTML/CSS
  • Fix SSA temp ordering bug in ChadScript.serveEmbedded() codegen

Changes

Bugfix: SSA temp ordering in serveEmbedded (src/codegen/stdlib/embed.ts)

generateServeEmbedded() allocated two SSA temps in the wrong order relative to their emission. The GEP result (onePtr) was emitted before the select (stripped), but stripped had the lower register number. LLVM requires numbered temps to be defined in ascending order, so this caused an instruction expected to be numbered '%N' or greater error whenever ChadScript.serveEmbedded() was used.

Fix: swap the nextTemp() allocation order to match emission order.

Examples: helpful console output

All examples now print a banner on startup explaining what they do. Server examples print full URLs (http://localhost:3000) instead of just port numbers.

Examples: ArgumentParser adoption

Examples that parse arguments now use ArgumentParser from src/argparse.ts instead of hand-rolled process.argv loops:

  • word-count.ts — supports -l, -w, -c flags to show only lines/words/chars
  • cli-parser-demo.ts — replaced 83 lines of manual parsing with argparse (auto --help)
  • http-server.ts--port/-p option
  • hackernews/app.ts--port/-p option
  • websocket/app.ts--port/-p option
  • string-search.ts — already used argparse, added --no-color/-C flag

Examples: colorized string-search

string-search.ts now outputs ripgrep-style colored results using ANSI escape codes (\x1b):

  • Magenta filenames
  • Green line numbers
  • Bold red matched text (all occurrences per line highlighted)
  • Cyan separator colons

Includes --no-color/-C flag to disable.

Examples: websocket overhaul

Replaced websocket-server.ts (inline HTML with prompt() dialogs) with websocket/app.ts:

  • Dark-themed chat UI with proper message input, auto-scrolling, and connection status
  • HTML/CSS served from examples/websocket/public/ via ChadScript.embedDir() + ChadScript.serveEmbedded()
  • Tracks connected users, broadcasts join/leave messages
  • Demonstrates the embed API as a static file server

README

Updated to use chad run as the primary way to run examples instead of chad build + running the binary.

Test plan

  • All 226 tests pass
  • All examples build: hello, parallel, query, timers, word-count, cli-parser-demo, string-search, http-server, websocket/app, hackernews/app
  • Verified runtime output for hello, timers, query, word-count, cli-parser-demo, string-search, http-server
  • --help works on all argparse examples
  • Colorized output renders correctly (ANSI codes verified)

@cs01 cs01 merged commit a22314f into main Feb 24, 2026
12 checks passed
@cs01 cs01 deleted the update-examples branch February 24, 2026 04:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant