From bff14309e4aff9ecaea94086f844c2686b624867 Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Thu, 6 Jun 2019 18:40:05 +0200 Subject: [PATCH] Predefined values for Ligodity. Added a few more tests for Ligodity. --- src/contracts/letin.mligo | 7 ++++ src/contracts/list.mligo | 10 +++++ src/contracts/match.mligo | 13 +++++++ src/contracts/new-syntax.mligo | 14 ++++--- src/contracts/record.mligo | 47 +++++++++++++++++++++++ src/operators/operators.ml | 49 +++++++++++++++++++++++- src/parser/ligodity/Parser.mly | 5 ++- src/parser/ligodity/Tests/match.mml | 13 +++++++ src/test/integration_tests.ml | 59 +++++++++++++++++++++-------- 9 files changed, 195 insertions(+), 22 deletions(-) create mode 100644 src/contracts/letin.mligo create mode 100644 src/contracts/list.mligo create mode 100644 src/contracts/match.mligo create mode 100644 src/contracts/record.mligo create mode 100644 src/parser/ligodity/Tests/match.mml diff --git a/src/contracts/letin.mligo b/src/contracts/letin.mligo new file mode 100644 index 000000000..fbdf8447c --- /dev/null +++ b/src/contracts/letin.mligo @@ -0,0 +1,7 @@ +type storage = int * int + +let%entry main (n: int) storage = + let x : int * int = + let x : int = 7 + in x + n, storage.(0) + storage.(1) + in (([] : operation list), x) diff --git a/src/contracts/list.mligo b/src/contracts/list.mligo new file mode 100644 index 000000000..31e2f7d50 --- /dev/null +++ b/src/contracts/list.mligo @@ -0,0 +1,10 @@ +type storage = int * int list + +type param = int list + +let%entry main (p : param) storage = + let storage = + match p with + [] -> storage + | hd::tl -> storage.(0) + hd, tl + in (([] : operation list), storage) diff --git a/src/contracts/match.mligo b/src/contracts/match.mligo new file mode 100644 index 000000000..1665e9f27 --- /dev/null +++ b/src/contracts/match.mligo @@ -0,0 +1,13 @@ +type storage = int + +type param = + Add of int +| Sub of int + +let%entry main (p : param) storage = + let storage = + storage + + (match p with + Add n -> n + | Sub n -> 0-n) + in (([] : operation list), storage) diff --git a/src/contracts/new-syntax.mligo b/src/contracts/new-syntax.mligo index f2fed5396..e29aa6444 100644 --- a/src/contracts/new-syntax.mligo +++ b/src/contracts/new-syntax.mligo @@ -14,8 +14,12 @@ type param = { } let%entry attempt (p:param) storage = - if Crypto.hash (Bytes.pack p.attempt) <> Bytes.pack storage.challenge then failwith "Failed challenge" ; - let contract : unit contract = Operation.get_contract sender in - let transfer : operation = Operation.transaction (unit , contract , 10.00tz) in - let storage : storage = storage.challenge <- p.new_challenge in - ((list [] : operation list), storage) + if Crypto.hash (Bytes.pack p.attempt) <> Bytes.pack storage.challenge + then failwith "Failed challenge" + else + let contract : unit contract = + Operation.get_contract sender in + let transfer : operation = + Operation.transaction (unit , contract , 10tz) in + let storage : storage = {challenge = p.new_challenge} + in (([] : operation list), storage) diff --git a/src/contracts/record.mligo b/src/contracts/record.mligo new file mode 100644 index 000000000..943ccf91d --- /dev/null +++ b/src/contracts/record.mligo @@ -0,0 +1,47 @@ +type foobar = { + foo : int ; + bar : int ; +} + +let fb : foobar = { + foo = 0 ; + bar = 0 ; +} + +type abc = { + a : int ; + b : int ; + c : int +} + +let abc : abc = { + a = 42 ; + b = 142 ; + c = 242 +} + +let a : int = abc.a +let b : int = abc.b +let c : int = abc.c + +let projection (r : foobar) : int = r.foo + r.bar + +let modify (r : foobar) : foobar = {foo = 256; bar = r.bar} + +let modify_abc (r : abc) : abc = {a = r.a; b = 2048; c = r.c} + +type big_record = { + a : int ; + b : int ; + c : int ; + d : int ; + e : int ; +} + +let br : big_record = { + a = 23 ; + b = 23 ; + c = 23 ; + d = 23 ; + e = 23 ; +} diff --git a/src/operators/operators.ml b/src/operators/operators.ml index 48fcd9a57..c7665ea9d 100644 --- a/src/operators/operators.ml +++ b/src/operators/operators.ml @@ -83,7 +83,54 @@ module Simplify = struct end module Ligodity = struct - include Pascaligo + let constants = [ + ("Current.balance", "BALANCE") ; + ("balance", "BALANCE") ; + ("Current.time", "NOW") ; + ("time", "NOW") ; + ("Current.amount" , "AMOUNT") ; + ("amount", "AMOUNT") ; + ("Current.gas", "STEPS_TO_QUOTA") ; + ("gas", "STEPS_TO_QUOTA") ; + ("Current.sender" , "SENDER") ; + ("sender", "SENDER") ; + ("Current.failwith", "FAILWITH") ; + ("failwith" , "FAILWITH") ; + + ("Crypto.hash" , "HASH") ; + ("Crypto.black2b", "BLAKE2B") ; + ("Crypto.sha256", "SHA256") ; + ("Crypto.sha512", "SHA512") ; + ("Crypto.hash_key", "HASH_KEY") ; + ("Crypto.check", "CHECK_SIGNATURE") ; + + ("Bytes.pack" , "PACK") ; + ("Bytes.unpack", "UNPACK") ; + ("Bytes.length", "SIZE") ; + ("Bytes.size" , "SIZE") ; + ("Bytes.concat", "CONCAT") ; + ("Bytes.slice", "SLICE") ; + ("Bytes.sub", "SLICE") ; + + ("String.length", "SIZE") ; + ("String.size", "SIZE") ; + ("String.slice", "SLICE") ; + ("String.sub", "SLICE") ; + ("String.concat", "CONCAT") ; + + ("List.length", "SIZE") ; + ("List.size", "SIZE") ; + ("List.iter", "ITER") ; + + ("Operation.transaction" , "CALL") ; + ("Operation.get_contract" , "GET_CONTRACT") ; + ("int" , "INT") ; + ("abs" , "ABS") ; + ("unit" , "UNIT") ; + ("source" , "SOURCE") ; + ] + + let type_constants = type_constants end end diff --git a/src/parser/ligodity/Parser.mly b/src/parser/ligodity/Parser.mly index cc76a8867..76267b6d3 100644 --- a/src/parser/ligodity/Parser.mly +++ b/src/parser/ligodity/Parser.mly @@ -5,9 +5,12 @@ open AST (* Rewrite "let pattern = e" as "let x = e;; let x1 = ...;; let x2 = ...;;" *) +(* module VMap = Utils.String.Map -(*let ghost_of value = Region.{region=ghost; value}*) +let ghost_of value = Region.{region=ghost; value} +*) + let ghost = Region.ghost (* let fail_syn_unif type1 type2 : 'a = diff --git a/src/parser/ligodity/Tests/match.mml b/src/parser/ligodity/Tests/match.mml new file mode 100644 index 000000000..1665e9f27 --- /dev/null +++ b/src/parser/ligodity/Tests/match.mml @@ -0,0 +1,13 @@ +type storage = int + +type param = + Add of int +| Sub of int + +let%entry main (p : param) storage = + let storage = + storage + + (match p with + Add n -> n + | Sub n -> 0-n) + in (([] : operation list), storage) diff --git a/src/test/integration_tests.ml b/src/test/integration_tests.ml index e11a88a82..cf93cc68d 100644 --- a/src/test/integration_tests.ml +++ b/src/test/integration_tests.ml @@ -439,17 +439,6 @@ let dispatch_counter_contract () : unit result = e_pair (e_typed_list [] t_operation) (e_int (op 42 n)) in expect_eq_n program "main" make_input make_expected -let basic_mligo () : unit result = - let%bind typed = mtype_file ~debug_simplify:true "./contracts/basic.mligo" in - let%bind result = evaluate_typed "foo" typed in - Ligo.AST_Typed.assert_value_eq (Ligo.AST_Typed.Combinators.e_a_empty_int (42 + 127), result) - -let counter_mligo () : unit result = - let%bind program = mtype_file "./contracts/counter.mligo" in - let make_input = fun n-> e_pair (e_int n) (e_int 42) in - let make_expected = fun n -> e_pair (e_typed_list [] t_operation) (e_int (42 + n)) in - expect_eq_n program "main" make_input make_expected - let failwith_mligo () : unit result = let%bind program = mtype_file "./contracts/failwith.mligo" in let make_input = e_pair (e_unit ()) (e_unit ()) in @@ -464,10 +453,47 @@ let guess_the_hash_mligo () : unit result = let guess_string_mligo () : unit result = let%bind program = mtype_file "./contracts/guess_string.mligo" in - let make_input = fun n-> e_pair (e_int n) (e_int 42) in - let make_expected = fun n -> e_pair (e_typed_list [] t_operation) (e_int (42 + n)) in + let make_input = fun n -> e_pair (e_int n) (e_int 42) in + let make_expected = fun n -> e_pair (e_typed_list [] t_operation) (e_int (42 + n)) + in expect_eq_n program "main" make_input make_expected + +let basic_mligo () : unit result = + let%bind typed = mtype_file ~debug_simplify:true "./contracts/basic.mligo" in + let%bind result = evaluate_typed "foo" typed in + Ligo.AST_Typed.assert_value_eq + (Ligo.AST_Typed.Combinators.e_a_empty_int (42 + 127), result) + +let counter_mligo () : unit result = + let%bind program = mtype_file "./contracts/counter.mligo" in + let make_input n = e_pair (e_int n) (e_int 42) in + let make_expected n = e_pair (e_typed_list [] t_operation) (e_int (42 + n)) in expect_eq_n program "main" make_input make_expected +let let_in_mligo () : unit result = + let%bind program = mtype_file "./contracts/letin.mligo" in + let make_input n = e_pair (e_int n) (e_pair (e_int 3) (e_int 5)) in + let make_expected n = + e_pair (e_typed_list [] t_operation) (e_pair (e_int (7+n)) (e_int (3+5))) + in expect_eq_n program "main" make_input make_expected + +let match_variant () : unit result = + let%bind program = mtype_file "./contracts/match.mligo" in + let make_input n = + e_pair (e_constructor "Sub" (e_int n)) (e_int 3) in + let make_expected n = + e_pair (e_typed_list [] t_operation) (e_int (3-n)) + in expect_eq_n program "main" make_input make_expected + +let mligo_list () : unit result = + let%bind program = mtype_file "./contracts/list.mligo" in + let make_input n = + e_pair (e_list [e_int n; e_int (2*n)]) + (e_pair (e_int 3) (e_list [e_int 8])) in + let make_expected n = + e_pair (e_typed_list [] t_operation) + (e_pair (e_int (n+3)) (e_list [e_int (2*n)])) + in expect_eq_n program "main" make_input make_expected + let main = test_suite "Integration (End to End)" [ test "type alias" type_alias ; test "function" function_ ; @@ -502,8 +528,11 @@ let main = test_suite "Integration (End to End)" [ test "closure" closure ; test "shared function" shared_function ; test "higher order" higher_order ; - test "basic mligo" basic_mligo ; - test "counter contract mligo" counter_mligo ; + test "basic (mligo)" basic_mligo ; + test "counter contract (mligo)" counter_mligo ; + test "let-in (mligo)" let_in_mligo ; + test "match variant (mligo)" match_variant ; + (* test "list matching (mligo)" mligo_list ; *) (* test "guess the hash mligo" guess_the_hash_mligo ; *) (* test "failwith mligo" failwith_mligo ; *) (* test "guess string mligo" guess_string_mligo ; *)