Skip to content

examples: add tiny_basic compiler - #60

Merged
garritfra merged 16 commits into
mainfrom
cursor/182cdfd0
Apr 27, 2026
Merged

garritfra merged 16 commits into
mainfrom
cursor/182cdfd0

Conversation

@garritfra

@garritfra garritfra commented Apr 27, 2026 •

Copy link
Copy Markdown
Owner

Fixes #9

Description

Adds a small but complete compiler example under examples/tiny_basic/ that
demonstrates the qbe crate end-to-end: a BASIC subset → AST → qbe::Module →
QBE IL on stdout. Pipe through qbe and a C compiler to get a runnable
binary.

The compiler itself is a single ~600 LOC main.rs with no external
dependencies, organized as three sequential passes (lexer → parser →
codegen). It exercises the parts of the API hello_world.rs doesn't reach:
stack-slot variables (Alloc4 / Store / Load), branching (Jnz,
Jmp), Cmp with all six comparison codes, calling printf via a private
DataDef, and per-line block labels.

Changes proposed in this pull request

  • New example examples/tiny_basic/:
    • main.rs — lexer, recursive-descent parser with precedence climbing,
      codegen with line-number target validation.
    • factorial.bas, fibonacci.bas, hello.bas — runnable sample programs.
    • README.md — language reference and copy-pasteable run recipe.
  • Top-level README.md reorganized into a list of examples linking to the
    per-example README.
  • CHANGELOG.md: new Unreleased section with the addition.

Tiny BASIC at a glance

10 LET N = 5
20 LET F = 1
30 IF N <= 1 THEN 60
40 LET F = F * N
50 LET N = N - 1
55 GOTO 30
60 PRINT F
70 END
cargo run --example tiny_basic examples/tiny_basic/factorial.bas \
  | qbe -o /tmp/out.s - && cc /tmp/out.s -o /tmp/p && /tmp/p
# -> 120

ToDo

  • I've read the CONTRIBUTING.md guidelines before submitting this PR
  • Proposed feature/fix is sufficiently tested (factorial, fibonacci, hello samples all run end-to-end through qbe + cc and produce expected output; cargo test (48 tests) and cargo clippy --all-targets -- -D warnings are clean)
  • Proposed feature/fix is sufficiently documented (per-example README plus inline doc comment with quick-start)
  • The "Unreleased" section in the changelog has been updated, if applicable

Addresses issue #9 by specifying a single-file BASIC-subset compiler in
examples/tiny_basic.rs (lexer + parser + codegen -> qbe::Module). Spec
nails down language scope, codegen strategy (stack slots, line-numbered
blocks), error handling, and explicit out-of-scope items.

Made-with: Cursor
Bite-sized TDD-flavored task list (10 tasks) that builds the example
incrementally: scaffolding -> lexer -> parser -> expressions -> codegen
skeleton -> LET/PRINT -> IF/GOTO -> END -> end-to-end factorial -> docs.
Each task ends in a verification step (round-trip through qbe + cc) and a
commit.

Made-with: Cursor
The lexer already skips REM-line content (so arbitrary characters in
comments don't produce lex errors), making the parser's REM bump-loop
dead code. Inline the parser arm.

Update the README pipe to include the cc step (matching the file-level
doc comment) and update the design spec to record that REM-line skipping
happens at lex time.

Made-with: Cursor
Cargo auto-detects examples/<name>/main.rs, so the compiler itself stays
a single ~600 LOC file but now lives next to runnable .bas sample
programs (hello, factorial, fibonacci) and a README with a
copy-pasteable run recipe. Top-level README links to that README so
users land on a working how-to instead of a raw shell pipe.

Made-with: Cursor
These were workflow scaffolding for the original implementation pass and
don't belong on main.

Made-with: Cursor
@garritfra
garritfra marked this pull request as ready for review April 27, 2026 12:39
@garritfra
garritfra merged commit f84497c into main Apr 27, 2026
8 checks passed
garritfra added a commit that referenced this pull request Apr 27, 2026
@garritfra
garritfra deleted the cursor/182cdfd0 branch April 27, 2026 12:39
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.

Simple example compiler

1 participant