Skip to main content

Build languages on Python.

Project description

Pydialect makes Python into a language platform, à la Racket. It provides the plumbing that allows to create, in Python, dialects that compile into Python at import time.

An extension to the Python language doesn’t need to make it into the Python core, or even be desirable for inclusion into the Python core, in order to be useful.

Building on functions and syntactic macros, customization of the language itself is one more tool for the programmer to extract patterns, at a higher level. Hence, beside language experimentation, such extensions can be used as a framework that allows shorter and/or more readable programs.

Pydialect places language-creation power in the hands of its users, without the need to go to extreme lengths to hack CPython itself or implement from scratch a custom language that compiles to Python AST or bytecode.

Pydialect is geared toward creating languages that extend Python and look almost like Python, but extend or modify its syntax and/or semantics. Hence dialects.

That said, Pydialect itself is only a lightweight infrastructure hook that makes it convenient to define and use dialects. To implement the actual semantics for your dialect (which is where all the interesting things happen), you may want to look at [MacroPy](https://github.com/azazel75/macropy). Examples can be found in [unpythonic](https://github.com/Technologicat/unpythonic); see especially the macros (comprising about one half of unpythonic). On packaging a set of semantics into a Pydialect definition, look at the example dialects included in the Pydialect distribution.

Example of a module using a dialect:

from __lang__ import lispython

print("hello, my dialect is {}".format(__lang__))

c = cons(1, 2)
assert tuple(c) == (1, 2)
assert car(c) == 1
assert cdr(c) == 2
assert ll(1, 2, 3) == llist((1, 2, 3))

x = let[(a, 21) in 2*a]
assert x == 42

x = letseq[((a, 1),
            (a, 2*a),
            (a, 2*a)) in
           a]
assert x == 4

a = lambda x: cond[x < 0, "nope",
                   x % 2 == 0, "even",
                   "odd"]
assert a(-1) == "nope"
assert a(2) == "even"
assert a(3) == "odd"

def fact(n):
    def f(k, acc):
        if k == 1:
            return acc
        f(k - 1, k*acc)  # implicit return in tail position, like in Lisps
    f(n, acc=1)
assert fact(4) == 24
fact(5000)  # automatic TCO, no crash

square = lambda x: x**2
assert square(3) == 9
assert square.__name__ == "square"  # lambdas are auto-named

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

pydialect-0.1.1.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

pydialect-0.1.1-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file pydialect-0.1.1.tar.gz.

File metadata

  • Download URL: pydialect-0.1.1.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3

File hashes

Hashes for pydialect-0.1.1.tar.gz
Algorithm Hash digest
SHA256 29cbba1bcd25223675ee640bd4df3d7587758b0dd7b06d7056e423e524e17684
MD5 69a36d276ce8faa9f868297253e0bf5b
BLAKE2b-256 e744a8c6aa99afe909f391cf4ff54bf6633704018f646616c3c628315fdaa690

See more details on using hashes here.

Provenance

File details

Details for the file pydialect-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pydialect-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.4.3

File hashes

Hashes for pydialect-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1fc386af3e8ae66cbc6d6716080924ae42987bc9a4d9a43939435d6f521e2edb
MD5 21c5b67a38626c645435c4f7ef774caa
BLAKE2b-256 05924db1c9879c5adfa473196f81ebdc59a497ebcb5e1d3189c14a8980a23b86

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page