Skip to main content

Python based stack machine

Project description

This work is inspired by Sven Havemanns work on Generative Modelling. The idea is to create a stack machine in Python that is minimalistic but easy to extended. Minimalistic means that it is boiled down to its bare minimum. All the rest is down with extensions written in Python

Sven has oriented himself at the Postscript interpreter and so is this state machine. Because it is based on Python, there are a few differences to the Postscript stack machine:

  • All objects on the stack are Python objects.

  • Postscript uses () to denote strings. PBSM uses the standard Python syntax for strings: ', " or """ enclose a valid string.

  • Postscript use `/` to push a literal on the stack. PBSM uses ' instead. This makes the syntax more consistent and uses less characters with special meaning.

Datatypes

PBSM uses standard object types of Python: int, float, string, bool and list. It introduces the following types:

  • Marker to denote the start of a list/executable list.

  • Symbol for a symbolic name.

  • Reference references a symbol

  • Procedure represents an executable list.

Commands

The core interpreter comes with a few commands only. These are:

  • [ together with ] creates the a list.

  • { together with } creates an executable list/a procedure.

  • cvx converts a list to an executable list/procedure

  • cvlit converts an executable list/procedure back to a normal/litaral list

  • def assigns a value to a symbol.

  • exec executes the object on the stack.

  • mark puts a mark on the stack

  • counttomark counts the elements on the stack up to the topmost mark

  • cleartomark removes all objects from the stack up to the topmost mark but not the mark itself.

That’s it. All the rest is done with extensions.

Execution

The stack machine maintains a stack of dictionaries in which the values of the symbols are stored. Every extention can register a dictionary with name/object pairs. Callable objects are executable. The machine tries to resolve the symbol by looking in each of the dictionaries in a top-down fashen. If it finds a value, it executes the value. Execution means:

  • If the object is callable, it is called.

  • If it is a symbol, the symbol is looked up and the process starts again recursively.

  • All other objects are put on the stack.

Examples

List creation:

[ 5.0 True "hello" ]

Executable list creation:

{ dup print }

This is syntactial sugar for:

[ 'dup 'print ] cvx

Procedure definitaion:

'average { 2 div } def

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

pbsm-0.3.1.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

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

pbsm-0.3.1-py2.py3-none-any.whl (16.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file pbsm-0.3.1.tar.gz.

File metadata

  • Download URL: pbsm-0.3.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pbsm-0.3.1.tar.gz
Algorithm Hash digest
SHA256 5de74d546d32498c2e4e9d4de95d241d325c86c12113f754982ab5104a62d3ef
MD5 6feafa6c879924e80b8d9dc7533a23ff
BLAKE2b-256 cc0cde07ee9c32148a4dc2515f26f0433fa12a74125be6319ccdf6cc5bd0bd32

See more details on using hashes here.

File details

Details for the file pbsm-0.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: pbsm-0.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for pbsm-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d22c8967c00826f8769f454fb2e2629ec6d3ef6f25f8c548f307564b387d8d73
MD5 62db01d68314690a644931a59d21a256
BLAKE2b-256 d3d0cd20cd6906c220fa974616a027fa85da9a5ac8b5561cdda0fc12a6185ca9

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