get_scope command: fix recursive handling

This commit is contained in:
Lesenechal Remi 2020-07-01 01:21:33 +02:00
parent 8a551114c3
commit 50a1e48f28
3 changed files with 90 additions and 88 deletions

View File

@ -6,117 +6,121 @@ let%expect_test _ =
run_ligo_good [ "get-scope" ; gs "lambda_letin.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "lambda_letin.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ f5 a0 ] in file "lambda_letin.mligo", line 9, characters 2-7 [ f#5 a#0 ] 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 [ k#4 j#2 i#1 g#3 a#0 ] 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 [ j#2 i#1 g#3 a#0 ] 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 [ j#2 i#1 a#0 ] in file "lambda_letin.mligo", line 5, character 4 to line 7, character 21
[ ] in file "lambda_letin.mligo", line 1, characters 0-9 [ ] in file "lambda_letin.mligo", line 1, characters 0-9
Variable definitions: Variable definitions:
(k4 -> k) in file "lambda_letin.mligo", line 6, characters 12-25 (k#4 -> 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 (j#2 -> 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 (i#1 -> 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 (g#3 -> g) in file "lambda_letin.mligo", line 5, characters 12-21
(f5 -> f) in file "lambda_letin.mligo", line 4, characters 8-9 (f#5 -> 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 (b#6 -> 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 (a#0 -> a) in file "lambda_letin.mligo", line 1, characters 0-9
Type definitions: |} ]; Type definitions: |} ];
run_ligo_good [ "get-scope" ; gs "letin.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "letin.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ d4 c1 a0 ] in file "letin.mligo", line 10, characters 2-11 [ d#4 c#1 a#0 ] in file "letin.mligo", line 10, characters 2-11
[ f3 e2 c1 a0 ] in file "letin.mligo", line 8, characters 4-17 [ f#3 e#2 c#1 a#0 ] 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 [ e#2 c#1 a#0 ] 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 [ c#1 a#0 ] 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 [ a#0 ] in file "letin.mligo", line 4, character 2 to line 10, character 11
[ ] in file "letin.mligo", line 1, characters 0-9 [ ] in file "letin.mligo", line 1, characters 0-9
Variable definitions: Variable definitions:
(f3 -> f) in file "letin.mligo", line 7, characters 12-21 (f#3 -> f) in file "letin.mligo", line 7, characters 12-21
(e2 -> e) in file "letin.mligo", line 6, characters 12-17 (e#2 -> 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 (d#4 -> 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 (c#1 -> 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 (b#5 -> 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 (a#0 -> a) in file "letin.mligo", line 1, characters 0-9
Type definitions: |} ] ; Type definitions: |} ] ;
run_ligo_good [ "get-scope" ; gs "lambda.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "lambda.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ f3 a0 ] in file "lambda.mligo", line 5, characters 2-7 [ f#3 a#0 ] in file "lambda.mligo", line 5, characters 2-7
[ j2 i1 a0 ] in file "lambda.mligo", line 4, characters 58-63 [ j#2 i#1 a#0 ] in file "lambda.mligo", line 4, characters 58-63
[ ] in file "lambda.mligo", line 1, characters 0-9 [ ] in file "lambda.mligo", line 1, characters 0-9
Variable definitions: Variable definitions:
(j2 -> j) in file "lambda.mligo", line 4, characters 58-63 (j#2 -> j) in file "lambda.mligo", line 4, characters 58-63
(i1 -> i) in file "lambda.mligo", line 4, characters 31-63 (i#1 -> i) in file "lambda.mligo", line 4, characters 31-63
(f3 -> f) in file "lambda.mligo", line 4, characters 8-9 (f#3 -> 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 (b#4 -> 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 (a#0 -> a) in file "lambda.mligo", line 1, characters 0-9
Type definitions: |} ] ; Type definitions: |} ] ;
run_ligo_good [ "get-scope" ; gs "match.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "match.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ s11 mytype0 c9 b5 a1 ] in file "match.mligo", line 19, characters 16-21 [ s#11 mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 19, characters 16-21
[ mytype0 c9 b5 a1 ] in file "match.mligo", line 20, characters 12-13 [ mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 20, characters 12-13
[ mytype0 d10 c9 b5 a1 ] in file "match.mligo", line 18, characters 22-32 [ mytype#0 d#10 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 22-32
[ mytype0 c9 b5 a1 ] in file "match.mligo", line 18, characters 9-32 [ mytype#0 c#9 b#5 a#1 ] in file "match.mligo", line 18, characters 9-32
[ tl8 mytype0 hd7 b5 a1 ] in file "match.mligo", line 15, characters 14-15 [ tl#8 mytype#0 hd#7 b#5 a#1 ] in file "match.mligo", line 15, characters 14-15
[ mytype0 c6 b5 a1 ] in file "match.mligo", line 14, characters 4-5 [ mytype#0 c#6 b#5 a#1 ] 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 [ mytype#0 b#5 a#1 ] 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 [ mytype#0 b#5 a#1 ] in file "match.mligo", line 11, characters 9-21
[ y4 mytype0 a1 ] in file "match.mligo", line 8, characters 13-18 [ y#4 mytype#0 a#1 ] in file "match.mligo", line 8, characters 13-18
[ x3 mytype0 a1 ] in file "match.mligo", line 7, characters 13-18 [ x#3 mytype#0 a#1 ] in file "match.mligo", line 7, characters 13-18
[ mytype0 c2 a1 ] in file "match.mligo", line 6, characters 22-27 [ mytype#0 c#2 a#1 ] in file "match.mligo", line 6, characters 22-27
[ mytype0 a1 ] in file "match.mligo", line 6, characters 9-27 [ mytype#0 a#1 ] in file "match.mligo", line 6, characters 9-27
[ mytype0 ] in file "match.mligo", line 3, characters 0-9 [ mytype#0 ] in file "match.mligo", line 3, characters 0-9
Variable definitions: Variable definitions:
(s11 -> s) in file "match.mligo", line 19, characters 16-21 (s#11 -> 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 (d#12 -> 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 (d#10 -> 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 (c#9 -> 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 (b#5 -> 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 (a#1 -> a) in file "match.mligo", line 3, characters 0-9
Type definitions: Type definitions:
(mytype0 -> mytype) in file "match.mligo", line 1, characters 0-40 |} ] ; (mytype#0 -> mytype) in file "match.mligo", line 1, characters 0-40 |} ] ;
run_ligo_good [ "get-scope" ; gs "rec.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "rec.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ k4 j3 i2 c1 a0 ] in file "rec.mligo", line 6, characters 4-11 [ c#5 b#6 a#0 ] in file "rec.mligo", line 9, characters 2-10
[ j3 i2 c1 a0 ] in file "rec.mligo", line 5, character 4 to line 6, character 11 [ c#5 a#0 ] in file "rec.mligo", line 8, character 2 to line 9, character 10
[ i2 c1 a0 ] in file "rec.mligo", line 4, characters 36-49 [ k#4 j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 6, characters 4-11
[ c1 a0 ] in file "rec.mligo", line 4, characters 36-49 [ j#3 i#2 c#1 a#0 ] in file "rec.mligo", line 5, character 4 to line 6, character 11
[ c1 a0 ] [ i#2 c#1 a#0 ] in file "rec.mligo", line 4, characters 36-49
[ c#1 a#0 ] in file "rec.mligo", line 4, characters 36-49
[ c#1 a#0 ]
[ ] in file "rec.mligo", line 1, characters 0-9 [ ] in file "rec.mligo", line 1, characters 0-9
Variable definitions: Variable definitions:
(k4 -> k) in file "rec.mligo", line 5, characters 12-21 (k#4 -> k) in file "rec.mligo", line 5, characters 12-21
(j3 -> j) (j#3 -> j)
(i2 -> i) (i#2 -> i)
(c1 -> c) in file "rec.mligo", line 4, character 2 to line 8, character 9 (c#5 -> c) in file "rec.mligo", line 4, characters 12-13
(b5 -> b) in file "rec.mligo", line 3, character 0 to line 8, character 9 (c#1 -> c) in file "rec.mligo", line 4, characters 12-13
(a0 -> a) in file "rec.mligo", line 1, characters 0-9 (b#7 -> b) in file "rec.mligo", line 3, character 0 to line 9, character 10
(b#6 -> b) in file "rec.mligo", line 8, characters 10-11
(a#0 -> a) in file "rec.mligo", line 1, characters 0-9
Type definitions: |} ] ; Type definitions: |} ] ;
run_ligo_good [ "get-scope" ; gs "shadowing.mligo" ; "--format=dev" ] ; run_ligo_good [ "get-scope" ; gs "shadowing.mligo" ; "--format=dev" ] ;
[%expect {| [%expect {|
Scopes: Scopes:
[ d4 c1 a0 ] in file "shadowing.mligo", line 10, characters 2-11 [ d#4 c#1 a#0 ] in file "shadowing.mligo", line 10, characters 2-11
[ e2 c1 a3 ] in file "shadowing.mligo", line 8, characters 4-13 [ e#2 c#1 a#3 ] 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 [ e#2 c#1 a#0 ] 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 [ c#1 a#0 ] 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 [ a#0 ] in file "shadowing.mligo", line 4, character 2 to line 10, character 11
[ ] in file "shadowing.mligo", line 1, characters 0-9 [ ] in file "shadowing.mligo", line 1, characters 0-9
Variable definitions: Variable definitions:
(e2 -> e) in file "shadowing.mligo", line 6, characters 12-17 (e#2 -> 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 (d#4 -> 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 (c#1 -> 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 (b#5 -> 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 (a#3 -> a) in file "shadowing.mligo", line 7, characters 12-21
(a0 -> a) in file "shadowing.mligo", line 1, characters 0-9 (a#0 -> a) in file "shadowing.mligo", line 1, characters 0-9
Type definitions: |} ] ; Type definitions: |} ] ;

View File

@ -12,22 +12,19 @@ let scopes : with_types:bool -> string -> string -> ((def_map * scopes), Main_er
let rec find_scopes' = fun (i,all_defs,env,scopes,lastloc) (e : Ast_core.expression) -> let rec find_scopes' = fun (i,all_defs,env,scopes,lastloc) (e : Ast_core.expression) ->
match e.content with match e.content with
| E_let_in { let_binder = (fn,_) ; rhs ; let_result } -> ( | E_let_in { let_binder = (fn,_) ; rhs ; let_result } -> (
match rhs.content with (*TODO : n needs location and should be used bellow in union with rhs *)
| E_recursive { fun_name ; fun_type ; lambda = { result;_ } } -> ( let (i,all_defs,_, scopes) = find_scopes' (i,all_defs,env,scopes,e.location) rhs in
(* Note: let (i,env) = add_shadowing_def (i,fn) (make_v_def_from_core fn rhs rhs.location rhs.location) env in
It is not entirely true that 'fun_name' is in 'result' scope; because only tail calls are allowed let all_defs = merge_defs env all_defs in
*) find_scopes' (i,all_defs,env,scopes,let_result.location) let_result
let def = make_v_def_option_type fun_name (Some fun_type) e.location e.location in )
let (i,env) = add_shadowing_def (i,fun_name) def env in | E_recursive { fun_name ; fun_type ; lambda = { result;_ } } -> (
find_scopes' (i,all_defs,env,scopes,result.location) result (* Note:
) It is not entirely true that 'fun_name' is in 'result' scope; because only tail calls are allowed
| _ -> ( *)
(*TODO : n needs location and should be used bellow in union with rhs *) let def = make_v_def_option_type fun_name (Some fun_type) e.location e.location in
let (i,all_defs,_, scopes) = find_scopes' (i,all_defs,env,scopes,e.location) rhs in let (i,env) = add_shadowing_def (i,fun_name) def env in
let (i,env) = add_shadowing_def (i,fn) (make_v_def_from_core fn rhs rhs.location rhs.location) env in find_scopes' (i,all_defs,env,scopes,result.location) result
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 } -> ( | E_lambda { binder ; input_type ; output_type = _ ; result } -> (
let (i,env) = add_shadowing_def (i,binder) (make_v_def_option_type binder input_type result.location result.location) env in let (i,env) = add_shadowing_def (i,binder) (make_v_def_option_type binder input_type result.location result.location) env in

View File

@ -5,4 +5,5 @@ let b =
let k = i + j + a in let k = i + j + a in
c (k,1) c (k,1)
in in
c (a,2) let b = 2 in
c (a, b)