From 8b991b853e65c6ea0739548325f2d226d60378a7 Mon Sep 17 00:00:00 2001 From: "Gabriele N. Tornetta" Date: Sat, 30 Sep 2023 11:30:44 +0100 Subject: [PATCH] Initial commit --- .github/FUNDING.yml | 3 + .github/workflows/checks.yml | 54 +++++ .github/workflows/release.yml | 20 ++ .github/workflows/tests.yml | 43 ++++ .gitignore | 144 +++++++++++++ LICENSE.txt | 9 + README.md | 140 +++++++++++++ pyproject.toml | 137 +++++++++++++ spasm/__init__.py | 7 + spasm/__main__.py | 58 ++++++ spasm/asm.py | 367 ++++++++++++++++++++++++++++++++++ tests/__init__.py | 3 + tests/test_asm.py | 84 ++++++++ 13 files changed, 1069 insertions(+) create mode 100644 .github/FUNDING.yml create mode 100644 .github/workflows/checks.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 spasm/__init__.py create mode 100644 spasm/__main__.py create mode 100644 spasm/asm.py create mode 100644 tests/__init__.py create mode 100644 tests/test_asm.py diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..9947e03 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,3 @@ +github: p403n1x87 +patreon: P403n1x87 +custom: "https://www.buymeacoffee.com/Q9C1Hnm28" diff --git a/.github/workflows/checks.yml b/.github/workflows/checks.yml new file mode 100644 index 0000000..f4353ed --- /dev/null +++ b/.github/workflows/checks.yml @@ -0,0 +1,54 @@ +name: Checks + +on: + push: + branches: + - main + pull_request: + +jobs: + typing: + runs-on: "ubuntu-latest" + + name: Typing + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - run: | + pip install hatch + hatch run lint:typing + + format: + runs-on: "ubuntu-latest" + + name: Formatting + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - run: | + pip install hatch + hatch run lint:fmt + + style: + runs-on: "ubuntu-latest" + + name: Style + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: "3.11" + + - run: | + pip install hatch + hatch run lint:style + \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..d42eab0 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,20 @@ +name: Release + +on: + release: + types: [published] + +jobs: + release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: '3.11' + + - run: | + pip install hatch hatch-vcs + hatch build + hatch publish --user=__token__ --auth=${{ secrets.PYPI_TOKEN }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..c4183ce --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,43 @@ +name: Tests + +on: + push: + branches: + - main + pull_request: + +concurrency: + group: ${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + tests: + runs-on: ${{ matrix.os }}-latest + strategy: + fail-fast: false + matrix: + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + os: ["ubuntu", "macos", "windows"] + + name: Test with Python ${{ matrix.python-version }} on ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }}-dev + + - name: Install dependencies + run: | + pip install hatch + + - name: Run tests + timeout-minutes: 30 + run: | + hatch run py${{ matrix.python-version }}:test -svv + + - name: Publish coverage metrics + run: | + hatch run py${{ matrix.python-version }}:cov + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b992e0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,144 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# Protobuf compiler +protoc* + +# Dynamic version file +spasm/_version.py diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..e048874 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2023-present Gabriele N. Tornetta + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..edd5170 --- /dev/null +++ b/README.md @@ -0,0 +1,140 @@ +# 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 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..c16c5d0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,137 @@ +[build-system] +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" + +[project] +name = "spasmlang" +dynamic = ["version"] +description = 'Simple Python Assembly Language' +readme = "README.md" +requires-python = ">=3.8" +license = "MIT" +keywords = [] +authors = [{ name = "Gabriele N. Tornetta", email = "phoenix1987@gmail.com" }] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: Implementation :: CPython", +] +dependencies = ["bytecode"] + +[project.urls] +Documentation = "https://github.com/unknown/spasmlang#readme" +Issues = "https://github.com/unknown/spasmlang/issues" +Source = "https://github.com/unknown/spasmlang" + +[project.scripts] +spasm = "spasm.__main__:main" + +[tool.hatch.version] +source = "vcs" + +[tool.hatch.build.hooks.vcs] +version-file = "spasm/_version.py" + +[tool.hatch.envs.default] +dependencies = ["coverage[toml]>=6.5", "pytest"] +[tool.hatch.envs.default.scripts] +test = "pytest {args:tests}" +test-cov = "coverage run -m pytest {args:tests}" +cov-report = ["- coverage combine", "coverage report"] +cov = ["test-cov", "cov-report"] + +[[tool.hatch.envs.default.matrix]] +python = ["3.8", "3.9", "3.10", "3.11", "3.12"] + +[tool.hatch.envs.lint] +detached = true +dependencies = ["black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243"] +[tool.hatch.envs.lint.scripts] +typing = "mypy --install-types --non-interactive spasm {args}" +style = ["ruff spasm/ {args}", "black --check --diff spasm/ {args}"] +fmt = ["black {args:.}", "ruff --fix {args:.}", "style"] +all = ["style", "typing"] + +[tool.black] +target-version = ["py37"] +line-length = 120 +skip-string-normalization = true + +[tool.ruff] +target-version = "py37" +line-length = 120 +select = [ + "A", + "ARG", + "B", + "C", + "DTZ", + "E", + "EM", + "F", + "FBT", + "I", + "ICN", + "ISC", + "N", + "PLC", + "PLE", + "PLR", + "PLW", + "Q", + "RUF", + "S", + "T", + "TID", + "UP", + "W", + "YTT", +] +ignore = [ + # Allow non-abstract empty methods in abstract base classes + "B027", + # Allow boolean positional values in function calls, like `dict.get(... True)` + "FBT003", + # Ignore checks for possible passwords + "S105", + "S106", + "S107", + # Ignore complexity + "C901", + "PLR0911", + "PLR0912", + "PLR0913", + "PLR0915", +] +unfixable = [ + # Don't touch unused imports + "F401", +] + +[tool.ruff.isort] +known-first-party = ["spasmlang"] + +[tool.ruff.flake8-tidy-imports] +ban-relative-imports = "all" + +[tool.ruff.per-file-ignores] +# Tests can use magic values, assertions, and relative imports +"tests/**/*" = ["PLR2004", "S101", "TID252"] + +[tool.coverage.run] +source_pkgs = ["spasmlang", "tests"] +branch = true +parallel = true +omit = ["spasmlang/__about__.py"] + +[tool.coverage.paths] +spasmlang = ["spasmlang", "*/spasmlang/spasmlang"] +tests = ["tests", "*/spasmlang/tests"] + +[tool.coverage.report] +exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"] diff --git a/spasm/__init__.py b/spasm/__init__.py new file mode 100644 index 0000000..2a9989f --- /dev/null +++ b/spasm/__init__.py @@ -0,0 +1,7 @@ +# SPDX-FileCopyrightText: 2023-present Gabriele N. Tornetta +# +# SPDX-License-Identifier: MIT + +from spasm.asm import Assembly + +__all__ = ["Assembly"] diff --git a/spasm/__main__.py b/spasm/__main__.py new file mode 100644 index 0000000..0774efb --- /dev/null +++ b/spasm/__main__.py @@ -0,0 +1,58 @@ +import importlib +import time +from argparse import ArgumentParser +from pathlib import Path +from types import CodeType + +from spasm._version import __version__ +from spasm.asm import Assembly + + +class SpasmError(Exception): + pass + + +def dump_code_to_file(code: CodeType, file: Path) -> None: + try: + data = importlib._bootstrap_external._code_to_timestamp_pyc( # type: ignore[attr-defined] + code, + time.time(), + len(code.co_code), + ) + except ValueError as e: + msg = "Cannot unmarshal code object" + raise SpasmError(msg) from e + except Exception as e: + msg = "Cannot create pyc file" + raise SpasmError(msg) from e + + with file.open("wb") as stream: + stream.write(data) + stream.flush() + + +def assemble(source: str, filename: str) -> CodeType: + asm = Assembly(name="", filename=filename, lineno=1) + asm.parse(source) + return asm.compile() + + +def main() -> None: + argp = ArgumentParser() + + argp.add_argument("file", type=Path) + argp.add_argument("-V", "--version", action="version", version=__version__) + + args = argp.parse_args() + + try: + dump_code_to_file( + assemble(args.file.read_text(), str(args.file.resolve())), + args.file.with_suffix(".pyc"), + ) + except Exception as e: + print("Spasm error: ", str(e)) # noqa: T201 + + +if __name__ == "__main__": + main() diff --git a/spasm/asm.py b/spasm/asm.py new file mode 100644 index 0000000..3c43a88 --- /dev/null +++ b/spasm/asm.py @@ -0,0 +1,367 @@ +# Use @ for labels +# Use % for try blocks +# use $ for string literals +# use # for comments +# use {} for bind opargs +# use [] for cellvars +# use () for freevars + +# Grammar: +# ident ::= [a-zA-Z_][a-zA-Z0-9_]* +# number ::= [0-9]+ +# label ::= ident ":" +# label_ref ::= "@" ident +# string_ref ::= "$" ident +# try_block_begin ::= "try" label_ref ["lasti"]? +# try_block_end ::= "tried" +# opcode ::= [A-Z][A-Z0-9_]* +# bind_opcode_arg ::= "{" ident "}" +# opcode_arg ::= label_ref | string | number | bind_opcode_arg | code_ref | ident["." ident]* +# instruction ::= opcode [opcode_arg]? +# code_begin ::= "code" ident "(" [ident ["," ident]] ")" +# code_end ::= "end" +# code_ref ::= "." ident +# line ::= label | try_block_begin | try_block_end | code_begin | code_end | instruction + +import dis +import sys +import typing as t +from dataclasses import dataclass +from types import CodeType + +import bytecode as bc # type: ignore[import] + + +def relocate(instrs: bc.Bytecode, lineno: int) -> bc.Bytecode: + new_instrs = bc.Bytecode() + for i in instrs: + if isinstance(i, bc.Instr): + new_i = i.copy() + new_i.lineno = lineno + new_instrs.append(new_i) + else: + new_instrs.append(i) + return new_instrs + + +class BaseOpArg(bc.Label): + # We cannot have arbitrary objects in Bytecode, so we subclass Label + def __init__(self, name: str, arg: str, lineno: t.Optional[int] = None) -> None: + self.name = name + self.arg = arg + self.lineno = lineno + + def __call__(self, data: t.Dict[str, t.Any], lineno: t.Optional[int] = None) -> bc.Instr: + raise NotImplementedError + + +class BindOpArg(BaseOpArg): + def __call__(self, bind_args: t.Dict[str, t.Any], lineno: t.Optional[int] = None) -> bc.Instr: + return bc.Instr(self.name, bind_args[self.arg], lineno=lineno if lineno is not None else self.lineno) + + +class CodeRefOpArg(BaseOpArg): + def __call__(self, codes: t.Dict[str, CodeType], lineno: t.Optional[int] = None) -> bc.Instr: + return bc.Instr(self.name, codes[self.arg], lineno=lineno if lineno is not None else self.lineno) + + +@dataclass +class CodeBegin: + name: str + args: t.List[str] + + +class CodeEnd: + pass + + +class Assembly: + def __init__( + self, name: t.Optional[str] = None, filename: t.Optional[str] = None, lineno: t.Optional[int] = None + ) -> None: + self._labels: t.Dict[str, bc.Label] = {} + self._ref_labels: t.Dict[str, bc.Label] = {} + self._tb: t.Optional[bc.TryBegin] = None + self._instrs = bc.Bytecode() + self._instrs.name = name or "" + self._instrs.filename = filename or __file__ + self._lineno = lineno + self._bind_opargs: t.Dict[int, BindOpArg] = {} + self._codes: t.Dict[str, Assembly] = {} + self._code_refs: t.Dict[int, CodeRefOpArg] = {} + + def _parse_ident(self, text: str) -> str: + if not text.isidentifier(): + raise ValueError("invalid identifier %s" % text) + + return text + + def _parse_number(self, text: str) -> t.Optional[int]: + try: + return int(text) + except ValueError: + return None + + def _parse_label(self, line: str) -> t.Optional[bc.Label]: + if not line.endswith(":"): + return None + + label_ident = self._parse_ident(line[:-1]) + if label_ident in self._labels: + raise ValueError("label %s already defined" % label_ident) + + label = self._labels[label_ident] = self._ref_labels.pop(label_ident, None) or bc.Label() + + return label + + def _parse_label_ref(self, text: str) -> t.Optional[bc.Label]: + if not text.startswith("@"): + return None + + label_ident = self._parse_ident(text[1:]) + + try: + return self._labels[label_ident] + except KeyError: + try: + return self._ref_labels[label_ident] + except KeyError: + label = self._ref_labels[label_ident] = bc.Label() + return label + + def _parse_string_ref(self, text: str) -> t.Optional[str]: + if not text.startswith("$"): + return None + + return self._parse_ident(text[1:]) + + def _parse_try_begin(self, line: str) -> t.Optional[bc.TryBegin]: + try: + head, label_ref, *lasti = line.split(maxsplit=2) + except ValueError: + return None + + if head != "try": + return None + + if self._tb is not None: + msg = "cannot start try block while another is open" + raise ValueError(msg) + + label = self._parse_label_ref(label_ref) + if label is None: + msg = "invalid label reference for try block" + raise ValueError(msg) + + tb = self._tb = bc.TryBegin(label, push_lasti=bool(lasti)) + + return tb + + def _parse_try_end(self, line: str) -> t.Optional[bc.TryEnd]: + if line != "tried": + return None + + if self._tb is None: + msg = "cannot end try block while none is open" + raise ValueError(msg) + + end = bc.TryEnd(self._tb) + + self._tb = None + + return end + + def _parse_opcode(self, text: str) -> str: + opcode = text.upper() + if opcode not in dis.opmap: + raise ValueError("unknown opcode %s" % opcode) + + return opcode + + def _parse_expr(self, text: str) -> t.Any: + frame = sys._getframe(1) + _globals = frame.f_globals.copy() + _globals["asm"] = bc + return eval(text, _globals, frame.f_locals) # noqa: S307 + + def _parse_opcode_arg(self, text: str) -> t.Union[bc.Label, str, int, t.Any]: + return ( + self._parse_label_ref(text) + or self._parse_string_ref(text) + or self._parse_number(text) + or self._parse_expr(text) + ) + + def _parse_bind_opcode_arg(self, text: str) -> t.Optional[str]: + if not text.startswith("{") or not text.endswith("}"): + return None + + return text[1:-1] + + def _parse_code_ref_arg(self, text: str) -> t.Optional[str]: + if not text.startswith("."): + return None + + return text[1:] + + def _parse_instruction(self, line: str) -> t.Optional[t.Union[bc.Instr, BindOpArg]]: + opcode, *args = line.split(maxsplit=1) + + if args: + (arg,) = args + + bind_arg = self._parse_bind_opcode_arg(arg) + if bind_arg is not None: + entry = BindOpArg(self._parse_opcode(opcode), bind_arg, lineno=self._lineno) + + # TODO: What happens if a bind arg occurs multiple times? + self._bind_opargs[len(self._instrs)] = entry + + return entry + + code_ref = self._parse_code_ref_arg(arg) + if code_ref is not None: + entry = CodeRefOpArg(self._parse_opcode(opcode), code_ref, lineno=self._lineno) + + self._code_refs[len(self._instrs)] = entry + + return entry + + return bc.Instr(self._parse_opcode(opcode), *map(self._parse_opcode_arg, args), lineno=self._lineno) + + def _parse_code_begin(self, line: str) -> t.Optional[CodeBegin]: + try: + if not line.endswith(")"): + return None + line = line[:-1] + + head, details = line.split(maxsplit=1) + if head != "code": + return None + except ValueError: + return None + + try: + name, _, arglist = details.partition("(") + + name = name.strip() + arglist = arglist.strip() + args = [arg.strip() for arg in arglist.split(",")] + except Exception as e: + msg = f"invalid code block header: {e}" + raise ValueError(msg) from e + + return CodeBegin(name, args) + + def _parse_code_end(self, line: str) -> t.Optional[CodeEnd]: + return CodeEnd() if line == "end" else None + + def _parse_line(self, line: str) -> t.Union[bc.Instr, bc.Label, bc.TryBegin, bc.TryEnd]: + entry = ( + self._parse_label(line) + or self._parse_try_begin(line) + or self._parse_try_end(line) + or self._parse_code_begin(line) + or self._parse_code_end(line) + or self._parse_instruction(line) + ) + + if entry is None: + raise ValueError("invalid line %s" % line) + + return entry + + def _validate(self) -> None: + if self._ref_labels: + raise ValueError("undefined labels: %s" % ", ".join(self._ref_labels)) + + def _parse_code(self, lines: t.Iterable[str]) -> None: + for line in lines: + entry = self._parse_line(line) + + if isinstance(entry, CodeEnd): + break + + self._instrs.append(entry) + + else: + msg = f"code block {self._instrs.name} not terminated" + raise ValueError(msg) + + self._validate() + + def _parse(self, lines: t.Iterable[str]) -> None: + for line in lines: + entry = self._parse_line(line) + + if isinstance(entry, CodeBegin): + code = self._codes[entry.name] = Assembly( + name=entry.name, filename=self._instrs.filename, lineno=self._lineno + ) + code._parse_code(lines) + + code._instrs.argnames = entry.args or None + code._instrs.argcount = len(code._instrs.argnames or []) + # TODO: Add support for other types of arguments + + continue + + if isinstance(entry, CodeEnd): + msg = "code end outside of code block" + raise ValueError(msg) + + self._instrs.append(entry) + + self._validate() + + def parse(self, text: str) -> None: + self._parse(_ for _ in (_.strip() for _ in text.splitlines()) if _ and not _.startswith("#")) + + def bind(self, args: t.Optional[t.Dict[str, t.Any]] = None, lineno: t.Optional[int] = None) -> bc.Bytecode: + if not self._bind_opargs and not self._code_refs: + if lineno is not None: + return relocate(self._instrs, lineno) + return self._instrs + + missing_bind_args = {_.arg for _ in self._bind_opargs.values()} - set(args or {}) + if missing_bind_args: + raise ValueError("missing bind args: %s" % ", ".join(missing_bind_args)) + + # If we have bind opargs, the bytecode we parsed has some + # BindOpArg placeholders that need to be resolved. Therefore, we + # make a copy of the parsed bytecode and replace the BindOpArg + # placeholders with the resolved values. + instrs = bc.Bytecode(self._instrs) + for i, arg in self._bind_opargs.items(): + instrs[i] = arg(t.cast(dict, args), lineno=lineno) + + if self._code_refs: + codes = {name: code.compile(args, lineno) for name, code in self._codes.items()} + for i, arg in self._code_refs.items(): + instrs[i] = arg(codes, lineno=lineno) + + return relocate(instrs, lineno) if lineno is not None else instrs + + def compile( # noqa: A003 + self, + bind_args: t.Optional[t.Dict[str, t.Any]] = None, + lineno: t.Optional[int] = None, + ) -> CodeType: + return self.bind(bind_args, lineno=lineno).to_code() + + def _label_ident(self, label: bc.Label) -> str: + return next(ident for ident, _ in self._labels.items() if _ is label) + + def dis(self) -> None: + for entry in self._instrs: + if isinstance(entry, bc.Instr): + print(f" {entry.name:<32}{entry.arg if entry.arg is not None else ''}") # noqa: T201 + elif isinstance(entry, BindOpArg): + print(f" {entry.name:<32}{{{entry.arg}}}") # noqa: T201 + elif isinstance(entry, bc.Label): + print(f"{self._label_ident(entry)}:") # noqa: T201 + elif isinstance(entry, bc.TryBegin): + print(f"try @{self._label_ident(entry.target)} (lasti={entry.push_lasti})") # noqa: T201 + + def __iter__(self) -> t.Iterator[bc.Instr]: + return iter(self._instrs) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..cb0554b --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2023-present Gabriele N. Tornetta +# +# SPDX-License-Identifier: MIT diff --git a/tests/test_asm.py b/tests/test_asm.py new file mode 100644 index 0000000..7a0c1c4 --- /dev/null +++ b/tests/test_asm.py @@ -0,0 +1,84 @@ +import sys + +import pytest + +from spasm import Assembly + + +def test_assembly_bind_args(): + asm = Assembly() + + asm.parse( + r""" + load_const {retval} + return_value + """ + ) + + assert eval(asm.compile({"retval": 42})) == 42 + + +@pytest.mark.skipif(sys.version_info[:2] != (3, 11), reason="CPython 3.11 bytecode only") +def test_assembly_exception_table(): + asm = Assembly() + + asm.parse( + r""" + resume 0 + + try @exception + load_const {answer} + load_const 42 + compare_op asm.Compare.NE + pop_jump_forward_if_false @correct_answer + load_const Exception("Not the answer") + raise_varargs 1 + tried + + correct_answer: + load_const None + return_value + + exception: + push_exc_info + return_value + """ + ) + + assert eval(asm.compile({"answer": 42})) is None + assert isinstance(eval(asm.compile({"answer": 41})), Exception) + + +@pytest.mark.skipif(sys.version_info[:2] != (3, 11), reason="CPython 3.11 bytecode only") +def test_assembly_sub_code(): + asm = Assembly() + + asm.parse( + r""" + 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 + pop_top + load_fast $who + return_value + end + + resume 0 + load_const .greet + make_function 0 + store_name $greet + load_const None + return_value + """ + ) + + _globals = {} + exec(asm.compile(), _globals) + + assert _globals["greet"]("World") == "World"