Skip to main content

Simple Python Assembly Language

Project description

spasmlang

PyPI - Version PyPI - Python Version

Synopsis

spasmlang is a simple Python assembly language. It is essentially a high-level interface on top of the bytecode package that allows you to generate bytecode from a simple assembly-like syntax.


Table of Contents

Installation

pip install spasmlang

Usage

The spasmlang package provides a single class, Assembly, that allows you to generate bytecode from a simple assembly-like syntax. See the examples below for a taste of its API.

You can also use the spasm command-line utility to compile assembly files directly to Python bytecode:

spasm example.pya  # generates example.pyc

Examples

This is how the classic "Hello, World!" program looks like, targeting the CPython 3.12 bytecode:

from spasm import Assembly

asm = Assembly()
asm.parse(
    r"""
    push_null
    load_const          print
    load_const          "Hello, World!"
    call                1
    return_value
    """
)
exec(asm.compile())

This is how you can compile the file example.pya to example.pyc to create a "Hello, World!" module, again targeting CPython 3.11:

# example.pya
    resume      0
    push_null
    load_name   $print
    load_const  "Hello, spasm!"
    precall     1
    call        1
    pop_top
    load_const  None
    return_value

Compile the assembly code with (assuming that you have installed spasmlang with CPython 3.11)

spasm example.pya

and then execute the generated module with e.g.

python3.11 -m example

This example shows how to create a module that exports a greet function that takes one argument, targeting CPython 3.11:

# greet.pya

code greet(who)
    resume                      0
    load_global                 (True, "print")
    load_const                  "Hello, "
    load_fast                   $who
    format_value                0
    build_string                2
    precall                     1
    call                        1
    return_value
end

    resume 0
    load_const                  .greet
    make_function               0
    store_name                  $greet
    load_const                  None
    return_value

Again, compile the assembly code with

spasm greet.pya

and test it with

$ python3.11 -c "from greet import greet; greet('spasmlang')"
Hello, spasmlang

License

spasmlang is distributed under the terms of the MIT license.

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

spasmlang-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

spasmlang-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: spasmlang-0.1.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for spasmlang-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8960fa109e93e0cfa6f3bc343d07e815f7d1711acfef3a831c7dd833e0b52310
MD5 ed40eae9eeb2cc602865395e0bc85fc4
BLAKE2b-256 537beb5ea64f86979b7cbc9886cce1f99c6e0465e293df498d40486d5b5bbf34

See more details on using hashes here.

File details

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

File metadata

  • Download URL: spasmlang-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.0

File hashes

Hashes for spasmlang-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4a9cae5abc71c0e59aa14ce85a5d878f64e6c6dda778ea33c4ab5ee87c0869f1
MD5 90fd558e7abbc39cec6feb0a53ed2c84
BLAKE2b-256 283608712d3b05573ad3d0084b2f19d3ff5e5b3d9e60d16bc9e6fe7a4a50f9f8

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