Simple Python Assembly Language
Project description
spasmlang
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
Built Distribution
File details
Details for the file spasmlang-0.2.1.tar.gz
.
File metadata
- Download URL: spasmlang-0.2.1.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87e91e98670fd9d94910f9bfb68ba6e9506e8d13a0bad1e2a78c84c0907b7bcc |
|
MD5 | bae69c1d26d491bca00bf10c342886f5 |
|
BLAKE2b-256 | eec9959230c40490feaf2069fe8bb8d670ff41b6df1a232a8e302b0f46b62c12 |
File details
Details for the file spasmlang-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: spasmlang-0.2.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.25.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ae9e1e966f76e2796ef5a5811cdfb846b4abf7e3f16533b92b97d8005495033 |
|
MD5 | c8ce8a067748f1e2fc6413ce8e258dd4 |
|
BLAKE2b-256 | 899536ccc2ffdcbf37a1be7708a92d22ea8c9f756ef2cd25ddb0777fb3f61770 |