diff options
Diffstat (limited to 'discocaml/discocaml.ml')
-rw-r--r-- | discocaml/discocaml.ml | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/discocaml/discocaml.ml b/discocaml/discocaml.ml index df5cef3..e244e26 100644 --- a/discocaml/discocaml.ml +++ b/discocaml/discocaml.ml @@ -14,7 +14,7 @@ let command_of_yojson = function type request = { expr : string; command : command } [@@deriving of_yojson { exn = true }] -type response_expr = { expr : string; has_redex : bool } +type response_expr = { expr : string; has_redex : bool; note : string option } [@@deriving to_yojson { exn = true }] type response = @@ -27,8 +27,9 @@ let%expect_test _ = let%expect_test _ = Yojson.Safe.to_channel stdout - (response_to_yojson (`Expr { expr = "foo"; has_redex = false })); - [%expect {| ["Expr",{"expr":"foo","has_redex":false}] |}] + (response_to_yojson + (`Expr { expr = "foo"; has_redex = false; note = None })); + [%expect {| ["Expr",{"expr":"foo","has_redex":false,"note":null}] |}] let handle_request { expr; command } : response = try @@ -36,13 +37,13 @@ let handle_request { expr; command } : response = let expr = Parse.expression buf in let expr = expr_of_parsetree expr in - let expr_response (expr : expr ast) : response = - let buf = Buffer.create 16 in - (let fmt = Format.formatter_of_buffer buf in - Pprintast.expression fmt (parsetree_of_expr expr); - Format.pp_print_flush fmt ()); - let has_redex = Option.is_some (Eval.find_redex_cbn expr) in - `Expr { expr = Buffer.contents buf; has_redex } + let expr_response ?(note : string option) (expr : expr ast) : response = + `Expr + { + expr = show_expr expr; + has_redex = Option.is_some (Eval.find_redex_cbn expr); + note; + } in let step_with (find_redex : expr ast -> expr index option) (expr : expr ast) @@ -54,10 +55,15 @@ let handle_request { expr; command } : response = match command with | `Parse -> expr_response expr - | `StepCBN -> expr_response (step_with Eval.find_redex_cbn expr) - | `StepCBV -> expr_response (step_with Eval.find_redex_cbv expr) + | `StepCBN -> + expr_response ~note:"After one CBN step:" + (step_with Eval.find_redex_cbn expr) + | `StepCBV -> + expr_response ~note:"After one CBV step:" + (step_with Eval.find_redex_cbv expr) | `DrawTree -> `Graphviz (Draw_tree.draw_tree expr) | `RunCBN | `RunCBV -> failwith "not implemented" with + | Eval.NotARedex expr -> `Error ("not a redex: " ^ show_expr expr) | Failure msg -> `Error msg | exn -> `Error ("uncaught exception: " ^ Printexc.to_string exn) |