Added a lot of docs

This commit is contained in:
Matej Sima 2019-06-11 03:37:12 +02:00
parent 6a85e5d422
commit 15ab06bb66
19 changed files with 293 additions and 155 deletions

View File

@ -1,44 +0,0 @@
---
id: language-basics-entrypoints
title: Entrypoints
---
## Defining an entry point
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
function main (const p : int ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), s + 1)
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Multiple entry points
Multiple entrypoints are currently not supported in Michelson, however with Ligo, you can work that around by using variants.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// variant defining pseudo multi-entrypoint actions
type action is
| Increment of int
| Decrement of int
function add (const a : int ; const b : int) : int is
block { skip } with a + b
function subtract (const a : int ; const b : int) : int is
block { skip } with a - b
// real entrypoint that re-routes the flow based on the action provided
function main (const p : action ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)),
case p of
| Increment n -> add(s, n)
| Decrement n -> subtract(s, n)
end)
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -1,22 +0,0 @@
---
id: language-basics-functions
title: Functions
---
## Defining a function
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// multiply(1, 2) = 2
function multiply (const a : int ; const b : int) : int is
begin
const result : int = a * b ;
end with result
// add(1, 2) = 3
function add (const a : int ; const b : int) : int is
block { skip } with a + b
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -1,18 +0,0 @@
---
id: language-basics-variables
title: Variables
---
## Defining a variable
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// int
const four : int = 4;
// string
const name : string = "John Doe";
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -0,0 +1,37 @@
---
id: cheat-sheet
title: Cheat Sheet
---
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
|Primitive |Example|
|--- |---|
|Strings | `"Tezos"`|
|Characters | `"t"`|
|Integers | `42`, `7`|
|Natural numbers | `42n`, `7n`|
|Unit| `unit`|
|Boolean|<pre><code>const hasDriversLicense: bool = False;<br/>const adult: bool = True;</code></pre> |
|Mutez (micro tez)| `42mtz`, `7mtz` |
|Address | `"tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx"`, `"KT1JepfBfMSqkQyf9B1ndvURghGsSB8YCLMD"`|
|Addition |`3 + 4`, `3n + 4n`|
|Multiplication & Division| `3 * 4`, `3n * 4n`, `10 / 5`, `10n / 5n`|
|Modulo| `10 mod 3`|
|Tuples| <pre><code>type name is (string * string);<br/>const winner: name = ("John", "Doe");<br/>const firstName: string = winner.0;<br/>const lastName: string = winner.1;</code></pre>|
|Types|`type age is int`, `type name is string` |
|Includes|```#include "library.ligo"```|
|Functions (short form)|<pre><code>function add (const a : int ; const b : int) : int is<br/>&nbsp;&nbsp;block { skip } with a + b</code></pre>|
|Functions (long form)|<pre><code>function add (const a : int ; const b : int) : int is<br/>&nbsp;&nbsp;block { <br/>&nbsp;&nbsp;&nbsp;&nbsp;const result: int = a + b;<br/>&nbsp;&nbsp;} with result</code></pre>|
|Options|<pre><code>type middleName is option(string);<br/>const middleName : middleName = Some("Foo");<br/>const middleName : middleName = None;</code></pre>|
|Assignment| ```const age: int = 5;```|
|Assignment on an existing variable <br/></br>*⚠️ This feature is not supported at the top-level scope, you can use it e.g. within functions. Works for Records and Maps as well.*| ```age := 18;```, ```p.age := 21``` |
|Annotations| ```("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx" : address)```|
|Variants|<pre><code>type action is<br/>&#124; Increment of int<br/>&#124; Decrement of int</code></pre>|
|Variant *(pattern)* matching|<pre><code>const a: action = Increment(5);<br/>case a of<br/>&#124; Increment n -> n + 1<br/>&#124; Decrement n -> n - 1<br/>end</code></pre>|
|Records|<pre><code>type person is record<br/>&nbsp;&nbsp;age: int ;<br/>&nbsp;&nbsp;name: string ;<br/>end<br/><br/>const john : person = record<br/>&nbsp;&nbsp;age = 18;<br/>&nbsp;&nbsp;name = "John Doe";<br/>end<br/><br/>const name: string = john.name;</code></pre>|
|Maps|<pre><code>type prices is map(nat, tez);<br/><br/>const prices : prices = map<br/>&nbsp;&nbsp;10n -> 60mtz;<br/>&nbsp;&nbsp;50n -> 30mtz;<br/>&nbsp;&nbsp;100n -> 10mtz;<br/>end<br/><br/>const price: option(tez) = prices[50n];</code></pre>|
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -0,0 +1,46 @@
---
id: entrypoints
title: Entrypoints
---
Entrypoints serve as a gate to our smart contracts. In LIGO each entrypoint is a function that accepts two arguments - first one is the parameter used to invoke the contract, and the second is the current storage of the contract. Each entrypoint has to return a list of operations to apply as a result of the smart contract call, and a new storage value.
> If you don't want to update the storage, don't worry, just re-cycle your last storage value.
## Defining an entry point
Contract below is effectively an empty contract, that takes a `unit` as a parameter, and returns a `unit` as well.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
function main (const p : unit ; const s : unit) : (list(operation) * unit) is
block {skip} with ((nil : list(operation)), s)
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Multiple entry points
Multiple entrypoints are currently not supported in Michelson yet, however with Ligo, you can work that around by using variants & pattern matching.
In the example below we have a simple counter contract, that can be either `Increment(int)`-ed, or `Decrement(int)`-ed.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// variant defining pseudo multi-entrypoint actions
type action is
| Increment of int
| Decrement of int
// real entrypoint that re-routes the flow based on the action (parameter) provided
function main (const action: action ; const counter: int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)),
case action of
| Increment number -> counter + number
| Decrement number -> counter - number
end)
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -0,0 +1,43 @@
---
id: functions
title: Functions
---
## Defining a function
Body of a function consists of two parts, the first part (**`block {}`** or **`begin ... end`**) - normally consists of logic *(flow conditions, variable declarations, etc.)*, and the second part (**`with ...`**) usually defines the return value of your function.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
const availableSupply: nat = 15n;
const totalSupply: nat = 100n;
function calculatePrice(const available: nat; const total: nat): nat is
begin
const price: nat = total / available
end with price
const price: nat = calculatePrice(availableSupply, totalSupply);
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Functions without an explicit body (shorter syntax)
A short hand syntax for the same function as above can inline the price calculation directly into the return statement.
While this approach can have it's benefits, it can decrease readability.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
const availableSupply: nat = 15n;
const totalSupply: nat = 100n;
function calculatePrice(const available: nat; const total: nat): nat is
block { skip } with total / available
const price: nat = calculatePrice(availableSupply, totalSupply);
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -1,10 +1,12 @@
--- ---
id: language-basics-operators id: operators
title: Operators title: Operators
--- ---
## Available operators ## Available operators
> This list is non-exhaustive, more operators will be added in the upcoming LIGO releases.
|Michelson |Pascaligo |Description | |Michelson |Pascaligo |Description |
|--- |--- |--- | |--- |--- |--- |
| `SENDER` | `sender` | Address that initiated the current transaction | `SENDER` | `sender` | Address that initiated the current transaction

