* [ligo pretty-print foo.mligo] works.

* [ligo pretty-print foo.religo] is a transpiler (for now) and needs
    more work.

  * Renamed functions *_ast into *_cst. One day, module AST should be
    renamed CST for clarity and consistency.
This commit is contained in:
Christian Rinderknecht 2020-05-25 20:29:48 +02:00
parent 041a48bc7b
commit 320e31cb41
9 changed files with 73 additions and 21 deletions

View File

@ -159,10 +159,22 @@ let preprocess =
let doc = "Subcommand: Preprocess the source file.\nWarning: Intended for development of LIGO and can break at any time." in
(Term.ret term, Term.info ~doc cmdname)
let pretty_print =
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 @@ Buffer.contents pp
) in
let term = Term.(const f $ source_file 0 $ syntax $ display_format) in
let cmdname = "pretty-print" in
let doc = "Subcommand: Pretty-print the source file."
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
let%bind pp = Compile.Of_source.pretty_print_cst source_file (Syntax_name syntax) in
ok @@ Format.asprintf "%s \n" (Buffer.contents pp)
)
in
@ -489,5 +501,6 @@ let run ?argv () =
print_ast_typed ;
print_mini_c ;
list_declarations ;
preprocess
preprocess;
pretty_print
]

View File

@ -129,7 +129,7 @@ let parsify_string syntax source =
let%bind applied = Self_ast_imperative.all_program parsified
in ok applied
let pretty_print_pascaligo source =
let pretty_print_pascaligo_cst source =
let%bind ast = Parser.Pascaligo.parse_file source in
let buffer = Buffer.create 59 in
let state =
@ -137,10 +137,10 @@ let pretty_print_pascaligo source =
~offsets:true
~mode:`Byte
~buffer in
Parser_pascaligo.ParserLog.pp_ast state ast;
Parser_pascaligo.ParserLog.pp_cst state ast;
ok buffer
let pretty_print_cameligo source =
let pretty_print_cameligo_cst source =
let%bind ast = Parser.Cameligo.parse_file source in
let buffer = Buffer.create 59 in
let state = (* TODO: Should flow from the CLI *)
@ -148,10 +148,10 @@ let pretty_print_cameligo source =
~offsets:true
~mode:`Point
~buffer in
Parser_cameligo.ParserLog.pp_ast state ast;
Parser_cameligo.ParserLog.pp_cst state ast;
ok buffer
let pretty_print_reasonligo source =
let pretty_print_reasonligo_cst source =
let%bind ast = Parser.Reasonligo.parse_file source in
let buffer = Buffer.create 59 in
let state = (* TODO: Should flow from the CLI *)
@ -159,16 +159,16 @@ let pretty_print_reasonligo source =
~offsets:true
~mode:`Point
~buffer in
Parser_cameligo.ParserLog.pp_ast state ast;
Parser_cameligo.ParserLog.pp_cst state ast;
ok buffer
let pretty_print syntax source =
let pretty_print_cst syntax source =
let%bind v_syntax =
syntax_to_variant syntax (Some source) in
match v_syntax with
PascaLIGO -> pretty_print_pascaligo source
| CameLIGO -> pretty_print_cameligo source
| ReasonLIGO -> pretty_print_reasonligo source
PascaLIGO -> pretty_print_pascaligo_cst source
| CameLIGO -> pretty_print_cameligo_cst source
| ReasonLIGO -> pretty_print_reasonligo_cst source
let preprocess_pascaligo = Parser.Pascaligo.preprocess
@ -183,3 +183,39 @@ let preprocess syntax source =
PascaLIGO -> preprocess_pascaligo source
| CameLIGO -> preprocess_cameligo source
| ReasonLIGO -> preprocess_reasonligo source
let pretty_print_pascaligo source =
let%bind ast = Parser.Pascaligo.parse_file source in
let () = ignore ast in (* TODO *)
let buffer = Buffer.create 131
in Trace.ok buffer
let pretty_print_cameligo source =
let%bind ast = Parser.Cameligo.parse_file source in
let doc = Parser_cameligo.Pretty.make ast in
let buffer = Buffer.create 131 in
let width =
match Terminal_size.get_columns () with
None -> 60
| Some c -> c in
let () = PPrint.ToBuffer.pretty 1.0 width buffer doc
in Trace.ok buffer
let pretty_print_reasonligo source =
let%bind ast = Parser.Reasonligo.parse_file source in
let doc = Parser_cameligo.Pretty.make ast in (* TODO *)
let buffer = Buffer.create 131 in
let width =
match Terminal_size.get_columns () with
None -> 60
| Some c -> c in
let () = PPrint.ToBuffer.pretty 1.0 width buffer doc
in Trace.ok buffer
let pretty_print syntax source =
let%bind v_syntax =
syntax_to_variant syntax (Some source) in
match v_syntax with
PascaLIGO -> pretty_print_pascaligo source
| CameLIGO -> pretty_print_cameligo source
| ReasonLIGO -> pretty_print_reasonligo source

View File

@ -19,8 +19,11 @@ let compile_contract_input : string -> string -> v_syntax -> Ast_imperative.expr
let%bind (storage,parameter) = bind_map_pair (compile_expression syntax) (storage,parameter) in
ok @@ Ast_imperative.e_pair storage parameter
let pretty_print source_filename syntax =
Helpers.pretty_print syntax source_filename
let pretty_print_cst source_filename syntax =
Helpers.pretty_print_cst syntax source_filename
let preprocess source_filename syntax =
Helpers.preprocess syntax source_filename
let pretty_print source_filename syntax =
Helpers.pretty_print syntax source_filename

View File

@ -628,7 +628,7 @@ let pp_verbatim state {value=name; region} =
let pp_loc_node state name region =
pp_ident state {value=name; region}
let rec pp_ast state {decl; _} =
let rec pp_cst state {decl; _} =
let apply len rank =
pp_declaration (state#pad len rank) in
let decls = Utils.nseq_to_list decl in

View File

@ -27,5 +27,5 @@ val expr_to_string :
(** {1 Pretty-printing of AST nodes} *)
val pp_ast : state -> AST.t -> unit
val pp_cst : state -> AST.t -> unit
val pp_expr : state -> AST.expr -> unit

View File

@ -855,7 +855,7 @@ let pp_verbatim state {value=name; region} =
let pp_loc_node state name region =
pp_ident state {value=name; region}
let rec pp_ast state {decl; _} =
let rec pp_cst state {decl; _} =
let apply len rank =
pp_declaration (state#pad len rank) in
let decls = Utils.nseq_to_list decl in

View File

@ -33,5 +33,5 @@ val instruction_to_string :
(** {1 Pretty-printing of AST nodes} *)
val pp_ast : state -> AST.t -> unit
val pp_cst : state -> AST.t -> unit
val pp_expr : state -> AST.expr -> unit

View File

@ -32,7 +32,7 @@ module type Printer =
val mk_state :
offsets:bool -> mode:[`Point|`Byte] -> buffer:Buffer.t -> state
val pp_ast : state -> ast -> unit
val pp_cst : state -> ast -> unit
val pp_expr : state -> expr -> unit
val print_tokens : state -> ast -> unit
val print_expr : state -> expr -> unit
@ -146,7 +146,7 @@ module Make (Lexer: Lexer.S)
if SSet.mem "ast" SubIO.options#verbose then
begin
Buffer.clear output;
ParserLog.pp_ast state ast;
ParserLog.pp_cst state ast;
Buffer.output_buffer stdout output
end
in flush_all (); close (); Ok ast

View File

@ -36,7 +36,7 @@ module type Printer =
val mk_state :
offsets:bool -> mode:[`Point|`Byte] -> buffer:Buffer.t -> state
val pp_ast : state -> ast -> unit
val pp_cst : state -> ast -> unit
val pp_expr : state -> expr -> unit
val print_tokens : state -> ast -> unit
val print_expr : state -> expr -> unit