Skip to main content

PySchemeTranspiler

This transpiler is only working in Python version 3.8.* due to changes in the AST module from 3.9 onwards

PYST is a transpiler between python and Scheme(Racket) source code. It tries to mimic the behavior of CPython and makes simple builtin functions available to the user. It works solely with the Python builtin AST module and does not have any dependencies.

Work in Progress

There might still be special edge cases not documented that can cause faulty transpilation. If you come across such a case, please submit the code that did not transpile correctly as an issue here on GitHub or email me privately.

Roadmap/Todos

Supported features

Language features

  • Variables
  • Constants
  • Arithmetic
  • Custom Functions
  • Builtins (print, input, range, len; Type converters: int, float, str, bool)
  • Types: int, float, str, bool, None, List[{Type}] (Indexing + append, pop and insert), Tuple[{Type, ...}]
  • If, elif, else (also nested) (comparators eg. != == >= and in (for List and Tuple) but not is or is not)
  • MultiAssign swapping (seq[n - 1], seq[n] = seq[n], seq[n - 1])
  • Augmented assignment (a += 17)
  • If expressions (var = a if b else c)
  • __name__ == '__main__' -> will always be true
  • For (also nested) (also multi-target e.g. for i, j in [[0, 1], [2, 3], [4, 5]]:)
  • Assert

Typing system

PYST has a fully fledged typing system and matches types at transpile-time. While most types can dynamically be deduced lists still need to be annotated in the standard python way, for example: myList: List[int] = [1,2,3]. This restriction is necessary because PYST can not infer a type for an empty list. Type annotations are always checked. To create a pending type you may assign a variable to None: var = None. This will make the type pending and allow later assigning of a different value. After a type is determined it may not be changed but can be set to None again. None can act as a nullptr value as in C++ to create optional returns. The variable which has a type but is set to a value of None may still be used like one with a value of its own type, any runtime errors may be avoided by the user (a None check for example: if var != None:).

Reserved names

To avoid undefined behavior during transpilation, you should avoid reassigning the special names: int, float, str, bool, list, print, input, range, len, deepcopy, toList and __{anything}__

Error and warning system

PYST tries to make errors as transparent as possible. If a transpilation error is encountered, a clear message explaining it and the exact place it occurred will be presented to the user. For example the following code will result in a transpilation error:

def willError(arg1: List[int]) -> int:
	return arg1[17]

my_list: List[float] = [1,2,3]

print(willError(my_list))
[TypeError] type <TList: <class 'float'>> can not be applied to argument of type <TList: <class 'int'>>
6>6:  print(willError(my_list))
            ^

Caveats

Multiple returns

Please ensure all paths in all functions return at least None (PYST will implicitly add this return if missing). Multiple return statements in functions must be written so that they are the last executed entity in their function, for example with an if statement:

def testFunc(something: int) -> int:
  if something:
    return something
  else:
    return None # This could also be a plain 'return'

Note that the else is mandatory here as the first return would fall through to the None return otherwise. PYST will intentionally fail when an incorrect usage of return could lead to unexpected behavior.

Usage

usage: pystranspile [-h] [-version] -input INPUT -output OUTPUT [-exportable]

Transpile simple Python to Scheme(Racket).

optional arguments:
  -h, --help      show this help message and exit
  -version        display the current version
  -input INPUT    path to file that should be transpiled
  -output OUTPUT  path to file the transpiled code should be saved in
  -exportable     don't wrap all usercode in a main function to allow easier exports (this might cause extra outputs)

Copyright (C) 2021 Rubin Raithel

You may abbreviate the above mentioned flags to -i, -o, -e and -v. PYST is installed as a globally available script and does therefore not require the python3 prefix but can still be invoked with it by typing python3 -m pyschemetranspiler.

Installation

PYST is currently not available on the PyPi and can therefore only be installed through a local clone of this repo and the command pip install . which will make the pystranspile command globally available.

License

PYST is currently licensed under the GPLv3 license.

Copyright (C) 2021 Rubin Raithel

Metadata

Release files for pyschemetranspiler 1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyschemetranspiler 1.3
File Size Uploaded Provenance Actions
pyschemetranspiler-1.3.tar.gz 36.7 kB Without provenance Details for pyschemetranspiler-1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyschemetranspiler 1.3
File Interpreter ABI Platform Actions
pyschemetranspiler-1.3-py3-none-any.whl Python 3 none any Details for pyschemetranspiler-1.3-py3-none-any.whl

Total release size: 77.0 kB

Release files / pyschemetranspiler-1.3.tar.gz

Download URL pyschemetranspiler-1.3.tar.gz
Size 36.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a84caabc0b785c63c12f9313200349e20a79ce512eb6dd1277941a1b2468af9f
BLAKE2b-256 checksum
How to use checksums
0bb109792e888f3bfda29904b7961c048942d541a60f801a6b94d2c033a1989a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release files / pyschemetranspiler-1.3-py3-none-any.whl

Download URL pyschemetranspiler-1.3-py3-none-any.whl
Size 40.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e59a69124eeb7e9b1a1bbefbc22f224646f2d2e34f9c808951ba1cf011cdeb28
BLAKE2b-256 checksum
How to use checksums
58ae57572368c0d4d71eadd73a7e504fbf75374c1d34224c5720ef2f43fec992
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Security for pyschemetranspiler 1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.3 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page