Skip to content

Compiler: Smalltalk source → Tinytalk bytecode #5

Description

@softwarewrighter

Today every demo in `examples/dN_*.bas` is hand-assembled bytecode:
the driver POKEs bytecodes into scratch RAM, then `GOSUB 12000`
dispatches them through the VM. This costs ~30-60 lines of fragile
BASIC per demo and means new demos spend more time on loader
plumbing than on the logic they're trying to show.

A Smalltalk-source compiler — parser + codegen producing the same 14
Tinytalk bytecodes — would let future demos be `.st` files. Concretely:
`examples/fibonacci.st` instead of `examples/d8_fibonacci.bas`.

Scope

Lexer / parser

  • Tokenize: integer literal, identifier, keyword (trailing `:`),
    binary selector (`+ - * / < > = , ;` etc.), block delimiters
    `[ ]`, statement terminator `.`.
  • Parse statements: `expr.` and `^ expr.` (return).
  • Parse expressions: receiver + cascade of unary / binary /
    keyword messages with standard Smalltalk precedence
    (unary > binary > keyword).
  • Block literals `[ :a :b | body ]` (only after v2 blocks).
  • Class declaration syntax: at minimum a file format declaring
    class X with instance variables A B C and methods M N P.

Codegen

  • PUSH_INT for integer literals.
  • PUSH_TEMP / STORE_TEMP / PUSH_FIELD / STORE_FIELD with a
    per-method symbol table.
  • SEND for unary / binary / keyword sends with correct arg count.
  • RETURN_TOP / POP / JUMP / JUMP_IF_FALSE for control flow.
  • Literal pool indices (depends on v1 strings, v1: strings + literal pool + Transcript primitive #4).

Image build

  • Replace per-demo `image_dN.bas` POKE / DATA tables with the
    compiler's output: class table + method dictionaries + bytecode
    pool in a single image format the VM loads.
  • CLI: `stc examples/foo.st > image/foo.bin` or similar.

Where to host

  1. In Smalltalk — dogfooded; v1+v2 unlock enough to self-host.
    Bootstrap by hand-assembling the compiler once, then it
    self-hosts.
  2. In Pascal or Rust — pragmatic for the v0..v2 era; ship
    `tools/stc.pas` (or in `pa24r` style, a Rust crate) that
    produces an image at CLI time.

(2) ships sooner and unblocks demos; (1) is the philosophically
correct end state.

Demos this unlocks

  • All ten v0-feasible demos in Demo backlog: more v0 demos (no dialect upgrade required) #1 become `.st` files.
  • Higher-tier demos (`eliza-lite`, `mud-room`, `caesar-cipher`,
    `roman-numerals`, `test-runner`) can be written in real
    Smalltalk syntax once v1 strings + this compiler exist.
  • D5 calc could grow into a real source-driven REPL: user types
    Smalltalk, compiler emits bytecode, VM runs it.

Web demo

`web-sw-cor24-smalltalk`
would either:

  • Continue bundling per-demo BASIC images, but generate them at
    build time from `.st` sources via the compiler, or
  • Ship the compiler in the WASM bundle so the browser compiles
    user-typed Smalltalk to bytecode — a real REPL.

The latter is the killer-app version of the live demo.

Related: dialect roadmap (#2), v1 strings (#4).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions