-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.ml
36 lines (33 loc) · 974 Bytes
/
command.ml
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
type t =
| Raise of int
| Start
| Hand
| Call
| Fold
| Quit
| Help
exception Malformed
(** [rid_spaces cmd_lst] is a helper function for parse_branch that takes
in a string list [cmd_lst] which represents the command, and gets
rid of trailing or leading spaces in each element and then filters for only
those elements which are empty strings. *)
let rid_spaces cmd_lst : string list =
let open List in
cmd_lst
|> List.map (fun s -> String.trim s)
|> filter (fun x -> String.length x > 0)
let parse (str : string) : t =
let open String in
let cmd_lst = str |> trim |> split_on_char ' ' |> rid_spaces in
match cmd_lst with
| "go" :: [] -> Start
| "hand" :: [] -> Hand
| "call" :: [] -> Call
| "fold" :: [] -> Fold
| "leave" :: [] -> Quit
| "raise" :: num :: [] -> begin match int_of_string_opt num with
| Some i -> Raise i
| None -> raise Malformed
end
| "help" :: [] -> Help
| _ -> raise Malformed