From 21904f7e53ee3fcf36595c836e9674974f6ed76a Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 29 Jun 2020 18:04:39 +0200 Subject: [PATCH] get_scope command : tests --- src/bin/expect_tests/get_scope.ml | 123 ++++++++++++++++++ src/bin/expect_tests/help_tests.ml | 6 + src/main/scopes/scopes.ml | 20 ++- .../contracts/get_scope_tests/lambda.mligo | 5 + .../get_scope_tests/lambda_letin.mligo | 9 ++ .../contracts/get_scope_tests/letin.mligo | 10 ++ .../contracts/get_scope_tests/match.mligo | 20 +++ src/test/contracts/get_scope_tests/rec.mligo | 8 ++ .../contracts/get_scope_tests/shadowing.mligo | 10 ++ 9 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 src/bin/expect_tests/get_scope.ml create mode 100644 src/test/contracts/get_scope_tests/lambda.mligo create mode 100644 src/test/contracts/get_scope_tests/lambda_letin.mligo create mode 100644 src/test/contracts/get_scope_tests/letin.mligo create mode 100644 src/test/contracts/get_scope_tests/match.mligo create mode 100644 src/test/contracts/get_scope_tests/rec.mligo create mode 100644 src/test/contracts/get_scope_tests/shadowing.mligo diff --git a/src/bin/expect_tests/get_scope.ml b/src/bin/expect_tests/get_scope.ml new file mode 100644 index 000000000..3bd123fcd --- /dev/null +++ b/src/bin/expect_tests/get_scope.ml @@ -0,0 +1,123 @@ +open Cli_expect + +let gs s = "../../test/contracts/get_scope_tests/"^s + +let%expect_test _ = + run_ligo_good [ "get-scope" ; gs "lambda_letin.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ f5 a0 ] in file "lambda_letin.mligo", line 9, characters 2-7 + [ k4 j2 i1 g3 a0 ] in file "lambda_letin.mligo", line 7, characters 4-21 + [ j2 i1 g3 a0 ] in file "lambda_letin.mligo", line 6, character 4 to line 7, character 21 + [ j2 i1 a0 ] in file "lambda_letin.mligo", line 5, character 4 to line 7, character 21 + [ ] in file "lambda_letin.mligo", line 1, characters 0-9 + + Variable definitions: + (k4 -> k) in file "lambda_letin.mligo", line 6, characters 12-25 + (j2 -> j) in file "lambda_letin.mligo", line 5, character 4 to line 7, character 21 + (i1 -> i) in file "lambda_letin.mligo", line 4, character 32 to line 7, character 21 + (g3 -> g) in file "lambda_letin.mligo", line 5, characters 12-21 + (f5 -> f) in file "lambda_letin.mligo", line 4, characters 8-9 + (b6 -> b) in file "lambda_letin.mligo", line 3, character 0 to line 9, character 7 + (a0 -> a) in file "lambda_letin.mligo", line 1, characters 0-9 + Type definitions: |} ]; + + run_ligo_good [ "get-scope" ; gs "letin.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ d4 c1 a0 ] in file "letin.mligo", line 10, characters 2-11 + [ f3 e2 c1 a0 ] in file "letin.mligo", line 8, characters 4-17 + [ e2 c1 a0 ] in file "letin.mligo", line 7, character 4 to line 8, character 17 + [ c1 a0 ] in file "letin.mligo", line 6, character 4 to line 8, character 17 + [ a0 ] in file "letin.mligo", line 4, character 2 to line 10, character 11 + [ ] in file "letin.mligo", line 1, characters 0-9 + + Variable definitions: + (f3 -> f) in file "letin.mligo", line 7, characters 12-21 + (e2 -> e) in file "letin.mligo", line 6, characters 12-17 + (d4 -> d) in file "letin.mligo", line 6, character 4 to line 8, character 17 + (c1 -> c) in file "letin.mligo", line 4, characters 10-15 + (b5 -> b) in file "letin.mligo", line 3, character 0 to line 10, character 11 + (a0 -> a) in file "letin.mligo", line 1, characters 0-9 + Type definitions: |} ] ; + + run_ligo_good [ "get-scope" ; gs "lambda.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ f3 a0 ] in file "lambda.mligo", line 5, characters 2-7 + [ j2 i1 a0 ] in file "lambda.mligo", line 4, characters 58-63 + [ ] in file "lambda.mligo", line 1, characters 0-9 + + Variable definitions: + (j2 -> j) in file "lambda.mligo", line 4, characters 58-63 + (i1 -> i) in file "lambda.mligo", line 4, characters 31-63 + (f3 -> f) in file "lambda.mligo", line 4, characters 8-9 + (b4 -> b) in file "lambda.mligo", line 3, character 0 to line 5, character 7 + (a0 -> a) in file "lambda.mligo", line 1, characters 0-9 + Type definitions: |} ] ; + + run_ligo_good [ "get-scope" ; gs "match.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ s11 mytype0 c9 b5 a1 ] in file "match.mligo", line 19, characters 16-21 + [ mytype0 c9 b5 a1 ] in file "match.mligo", line 20, characters 12-13 + [ mytype0 d10 c9 b5 a1 ] in file "match.mligo", line 18, characters 22-32 + [ mytype0 c9 b5 a1 ] in file "match.mligo", line 18, characters 9-32 + [ tl8 mytype0 hd7 b5 a1 ] in file "match.mligo", line 15, characters 14-15 + [ mytype0 c6 b5 a1 ] in file "match.mligo", line 14, characters 4-5 + [ mytype0 b5 a1 ] in file "match.mligo", line 13, character 4 to line 14, character 5 + [ mytype0 b5 a1 ] in file "match.mligo", line 11, characters 9-21 + [ y4 mytype0 a1 ] in file "match.mligo", line 8, characters 13-18 + [ x3 mytype0 a1 ] in file "match.mligo", line 7, characters 13-18 + [ mytype0 c2 a1 ] in file "match.mligo", line 6, characters 22-27 + [ mytype0 a1 ] in file "match.mligo", line 6, characters 9-27 + [ mytype0 ] in file "match.mligo", line 3, characters 0-9 + + Variable definitions: + (s11 -> s) in file "match.mligo", line 19, characters 16-21 + (d12 -> d) in file "match.mligo", line 17, character 0 to line 20, character 3 + (d10 -> d) in file "match.mligo", line 18, characters 17-18 + (c9 -> c) in file "match.mligo", line 10, character 0 to line 15, character 3 + (b5 -> b) in file "match.mligo", line 5, character 0 to line 8, character 3 + (a1 -> a) in file "match.mligo", line 3, characters 0-9 + Type definitions: + (mytype0 -> mytype) in file "match.mligo", line 1, characters 0-40 |} ] ; + + run_ligo_good [ "get-scope" ; gs "rec.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ k5 j4 i3 generated2 c1 a0 ] in file "rec.mligo", line 6, characters 4-11 + [ j4 i3 generated2 c1 a0 ] in file "rec.mligo", line 5, character 4 to line 6, character 11 + [ i3 generated2 c1 a0 ] in file "rec.mligo", line 4, characters 36-49 + [ generated2 c1 a0 ] in file "rec.mligo", line 4, characters 36-49 + [ c1 a0 ] + [ ] in file "rec.mligo", line 1, characters 0-9 + + Variable definitions: + (k5 -> k) in file "rec.mligo", line 5, characters 12-21 + (j4 -> j) + (i3 -> i) + (generated2 -> generated) in file "rec.mligo", line 4, characters 36-49 + (c1 -> c) in file "rec.mligo", line 4, character 2 to line 8, character 9 + (b6 -> b) in file "rec.mligo", line 3, character 0 to line 8, character 9 + (a0 -> a) in file "rec.mligo", line 1, characters 0-9 + Type definitions: |} ] ; + + run_ligo_good [ "get-scope" ; gs "shadowing.mligo" ; "--format=dev" ] ; + [%expect {| + Scopes: + [ d4 c1 a0 ] in file "shadowing.mligo", line 10, characters 2-11 + [ e2 c1 a3 ] in file "shadowing.mligo", line 8, characters 4-13 + [ e2 c1 a0 ] in file "shadowing.mligo", line 7, character 4 to line 8, character 13 + [ c1 a0 ] in file "shadowing.mligo", line 6, character 4 to line 8, character 13 + [ a0 ] in file "shadowing.mligo", line 4, character 2 to line 10, character 11 + [ ] in file "shadowing.mligo", line 1, characters 0-9 + + Variable definitions: + (e2 -> e) in file "shadowing.mligo", line 6, characters 12-17 + (d4 -> d) in file "shadowing.mligo", line 6, character 4 to line 8, character 13 + (c1 -> c) in file "shadowing.mligo", line 4, characters 10-15 + (b5 -> b) in file "shadowing.mligo", line 3, character 0 to line 10, character 11 + (a3 -> a) in file "shadowing.mligo", line 7, characters 12-21 + (a0 -> a) in file "shadowing.mligo", line 1, characters 0-9 + Type definitions: |} ] ; \ No newline at end of file diff --git a/src/bin/expect_tests/help_tests.ml b/src/bin/expect_tests/help_tests.ml index 5a3bce777..abb06515d 100644 --- a/src/bin/expect_tests/help_tests.ml +++ b/src/bin/expect_tests/help_tests.ml @@ -40,6 +40,9 @@ let%expect_test _ = evaluate-value Subcommand: Evaluate a given definition. + get-scope + Subcommand: Return the JSON encoded environment for a given file. + interpret Subcommand: Interpret the expression in the context initialized by the provided source file. @@ -140,6 +143,9 @@ let%expect_test _ = evaluate-value Subcommand: Evaluate a given definition. + get-scope + Subcommand: Return the JSON encoded environment for a given file. + interpret Subcommand: Interpret the expression in the context initialized by the provided source file. diff --git a/src/main/scopes/scopes.ml b/src/main/scopes/scopes.ml index 5fc336e81..739550332 100644 --- a/src/main/scopes/scopes.ml +++ b/src/main/scopes/scopes.ml @@ -12,10 +12,22 @@ let scopes : string -> string -> ((def_map * scopes), Main_errors.all) result = let rec find_scopes' = fun (i,all_defs,env,scopes,lastloc) (e : Ast_core.expression) -> match e.content with | E_let_in { let_binder = (n,_) ; rhs ; let_result } -> ( - let (i,all_defs,_, scopes) = find_scopes' (i,all_defs,env,scopes,e.location) rhs in - let (i,env) = add_shadowing_def (i,n) (make_v_def_from_core (get_binder_name n) rhs e.location rhs.location) env in - let all_defs = merge_defs env all_defs in - find_scopes' (i,all_defs,env,scopes,let_result.location) let_result + match rhs.content with + | E_recursive { fun_name ; fun_type ; lambda = { result;_ } } -> ( + (* Note: + It is not entirely true that 'fun_name' is in 'result' scope; because only tail calls are allowed + *) + let def = make_v_def_option_type (get_binder_name fun_name) (Some fun_type) e.location e.location in + let (i,env) = add_shadowing_def (i,fun_name) def env in + find_scopes' (i,all_defs,env,scopes,result.location) result + ) + | _ -> ( + (*TODO : n needs location and should be used bellow in union with rhs *) + let (i,all_defs,_, scopes) = find_scopes' (i,all_defs,env,scopes,e.location) rhs in + let (i,env) = add_shadowing_def (i,n) (make_v_def_from_core (get_binder_name n) rhs rhs.location rhs.location) env in + let all_defs = merge_defs env all_defs in + find_scopes' (i,all_defs,env,scopes,let_result.location) let_result + ) ) | E_lambda { binder ; input_type ; output_type = _ ; result } -> ( let (i,env) = add_shadowing_def (i,binder) (make_v_def_option_type (get_binder_name binder) input_type result.location result.location) env in diff --git a/src/test/contracts/get_scope_tests/lambda.mligo b/src/test/contracts/get_scope_tests/lambda.mligo new file mode 100644 index 000000000..19db8b4c7 --- /dev/null +++ b/src/test/contracts/get_scope_tests/lambda.mligo @@ -0,0 +1,5 @@ +let a = 1 + +let b = + let f : (int-> int -> int) = fun (i : int) (j : int) -> j + i in + f 2 3 \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/lambda_letin.mligo b/src/test/contracts/get_scope_tests/lambda_letin.mligo new file mode 100644 index 000000000..4c4015683 --- /dev/null +++ b/src/test/contracts/get_scope_tests/lambda_letin.mligo @@ -0,0 +1,9 @@ +let a = 1 + +let b = + let f : (int -> int -> int) = fun (i : int) (j : int) -> + let g = j + i + a in + let k = j + i + a + g in + j + i + a + g + k + in + f 2 a \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/letin.mligo b/src/test/contracts/get_scope_tests/letin.mligo new file mode 100644 index 000000000..43d61dfd1 --- /dev/null +++ b/src/test/contracts/get_scope_tests/letin.mligo @@ -0,0 +1,10 @@ +let a = 1 + +let b = + let c = 0 + a in + let d = + let e = a + c in + let f = a + c + e in + a + c + e + f + in + a + c + d \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/match.mligo b/src/test/contracts/get_scope_tests/match.mligo new file mode 100644 index 000000000..5ae94b4c6 --- /dev/null +++ b/src/test/contracts/get_scope_tests/match.mligo @@ -0,0 +1,20 @@ +type mytype = Foo of int | Bar of string + +let a = 1 + +let b = + match (let c = a in Foo c) with + | Foo x -> x + a + | Bar y -> 1 + a + +let c = + match ([ 1 ; 2 ;3 ]) with + | [] -> + let c = 2 in + a + | hd::tl -> 2 + +let d = + match (let d = 1 in Some (a+d)) with + | Some (s) -> s + a + | None -> a \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/rec.mligo b/src/test/contracts/get_scope_tests/rec.mligo new file mode 100644 index 000000000..4d95f0a55 --- /dev/null +++ b/src/test/contracts/get_scope_tests/rec.mligo @@ -0,0 +1,8 @@ +let a = 1 + +let b = + let rec c : int*int -> int = fun ((i,j):int*int) -> + let k = i + j + a in + c (k,1) + in + c (a,2) \ No newline at end of file diff --git a/src/test/contracts/get_scope_tests/shadowing.mligo b/src/test/contracts/get_scope_tests/shadowing.mligo new file mode 100644 index 000000000..c4ceae627 --- /dev/null +++ b/src/test/contracts/get_scope_tests/shadowing.mligo @@ -0,0 +1,10 @@ +let a = 1 + +let b = + let c = 0 + a in + let d = + let e = a + c in + let a = a + c + e in + a + c + e + in + a + c + d \ No newline at end of file