1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
open Ast

(*
--# Explain Value as JSON
--#
--# Returns a JSON string representation of the explain output.
--#
--# @name explain_json
--# @param x :: Any The value to explain.
--# @return :: String The JSON description.
--# @family explain
--# @seealso explain
--# @export
*)
let register ~eval_call env =
  Env.add "explain_json"
    (make_builtin ~name:"explain_json" 1 (fun args env ->
      match args with
      | [v] ->
          (match Env.find_opt "explain" env with
           | Some explain_fn ->
               let explain_result = eval_call env explain_fn [(None, Ast.mk_expr (Value v))] in
               (match explain_result with
                | VError _ -> explain_result
                | _ -> VString (Utils.value_to_string explain_result))
           | None -> Error.make_error GenericError "Function `explain_json`: explain function not found in environment.")
      | _ -> Error.arity_error_named "explain_json" 1 (List.length args)
    ))
    env