View File

@ -0,0 +1,72 @@
---
id: types
title: Types
---
## Built-in types
For the list of built-in types, please refer to the [Cheat Sheet](language-basics/cheat-sheet.md). LIGO's type system is built on top of Michelson, but offers a handful of features like type aliasing, or groupping of multiple types into a single powerful type.
## Type aliases
Type aliasing is a great choice when working towards a readable / maintainable smart contract. One well typed variable is worth a thousand words. For example we can choose to *alias* a string, as an animal breed - this will allow us to comunicate our intent with added clarity.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
type animalBreed is string;
const dogBreed: animalBreed = "Saluki";
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Defining custom types
### Simple types
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// accountBalances is a simple type, a map of address <-> tez
type accountBalances is map(address, tez);
const ledger: accountBalances = map
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) -> 10mtz
end
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Composed types
Often our contracts will require complex data structures, which will in turn require a well-typed storage, or functions to work with. LIGO offers a simple way to compose simple types, into larger & more expressive composed types.
In the example below you can see definition of data types for a ledger, that keeps a balance & number of previous transactions for a given account.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// alias two types
type account is address;
type numberOfTransactions is nat;
// accountData consists of a record with two fields (balance, numberOfTransactions)
type accountData is record
balance: tez;
numberOfTransactions: numberOfTransactions;
end
// our ledger / accountBalances is a map of account <-> accountData
type accountBalances is map(account, accountData);
// pseudo-JSON representation of our map
// { "tz1...": {balance: 10mtz, numberOfTransactions: 5n} }
const ledger: accountBalances = map
("tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx": address) -> record
balance = 10mtz;
numberOfTransactions = 5n;
end
end
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -0,0 +1,40 @@
---
id: variables
title: Variables
---
## Defining a variable
Variables in LIGO can be defined in two ways - by using either the `const` or `var` keywords. `const` can be used both at global (top-level) and local scope (within functions/blocks), while `var` can be used for mutable values in the local scope.
### Imutable variables using `const`
> ⚠️ Currently const values are mutable as well, however this is something that will change in the upcoming release. For the time being think of `const` as a semantical way to indicate developer intentions.
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
const four: int = 4;
```
<!--END_DOCUSAURUS_CODE_TABS-->
### Mutable variables using `var`
> ⚠️ `var` can't be used in the global scope
<!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo-->
```Pascal
// won't work, use const for global values instead
var four: int = 4;
// value of `number` can be mutated within local scope
function addFour(var number: int): int is
block {
number := number + 4;
} with number;
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -0,0 +1,8 @@
---
id: editor-support
title: Editor Support
---
Good editor support is the basic component of proper development experience - currently, we provide support for VSCode via an [extension](https://marketplace.visualstudio.com/items?itemName=Brice.ligo).
Currently the extension supports Pascaligo for syntax highlighting (work in progress). But it aims to support debug, gas optimization, dry run and other relevant features in the near future.

View File

@ -1,5 +1,5 @@
--- ---
id: setup-installation id: installation
title: Installation title: Installation
--- ---
@ -33,6 +33,3 @@ ligo --help
## Manual installation (advanced) ## Manual installation (advanced)
For now, please refer to the steps described in the [Dockerfile](https://gitlab.com/ligolang/ligo/blob/master/docker/Dockerfile). For now, please refer to the steps described in the [Dockerfile](https://gitlab.com/ligolang/ligo/blob/master/docker/Dockerfile).

View File

@ -29,9 +29,9 @@ class HomeSplash extends React.Component {
<div className="tabs"> <div className="tabs">
<div className="nav-tabs"> <div className="nav-tabs">
<div id="tab-group-3-tab-4" className="nav-link active" data-group="group_3" <div id="tab-group-3-tab-4" className="nav-link active" data-group="group_3"
data-tab="tab-group-3-content-4">Pascaligo</div> data-tab="tab-group-3-content-4">PascaLIGO</div>
<div className="nav-link" data-group="group_3" <div className="nav-link" data-group="group_3"
data-tab="tab-group-3-content-5">Cameligo</div> data-tab="tab-group-3-content-5">CameLIGO</div>
<div className="nav-link">Reasonligo (coming soon) </div> <div className="nav-link">Reasonligo (coming soon) </div>
{/* <div id="tab-group-3-tab-5" className="nav-link" data-group="group_3" {/* <div id="tab-group-3-tab-5" className="nav-link" data-group="group_3"
data-tab="tab-group-3-content-5">Camligo</div> */} data-tab="tab-group-3-content-5">Camligo</div> */}

View File

@ -39,7 +39,7 @@ function Versions(props) {
<a <a
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${ href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language ? props.language + '/' : '' props.language ? props.language + '/' : ''
}setup-installation`}> }setup/installation`}>
Documentation Documentation
</a> </a>
</td> </td>
@ -63,7 +63,7 @@ function Versions(props) {
<a <a
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${ href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language ? props.language + '/' : '' props.language ? props.language + '/' : ''
}next/setup-installation`}> }next/setup/installation`}>
Documentation Documentation
</a> </a>
</td> </td>
@ -88,7 +88,7 @@ function Versions(props) {
<a <a
href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${ href={`${siteConfig.baseUrl}${siteConfig.docsUrl}/${
props.language ? props.language + '/' : '' props.language ? props.language + '/' : ''
}${version}/setup-installation`}> }${version}/setup/installation`}>
Documentation Documentation
</a> </a>
</td> </td>

View File

@ -1,7 +1,7 @@
{ {
"docs": { "docs": {
"Setup": ["setup-installation"], "Setup": ["setup/installation", "setup/editor-support"],
"Language Basics": ["language-basics-variables", "language-basics-functions", "language-basics-entrypoints", "language-basics-operators"], "Language Basics": ["language-basics/cheat-sheet", "language-basics/types", "language-basics/variables", "language-basics/functions", "language-basics/entrypoints", "language-basics/operators"],
"API": ["api-cli-commands"] "API": ["api-cli-commands"]
}, },
"contributors-docs": { "contributors-docs": {

View File

@ -97,7 +97,7 @@ const siteConfig = {
// For no header links in the top nav bar -> headerLinks: [], // For no header links in the top nav bar -> headerLinks: [],
headerLinks: [ headerLinks: [
{doc: 'setup-installation', label: 'Docs'}, {doc: 'setup/installation', label: 'Docs'},
{doc: 'api-cli-commands', label: 'CLI'}, {doc: 'api-cli-commands', label: 'CLI'},
{doc: 'tutorials/first-smart-contract', label: 'Tutorials'}, {doc: 'tutorials/first-smart-contract', label: 'Tutorials'},
{ blog: true, label: 'Blog' }, { blog: true, label: 'Blog' },

View File

@ -29,20 +29,24 @@
background: transparent; background: transparent;
} }
.homeContainer .tabs { .tabs {
max-width: 800px; max-width: 800px;
margin: 0 auto; margin: 0 auto;
border-top: none; border-top: none;
border-bottom: 4px solid #e0e0e0; border-bottom: 4px solid #e0e0e0;
} }
.homeContainer .tabs .nav-tabs > div { .tabs .nav-tabs > div {
cursor: pointer; cursor: pointer;
color: #24292e; color: #24292e;
border-bottom: none; border-bottom: none;
padding-bottom: 8px;
}
.tab-content {
padding-top: 12px;
} }
.homeContainer .tabs .nav-tabs > div.active { .tabs .nav-tabs > div.active {
border-bottom: 4px solid #1A1A1A; border-bottom: 4px solid #1A1A1A;
} }
@ -53,11 +57,11 @@
border-bottom: none; border-bottom: none;
} }
.homeContainer .tab-content { .tab-content {
border-top: 4px solid #e0e0e0; border-top: 4px solid #e0e0e0;
} }
.homeContainer .nav-tabs { .nav-tabs {
border: none; border: none;
position: relative; position: relative;
top: 4px; top: 4px;
@ -76,11 +80,15 @@
blockquote { blockquote {
background-color: rgba(26, 26, 26, 0.6); background-color: rgba(26, 26, 26, 0.3);
border-left: 8px solid rgba(26, 26, 26, 0.7); border-left: 8px solid rgba(26, 26, 26, 0.1);
color: rgba(255,255,255, 0.8); color: rgba(255,255,255, 1);
} }
blockquote code {
opacity: 0.7;
}
/*
blockquote a { blockquote a {
color: rgba(255,255,255, 0.8); color: rgba(255,255,255, 0.8);
border-bottom: 1px solid rgba(255,255,255, 0.8); border-bottom: 1px solid rgba(255,255,255, 0.8);
@ -89,7 +97,7 @@ blockquote a {
blockquote a:hover { blockquote a:hover {
color: rgba(255,255,255, 1); color: rgba(255,255,255, 1);
border-bottom: 1px solid rgba(255,255,255, 1); border-bottom: 1px solid rgba(255,255,255, 1);
} } */
/* /*
blockquote { blockquote {
background-color: rgba(252, 214, 0, 0.687); background-color: rgba(252, 214, 0, 0.687);
@ -98,11 +106,11 @@ blockquote {
} */ } */
a { a {
color: rgba(12, 12, 12, 0.8); color: rgba(178, 33, 12, 0.8);
} }
a:hover { a:hover {
color: rgb(12, 12, 12); color: rgba(178, 33, 12, 1);
} }
.homeContainer .homeWrapper .projectLogo { .homeContainer .homeWrapper .projectLogo {
@ -198,6 +206,11 @@ a:hover {
visibility: hidden; visibility: hidden;
} }
code {
background: rgb(240, 240, 240);
color: #444;
}
@media only screen and (min-device-width: 360px) and (max-device-width: 736px) { @media only screen and (min-device-width: 360px) and (max-device-width: 736px) {
} }

View File

@ -9,7 +9,7 @@ original_id: language-basics-entrypoints
<!--DOCUSAURUS_CODE_TABS--> <!--DOCUSAURUS_CODE_TABS-->
<!--Pascaligo--> <!--Pascaligo-->
```Pascal ```Pascal
function main (const p : int ; const s : int) : (list(operation) * unit) is function main (const p : int ; const s : int) : (list(operation) * int) is
block {skip} with ((nil : list(operation)), s + 1) block {skip} with ((nil : list(operation)), s + 1)
``` ```
<!--END_DOCUSAURUS_CODE_TABS--> <!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -1,39 +0,0 @@
---
id: version-next-setup-installation
title: Installation
original_id: setup-installation
---
There are currently two ways to get started with Ligo, both of those will allow you to use the Ligo CLI with your contracts. You can choose to use either the Docker image, or to compile & build the Ligo CLI yourself.
## Dockerized installation (recommended)
> 🐳 You can find instructions on how to install Docker [here](https://docs.docker.com/install/).
Easiest way to use LIGO is through the Docker image available at [Docker Hub](https://hub.docker.com/r/ligolang/ligo). Sources for the image can be found on [Gitlab](https://gitlab.com/ligolang/ligo/blob/master/docker/Dockerfile).
You can either run the docker image yourself, or you can setup a global ligo executable as shown below.
### Setting up a globally available `ligo` executable
> You can install additional ligo versions by replacing `next` with the required version number
```zsh
# next (pre-release)
curl https://gitlab.com/ligolang/ligo/raw/dev/scripts/installer.sh | bash "next"
# e.g. 1.0.0 (stable)
curl https://gitlab.com/ligolang/ligo/raw/master/scripts/installer.sh | bash "1.0.0"
```
**Verify your ligo installation by running:**
```zsh
ligo --help
```
## Manual installation (advanced)
For now, please refer to the steps described in the [Dockerfile](https://gitlab.com/ligolang/ligo/blob/master/docker/Dockerfile).

View File

@ -1,13 +1,16 @@
{ {
"version-next-docs": { "version-next-docs": {
"Setup": [ "Setup": [
"version-next-setup-installation" "version-next-setup/installation",
"version-next-setup/editor-support"
], ],
"Language Basics": [ "Language Basics": [
"version-next-language-basics-variables", "version-next-language-basics/cheat-sheet",
"version-next-language-basics-functions", "version-next-language-basics/types",
"version-next-language-basics-entrypoints", "version-next-language-basics/variables",
"version-next-language-basics-operators" "version-next-language-basics/functions",
"version-next-language-basics/entrypoints",
"version-next-language-basics/operators"
], ],
"API": [ "API": [
"version-next-api-cli-commands" "version-next-api-cli-commands"