diff --git a/src/bin/cli.ml b/src/bin/cli.ml index d8be0d864..edb571cd0 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -138,6 +138,57 @@ let compile_file = let doc = "Subcommand: compile a contract." in (Term.ret term , Term.info ~doc cmdname) +let print_cst = + let f source_file syntax display_format = ( + toplevel ~display_format @@ + let%bind pp = Compile.Of_source.pretty_print source_file (Syntax_name syntax) in + ok @@ Format.asprintf "%s \n" (Buffer.contents pp) + ) + in + let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let cmdname = "print-cst" in + let doc = "Subcommand: print the cst. Warning: intended for development of LIGO and can break at any time." in + (Term.ret term, Term.info ~doc cmdname) + +let print_ast = + let f source_file syntax display_format = ( + toplevel ~display_format @@ + let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in + ok @@ Format.asprintf "%a\n" Compile.Of_simplified.pretty_print simplified + ) + in + let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let cmdname = "print-ast" in + let doc = "Subcommand: print the ast. Warning: intended for development of LIGO and can break at any time." in + (Term.ret term, Term.info ~doc cmdname) + +let print_typed_ast = + let f source_file syntax display_format = ( + toplevel ~display_format @@ + let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in + let%bind typed,_ = Compile.Of_simplified.compile simplified in + ok @@ Format.asprintf "%a\n" Compile.Of_typed.pretty_print typed + ) + in + let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let cmdname = "print-typed-ast" in + let doc = "Subcommand: print the typed ast. Warning: intended for development of LIGO and can break at any time." in + (Term.ret term, Term.info ~doc cmdname) + +let print_mini_c = + let f source_file syntax display_format = ( + toplevel ~display_format @@ + let%bind simplified = Compile.Of_source.compile source_file (Syntax_name syntax) in + let%bind typed,_ = Compile.Of_simplified.compile simplified in + let%bind mini_c = Compile.Of_typed.compile typed in + ok @@ Format.asprintf "%a\n" Compile.Of_mini_c.pretty_print mini_c + ) + in + let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let cmdname = "print-mini-c" in + let doc = "Subcommand: print mini c. Warning: intended for development of LIGO and can break at any time." in + (Term.ret term, Term.info ~doc cmdname) + let measure_contract = let f source_file entry_point syntax display_format = toplevel ~display_format @@ @@ -371,4 +422,8 @@ let run ?argv () = run_function ; evaluate_value ; dump_changelog ; + print_cst ; + print_ast ; + print_typed_ast ; + print_mini_c ] diff --git a/src/bin/expect_tests/help_tests.ml b/src/bin/expect_tests/help_tests.ml index b42ac2c8b..b385abd14 100644 --- a/src/bin/expect_tests/help_tests.ml +++ b/src/bin/expect_tests/help_tests.ml @@ -47,6 +47,22 @@ let%expect_test _ = measure-contract Subcommand: measure a contract's compiled size in bytes. + print-ast + Subcommand: print the ast. Warning: intended for development of + LIGO and can break at any time. + + print-cst + Subcommand: print the cst. Warning: intended for development of + LIGO and can break at any time. + + print-mini-c + Subcommand: print mini c. Warning: intended for development of + LIGO and can break at any time. + + print-typed-ast + Subcommand: print the typed ast. Warning: intended for development + of LIGO and can break at any time. + run-function Subcommand: run a function with the given parameter. @@ -104,6 +120,22 @@ let%expect_test _ = measure-contract Subcommand: measure a contract's compiled size in bytes. + print-ast + Subcommand: print the ast. Warning: intended for development of + LIGO and can break at any time. + + print-cst + Subcommand: print the cst. Warning: intended for development of + LIGO and can break at any time. + + print-mini-c + Subcommand: print mini c. Warning: intended for development of + LIGO and can break at any time. + + print-typed-ast + Subcommand: print the typed ast. Warning: intended for development + of LIGO and can break at any time. + run-function Subcommand: run a function with the given parameter. diff --git a/src/main/compile/helpers.ml b/src/main/compile/helpers.ml index 479b4cd33..317b92736 100644 --- a/src/main/compile/helpers.ml +++ b/src/main/compile/helpers.ml @@ -133,3 +133,41 @@ let parsify_string = fun (syntax : v_syntax) source_filename -> let%bind parsified = parsify source_filename in let%bind applied = Self_ast_simplified.all_program parsified in ok applied + +let pretty_print_pascaligo = fun source -> + let%bind ast = Parser.Pascaligo.parse_file source in + let buffer = Buffer.create 59 in + let state = Parser.Pascaligo.ParserLog.mk_state + ~offsets:true + ~mode:`Byte + ~buffer in + Parser.Pascaligo.ParserLog.pp_ast state ast; + ok buffer + +let pretty_print_cameligo = fun source -> + let%bind ast = Parser.Cameligo.parse_file source in + let buffer = Buffer.create 59 in + let state = Parser.Cameligo.ParserLog.mk_state + ~offsets:true + ~mode:`Byte + ~buffer in + Parser.Cameligo.ParserLog.pp_ast state ast; + ok buffer + +let pretty_print_reasonligo = fun source -> + let%bind ast = Parser.Reasonligo.parse_file source in + let buffer = Buffer.create 59 in + let state = Parser.Reasonligo.ParserLog.mk_state + ~offsets:true + ~mode:`Byte + ~buffer in + Parser.Reasonligo.ParserLog.pp_ast state ast; + ok buffer + +let pretty_print = fun syntax source_filename -> + let%bind v_syntax = syntax_to_variant syntax (Some source_filename) in + (match v_syntax with + | Pascaligo -> pretty_print_pascaligo + | Cameligo -> pretty_print_cameligo + | ReasonLIGO -> pretty_print_reasonligo) + source_filename \ No newline at end of file diff --git a/src/main/compile/of_mini_c.ml b/src/main/compile/of_mini_c.ml index be27f0f6b..3b48d3921 100644 --- a/src/main/compile/of_mini_c.ml +++ b/src/main/compile/of_mini_c.ml @@ -32,3 +32,6 @@ let aggregate_and_compile_contract = fun (program : Types.program) name -> let aggregate_and_compile_expression = fun program exp -> aggregate_and_compile program (ExpressionForm exp) + +let pretty_print program = + Mini_c.PP.program program \ No newline at end of file diff --git a/src/main/compile/of_simplified.ml b/src/main/compile/of_simplified.ml index 59df4d647..072243a9c 100644 --- a/src/main/compile/of_simplified.ml +++ b/src/main/compile/of_simplified.ml @@ -18,3 +18,6 @@ let apply (entry_point : string) (param : Ast_simplified.expression) : Ast_simpl { expression = Ast_simplified.E_application (entry_point_var, param) ; location = Virtual "generated application" } in ok applied + +let pretty_print formatter (program : Ast_simplified.program) = + Ast_simplified.PP.program formatter program \ No newline at end of file diff --git a/src/main/compile/of_source.ml b/src/main/compile/of_source.ml index 0c43d7d45..3a075ac9e 100644 --- a/src/main/compile/of_source.ml +++ b/src/main/compile/of_source.ml @@ -18,3 +18,6 @@ let compile_contract_input : string -> string -> v_syntax -> Ast_simplified.expr fun storage parameter syntax -> let%bind (storage,parameter) = bind_map_pair (compile_expression syntax) (storage,parameter) in ok @@ Ast_simplified.e_pair storage parameter + +let pretty_print source_filename syntax = + Helpers.pretty_print syntax source_filename \ No newline at end of file diff --git a/src/main/compile/of_typed.ml b/src/main/compile/of_typed.ml index 0aa705405..a69f32c9d 100644 --- a/src/main/compile/of_typed.ml +++ b/src/main/compile/of_typed.ml @@ -21,4 +21,7 @@ let assert_equal_contract_type : check_type -> string -> Ast_typed.program -> As ) | _ -> dummy_fail ) - | _ -> dummy_fail ) \ No newline at end of file + | _ -> dummy_fail ) + +let pretty_print ppf program = + Ast_typed.PP.program ppf program \ No newline at end of file