Commit 2f528d6b authored by David Declerck's avatar David Declerck
Browse files

Add new file

parent fe5a72a2
title=A Solidity parser in OCaml with Menhir
authors=David Declerck
date=2020-05-19
category=Blockchains
tags=
<a href="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png" title="A Solidity parser in OCaml with Menhir"><a href="https://www.ocamlpro.com/2020/05/19/ocaml-solidity-parser-with-menhir/" title="A Solidity parser in OCaml with Menhir"><img width="420" height="420" src="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" srcset="https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover.png 420w, https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover-300x300.png 300w, https://www.ocamlpro.com/wp-content/uploads/2020/05/solidity-cover-150x150.png 150w" sizes="(max-width: 420px) 100vw, 420px" /></a></a>
<blockquote class="wp-block-quote"><p id="11a3" class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh" data-selectable-paragraph=""><em class="in">This article is cross-posted on Origin Labs’ Dune Network <a href="https://medium.com/dune-network/a-solidity-parser-in-ocaml-with-menhir-e1064f94e76b">blog</a>.</em></p></blockquote>
<p>We are happy to announce the first release of <a href="https://gitlab.com/o-labs/solidity-parser-ocaml" target="_blank" rel="noopener nofollow noreferrer">our Solidity parser</a>, written in OCaml using <a href="http://gallium.inria.fr/~fpottier/menhir/" target="_blank" rel="noopener nofollow noreferrer">Menhir.</a> This is a joint effort with <a href="https://www.origin-labs.com/">Origin Labs</a>, the company dedicated to blockchain challenges, to implement a full interpreter for the <a href="https://solidity.readthedocs.io/en/v0.6.8/" target="_blank" rel="noopener nofollow noreferrer">Solidity language</a> directly in a blockchain.</p>
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1500/1*USQMP1HCMH3BW5KQ5DuOkQ.png" alt="Solidity"/></figure>
<p>Solidity is probably the most popular language for smart-contracts, small pieces of code triggered when accounts receive transactions on a blockchain.Solidity is an object-oriented strongly-typed language with a Javascript-like syntax.</p>
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1400/1*X-eldSjyZO6kJvHL_VYeCQ.png" alt=""/></figure>
<p>Solidity was first implemented for the <a href="https://ethereum.org/" target="_blank" rel="noopener nofollow noreferrer">Ethereum</a> blockchain, with a compiler to the EVM, the Ethereum Virtual Machine.</p>
<figure class="wp-block-image"><img src="https://miro.medium.com/max/1400/1*pt7nACsxaiY4gCk8vjbeOQ.png" alt=""/></figure>
<p>Dune Network takes a different approach, as Solidity smart-contracts will be executed natively, after type-checking. Solidity will be the third native language on Dune Network, with <a href="https://dune.network/docs/dune-node-mainnet/whitedoc/michelson.html" target="_blank" rel="noopener nofollow noreferrer">Michelson</a>, a low-level strongly-typed language inherited from Tezos, and <a href="https://dune.network/docs/dune-node-mainnet/love-doc/introduction.html" target="_blank" rel="noopener nofollow noreferrer">Love</a>, an higher-level strongly-typed language, also implemented jointly by OCamlPro and Origin Labs.</p>
<p>A first step has been accomplished, with the completion of the Solidity parser and printer, written in OCaml with Menhir.</p>
<p>This parser (and its printer companion) is now available as a standalone library under the LGPLv3 license with Linking Exception, allowing its integration in all projects. The source code is available at <a href="https://gitlab.com/o-labs/solidity-parser-ocaml" target="_blank" rel="noopener nofollow noreferrer">https://gitlab.com/o-labs/solidity-parser-ocaml</a>.</p>
<p>Our parser should support all of Solidity 0.6, with the notable exception of inline assembly (may be added in a future release).</p>
<h1 id="4d19">Example contract</h1>
<p>Here is an example of a very simple contract that stores an integer value and allows the contract’s owner to add an arbitrary value to this value, and any other contract to read this value:</p>
<pre>pragma solidity >=0.6.0 <0.7.0;contract C { address owner; int x; constructor() public { owner = msg.sender; x = 0; } function add(int d) public { require(msg.sender == owner); x += d; } function read_x() public view returns(int) { return x; }}</pre>
<h1 class="jr js cu bk bj jt fq ju fs jv jw jx jy jz ka kb kc" id="d42c">Parser Usage</h1>
<h2 class="kk js cu bk bj jt ku kv kw kx ky kz la lb lc ld le" id="d47c">Executable</h2>
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">Our parser comes with a small executable that demonstrates the library usage. Simply run:</p>
<pre class="wp-block-preformatted">./solp contract.sol</pre>
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">This will parse the file <code class="jh lf lg lh kl b">contract.sol</code> and reprint it on the terminal.</p>
<h2 class="kk js cu bk bj jt ku kv kw kx ky kz la lb lc ld le" id="e6b5">Library</h2>
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">To use our parser as a library, add it to your program’s dependencies and use the following function:</p>
<pre class="wp-block-preformatted">Solidity_parser.parse_contract_file : string -&gt; Solidity_parser.Solidity_types.module_</pre>
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">It takes a filename and returns a Solidity AST.</p>
<p class="ht ih cu bk hv b hw hx ii hy hz ij ia ib ik ic id il ie if im ig fh">If you wish to print this AST, you may turn it into its string representation by sending it to the following function:</p>
<pre class="wp-block-preformatted">Solidity_parser.Printer.string_of_code : Solidity_parser.Solidity_types.module_ -&gt; string</pre>
<h1 class="jr js cu bk bj jt fq ju fs jv jw jx jy jz ka kb kc" id="9eee">Conclusion</h1>
<p class="ht ih cu bk hv b hw kd ii hy ke ij ia kf ik ic kg il ie kh im ig fh">Of course, all of this is Work In Progress, but we are quite happy to share it with the OCaml community. We think there is a tremendous work to be done around blockchains for experts in formal methods. Do not hesitate to contact us if you want to use this library!</p>
<blockquote class="wp-block-quote"><p><strong class="hv li">About OCamlPro</strong></p><p>OCamlPro is a company founded in 2011, with the mission to help industrial users benefit from state-of-the art programming languages like OCaml and Rust. We design, create and implement custom ad-hoc software for our clients. We also have a long experience in developing and maintaining open-source tooling for OCaml, such as Opam, TryOCaml, ocp-indent etc. and we contribute to the core-development of OCaml, notably with our work on the Flambda optimizer branch with Jane Street. Another area of expertise is that of Formal Methods, with tools such as our SMT Solver Alt-Ergo (check our Alt-Ergo Users’ Club). We provide consulting and vocational trainings in OCaml and Rust, and we can build courses on formal methods on-demand. Do not hesitate to reach out by email: <a class="ch dk io ip iq ir" rel="noopener nofollow noreferrer" href="mailto:contact@ocamlpro.com" target="_blank">contact@ocamlpro.com</a>.</p></blockquote>
<blockquote class="wp-block-quote"><p><strong class="hv li">About Origin Labs</strong></p><p>Origin Labs is a company founded in 2019 by the former blockchain team at OCamlPro. At Origin Labs, they have been developing Dune Network, a fork of the Tezos blockchain, its ecosystem, and applications over the Dune Network platform. At OCamlPro, they developed TzScan, the most popular block explorer at the time, Liquidity, a smart contract language, and were involved in the development of the core protocol and node.Do not hesitate to reach out by email: <a class="ch dk io ip iq ir" rel="noopener nofollow noreferrer" href="mailto:contact@ocamlpro.com" target="_blank">contact@origin-labs.com</a>.</p></blockquote>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment