Merge branch 'dev' of gitlab.com:ligolang/ligo into feature/doc-pascaligo-loop

This commit is contained in:
Christian Rinderknecht 2020-02-03 14:18:06 +01:00
commit d92a5499e1
5 changed files with 160 additions and 4 deletions

View File

@ -0,0 +1,128 @@
---
id: bytes-reference
title: Bytes
---
## Bytes.concat(b1: bytes, b2: bytes) : bytes
Concatenate together two `bytes` arguments and return the result.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function concat_op (const s : bytes) : bytes is
begin skip end with bytes_concat(s , 0x7070)
```
<!--CameLIGO-->
```cameligo
let concat_op (s : bytes) : bytes =
Bytes.concat s 0x7070
```
<!--ReasonLIGO-->
```reasonligo
let concat_op = (s: bytes): bytes => Bytes.concat(s, 0x7070);
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Bytes.slice(pos1: nat, pos2: nat, data: bytes) : bytes
Extract the bytes between `pos1` and `pos2`. **Positions are zero indexed and
inclusive**. For example if you gave the input "ff7a7aff" to the following:
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function slice_op (const s : bytes) : bytes is
begin skip end with bytes_slice(1n , 2n , s)
```
<!--CameLIGO-->
```cameligo
let slice_op (s : bytes) : bytes =
Bytes.slice 1n 2n s
```
<!--ReasonLIGO-->
```
let slice_op = (s: bytes): bytes => Bytes.slice(1n, 2n, s);
```
<!--END_DOCUSAURUS_CODE_TABS-->
It would return "7a7a" rather than "ff7a" or "ff" or "7a".
## Bytes.pack(data: a') : bytes
Converts Michelson data structures to a binary format for serialization.
> ⚠️ `PACK` and `UNPACK` are features of Michelson that are intended to be used by people that really know what they're doing. There are several failure cases (such as `UNPACK`ing a lambda from an untrusted source), most of which are beyond the scope of this document. Don't use these functions without doing your homework first.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function id_string (const p : string) : option(string) is block {
const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
```
<!--CameLIGO-->
```cameligo
let id_string (p: string) : string option =
let packed: bytes = Bytes.pack p in
((Bytes.unpack packed): string option)
```
<!--ReasonLIGO-->
```reasonligo
let id_string = (p: string) : option(string) => {
let packed : bytes = Bytes.pack(p);
((Bytes.unpack(packed)): option(string));
};
```
<!--END_DOCUSAURUS_CODE_TABS-->
## Bytes.unpack(packed: bytes) : a'
Reverses the result of using `unpack` on data, going from Michelson's binary
serialization format to the `option` type annotated on the call.
> ⚠️ `PACK` and `UNPACK` are features of Michelson that are intended to be used by people that really know what they're doing. There are several failure cases (such as `UNPACK`ing a lambda from an untrusted source), most of which are beyond the scope of this document. Don't use these functions without doing your homework first.
<!--DOCUSAURUS_CODE_TABS-->
<!--PascaLIGO-->
```pascaligo
function id_string (const p : string) : option(string) is block {
const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
```
<!--CameLIGO-->
```cameligo
let id_string (p: string) : string option =
let packed: bytes = Bytes.pack p in
((Bytes.unpack packed): string option)
```
<!--ReasonLIGO-->
```reasonligo
let id_string = (p: string) : option(string) => {
let packed : bytes = Bytes.pack(p);
((Bytes.unpack(packed)): option(string));
};
```
<!--END_DOCUSAURUS_CODE_TABS-->

View File

@ -15,3 +15,22 @@ let%expect_test _ =
* Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new
* Check the changelog by running 'ligo changelog' |} ] ;
run_ligo_bad [ "compile-contract" ; "../../test/contracts/negative/error_function_arguments.religo" ; "main" ] ;
[%expect {|
ligo: in file "error_function_arguments.religo", line 1, characters 14-27. : It looks like you are defining a function, however we do not
understand the parameters declaration.
Examples of valid functions:
let x = (a: string, b: int) : int => 3;
let tuple = ((a, b): (int, int)) => a + b;
let x = (a: string) : string => "Hello, " ++ a;
{"location":"in file \"error_function_arguments.religo\", line 1, characters 14-27"}
If you're not sure how to fix this error, you can
do one of the following:
* Visit our documentation: https://ligolang.org/docs/intro/what-and-why/
* Ask a question on our Discord: https://discord.gg/9rhYaEt
* Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new
* Check the changelog by running 'ligo changelog' |} ] ;

View File

@ -55,7 +55,14 @@ module Errors =
let wrong_function_arguments (expr: AST.expr) =
let title () = "" in
let message () = "Wrong function arguments.\n" in
let message () = "It looks like you are defining a function, \
however we do not\n\
understand the parameters declaration.\n\
Examples of valid functions:\n\
let x = (a: string, b: int) : int => 3;\n\
let tuple = ((a, b): (int, int)) => a + b; \n\
let x = (a: string) : string => \"Hello, \" ++ a;\n"
in
let expression_loc = AST.expr_to_region expr in
let data = [
("location",

View File

@ -0,0 +1,2 @@
let div = (a, b : nat * nat) : option (nat) =>
if (b == 0n) { None; } else { Some (a/b); }

View File

@ -414,7 +414,7 @@ let bytes_arithmetic () : unit result =
let%bind foototo = e_bytes_hex "0f007070" in
let%bind toto = e_bytes_hex "7070" in
let%bind empty = e_bytes_hex "" in
let%bind tata = e_bytes_hex "7a7a7a7a" in
let%bind tata = e_bytes_hex "ff7a7aff" in
let%bind at = e_bytes_hex "7a7a" in
let%bind ba = e_bytes_hex "ba" in
let%bind () = expect_eq program "concat_op" foo foototo in
@ -476,7 +476,7 @@ let bytes_arithmetic_mligo () : unit result =
let%bind foototo = e_bytes_hex "0f007070" in
let%bind toto = e_bytes_hex "7070" in
let%bind empty = e_bytes_hex "" in
let%bind tata = e_bytes_hex "7a7a7a7a" in
let%bind tata = e_bytes_hex "ff7a7aff" in
let%bind at = e_bytes_hex "7a7a" in
let%bind ba = e_bytes_hex "ba" in
let%bind () = expect_eq program "concat_op" foo foototo in
@ -496,7 +496,7 @@ let bytes_arithmetic_religo () : unit result =
let%bind foototo = e_bytes_hex "0f007070" in
let%bind toto = e_bytes_hex "7070" in
let%bind empty = e_bytes_hex "" in
let%bind tata = e_bytes_hex "7a7a7a7a" in
let%bind tata = e_bytes_hex "ff7a7aff" in
let%bind at = e_bytes_hex "7a7a" in
let%bind ba = e_bytes_hex "ba" in
let%bind () = expect_eq program "concat_op" foo foototo in