Parser and transpiler for the Kahmi DSL.
Project description
kahmi-dsl
Kahmi is a build system and DSL for Python heavily inspired by Groovy and Gradle. The kahmi-dsl
package implements the parser and transpiler for the Kahmi language.
Syntax
The Kahmi DSL might look familar to you if you have used Groovy before. While not any Python code is valid Kahmi DSL code, the full Python language is supported in various places. Additionally, Kahmi DSL allows you to define multi-line lambdas inline with full Python code.
Every Kahmi file starts with a "context" object. There are four main syntactical elements that are supported by the Kahmi language:
let
definitions for local variables- member assignments to the current context
- calls and call blocks
- Python expressions (with multi-line lambda support)
let re = __import__('re')
msg = "Hello, World!"
print(re.sub(r'[,!]', '', self.msg)) # prints: Hello World
re.sub(r'[A-z ]', '', self.msg) {
print(self) # prints: ,!
}
You can transpile it into Python code to see what's going on under the hood:
$ python -m kahmi.dsl build.kahmi -E
re = __import__('re')
self.msg = 'Hello, World!'
__lookup__('print', locals(), self)(re.sub('[,!]', '', self.msg))
def __call_re_sub(self):
__lookup__('print', locals(), self)(self)
value = __lookup__('re', locals(), self).sub('[A-z ]', '', self.msg)
__call_re_sub(value)
What you see as self here is the current "context" object and __lookup__()
is a helper
that allows you to call local variables, members of the context or builtins.
Lambdas
Lambdas are implemented by placing function definitions for you before they are used. Lambdas can be nested and they are properly taking the right scope in most cases (exceptions are Python expressions that introduce a new scope like generator, tuple, list, set and dict comprehensions).
let myFunc = () => {
return name => { return f'Hello, {name}' }
}
myFunc() {
print(self('Sam')) # prints: Hello, Sam
}
Again, transpiling into pure Python code help to understand what is going on:
ef lambda_build_kahmi_1_13():
def lambda_build_kahmi_2_8(name):
return f'Hello, {name!r}'
return lambda_build_kahmi_2_8
myFunc = lambda_build_kahmi_1_13
def __call_myFunc(self):
__lookup__('print', locals(), self)(self('Sam'))
value = __lookup__('myFunc', locals(), self)()
__call_myFunc(value)
CLI
The command-line interface for the kahmi-dsl
package is very simplistic. It is not expected that
it provides much value outside of development as it only provides a limited method of building
and passing the root context object to the script.
usage: python -m kahmi.dsl [-h] [-c ENTRYPOINT] [-E] [file]
positional arguments:
file
optional arguments:
-h, --help show this help message and exit
-c ENTRYPOINT, --context ENTRYPOINT
-E, --transpile
If no -c,--context
is provided a kahmi.dsl.__main__.VoidContext
object is used which is really
just an instance of an empty class.
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
Built Distribution
File details
Details for the file kahmi-dsl-0.0.1.tar.gz
.
File metadata
- Download URL: kahmi-dsl-0.0.1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccaf071fe2f6859d66cfb165c995bd0e58c31a155d14a446ab50ca36f95b25f4 |
|
MD5 | 7128e5e90d5d92338c73d4250e28676a |
|
BLAKE2b-256 | e07714288f38dbea7118c938af665c280fb7670dd5565661eb1f3d5fc4b7c440 |
File details
Details for the file kahmi_dsl-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: kahmi_dsl-0.0.1-py3-none-any.whl
- Upload date:
- Size: 11.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f7e4944dcf63128e94dad699d88db4e881f623cdd0f4f0bad55130db5f6d1fc |
|
MD5 | 8f97a754e1aeae4085dcf4ef91166c4b |
|
BLAKE2b-256 | 27dd067778535761198502fb0327d0c768d6643d18f0109ca16d4f9166faf42c |