Skip to main content

gotopy2

A minimalist, modern Python interpreter and library for CBMBASIC-style control flow using line numbers, GOTO, GOSUB, and RETURN.

  • Programs are Python dicts mapping line numbers to functions or lambdas.
  • Each line operates on a shared globals dict and a runtime object.
  • Supports GOTO, GOSUB, RETURN, HALT, and calling other gotopy files (run_file).
  • Designed for educational, retro, and DSL experiments.

Features

  • Sequential line-numbered execution (like BASIC)
  • goto(line), gosub(line), return_() for control flow
  • halt() to stop execution
  • run_file(filename) to load and run other gotopy programs
  • Type-safe: ships with ProgramType for static analysis
  • Fully tested and type-checked

Installation

This project uses Poetry for dependency management. To install:

poetry install

Usage Example

Modern, type-annotated style

from gotopy import run_program, ProgramType

program: ProgramType = {
    10: lambda g, r: g.update(x=1),
    20: lambda g, r: print('X is', g['x']),
    30: lambda g, r: r.goto(50) if g['x'] < 3 else r.halt(),
    40: lambda g, r: r.halt(),
    50: lambda g, r: g.update(x=g['x']+1),
}

run_program(program)

Classic lambda style (backward compatible)

# You can also use the classic style with plain lambdas and no type annotations:
program = {
    10: lambda g, r: g.update(x=1),
    20: lambda g, r: g.update(x=g['x'] + 1),
    30: lambda g, r: None,
}
g = {}
run_program(program, g)

Both styles are tested and supported.

Writing Programs

  • Each line is a lambda or function: lambda globals, runtime: ...
  • Use runtime.goto(line), runtime.gosub(line), runtime.return_(), runtime.halt() for control flow.
  • Use runtime.run_file(filename) to call another gotopy file (must define a program dict).

Example: Nested run_file usage

You can run a subprogram from a main program using run_file. The subprogram should halt itself when done.

main.py

from gotopy2 import run_program, ProgramType

def step10(g, r):
    # Import and run a subprogram from the tests directory
    import os
    subprog_path = os.path.join(os.path.dirname(__file__), "tests", "subprog.py")
    r.run_file(subprog_path, g)
def step20(g, r):
    g['done'] = True
    r.halt()
program = {
    10: step10,
    20: step20,
}
g = {}
run_program(program, g)
print(g['msg'])  # Output: hello from subprog

tests/subprog.py

program = {
    100: lambda g, r: g.update(msg='hello from subprog'),
    110: lambda g, r: r.halt(),
}

Example: GOSUB/RETURN

from gotopy import run_program, ProgramType

program: ProgramType = {
    10: lambda g, r: r.gosub(100),
    20: lambda g, r: print('Back from subroutine!') or r.halt(),
    100: lambda g, r: print('In subroutine'),
    110: lambda g, r: r.return_(),
}
run_program(program)

Testing

Run all tests:

poetry run python3 -m unittest discover tests

Type Checking & Linting

Run type checks and linting:

poetry run mypy .
poetry run flake8

Directory Structure

gotopy/
├── gotopy/
│   ├── __init__.py
│   └── gotopy.py
├── tests/
│   ├── __init__.py
│   ├── test_gotopy.py
│   ├── test_nested_run_file_subprog.py
│   └── subprog.py
├── pyproject.toml
├── .flake8
├── mypy.ini
└── README.md

License

MIT

Metadata

Release files for gotopy2 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gotopy2 0.1.1
File Size Uploaded Provenance
gotopy2-0.1.1.tar.gz 3.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for gotopy2 0.1.1
File Interpreter ABI Platform
gotopy2-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 7.1 kB

Release files / gotopy2-0.1.1.tar.gz

Download URL gotopy2-0.1.1.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4a7cf4fa5f7c67f00e2723d2a8a6c1de3527f13c6f7720b3c0c0b001561850ab
BLAKE2b-256 checksum
How to use checksums
5c545843c5022e7a33f0482a8f106e612fa556e8672c70c50703ac95649df4ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-37-amd64

Release files / gotopy2-0.1.1-py3-none-any.whl

Download URL gotopy2-0.1.1-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4f4dbba7ed5f0a8afb52a9c659c1364681d4e78528f66a5940c15ba2089c894
BLAKE2b-256 checksum
How to use checksums
e2271b58e49b432e901042beb540dc9e63d057589f447222502f5a705ace0a20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-37-amd64

Security for gotopy2 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page