Skip to main content

No project description provided

Project description

gotopy

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 gotopy import run_program

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

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

gotopy2-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

gotopy2-0.1.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file gotopy2-0.1.0.tar.gz.

File metadata

  • Download URL: gotopy2-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-37-amd64

File hashes

Hashes for gotopy2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a0b597ff3cd9cc2fe588e881d69b70efcaa602f7f5d6adfb02855722449e6cfe
MD5 b0f8c7bce884530451ccf09576f30296
BLAKE2b-256 c35fc6fe3dd614befb51edd4de95dfa0e6cad1bd0b2abaa3dfa1f9b6b9c4daae

See more details on using hashes here.

File details

Details for the file gotopy2-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: gotopy2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-37-amd64

File hashes

Hashes for gotopy2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3e615a69612b250fd788537ad455459b544d0ec522b48aa92a8dce8b5fce48e8
MD5 46b54e77b120868f59b8e2e25ac17e00
BLAKE2b-256 ef3a31e2d8e5d686f15d54b12eba3ec17ddf78662c792c564735470315013761

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