From 44dd39dcb277c3a58aae913b0f986f76eaa63b7c Mon Sep 17 00:00:00 2001 From: John David Pressman Date: Mon, 20 Jan 2020 19:42:22 -0800 Subject: [PATCH 1/2] Fix examples on front page of ligolang.org, add reminder to change in future --- gitlab-pages/website/core/CodeExamples.js | 42 +++++++++++------------ src/test/contracts/website2.mligo | 12 ++++--- src/test/contracts/website2.religo | 4 +++ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/gitlab-pages/website/core/CodeExamples.js b/gitlab-pages/website/core/CodeExamples.js index 0b636c356..1180aa9c5 100644 --- a/gitlab-pages/website/core/CodeExamples.js +++ b/gitlab-pages/website/core/CodeExamples.js @@ -24,51 +24,51 @@ ${pre}`; const CAMELIGO_EXAMPLE = `${pre}ocaml type storage = int -(* variant defining pseudo multi-entrypoint - actions *) +(* variant defining pseudo multi-entrypoint actions *) + type action = - | Increment of int - | Decrement of int +| Increment of int +| Decrement of int -let add (a: int) (b: int): int = a + b +let add (a: int) (b: int) : int = a + b +let sub (a: int) (b: int) : int = a - b -let subtract (a: int) (b: int): int = a - b +(* real entrypoint that re-routes the flow based on the action provided *) -(* real entrypoint that re-routes the flow - based on the action provided *) -let%entry main(p : action) storage = - let storage = - match p with - | Increment n -> add storage n - | Decrement n -> subtract storage n - in (([] : operation list), storage) +let main (p,s: action * storage) = + let storage = + match p with + | Increment n -> add s n + | Decrement n -> sub s n + in ([] : operation list), storage ${pre}`; const REASONLIGO_EXAMPLE = `${pre}reasonligo type storage = int; -/* variant defining pseudo multi-entrypoint - actions */ +/* variant defining pseudo multi-entrypoint actions */ + type action = | Increment(int) | Decrement(int); let add = (a: int, b: int): int => a + b; +let sub = (a: int, b: int): int => a - b; -let subtract = (a: int, b: int): int => a - b; +/* real entrypoint that re-routes the flow based on the action provided */ -/* real entrypoint that re-routes the flow - based on the action provided */ -let main = (p: action, storage) => { +let main2 = (p: action, storage) => { let storage = switch (p) { | Increment(n) => add(storage, n) - | Decrement(n) => subtract(storage, n) + | Decrement(n) => sub(storage, n) }; ([]: list(operation), storage); }; +let main = (x: (action, storage)) => main2(x[0],x[1]); + ${pre}`; diff --git a/src/test/contracts/website2.mligo b/src/test/contracts/website2.mligo index 17d4c2bc3..77259ad76 100644 --- a/src/test/contracts/website2.mligo +++ b/src/test/contracts/website2.mligo @@ -1,3 +1,5 @@ +(* IF YOU CHANGE THIS, CHANGE THE EXAMPLE ON THE FRONT PAGE OF THE WEBSITE *) + type storage = int (* variant defining pseudo multi-entrypoint actions *) @@ -11,9 +13,11 @@ let sub (a: int) (b: int) : int = a - b (* real entrypoint that re-routes the flow based on the action provided *) -let main (ps: action * storage) = +let main (p,s: action * storage) = let storage = - match ps.0 with - | Increment n -> add ps.1 n - | Decrement n -> sub ps.1 n + match p with + | Increment n -> add s n + | Decrement n -> sub s n in ([] : operation list), storage + +(* IF YOU CHANGE THIS, CHANGE THE EXAMPLE ON THE FRONT PAGE OF THE WEBSITE *) diff --git a/src/test/contracts/website2.religo b/src/test/contracts/website2.religo index 67e5ac125..fbb4a108d 100644 --- a/src/test/contracts/website2.religo +++ b/src/test/contracts/website2.religo @@ -1,3 +1,5 @@ +(* IF YOU CHANGE THIS, CHANGE THE EXAMPLE ON THE FRONT PAGE OF THE WEBSITE *) + type storage = int; /* variant defining pseudo multi-entrypoint actions */ @@ -21,3 +23,5 @@ let main2 = (p: action, storage) => { }; let main = (x: (action, storage)) => main2(x[0],x[1]); + +(* IF YOU CHANGE THIS, CHANGE THE EXAMPLE ON THE FRONT PAGE OF THE WEBSITE *) From 3223f9cfe168117e6a8f77b540f81a61d3fd9f02 Mon Sep 17 00:00:00 2001 From: Sander Date: Tue, 21 Jan 2020 12:24:51 +0000 Subject: [PATCH 2/2] Add sub commands to print different stages. --- src/bin/cli.ml | 55 ++++++++++++++++++++++++++++++ src/bin/expect_tests/help_tests.ml | 32 +++++++++++++++++ src/main/compile/helpers.ml | 38 +++++++++++++++++++++ src/main/compile/of_mini_c.ml | 3 ++ src/main/compile/of_simplified.ml | 3 ++ src/main/compile/of_source.ml | 3 ++ src/main/compile/of_typed.ml | 5 ++- 7 files changed, 138 insertions(+), 1 deletion(-) 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