Skip to main content

BBC BASIC tokeniser and detokeniser for Acorn 8-bit and 32-bit BASIC source files

Project description

oaknut-basic

oaknut-basic

PyPI version CI Python versions License: MIT Documentation

Read the documentation — getting started, the command reference, and the API.

Convert BBC BASIC programs between their compact on-disc tokenised form and a plain-text listing — the two directions a real BBC Micro performs when you LOAD a program and LIST it — plus line numbering for source typed without numbers.

The problem

A tokenised BBC BASIC program is bytecode, not text: keywords like PRINT and GOTO are single bytes, line numbers are packed into each line's header, and a reference such as GOTO 100 is scrambled into a three-byte form that can never be mistaken for a line terminator. A text codec cannot read it; decoding one as text produces garbage.

oaknut-basic reproduces the BBC BASIC II ROM's tokeniser and de-tokeniser exactly — every token value, flag, and the line-number encoding — so a program round-trips between bytes and text byte-for-byte.

Installation

Install with the [cli] extra for the oaknut-basic command, or bare for the library only:

uv tool install "oaknut-basic[cli]"     # the command-line tool
uv add oaknut-basic                       # the importable library

pip works identically with the same names. oaknut-basic requires Python 3.11 or newer.

Command-line usage

$ oaknut-basic --help
Usage: oaknut-basic [OPTIONS] COMMAND [ARGS]...

  Tools for BBC BASIC source and tokenised programs.

Options:
  --version  Show the version and exit.
  --help     Show this message and exit.

Commands:
  detokenise  De-tokenise a stored BBC BASIC program into source text.
  number      Prepend ascending line numbers to an unnumbered BBC BASIC...
  tokenise    Tokenise BBC BASIC source text into a stored program.

Every command reads from a file or standard input and writes to a file or standard output, so each works file-to-file and as a pipe stage. That makes it compose with oaknut-disc to edit a program in place on a disc image:

disc get game.ssd MENU - | oaknut-basic detokenise > menu.bas
oaknut-basic tokenise menu.bas | disc put game.ssd MENU -

Tokenising and de-tokenising are exact inverses, so a program survives a there-and-back trip unchanged. tokenise can also number unnumbered source on the way in (--start / --step), exactly as typing it under AUTO would.

Library usage

The library is function-shaped — tokenise, detokenise, and number_lines, all importable from oaknut.basic:

from oaknut.basic import tokenise, detokenise

program = tokenise('10 PRINT "HELLO"\n20 GOTO 10\n')   # str -> bytes
listing = detokenise(program)                          # bytes -> str
assert tokenise(detokenise(program)) == program        # byte-exact

When the program lives in a disc image, prefer the path-object wrappers DFSPath.read_basic / write_basic (and the ADFS equivalents), which compose the codec with the disc's character encoding and the correct load address.

References

  • BBC BASIC — Wikipedia overview of the language and its versions.
  • BBC BASIC program format — BeebWiki reference for the on-disc tokenised format and the token table.

Part of oaknut

oaknut-basic is one package in the oaknut monorepo of tools for Acorn computer filesystems, files, and formats. It backs the read_basic / write_basic methods of the oaknut-dfs and oaknut-adfs packages, and is usable on its own for .bas / .bbc files outside a disc image.

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oaknut_basic-12.7.1.tar.gz (33.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oaknut_basic-12.7.1-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file oaknut_basic-12.7.1.tar.gz.

File metadata

  • Download URL: oaknut_basic-12.7.1.tar.gz
  • Upload date:
  • Size: 33.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for oaknut_basic-12.7.1.tar.gz
Algorithm Hash digest
SHA256 845bb683b2d3c00ac8cd4993283ff00590807c9f2d4995a297de23377381de34
MD5 1237263d0ddd439bfbec8c996c6905d6
BLAKE2b-256 c37d7a925e2881ddbeaa936da89a0dc9be59872b6070e85da288689e0a26e134

See more details on using hashes here.

File details

Details for the file oaknut_basic-12.7.1-py3-none-any.whl.

File metadata

  • Download URL: oaknut_basic-12.7.1-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for oaknut_basic-12.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f10635249d0e6c9974665e5e5197e0f7a36ff61712658177f2c9130e36dc6e3a
MD5 47658f19b64ae08d9878576786973b64
BLAKE2b-256 0100ffa15ff33426894eafc30707f12847e2cce977e48d1ddf5c93a495162841

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page