spasmlang


Namespasmlang JSON
Version 0.2.1 PyPI version JSON
download
home_pageNone
SummarySimple Python Assembly Language
upload_time2023-12-15 23:31:22
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords assembly development python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # spasmlang

[![PyPI - Version](https://img.shields.io/pypi/v/spasmlang.svg)](https://pypi.org/project/spasmlang)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/spasmlang.svg)](https://pypi.org/project/spasmlang)


## Synopsis

`spasmlang` is a **s**imple **P**ython **as**se**m**bly **lang**uage. It is
essentially a high-level interface on top of the [bytecode][bytecode] package
that allows you to generate bytecode from a simple assembly-like syntax.

-----

**Table of Contents**

- [Installation](#installation)
- [Usage](#usage)
- [Examples](#examples)
- [License](#license)


## Installation

```console
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](#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:

```console
spasm example.pya  # generates example.pyc
```


## Examples

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

```python
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)
    
```console
spasm example.pya
```

and then execute the generated module with e.g.
    
```console
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

```console
spasm greet.pya
```

and test it with

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


## License

`spasmlang` is distributed under the terms of the
[MIT](https://spdx.org/licenses/MIT.html) license.


[bytecode]: https://github.com/MatthieuDartiailh/bytecode

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "spasmlang",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "assembly,development,python",
    "author": null,
    "author_email": "\"Gabriele N. Tornetta\" <phoenix1987@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/ee/c9/959230c40490feaf2069fe8bb8d670ff41b6df1a232a8e302b0f46b62c12/spasmlang-0.2.1.tar.gz",
    "platform": null,
    "description": "# spasmlang\n\n[![PyPI - Version](https://img.shields.io/pypi/v/spasmlang.svg)](https://pypi.org/project/spasmlang)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/spasmlang.svg)](https://pypi.org/project/spasmlang)\n\n\n## Synopsis\n\n`spasmlang` is a **s**imple **P**ython **as**se**m**bly **lang**uage. It is\nessentially a high-level interface on top of the [bytecode][bytecode] package\nthat allows you to generate bytecode from a simple assembly-like syntax.\n\n-----\n\n**Table of Contents**\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Examples](#examples)\n- [License](#license)\n\n\n## Installation\n\n```console\npip install spasmlang\n```\n\n\n## Usage\n\nThe `spasmlang` package provides a single class, `Assembly`, that allows you to\ngenerate bytecode from a simple assembly-like syntax. See the [examples](#examples)\nbelow for a taste of its API.\n\nYou can also use the `spasm` command-line utility to compile assembly files\ndirectly to Python bytecode:\n\n```console\nspasm example.pya  # generates example.pyc\n```\n\n\n## Examples\n\nThis is how the classic \"Hello, World!\" program looks like, targeting the\nCPython 3.12 bytecode:\n\n```python\nfrom spasm import Assembly\n\nasm = Assembly()\nasm.parse(\n    r\"\"\"\n    push_null\n    load_const          print\n    load_const          \"Hello, World!\"\n    call                1\n    return_value\n    \"\"\"\n)\nexec(asm.compile())\n```\n\nThis is how you can compile the file `example.pya` to `example.pyc` to create\na \"Hello, World!\" module, again targeting CPython 3.11:\n\n```\n# example.pya\n    resume      0\n    push_null\n    load_name   $print\n    load_const  \"Hello, spasm!\"\n    precall     1\n    call        1\n    pop_top\n    load_const  None\n    return_value\n```\n\nCompile the assembly code with (assuming that you have installed `spasmlang`\nwith CPython 3.11)\n    \n```console\nspasm example.pya\n```\n\nand then execute the generated module with e.g.\n    \n```console\npython3.11 -m example\n```\n\nThis example shows how to create a module that exports a `greet` function that\ntakes one argument, targeting CPython 3.11:\n\n```\n# greet.pya\n\ncode greet(who)\n    resume                      0\n    load_global                 (True, \"print\")\n    load_const                  \"Hello, \"\n    load_fast                   $who\n    format_value                0\n    build_string                2\n    precall                     1\n    call                        1\n    return_value\nend\n\n    resume 0\n    load_const                  .greet\n    make_function               0\n    store_name                  $greet\n    load_const                  None\n    return_value\n```\n\nAgain, compile the assembly code with\n\n```console\nspasm greet.pya\n```\n\nand test it with\n\n```console\n$ python3.11 -c \"from greet import greet; greet('spasmlang')\"\nHello, spasmlang\n```\n\n\n## License\n\n`spasmlang` is distributed under the terms of the\n[MIT](https://spdx.org/licenses/MIT.html) license.\n\n\n[bytecode]: https://github.com/MatthieuDartiailh/bytecode\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Simple Python Assembly Language",
    "version": "0.2.1",
    "project_urls": {
        "Documentation": "https://github.com/P403n1x87/spasmlang#readme",
        "Issues": "https://github.com/P403n1x87/spasmlang/issues",
        "Source": "https://github.com/P403n1x87/spasmlang"
    },
    "split_keywords": [
        "assembly",
        "development",
        "python"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "899536ccc2ffdcbf37a1be7708a92d22ea8c9f756ef2cd25ddb0777fb3f61770",
                "md5": "c8ce8a067748f1e2fc6413ce8e258dd4",
                "sha256": "5ae9e1e966f76e2796ef5a5811cdfb846b4abf7e3f16533b92b97d8005495033"
            },
            "downloads": -1,
            "filename": "spasmlang-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c8ce8a067748f1e2fc6413ce8e258dd4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8454,
            "upload_time": "2023-12-15T23:31:24",
            "upload_time_iso_8601": "2023-12-15T23:31:24.138918Z",
            "url": "https://files.pythonhosted.org/packages/89/95/36ccc2ffdcbf37a1be7708a92d22ea8c9f756ef2cd25ddb0777fb3f61770/spasmlang-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "eec9959230c40490feaf2069fe8bb8d670ff41b6df1a232a8e302b0f46b62c12",
                "md5": "bae69c1d26d491bca00bf10c342886f5",
                "sha256": "87e91e98670fd9d94910f9bfb68ba6e9506e8d13a0bad1e2a78c84c0907b7bcc"
            },
            "downloads": -1,
            "filename": "spasmlang-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "bae69c1d26d491bca00bf10c342886f5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 8849,
            "upload_time": "2023-12-15T23:31:22",
            "upload_time_iso_8601": "2023-12-15T23:31:22.479292Z",
            "url": "https://files.pythonhosted.org/packages/ee/c9/959230c40490feaf2069fe8bb8d670ff41b6df1a232a8e302b0f46b62c12/spasmlang-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-15 23:31:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "P403n1x87",
    "github_project": "spasmlang#readme",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "spasmlang"
}
        
Elapsed time: 0.15750s