Commits

Corentin Derbois committed 0cf7f47

Add option parser

Comments (0)

Files changed (4)

 SRCSYACC		=	parser.mly
 
 SRCS				= $(SRCSLEX:.mll=.ml) $(SRCSYACC:.mly=.ml)
-SRCS				+= main.ml
+SRCS				+= option.ml main.ml
 
 OBJS				= $(SRCS:.ml=.cmx)
 
+class compiler file = 
+object(this)
+
+
+
+end
-let main () = print_endline "SmallCC"
+let usage () =
+  print_endline "./scc [FILE...]";
+  exit 1
+
+let main () =
+  print_endline "SmallCC, oh yeah";
+  if (Array.length Sys.argv) == 1 then usage ();
+  Option.option#parse ()
+  
 
 let _ = main ()
+exception OptionError of string * string
+
+class option argv = 
+object(this)
+
+  val mutable files = []
+  val mutable output = "a.out"
+
+  method getFiles () = files
+
+  method addFile (filepath:string) =
+    files = filepath::files
+
+  method next (l:string list) (f:string -> unit) = match l with
+      []   -> raise (OptionError("next", "Option missing"))
+    | e::r -> f e; this#rec_parce r
+
+  method rec_parce l = match l with
+        []                  ->
+          ()
+      | e::r when e == "-o" ->
+          this#next r (fun e -> output = e; ())
+      | e::r                ->
+          this#addFile e; this#rec_parce r
+  
+  method parse () =
+    let _::args = Array.to_list argv in 
+      this#rec_parce (args)
+
+end
+
+let option =
+  let a = new option Sys.argv in
+    a