Skip to main content

tools for working with chiklisp language; compiler, repl, python and wasm bindings

Project description

klvm_tools_rs

GitHub Coverage Status Build Crate Build Wheels

PyPI Crates.io

Theory of operation of the modern compiler: ./HOW_CHIKLISP_IS_COMPILED.md

This repo can be installed via cargo

cargo install klvm_tools_rs

or via pip

pip install klvm_tools_rs@git+https://github.com/Chik-Network/klvm_tools_rs.git@e17412032aa7d3b8b1d1f931893fb5802eee626a

Note: pip installs a subset of the tools installed by cargo, including brun, run, opc and opd.

The most current version of the language is in the nightly branch:

[nightly](https://github.com/Chik-Network/klvm_tools_rs/tree/nightly)

To install from a specific branch:

cargo install --no-default-features --git 'https://github.com/Chik-Network/klvm_tools_rs' --branch nightly

To install a git checkout into your current python environment (must be in some kind of venv or conda environment):

git clone https://github.com/Chik-Network/klvm_tools_rs
cd klvm_tools_rs
maturin develop

Install from PYPI:

pip install -i https://pypi.chiknetwork.com/nightlies/ klvm_tools_rs

Most people still compile chiklisp via python. One way to set up compilation in that way is like this:

import json
from klvm_tools_rs import compile_klvm

def compile_module_with_symbols(include_paths,source):
    path_obj = Path(source)
    file_path = path_obj.parent
    file_stem = path_obj.stem
    target_file = file_path / (file_stem + ".klvm.hex")
    sym_file = file_path / (file_stem + ".sym")
    compile_result = compile_klvm(source, str(target_file.absolute()), include_paths, True)
    symbols = compile_result['symbols']
    if len(symbols) != 0:
        with open(str(sym_file.absolute()),'w') as symfile:
            symfile.write(json.dumps(symbols))

The command line tools provided:

- run -- Compiles KLVM code from chiklisp

Most commonly, you'll compile chiklisp like this:

  ./target/debug/run -O -i include_dir chiklisp.clsp

'run' outputs the code resulting from compiling the program, or an error.

- repl -- Accepts chiklisp forms and expressions and produces results
          interactively.
          
Run like:

  ./target/debug/repl
  
Example session:

>>> (defmacro assert items
   (if (r items)
       (list if (f items) (c assert (r items)) (q . (x)))
     (f items)
     )
   )
(q)
>>> (assert 1 1 "hello")
(q . hello)
>>> (assert 1 0 "bye")
failed: CompileErr(Srcloc { file: "*macros*", line: 2, col: 26, until: Some(Until { line: 2, col: 82 }) }, "klvm raise in (8) (())")
>>> 

- cldb -- Stepwise run chiklisp programs with program readable yaml output.

  ./target/debug/cldb '(mod (X) (x X))' '(4)'
  ---
  - Arguments: (() (4))
    Operator: "4"
    Operator-Location: "*command*(1):11"
    Result-Location: "*command*(1):11"
    Row: "0"
    Value: (() 4)
  - Env: "4"
    Env-Args: ()
    Operator: "2"
    Operator-Location: "*command*(1):11"
    Result-Location: "*command*(1):13"
    Row: "1"
    Value: "4"
  - Arguments: (4)
    Failure: klvm raise in (8 5) (() 4)
    Failure-Location: "*command*(1):11"
    Operator: "8"
    Operator-Location: "*command*(1):13"

- brun -- Runs a "binary" program.  Instead of serving as a chiklisp
  compiler, instead runs klvm programs.

As 'brun' from the python code:

$ ./target/debug/run '(mod (X) (defun fact (N X) (if (> 2 X) N (fact (* X N) (- X 1)))) (fact 1 X))'
(a (q 2 2 (c 2 (c (q . 1) (c 5 ())))) (c (q 2 (i (> (q . 2) 11) (q . 5) (q 2 2 (c 2 (c (* 11 5) (c (- 11 (q . 1)) ()))))) 1) 1))
$ ./target/debug/brun '(a (q 2 2 (c 2 (c (q . 1) (c 5 ())))) (c (q 2 (i (> (q . 2) 11) (q . 5) (q 2 2 (c 2 (c (* 11 5) (c (- 11 (q . 1)) ()))))) 1) 1))' '(5)'
120

- opc -- crush klvm s-expression form to hex.

As 'opc' from the python code.

opc '(a (q 2 2 (c 2 (c (q . 1) (c 5 ())))) (c (q 2 (i (> (q . 2) 11) (q . 5) (q 2 2 (c 2 (c (* 11 5) (c (- 11 (q . 1)) ()))))) 1) 1))'
ff02ffff01ff02ff02ffff04ff02ffff04ffff0101ffff04ff05ff8080808080ffff04ffff01ff02ffff03ffff15ffff0102ff0b80ffff0105ffff01ff02ff02ffff04ff02ffff04ffff12ff0bff0580ffff04ffff11ff0bffff010180ff808080808080ff0180ff018080

- opd -- disassemble hex to s-expression form.

As 'opd' from the python code.

opd 'ff02ffff01ff02ff02ffff04ff02ffff04ffff0101ffff04ff05ff8080808080ffff04ffff01ff02ffff03ffff15ffff0102ff0b80ffff0105ffff01ff02ff02ffff04ff02ffff04ffff12ff0bff0580ffff04ffff11ff0bffff010180ff808080808080ff0180ff018080'
(a (q 2 2 (c 2 (c (q . 1) (c 5 ())))) (c (q 2 (i (> (q . 2) 11) (q . 5) (q 2 2 (c 2 (c (* 11 5) (c (- 11 (q . 1)) ()))))) 1) 1))

History

This is a second-hand port of chik's klvm tools to rust via the work of ChikMineJP porting to typescript. This would have been a lot harder to get to where it is without prior work mapping out the types of various semi-dynamic things (thanks, ChikMineJP).

Some reasons for doing this are:

  • Chik switched the klvm implementation to rust: klvm_rs, and this code may both pick up speed and track klvm better being in the same language.

  • I wrote a new compiler with a simpler, less intricate structure that should be easier to improve and verify in the future in ocaml: ochiklisp.

  • Also it's faster even in this unoptimized form.

All acceptance tests i've brought over so far work, and more are being added. As of now, I'm not aware of anything that shouldn't be authentic when running these command line tools from klvm_tools in their equivalents in this repository

  • opc

  • opd

  • run

  • brun

  • repl

argparse was ported to javascript and I believe I have faithfully reproduced it as it is used in cmds, so command line parsing should work similarly in all three versions.

The directory structure is expected to be:

src/classic  <-- any ported code with heritage pointing back to
                 the original chik repo.
                
src/compiler <-- a newer compiler (ochiklisp) with a simpler
                 structure.  Select new style compilation by
                 including a `(include *standard-cl-21*)`
                 form in your toplevel `mod` form.

Mac M1

Use cargo build --no-default-features due to differences in how mac m1 and other platforms handle python extensions.

Use with chik-blockchain

# Activate your venv, then
$ maturin develop --release

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

klvm_tools_rs-0.3.0.tar.gz (551.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

klvm_tools_rs-0.3.0-cp39-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.9+Windows x86-64

klvm_tools_rs-0.3.0-cp39-abi3-musllinux_1_1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.1+ x86-64

klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9+macOS 13.0+ x86-64

klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.9+macOS 13.0+ ARM64

File details

Details for the file klvm_tools_rs-0.3.0.tar.gz.

File metadata

  • Download URL: klvm_tools_rs-0.3.0.tar.gz
  • Upload date:
  • Size: 551.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for klvm_tools_rs-0.3.0.tar.gz
Algorithm Hash digest
SHA256 5d983639420c0c11d24b334bf7f67e1ff635a04ebdc625909d8a863f0491d230
MD5 ed23b51ea4c6724fcbc2a6559f441858
BLAKE2b-256 d06acb3fe6b2ee5c743025e8d9992d496203f4cbd242081b5eec321e90e4f0d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0.tar.gz:

Publisher: build-test.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 d5ef41d609ea78056334b817330fdd3fd9f1574384878a71e04c487cbe7b4c39
MD5 a1f9e90a274822bbc2d90fa129f2e6f4
BLAKE2b-256 665c3c37ee477896e375f2ea7635fae8e90a8819c0557543d561a3e3a107c609

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-win_amd64.whl:

Publisher: build-test.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1cc54e25e62962d44a6c808cb9aaa35e3d5c092ebc11c58ab13a6ce187be697f
MD5 28e22259962aad4bcdb6295120660b08
BLAKE2b-256 c06988491d18662c13d0c77c0a32075182142db0753453cce8d225a227cd208f

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-musllinux_1_1_x86_64.whl:

Publisher: build-test.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 de3839be4f2c597363a4268b02a71042867b8d12f814968127582c9acd37a234
MD5 e4a7b8e5ef4e9e1d9bd4cdc55bf7ccb5
BLAKE2b-256 6aa3d0f60c73c362ae8a7db2788b07b57ed652fee30371c4fd2c3615e60735a4

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_x86_64.whl:

Publisher: build-test.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 c5703cc5241c280da40e3c7dd87e91fa9000f80de4520d5608d2181e53f750d3
MD5 60126348ea5956f40cedb03d3860db6a
BLAKE2b-256 5cb3525a2ae951fb08294482b6a0772948216bb545de6b1bc359990b8f163fb0

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-manylinux_2_28_aarch64.whl:

Publisher: build-arm64-wheels.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 cfa7ebb6f6dbbe17d907150e195cdc88d6bd9a2f4d275583bc88399b5a2afd97
MD5 50a54475a344ca8214d0deebaec8cda4
BLAKE2b-256 3920e686eb3ed86795e00caebf8cdceda339fd57c193327455e2a26628b60f53

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_x86_64.whl:

Publisher: build-test.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_arm64.whl.

File metadata

File hashes

Hashes for klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_arm64.whl
Algorithm Hash digest
SHA256 1d2b4d2b00650b2ea616caa7eead249929a10c51cf70c161b6f680a0e82902b1
MD5 123a4d7e52b20b6e68c84fbaf4cdb3a2
BLAKE2b-256 5a5c61ad9639f21c95b0925f891d6e7f91b660452ce98cae1aadb8ec4a6ae5ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for klvm_tools_rs-0.3.0-cp39-abi3-macosx_13_0_arm64.whl:

Publisher: build-m1-wheel.yml on Chik-Network/klvm_tools_rs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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