From e127528e954247c936f71aac3a06e7bd833bcd97 Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Thu, 30 Jan 2020 12:13:24 -0800 Subject: [PATCH 1/6] Add bytes reference page to docs --- gitlab-pages/docs/reference/bytes.md | 128 +++++++++++++++++++++++++++ src/test/integration_tests.ml | 6 +- 2 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 gitlab-pages/docs/reference/bytes.md diff --git a/gitlab-pages/docs/reference/bytes.md b/gitlab-pages/docs/reference/bytes.md new file mode 100644 index 000000000..3a3349550 --- /dev/null +++ b/gitlab-pages/docs/reference/bytes.md @@ -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. + + + + + +```pascaligo +function concat_op (const s : bytes) : bytes is + begin skip end with bytes_concat(s , 0x7070) +``` + + + +```cameligo +let concat_op (s : bytes) : bytes = + Bytes.concat s 0x7070 +``` + + + +```reasonligo +let concat_op = (s: bytes): bytes => Bytes.concat(s, 0x7070); +``` + + + +## 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: + + + + + +```pascaligo +function slice_op (const s : bytes) : bytes is + begin skip end with bytes_slice(1n , 2n , s) +``` + + + +```cameligo +let slice_op (s : bytes) : bytes = + Bytes.slice 1n 2n s +``` + + + +``` +let slice_op = (s: bytes): bytes => Bytes.slice(1n, 2n, s); +``` + + + +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. + + + + +```pascaligo +function id_string (const p : string) : option(string) is block { + const packed : bytes = bytes_pack(p) ; +} with (bytes_unpack(packed): option(string)) +``` + + +```cameligo +let id_string (p: string) : string option = + let packed: bytes = Bytes.pack p in + ((Bytes.unpack packed): string option) +``` + + +```reasonligo +let id_string = (p: string) : option(string) => { + let packed : bytes = Bytes.pack(p); + ((Bytes.unpack(packed)): option(string)); +}; +``` + + + +## 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. + + + + +```pascaligo +function id_string (const p : string) : option(string) is block { + const packed : bytes = bytes_pack(p) ; +} with (bytes_unpack(packed): option(string)) +``` + + +```cameligo +let id_string (p: string) : string option = + let packed: bytes = Bytes.pack p in + ((Bytes.unpack packed): string option) +``` + + +```reasonligo +let id_string = (p: string) : option(string) => { + let packed : bytes = Bytes.pack(p); + ((Bytes.unpack(packed)): option(string)); +}; +``` + + diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index 789f2a6c4..6f5517cbd 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -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 @@ -434,7 +434,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 @@ -454,7 +454,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 From 6beccd09a8f020b12f1cf8736ee8ea651cca0f9a Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Sun, 2 Feb 2020 18:08:16 +0100 Subject: [PATCH 2/6] Wrong function arguments error message. --- src/bin/expect_tests/syntax_error_tests.ml | 19 +++++++++++++++++++ src/passes/1-parser/reasonligo.ml | 9 ++++++++- .../negative/error_function_arguments.religo | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 src/test/contracts/negative/error_function_arguments.religo diff --git a/src/bin/expect_tests/syntax_error_tests.ml b/src/bin/expect_tests/syntax_error_tests.ml index bf835d78b..0d29a80d1 100644 --- a/src/bin/expect_tests/syntax_error_tests.ml +++ b/src/bin/expect_tests/syntax_error_tests.ml @@ -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.ligo" ; "main" ] ; + [%expect {| + ligo: in file "basic.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 \"basic.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' |} ] ; + diff --git a/src/passes/1-parser/reasonligo.ml b/src/passes/1-parser/reasonligo.ml index 17091363b..5254018e8 100644 --- a/src/passes/1-parser/reasonligo.ml +++ b/src/passes/1-parser/reasonligo.ml @@ -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", diff --git a/src/test/contracts/negative/error_function_arguments.religo b/src/test/contracts/negative/error_function_arguments.religo new file mode 100644 index 000000000..b80d7f002 --- /dev/null +++ b/src/test/contracts/negative/error_function_arguments.religo @@ -0,0 +1,2 @@ +let div = (a, b : nat * nat) : option (nat) => + if (b == 0n) { None; } else { Some (a/b); } \ No newline at end of file From 360ee7b45e6769f5c954bb57a1da2c2f9875b9c5 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Sun, 2 Feb 2020 20:07:19 +0100 Subject: [PATCH 3/6] Wrong extension --- src/bin/expect_tests/syntax_error_tests.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/expect_tests/syntax_error_tests.ml b/src/bin/expect_tests/syntax_error_tests.ml index 0d29a80d1..b1dd0406a 100644 --- a/src/bin/expect_tests/syntax_error_tests.ml +++ b/src/bin/expect_tests/syntax_error_tests.ml @@ -15,7 +15,7 @@ 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.ligo" ; "main" ] ; + run_ligo_bad [ "compile-contract" ; "../../test/contracts/negative/error_function_arguments.religo" ; "main" ] ; [%expect {| ligo: in file "basic.religo", line 1, characters 14-27. : It looks like you are defining a function, however we do not understand the parameters declaration. From c1f5080b8163f0073296a54d5311d29f2a643cfe Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Mon, 3 Feb 2020 09:30:42 +0100 Subject: [PATCH 4/6] Spacing. --- src/bin/expect_tests/syntax_error_tests.ml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/bin/expect_tests/syntax_error_tests.ml b/src/bin/expect_tests/syntax_error_tests.ml index b1dd0406a..d25fc699f 100644 --- a/src/bin/expect_tests/syntax_error_tests.ml +++ b/src/bin/expect_tests/syntax_error_tests.ml @@ -15,15 +15,15 @@ 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" ] ; + run_ligo_bad [ "compile-contract" ; "../../test/contracts/negative/error_function_arguments.ligo" ; "main" ] ; [%expect {| - ligo: in file "basic.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 \"basic.religo\", line 1, characters 14-27"} + 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 \"basic.religo\", line 1, characters 14-27"} If you're not sure how to fix this error, you can From 952e291066c00bc7ea3fad6fad8116f5e86a92f7 Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Mon, 3 Feb 2020 10:06:28 +0100 Subject: [PATCH 5/6] Typo --- src/bin/expect_tests/syntax_error_tests.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/expect_tests/syntax_error_tests.ml b/src/bin/expect_tests/syntax_error_tests.ml index d25fc699f..91aa34cd2 100644 --- a/src/bin/expect_tests/syntax_error_tests.ml +++ b/src/bin/expect_tests/syntax_error_tests.ml @@ -15,7 +15,7 @@ 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.ligo" ; "main" ] ; + 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. From 1146292a7676b3d1acc8626efc82bc7d98c9749c Mon Sep 17 00:00:00 2001 From: Sander Spies Date: Mon, 3 Feb 2020 11:26:23 +0100 Subject: [PATCH 6/6] Argh --- src/bin/expect_tests/syntax_error_tests.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin/expect_tests/syntax_error_tests.ml b/src/bin/expect_tests/syntax_error_tests.ml index 91aa34cd2..8969c68a7 100644 --- a/src/bin/expect_tests/syntax_error_tests.ml +++ b/src/bin/expect_tests/syntax_error_tests.ml @@ -23,7 +23,7 @@ let%expect_test _ = 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 \"basic.religo\", line 1, characters 14-27"} + {"location":"in file \"error_function_arguments.religo\", line 1, characters 14-27"} If you're not sure how to fix this error, you can