Skip to content

Commit

Permalink
feat(engine/ast_utils): add Debug helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
W95Psp committed Aug 19, 2024
1 parent 537a9b1 commit 34b9977
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions engine/lib/ast_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,23 @@ module Make (F : Features.T) = struct
let item : AST.item -> Ast.Full.item = Stdlib.Obj.magic
end

module Debug : sig
val expr : ?label:string -> AST.expr -> unit
(** Prints an expression pretty-printed as Rust, with its full
AST encoded as JSON, available as a file, so that one can
`jless` or `jq` into it. *)
end = struct
let expr ?(label = "") (e : AST.expr) : unit =
let path = tempfile_path ~suffix:".json" in
Core.Out_channel.write_all path
~data:([%yojson_of: AST.expr] e |> Yojson.Safe.pretty_to_string);
let e = LiftToFullAst.expr e in
"```rust " ^ label ^ "\n" ^ Print_rust.pexpr_str e
^ "\n```\x1b[34m JSON-encoded AST available at \x1b[1m" ^ path
^ "\x1b[0m (hint: use `jless " ^ path ^ "`)"
|> Stdio.prerr_endline
end

let unbox_expr' (next : expr -> expr) (e : expr) : expr =
match e.e with
| App { f = { e = GlobalVar f; _ }; args = [ e ]; _ }
Expand Down
14 changes: 14 additions & 0 deletions engine/lib/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ module MyInt64 = struct

let yojson_of_t (int64 : t) : Yojson.Safe.t = `Intlit (to_string int64)
end

include (
struct
let id = ref 0

let tempfile_path ~suffix =
id := !id + 1;
Core.Filename.(
concat temp_dir_name ("hax-debug-" ^ Int.to_string !id ^ suffix))
end :
sig
val tempfile_path : suffix:string -> string
(** Generates a temporary file path that ends with `suffix` *)
end)

0 comments on commit 34b9977

Please sign in to comment.