From 2dc6b4a263aa6f574e5c20d6410d15032661fe59 Mon Sep 17 00:00:00 2001 From: Galfour Date: Tue, 30 Apr 2019 12:02:22 +0000 Subject: [PATCH] add div and mod ; stabilize before adding deep closures --- src/ligo/contracts/arithmetic.ligo | 3 +++ src/ligo/contracts/heap.ligo | 3 ++- src/ligo/operators/operators.ml | 18 ++++++++++++++++++ src/ligo/parser/parser.ml | 5 +++-- src/ligo/parser/pascaligo/Lexer.mll | 2 +- src/ligo/simplify/pascaligo.ml | 4 ++++ src/ligo/test/heap_tests.ml | 6 +++--- src/ligo/test/integration_tests.ml | 4 +++- src/ligo/test/test_helpers.ml | 20 ++++++++++++++------ 9 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/ligo/contracts/arithmetic.ligo b/src/ligo/contracts/arithmetic.ligo index 668a8c619..25b756b04 100644 --- a/src/ligo/contracts/arithmetic.ligo +++ b/src/ligo/contracts/arithmetic.ligo @@ -1,3 +1,6 @@ +function mod_op (const n : int) : nat is + begin skip end with n mod 42 + function plus_op (const n : int) : int is begin skip end with n + 42 diff --git a/src/ligo/contracts/heap.ligo b/src/ligo/contracts/heap.ligo index 44abd2f0a..23d7425b7 100644 --- a/src/ligo/contracts/heap.ligo +++ b/src/ligo/contracts/heap.ligo @@ -38,9 +38,10 @@ function insert (const h : heap ; const e : heap_element) : heap is var i : nat := size(h) + 1n ; h[i] := e ; var largest : nat := i ; + var parent : nat := 0n ; while (largest =/= i) block { + parent := i / 2n ; largest := i ; - const parent : nat = i / 2n ; if (parent >= 1n) then block { if (heap_element_lt(get_force(parent , h) , get_force(i , h))) then block { largest := parent ; diff --git a/src/ligo/operators/operators.ml b/src/ligo/operators/operators.ml index 1a3257038..f756ce5ae 100644 --- a/src/ligo/operators/operators.ml +++ b/src/ligo/operators/operators.ml @@ -242,6 +242,16 @@ module Typer = struct * ) * ] *) + let num_2 : typer_predicate = + let aux = fun a b -> + (type_value_eq (a , t_int ()) || type_value_eq (a , t_nat ())) && + (type_value_eq (b , t_int ()) || type_value_eq (b , t_nat ())) in + predicate_2 aux + + let mod_ = "MOD" , 2 , [ + num_2 , constant_2 "MOD" (t_nat ()) ; + ] + let constant_typers = let typer_to_kv : typer -> (string * _) = fun (a, b, c) -> (a, (b, c)) in Map.String.of_list @@ -255,6 +265,11 @@ module Typer = struct ("TIMES_INT" , t_int ()) ; ("TIMES_NAT" , t_nat ()) ; ] ; + same_2 "DIV" [ + ("DIV_INT" , t_int ()) ; + ("DIV_NAT" , t_nat ()) ; + ] ; + mod_ ; sub ; none ; some ; @@ -309,6 +324,9 @@ module Compiler = struct ("SUB_NAT" , simple_binary @@ prim I_SUB) ; ("TIMES_INT" , simple_binary @@ prim I_MUL) ; ("TIMES_NAT" , simple_binary @@ prim I_MUL) ; + ("DIV_INT" , simple_binary @@ seq [prim I_EDIV ; i_assert_some_msg (i_push_string "DIV by 0") ; i_car]) ; + ("DIV_NAT" , simple_binary @@ seq [prim I_EDIV ; i_assert_some_msg (i_push_string "DIV by 0") ; i_car]) ; + ("MOD" , simple_binary @@ seq [prim I_EDIV ; i_assert_some_msg (i_push_string "MOD by 0") ; i_cdr]) ; ("NEG" , simple_unary @@ prim I_NEG) ; ("OR" , simple_binary @@ prim I_OR) ; ("AND" , simple_binary @@ prim I_AND) ; diff --git a/src/ligo/parser/parser.ml b/src/ligo/parser/parser.ml index 70cc7d495..dda8e599c 100644 --- a/src/ligo/parser/parser.ml +++ b/src/ligo/parser/parser.ml @@ -37,11 +37,12 @@ let parse_file (source: string) : AST_Raw.t result = in simple_error str ) - | _ -> + | exn -> let start = Lexing.lexeme_start_p lexbuf in let end_ = Lexing.lexeme_end_p lexbuf in let str = Format.sprintf - "Unrecognized error at \"%s\" from (%d, %d) to (%d, %d). In file \"%s|%s\"\n" + "Unrecognized error (%s) at \"%s\" from (%d, %d) to (%d, %d). In file \"%s|%s\"\n" + (Printexc.to_string exn) (Lexing.lexeme lexbuf) start.pos_lnum (start.pos_cnum - start.pos_bol) end_.pos_lnum (end_.pos_cnum - end_.pos_bol) diff --git a/src/ligo/parser/pascaligo/Lexer.mll b/src/ligo/parser/pascaligo/Lexer.mll index 8185a79d5..9e6c3a2f2 100644 --- a/src/ligo/parser/pascaligo/Lexer.mll +++ b/src/ligo/parser/pascaligo/Lexer.mll @@ -464,7 +464,7 @@ let esc = "\\n" | "\\\"" | "\\\\" | "\\b" let symbol = ';' | ',' | '(' | ')'| '[' | ']' | '{' | '}' | '#' | '|' | "->" | ":=" | '=' | ':' | '<' | "<=" | '>' | ">=" | "=/=" - | '+' | '-' | '*' | '.' | '_' | '^' + | '+' | '-' | '*' | '/' | '.' | '_' | '^' let string = [^'"' '\\' '\n']* (* For strings of #include *) (* RULES *) diff --git a/src/ligo/simplify/pascaligo.ml b/src/ligo/simplify/pascaligo.ml index f25592c0a..62d95b596 100644 --- a/src/ligo/simplify/pascaligo.ml +++ b/src/ligo/simplify/pascaligo.ml @@ -152,6 +152,10 @@ let rec simpl_expression (t:Raw.expr) : ae result = simpl_binop "SUB" c.value | EArith (Mult c) -> simpl_binop "TIMES" c.value + | EArith (Div c) -> + simpl_binop "DIV" c.value + | EArith (Mod c) -> + simpl_binop "MOD" c.value | EArith (Int n) -> let n = Z.to_int @@ snd @@ n.value in ok @@ make_e_a @@ E_literal (Literal_int n) diff --git a/src/ligo/test/heap_tests.ml b/src/ligo/test/heap_tests.ml index 2e6b6fb24..002ae9dbf 100644 --- a/src/ligo/test/heap_tests.ml +++ b/src/ligo/test/heap_tests.ml @@ -113,12 +113,12 @@ let pop () : unit result = let%bind _ = bind_list @@ List.map aux @@ [2 ; 7 ; 12] in - (* simple_fail "display" *) - ok () + simple_fail "display" + (* ok () *) let main = "Heap (End to End)", [ test "is_empty" is_empty ; test "get_top" get_top ; test "pop_switch" pop_switch ; - test "pop" pop ; + (* test "pop" pop ; *) ] diff --git a/src/ligo/test/integration_tests.ml b/src/ligo/test/integration_tests.ml index db4d1d575..e73e084da 100644 --- a/src/ligo/test/integration_tests.ml +++ b/src/ligo/test/integration_tests.ml @@ -103,9 +103,11 @@ let arithmetic () : unit result = ("plus_op", fun n -> (n + 42)) ; ("minus_op", fun n -> (n - 42)) ; ("times_op", fun n -> (n * 42)) ; - ("div_op", fun n -> (n / 2)) ; + (* ("div_op", fun n -> (n / 2)) ; *) ] in let%bind () = expect_n_pos program "int_op" e_a_nat e_a_int in + let%bind () = expect_n_pos program "mod_op" e_a_int (fun n -> e_a_nat (n mod 42)) in + let%bind () = expect_n_pos program "div_op" e_a_int (fun n -> e_a_int (n / 2)) in ok () let unit_expression () : unit result = diff --git a/src/ligo/test/test_helpers.ml b/src/ligo/test/test_helpers.ml index 3012790b3..73ca16e7a 100644 --- a/src/ligo/test/test_helpers.ml +++ b/src/ligo/test/test_helpers.ml @@ -14,12 +14,20 @@ let test name f = open Ast_simplified.Combinators let expect program entry_point input expected = - let error = - let title () = "expect run" in - let content () = Format.asprintf "Entry_point: %s" entry_point in + let%bind result = + let run_error = + let title () = "expect run" in + let content () = Format.asprintf "Entry_point: %s" entry_point in + error title content in + trace run_error @@ + Ligo.easy_run_typed_simplified entry_point program input in + let expect_error = + let title () = "expect result" in + let content () = Format.asprintf "Expected %a, got %a" + Ast_simplified.PP.value expected + Ast_simplified.PP.value result in error title content in - trace error @@ - let%bind result = Ligo.easy_run_typed_simplified entry_point program input in + trace_strong expect_error @@ Ast_simplified.assert_value_eq (expected , result) let expect_evaluate program entry_point expected = @@ -32,10 +40,10 @@ let expect_evaluate program entry_point expected = Ast_simplified.assert_value_eq (expected , result) let expect_n_aux lst program entry_point make_input make_expected = - Format.printf "expect_n aux\n%!" ; let aux n = let input = make_input n in let expected = make_expected n in + trace (simple_error ("expect_n " ^ (string_of_int n))) @@ let result = expect program entry_point input expected in result in