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 and start

pip install pytunium              # installs pytunia, too
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 pytunium/examples.

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

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. pytunia checks the syntax of the program first, so the error messages are the same as when assembling.

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.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 pytunium 0.1
File Size Uploaded Provenance Actions
pytunium-0.1.tar.gz 43.8 kB With provenance Details for pytunium-0.1.tar.gz

Built distribution (wheel)

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

Total release size: 84.3 kB

Release files / pytunium-0.1.tar.gz

Download URL pytunium-0.1.tar.gz
Size 43.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fbf27147ab94bf5da98adf85c18ddd1f7da03436a5c35318720d1d1103a95f93
BLAKE2b-256 checksum
How to use checksums
e9533e2ccaaa79365f7724d2944a934f17d5f6705f8315ae8d6205616d12762d
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.1-py3-none-any.whl

Download URL pytunium-0.1-py3-none-any.whl
Size 40.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
78da95e3811a79a21ed090e5f743be1d4ad16c74c1feed456dde07a8e7b308a9
BLAKE2b-256 checksum
How to use checksums
3782b11807402fd1d866cb20c7d6f6b489f633abccd3ed7bcfaa4f120b5a26cb
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.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.

Full provenance

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

Select from 2 files

Attestation for pytunium-0.1.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