Made Command.t a tuple

This commit is contained in:
Neale Pickett 2008-03-03 08:42:30 -07:00
parent ba2f2ef892
commit 1795f5aa37
1 changed files with 16 additions and 29 deletions

View File

@ -1,33 +1,27 @@
type t = {sender: string option; type t = string option * string * string list * string option
name: string;
args: string list;
text: string option}
let create sender name args text = let create sender name args text =
{sender = sender; (sender, name, args, text)
name = name;
args = args;
text = text}
let anon = create None let anon = create None
let as_string cmd = let as_string (sender, name, args, text) =
let ret = Buffer.create 120 in let ret = Buffer.create 120 in
(match cmd.sender with (match sender with
| None -> | None ->
() ()
| Some s -> | Some s ->
Buffer.add_char ret ':'; Buffer.add_char ret ':';
Buffer.add_string ret s; Buffer.add_string ret s;
Buffer.add_char ret ' '); Buffer.add_char ret ' ');
Buffer.add_string ret cmd.name; Buffer.add_string ret name;
(match cmd.args with (match args with
| [] -> | [] ->
() ()
| l -> | l ->
Buffer.add_char ret ' '; Buffer.add_char ret ' ';
Buffer.add_string ret (String.concat " " l)); Buffer.add_string ret (String.concat " " l));
(match cmd.text with (match text with
| None -> | None ->
() ()
| Some txt -> | Some txt ->
@ -49,12 +43,9 @@ let rec from_string line =
let rec loop sender acc line = let rec loop sender acc line =
let c = (if (line = "") then None else (Some line.[0])) in let c = (if (line = "") then None else (Some line.[0])) in
match (c, acc) with match (c, acc) with
| (None, cmd :: args) -> | (None, name :: args) ->
(* End of line, no text part *) (* End of line, no text part *)
{sender = sender; create sender name args None
name = cmd;
args = args;
text = None}
| (None, []) -> | (None, []) ->
(* End of line, no text part, no args, no command *) (* End of line, no text part, no args, no command *)
raise (Failure "No command, eh?") raise (Failure "No command, eh?")
@ -62,12 +53,9 @@ let rec from_string line =
(* First word, starts with ':' *) (* First word, starts with ':' *)
let (word, rest) = extract_word line in let (word, rest) = extract_word line in
loop (Some (Str.string_after word 1)) acc rest loop (Some (Str.string_after word 1)) acc rest
| (Some ':', cmd :: args) -> | (Some ':', name :: args) ->
(* Not first word, starts with ':' *) (* Not first word, starts with ':' *)
{sender = sender; create sender name args (Some (Str.string_after line 1))
name = cmd;
args = args;
text = Some (Str.string_after line 1)}
| (Some _, _) -> | (Some _, _) ->
(* Argument *) (* Argument *)
let (word, rest) = extract_word line in let (word, rest) = extract_word line in
@ -76,10 +64,9 @@ let rec from_string line =
loop None [] line loop None [] line
let as_tuple cmd = let as_tuple cmd = cmd
(cmd.sender, cmd.name, cmd.args, cmd.text)
let sender cmd = cmd.sender let sender (sender, name, args, text) = sender
let name cmd = cmd.name let name (sender, name, args, text) = name
let args cmd = cmd.args let args (sender, name, args, text) = args
let text cmd = cmd.text let text (sender, name, args, text) = text