From 6bea013cc69b8d11c24fd1e6041677e8e9310f66 Mon Sep 17 00:00:00 2001 From: Nathan Ringo Date: Fri, 19 Jan 2024 23:06:59 -0600 Subject: "Run mode" done. --- discocaml/discocaml.ml | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'discocaml') diff --git a/discocaml/discocaml.ml b/discocaml/discocaml.ml index e244e26..676cd67 100644 --- a/discocaml/discocaml.ml +++ b/discocaml/discocaml.ml @@ -18,7 +18,10 @@ type response_expr = { expr : string; has_redex : bool; note : string option } [@@deriving to_yojson { exn = true }] type response = - [ `Error of string | `Expr of response_expr | `Graphviz of string ] + [ `Error of string + | `Expr of response_expr + | `Exprs of string option * string array + | `Graphviz of string ] [@@deriving to_yojson { exn = true }] let%expect_test _ = @@ -31,6 +34,11 @@ let%expect_test _ = (`Expr { expr = "foo"; has_redex = false; note = None })); [%expect {| ["Expr",{"expr":"foo","has_redex":false,"note":null}] |}] +let%expect_test _ = + Yojson.Safe.to_channel stdout + (response_to_yojson (`Exprs (Some "A", [| "B"; "C" |]))); + [%expect {| ["Exprs","A",["B","C"]] |}] + let handle_request { expr; command } : response = try let buf = Lexing.from_string expr in @@ -52,6 +60,16 @@ let handle_request { expr; command } : response = | Some i -> Eval.reduce expr i | None -> failwith "no redex" in + let run_with (find_redex : expr ast -> expr index option) : + expr ast -> expr ast Seq.t = + let rec loop (expr : expr ast) : expr ast Seq.t = + Seq.cons expr (fun () -> + match find_redex expr with + | Some i -> loop (Eval.reduce expr i) () + | None -> Nil) + in + loop + in match command with | `Parse -> expr_response expr @@ -62,7 +80,16 @@ let handle_request { expr; command } : response = 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" + | `RunCBN -> + `Exprs + ( Some "Evaluating with CBN:", + run_with Eval.find_redex_cbn expr + |> Seq.take 100 |> Seq.map show_expr |> Array.of_seq ) + | `RunCBV -> + `Exprs + ( Some "Evaluating with CBV:", + run_with Eval.find_redex_cbv expr + |> Seq.take 100 |> Seq.map show_expr |> Array.of_seq ) with | Eval.NotARedex expr -> `Error ("not a redex: " ^ show_expr expr) | Failure msg -> `Error msg -- cgit v1.2.3