Skip to main content

pytunium

A simulator for programs written in the assembler language of pytunia, the JVM assembler of the lecture Formal Languages and Their Applications (Chapter 12).

The window has three columns:

  1. Program: the assembler program. The next instruction is highlighted in yellow, the instruction executed before in blue.
  2. Stack: the stack of the JVM. The cell that SP points to (the top of the stack) is orange, the cell that LV points to (the first argument of the current method) is blue. Each frame is marked with the name of its method.
  3. Working area: what the current instruction does. For iadd, the two operands move from the stack into the working area, the sum appears, and the sum moves back onto the stack.

Below the columns, the output area shows what the program prints, and the warnings and errors of the simulator.

Installation

pytunium is available on PyPI:

pip install pytunium

This installs pytunia, too: it checks the syntax of the program before the simulation starts, so the error messages are the same as when assembling.

If several versions of Python are installed, install pytunium for the Python that you want to start it with:

python3 -m pip install pytunium   # macOS and Linux
py -m pip install pytunium        # Windows

To update pytunium to the newest version:

pip install --upgrade pytunium

pytunium needs Python 3.12 or newer with Tkinter. The installers from python.org and Anaconda contain Tkinter; with Homebrew, install it with brew install python-tk, on Debian/Ubuntu with sudo apt install python3-tk. To check whether Tkinter works, run python3 -m tkinter: it opens a small window.

The development version from GitHub, which may contain changes that are not on PyPI yet:

pip install git+https://github.com/karlstroetmann/pytunium

Starting the simulator

pytunium Sum.jas                  # or: python3 -m pytunium Sum.jas

Without a file, the example Sum.jas from Chapter 12 is shown. The examples Sum.jas, Hello.jas and Primes.jas (generated by the compiler of Chapter 13) are in the folder pytunium/examples of the repository, and of the installed package.

Options: --font-size N (default 14) and --speed N (1 = slow, 10 = no animation).

Controls

Button / key Effect
Play / Return runs the program, animated with the chosen speed
Step / Space, → executes one instruction
Stop / Return, Esc stops Play
Reset / ⌘R (Ctrl+R) reloads the file from disk and starts again
Open… / ⌘O (Ctrl+O) loads another program
Speed 1 (slow) … 9 (fast), 10 = no animation
click on a line number sets or removes a breakpoint (Play stops before it)
⌘+ / ⌘− / ⌘0 larger / smaller / normal font (useful for a projector)
mouse wheel on the stack scrolls the stack

Reset reads the file again, so you can edit the program in an editor and press Reset to simulate the new version.

The memory model

As in Chapter 12, there is a single stack. A method call does not copy its arguments: the arguments on top of the caller's operand stack become the first local variables of the new frame, and LV points to the first of them. A frame with L local variables (.limit locals L) looks like this:

LV+L+2 …          operand stack of the method
LV+L+1            saved LV: the value of LV in the calling method
LV+L              return address: where the caller continues
LV+n … LV+L-1     the other local variables (shown as ? until assigned)
LV+0 … LV+n-1     the n arguments

The two grey cells are the bookkeeping cells. ireturn removes the frame, puts the result where the arguments were, and restores LV from the saved value. Cells above SP keep their old values (shown faintly), since pop only decrements SP.

What is simulated

  • int arithmetic with the semantics of Java: overflow wraps around, idiv rounds towards 0, the remainder of irem has the sign of the dividend, the shifts use the lowest 5 bits of the shift distance: iadd isub imul idiv irem ineg iand ior ixor ishl ishr iushr iinc
  • constants and the stack: iconst_<n> bipush sipush ldc aconst_null dup pop swap nop
  • local variables: iload istore aload astore (also iload_<n> etc.)
  • jumps: goto if<cond> if_icmp<cond> if_acmpeq if_acmpne ifnull ifnonnull
  • static methods of the same class: invokestatic ireturn areturn return
  • printing: getstatic java.lang.System.out java.io.PrintStream (or err) and invokevirtual java.io.PrintStream.print(…) / println(…) with an int, char, boolean, java.lang.String or java.lang.Object argument.

Other instructions (objects, arrays, long, float, double, …) are reported when the program is loaded and stop the simulation when they are reached. Constructors (<init>) are not executed: the simulation starts with main.

Checks

The simulator stops with an explanation when something happens that the JVM would reject or that ends the program:

  • the operand stack grows beyond .limit stack (a warning the first time this happens in a method, and Play stops; the cells beyond the limit are always marked red),
  • a local variable beyond .limit locals is used, or one is read before a value has been stored in it,
  • an instruction finds too few values or values of the wrong type on the stack,
  • a method runs past its last instruction,
  • a division by zero (java.lang.ArithmeticException).

When the program ends, the output area lists for every method the largest number of values its operand stack has held, compared with .limit stack.

Development

pip install -e .                  # in a clone of the repository
python3 -m unittest discover -s tests

A release on GitHub with the tag v + version (e.g. v0.1, the version is __version__ in pytunium/__init__.py) publishes the package on PyPI (see .github/workflows/publish.yml).

The machine (pytunium/machine.py) does not depend on the user interface: every call of Machine.step() returns an Event that describes which cells were taken from the stack, which cells were written and the formula of the working area. The user interface (pytunium/gui.py) animates these events.

pytunium/
  program.py   parser for pytunia programs (Java notation and descriptors)
  loader.py    checks the syntax with pytunia, then parses
  machine.py   the simulated JVM; one Event per instruction
  values.py    values in the cells, Java int arithmetic
  gui.py       Tkinter user interface
  examples/    Sum.jas, Hello.jas, Primes.jas
tests/         unit tests (no window is opened)

License

BSD 3-Clause, see LICENSE.txt.

Metadata

Release files for pytunium 0.2

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

Source distribution (sdist)

Source distribution for pytunium 0.2
File Size Uploaded Provenance Actions
pytunium-0.2.tar.gz 44.4 kB With provenance Details for pytunium-0.2.tar.gz

Built distribution (wheel)

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

Total release size: 85.0 kB

Release files / pytunium-0.2.tar.gz

Download URL pytunium-0.2.tar.gz
Size 44.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2061e6ff1ab764da5d19ab0f44429236c3616381732abba39c4bd9bb7b193cbe
BLAKE2b-256 checksum
How to use checksums
c7b576589fee8d44e4023d572ebc1d6ac1831c7003c23ef4c83d0d122ca6799b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Release files / pytunium-0.2-py3-none-any.whl

Download URL pytunium-0.2-py3-none-any.whl
Size 40.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07df3358a836653b20a1c12ab608be6a0c6719e71daf00ddc1e5a0fed310b20f
BLAKE2b-256 checksum
How to use checksums
257c259ebef274995e3c08a68dd5ada9022f76d2cd566b4e53233a78501157e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

Security for pytunium 0.2

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pytunium-0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 29, 2026.

Transparency log

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.

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