-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcommand.ml
executable file
·43 lines (38 loc) · 1.24 KB
/
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
37
38
39
40
41
42
43
open Pervasives
type object_phrase = string list
type command =
| Set of object_phrase
| Inventory
| History
| Help
| Quit (*TODO *)
| Tutorial
| Cheat
exception Empty
exception Malformed
(**[parse string] is the command type which a player wants to be acted upon.
It takes in a string and converts it to a type of command.
Raises: Empty if no command given or just whitespace;
Malformed if not a recognized command;*)
let parse str =
let wordlst = String.split_on_char ' ' (String.trim str) in
let filtered = List.filter (fun s -> String.length s >= 1) wordlst in
if List.length filtered > 0 then let cmd = List.hd filtered in
match cmd with
| "set" -> if List.length filtered > 0 then
let obj = begin match filtered with
| [] -> []
| h::t -> t end
in (Set obj) else raise Malformed
| "inventory" -> if List.length filtered = 1 then Inventory
else raise Malformed
| "history" -> if List.length filtered = 1 then History
else raise Malformed
| "quit" -> if List.length filtered = 1 then Quit
else raise Malformed
| "tutorial" -> Tutorial
| "help" -> Help
| "cheat" -> Cheat
| _ -> raise Malformed
else
raise Empty