Skip to main content

Parser and transpiler for the Craftr DSL.

Project description

craftr-dsl

The Craftr DSL is a transpiler for the Python language that introduces the concept of closures an function calls without parentheses into the language.

Getting started

Installation

From Pip:

$ pip install craftr-dsl

Latest from GitHub:

$ pip install git+https://github.com/craftr-build/craftr-dsl

Requirements: Python 3.8 or newer

Hello, World!

A convoluted Hello, World! example in Craftr DSL might look like this:

# hello.craftr
world = { self('World!') }
world { print('Hello,', self) }

This is transpiled to

# $ python -m craftr.dsl hello.craftr -E | grep -v -e '^$'
def _closure_1(self, *arguments, **kwarguments):
    self('World!')
world = _closure_1
def _closure_2(self, *arguments, **kwarguments):
    print('Hello,', self)
world(_closure_2)

And evaluates to

# $ python -m craftr.dsl hello.craftr
Hello, World!

Language features

Closures

Closures are formed with the following syntax: [ arg -> | (arg1, arg2, ...) -> ] { body }. A closure without an argument list automatically has the signature (self, *argnames, **kwargnames).

Craftr DSLPython
filter({ self % 2 }, range(5))
def _closure_1(self, *argnames, **kwargnames):
    self % 2
filter(_closure_1, range(5))
filter(x -> x % 2, range(5))
def _closure_1(x):
    return x % 2
filter(_closure_1, range(5))
reduce((a, b) -> {
  a.append(b * 2)
  return a
}, [1, 2, 3], [])
def _closure_1(a, b):
    a.append(b * 2)
    return a
reduce(_closure_1, [1, 2, 3], [])

Function calls without parentheses

Such function calls are only supported at the statement level. A function can be called without parentheses by simply omitting them. Variadic and keyword arguments are supported as expected. Applying a closure on an object is basically the same as calling that object with the function, and arguments following the closure are still supported.

Craftr DSLPython
print 'Hello, World!', file=sys.stderr
print('Hello, World!', file=sys.stderr)
map {
  print('Hello,', self)
}, ['John', 'World']
def _closure_1(self):
    print('Hello,', self)
map(_closure_1, ['John', 'World'])
list(map {  # Not allowed inside an expression
  print('Hello,', self)
}, ['John', 'World'])
craftr.dsl.rewrite.SyntaxError: 
  in <stdin> at line 1: expected ) but got TokenProxy(Token(type=<Token.Control: 8>, value='{', pos=Cursor(offset=9, line=1, column=9)))
  |list(map {
  |~~~~~~~~~^

Limitations

Craftr DSL is intended to behave as a complete syntactic superset of standard Python. However there are currently some limitations, namely:

  • Literal sets cannot be expressed due to the grammar conflict with parameter-less closures
  • Type annotations are not currently supported
  • The walrus operator is not currently supported

Copyright © 2021 Niklas Rosenstein

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

craftr-dsl-0.3.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

craftr_dsl-0.3.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file craftr-dsl-0.3.0.tar.gz.

File metadata

  • Download URL: craftr-dsl-0.3.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for craftr-dsl-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c35fdea97941667a93c1885ef70173b613f33245e6e030b4b8ea1cc85fe5454d
MD5 d15d5118d28cb5f915bceb919a534d9f
BLAKE2b-256 e81fe7435484f44403d0751f218f5e4b7c88fec86c4b78df81b5645c6ea3cccf

See more details on using hashes here.

File details

Details for the file craftr_dsl-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: craftr_dsl-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for craftr_dsl-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6b1c45481996a822a97b2fef37fc56cbc55d294789f8895aaaaa1dab7be1c45
MD5 398fed465c7afd9818ffa939d0bf3626
BLAKE2b-256 2387d9415ec4e22d5817cc74186d2411d60c00e1522bc142f7f89f28d3d79668

See more details on using hashes here.

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