From 4026411e109f49795ebdf5a39037746bc0b4f9a0 Mon Sep 17 00:00:00 2001 From: Lesenechal Remi Date: Mon, 29 Jun 2020 18:31:44 +0200 Subject: [PATCH] get-scope --with-types flag --- src/bin/cli.ml | 13 ++++++++++--- src/main/scopes/misc.ml | 20 ++++++++++---------- src/main/scopes/scopes.ml | 8 ++++---- src/main/scopes/types.ml | 5 +++-- 4 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 49f666e45..1da631669 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -96,6 +96,13 @@ let disable_michelson_typechecking = info ~doc ["disable-michelson-typechecking"] in value @@ flag info +let with_types = + let open Arg in + let info = + let doc = "tries to infer types for all named expressions" in + info ~doc ["with-types"] in + value @@ flag info + let predecessor_timestamp = let open Arg in let info = @@ -485,12 +492,12 @@ let transpile_expression = let get_scope = - let f source_file syntax display_format = + let f source_file syntax display_format with_types = return_result ~display_format Ligo.Scopes.Formatter.scope_format @@ - Ligo.Scopes.scopes source_file syntax + Ligo.Scopes.scopes ~with_types source_file syntax in let term = - Term.(const f $ source_file 0 $ syntax $ display_format) in + Term.(const f $ source_file 0 $ syntax $ display_format $ with_types) in let cmdname = "get-scope" in let doc = "Subcommand: Return the JSON encoded environment for a given file." in (Term.ret term , Term.info ~doc cmdname) diff --git a/src/main/scopes/misc.ml b/src/main/scopes/misc.ml index aab0066d4..bda2fc036 100644 --- a/src/main/scopes/misc.ml +++ b/src/main/scopes/misc.ml @@ -19,8 +19,8 @@ let add_shadowing_def : (int * _ Var.t) -> def -> def_map -> (int * def_map) = let env = Def_map.add definition_id def shadow in (i,env) -let make_v_def_from_core : string -> string -> string -> Ast_core.expression -> Location.t -> Location.t -> def = - fun source_file syntax name exp range body_range -> +let make_v_def_from_core : with_types:bool -> string -> string -> string -> Ast_core.expression -> Location.t -> Location.t -> def = + fun ~with_types source_file syntax name exp range body_range -> let t = to_option @@ let%bind typed_prg,state = Compile.Utils.type_file source_file syntax Env in let env = Ast_typed.program_environment Environment.default typed_prg in @@ -29,27 +29,27 @@ let make_v_def_from_core : string -> string -> string -> Ast_core.expression -> in (* TODO : the source_file is given here but it should only be the declarations seen so far, otherwise nothing will be typed if an error occurs later in the file *) - make_v_def name t range body_range + make_v_def ~with_types name t range body_range -let make_v_def_option_type : string -> string -> string -> Ast_core.type_expression option -> Location.t -> Location.t -> def = - fun source_file syntax name maybe_typed range body_range -> +let make_v_def_option_type : with_types:bool -> string -> string -> string -> Ast_core.type_expression option -> Location.t -> Location.t -> def = + fun ~with_types source_file syntax name maybe_typed range body_range -> match maybe_typed with | Some t -> let t' = to_option @@ let%bind typed_prg,_ = Compile.Utils.type_file source_file syntax Env in let env = Ast_typed.program_environment Environment.default typed_prg in Compile.Of_core.evaluate_type env t in - make_v_def name t' range body_range - | None -> make_v_def name None range body_range + make_v_def ~with_types name t' range body_range + | None -> make_v_def ~with_types name None range body_range let make_v_def_ppx_type : - string -> string -> string -> (Ast_typed.type_expression -> Ast_typed.type_expression) -> + with_types:bool -> string -> string -> string -> (Ast_typed.type_expression -> Ast_typed.type_expression) -> Ast_core.expression -> Location.t -> Location.t -> def = - fun source_file syntax name f exp range body_range -> + fun ~with_types source_file syntax name f exp range body_range -> let t = to_option @@ let%bind typed_prg,state = Compile.Utils.type_file source_file syntax Env in let env = Ast_typed.program_environment Environment.default typed_prg in let%bind (e,_) = Compile.Of_core.compile_expression ~env ~state exp in let v = f e.type_expression in ok v in - make_v_def name t range body_range \ No newline at end of file + make_v_def ~with_types name t range body_range \ No newline at end of file diff --git a/src/main/scopes/scopes.ml b/src/main/scopes/scopes.ml index 739550332..5e9734d7e 100644 --- a/src/main/scopes/scopes.ml +++ b/src/main/scopes/scopes.ml @@ -4,10 +4,10 @@ open Misc module Formatter = Formatter -let scopes : string -> string -> ((def_map * scopes), Main_errors.all) result = fun source_file syntax -> - let make_v_def_from_core = make_v_def_from_core source_file syntax in - let make_v_def_option_type = make_v_def_option_type source_file syntax in - let make_v_def_ppx_type = make_v_def_ppx_type source_file syntax in +let scopes : with_types:bool -> string -> string -> ((def_map * scopes), Main_errors.all) result = fun ~with_types source_file syntax -> + let make_v_def_from_core = make_v_def_from_core ~with_types source_file syntax in + let make_v_def_option_type = make_v_def_option_type ~with_types source_file syntax in + let make_v_def_ppx_type = make_v_def_ppx_type ~with_types source_file syntax in let rec find_scopes' = fun (i,all_defs,env,scopes,lastloc) (e : Ast_core.expression) -> match e.content with diff --git a/src/main/scopes/types.ml b/src/main/scopes/types.ml index 2aa6de886..9d7610daf 100644 --- a/src/main/scopes/types.ml +++ b/src/main/scopes/types.ml @@ -27,8 +27,9 @@ module Definitions = struct | Type t -> t.range | Variable v -> v.range - let make_v_def : string -> Ast_typed.type_expression option -> Location.t -> Location.t -> def = - fun name t range body_range -> + let make_v_def : with_types:bool -> string -> Ast_typed.type_expression option -> Location.t -> Location.t -> def = + fun ~with_types name t range body_range -> + let t = if with_types then t else None in Variable { name ; range ; body_range ; t ; references = None } let make_t_def : string -> Ast_core.declaration Location.wrap -> Ast_core.type_expression -> def =