blob: df5cef32e5f2b763fcaff10ae9b32d07d12ea181 (
plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
open Ast
type command = [ `Parse | `DrawTree | `RunCBN | `RunCBV | `StepCBN | `StepCBV ]
let command_of_yojson = function
| `String "Parse" -> Ok `Parse
| `String "DrawTree" -> Ok `DrawTree
| `String "RunCBN" -> Ok `RunCBN
| `String "RunCBV" -> Ok `RunCBV
| `String "StepCBN" -> Ok `StepCBN
| `String "StepCBV" -> Ok `StepCBV
| _ -> Error "invalid command"
type request = { expr : string; command : command }
[@@deriving of_yojson { exn = true }]
type response_expr = { expr : string; has_redex : bool }
[@@deriving to_yojson { exn = true }]
type response =
[ `Error of string | `Expr of response_expr | `Graphviz of string ]
[@@deriving to_yojson { exn = true }]
let%expect_test _ =
Yojson.Safe.to_channel stdout (response_to_yojson (`Error "foo"));
[%expect {| ["Error","foo"] |}]
let%expect_test _ =
Yojson.Safe.to_channel stdout
(response_to_yojson (`Expr { expr = "foo"; has_redex = false }));
[%expect {| ["Expr",{"expr":"foo","has_redex":false}] |}]
let handle_request { expr; command } : response =
try
let buf = Lexing.from_string expr in
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 }
in
let step_with (find_redex : expr ast -> expr index option) (expr : expr ast)
: expr ast =
match find_redex expr with
| Some i -> Eval.reduce expr i
| None -> failwith "no redex"
in
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)
| `DrawTree -> `Graphviz (Draw_tree.draw_tree expr)
| `RunCBN | `RunCBV -> failwith "not implemented"
with
| Failure msg -> `Error msg
| exn -> `Error ("uncaught exception: " ^ Printexc.to_string exn)
|