Skip to main content

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

Project description

clvm_tools_rs

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

This repo can be installed via cargo

cargo install clvm_tools_rs

or via pip

pip install clvm_tools_rs@git+https://github.com/Chia-Network/clvm_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/Chia-Network/clvm_tools_rs/tree/nightly)

To install from a specific branch:

cargo install --no-default-features --git 'https://github.com/Chia-Network/clvm_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/Chia-Network/clvm_tools_rs
cd clvm_tools_rs
maturin develop

Install from PYPI:

pip install -i https://pypi.chia.net/nightlies/ clvm_tools_rs

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

import json
from clvm_tools_rs import compile_clvm

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 + ".clvm.hex")
    sym_file = file_path / (file_stem + ".sym")
    compile_result = compile_clvm(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 CLVM code from chialisp

Most commonly, you'll compile chialisp like this:

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

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

- repl -- Accepts chialisp 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 }) }, "clvm raise in (8) (())")
>>> 

- cldb -- Stepwise run chialisp 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: clvm 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 chialisp
  compiler, instead runs clvm 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 clvm 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 chia's clvm tools to rust via the work of ChiaMineJP 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, ChiaMineJP).

Some reasons for doing this are:

  • Chia switched the clvm implementation to rust: clvm_rs, and this code may both pick up speed and track clvm 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: ochialisp.

  • 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 clvm_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 chia repo.
                
src/compiler <-- a newer compiler (ochialisp) 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 chia-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

clvm_tools_rs-0.1.45.tar.gz (549.1 kB view details)

Uploaded Source

Built Distributions

clvm_tools_rs-0.1.45-cp38-abi3-win_amd64.whl (1.4 MB view details)

Uploaded CPython 3.8+ Windows x86-64

clvm_tools_rs-0.1.45-cp38-abi3-musllinux_1_1_x86_64.whl (1.6 MB view details)

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

clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_x86_64.whl (1.5 MB view details)

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

clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_aarch64.whl (1.5 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.28+ ARM64

clvm_tools_rs-0.1.45-cp38-abi3-macosx_13_0_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8+ macOS 13.0+ x86-64

clvm_tools_rs-0.1.45-cp38-abi3-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

File details

Details for the file clvm_tools_rs-0.1.45.tar.gz.

File metadata

  • Download URL: clvm_tools_rs-0.1.45.tar.gz
  • Upload date:
  • Size: 549.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for clvm_tools_rs-0.1.45.tar.gz
Algorithm Hash digest
SHA256 73848e0504e42bbd9627497c7307147cce9a04678936b5aec5c5a6be4b372c84
MD5 cb5053fa0242e14fe380a7580cc10269
BLAKE2b-256 ba611369700ba72256b0ffaeb5b6407545b2b3fd3ea6a990687511828d8cc4b0

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 3153335cafd180a6a308fef0d3808777fb5f7e13edd43e9ee344afd212e05516
MD5 df4cd0597fcf74a0a15bc6b1d1524370
BLAKE2b-256 057e5a0c10181cee6aa5e3538bb84ba11e285e1ba2af6a6520e29b1e559b1c34

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 079b849e0c289b6c8e7e93c027d9ee8083714ad07c5d19a1c0f7bd88efa9615f
MD5 51132f338387e8acb286dc104c100a2a
BLAKE2b-256 ad0aceadddaa13a971c54eee29a4d803dce4c30824671caf485ab52673028cbe

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 792fb5be40d317e32b86e44f9a3bbbf4d96827b304877e678eaff0abca212b3e
MD5 d174110398042bc40e2276326ca3c9bf
BLAKE2b-256 19865d5c34ac56f7d1241f41cf49e266c4c504115d4c71da8d8ff80b58ba73b4

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 fe0dcb7cad0edd1c9b1f7b0e3e6864cea1e13c3f8a6667de83c4721ff5626b4f
MD5 2cda4976ae4d16e47784fda84635ea94
BLAKE2b-256 ed63d5184653ec03f78ca831b4dfaa7c2f814b1709257253f15202345e6f2a30

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d1cd3240fdb9560c5cc860ac15196a00b70935827018deacf01651fcd9bfa6e5
MD5 fc6f9821c921f92b4a350b9608b86a6d
BLAKE2b-256 a54e53e7f79f877b1de101829da203419f09bd02c418485918f6d1040e73549a

See more details on using hashes here.

File details

Details for the file clvm_tools_rs-0.1.45-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for clvm_tools_rs-0.1.45-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 fc5a60fa6bbbfb8875258266b82251c4cc9279e3d6435eaaada0b2c033816b1a
MD5 ce122a9a232180ea4ce9cc95dcd9bbd6
BLAKE2b-256 53abc0996508ae9cf855e18c20804fcbdabc518183b89a68db127039b1ac920d

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