Эх сурвалжийг харах

Cleanup main file, added some documentation

Taddeus Kroes 12 жил өмнө
parent
commit
0f6da945f4
3 өөрчлөгдсөн 45 нэмэгдсэн , 43 устгасан
  1. 2 1
      globals.mli
  2. 36 34
      main.ml
  3. 7 8
      stringify.ml

+ 2 - 1
globals.mli

@@ -1,4 +1,5 @@
-(** Global variables with runtime values, used in multiple modules. *)
+(** Global variables with runtime (input-based) values, used in multiple
+    modules. *)
 
 (** Values of command-line arguments, to be set by the [Main] module. *)
 val args : Types.args_record

+ 36 - 34
main.ml

@@ -2,13 +2,12 @@ open Printf
 open Lexing
 open Types
 open Util
-open Globals
 
 (* For some reason OCaml wants me to redefine this type's implementation -.- *)
 type phase_func = Types.intermediate -> Types.intermediate
 
 let always _ = true
-let when_optimize _ = args.optimize
+let when_optimize _ = Globals.args.optimize
 
 (* List of all phases, which will be executed in the order defined here. *)
 let phases = [
@@ -40,30 +39,8 @@ let phases = [
    "Output assembly");
 ]
 
-(* Compile CVC file to assembly code *)
-let compile () =
-  let rec run_phases input = function
-    | [] -> ()
-    | (id, phase, cond, msg) :: tl ->
-      let output =
-        if cond () then begin
-          log_plain_line 2 (expand 13 ("- " ^ id ^ ":") ^ msg);
-          let output = phase input in
-          if id = args.endphase || args.verbose >= 2 then begin
-            ignore (Print.phase output)
-          end;
-          output
-        end else
-          input
-      in
-      if id = args.endphase then () else run_phases output tl
-  in
-  run_phases Empty phases
-
-(* Main function, returns exit status
- * Command-line arguments are stored in Util.args
- * () -> int *)
-let main () =
+(* Parse command-line arguments *)
+let parse_args () =
   let rec upto_usage = function
     | [] -> ""
     | (id, _, _, msg) :: tl ->
@@ -73,23 +50,23 @@ let main () =
     ("<file>", Arg.Rest (fun s -> ()),
              "   Optional input file (default is to read from stdin)");
 
-    ("-o", Arg.String (fun s -> args.outfile <- Some s),
+    ("-o", Arg.String (fun s -> Globals.args.outfile <- Some s),
          "<file> Output file (defaults to foo.s for foo.cvc)");
 
-    ("-v", Arg.Int (fun i -> args.verbose <- i),
+    ("-v", Arg.Int (fun i -> Globals.args.verbose <- i),
          "<num>  Set verbosity (0: nothing, 1: errors, 2: intermediate, 3: debug)");
 
-    ("-nocpp", Arg.Unit (fun _ -> args.cpp <- false),
+    ("-nocpp", Arg.Unit (fun _ -> Globals.args.cpp <- false),
              "   Disable C preprocessor");
-    ("-cpp", Arg.Unit (fun _ -> args.cpp <- true),
+    ("-cpp", Arg.Unit (fun _ -> Globals.args.cpp <- true),
            "     Enable C preprocessor (overwrite earlier -nocpp)");
 
-    ("-noopt", Arg.Unit (fun _ -> args.optimize <- false),
+    ("-noopt", Arg.Unit (fun _ -> Globals.args.optimize <- false),
              "   Disable optimization");
-    ("-opt", Arg.Unit (fun _ -> args.optimize <- true),
+    ("-opt", Arg.Unit (fun _ -> Globals.args.optimize <- true),
            "     Enable optimization (overwrite earlier -nocpp)");
 
-    ("-upto", Arg.String (fun s -> args.endphase <- s),
+    ("-upto", Arg.String (fun s -> Globals.args.endphase <- s),
             "<phase> Stop after the specified phase, and print the intermediate \
              representation to stderr.\n        \
              Possible options are (in order of execution):" ^ upto_usage phases);
@@ -100,9 +77,34 @@ let main () =
      [-v <verbosity>] [-upto <phase>] [<file>]"
   in
 
+  Arg.parse args_spec (fun s -> Globals.args.infile <- Some s) usage
+
+(* Compile CVC file to assembly code *)
+let compile () =
+  let rec run_phases input = function
+    | [] -> ()
+    | (id, phase, cond, msg) :: tl ->
+      let output =
+        if cond () then begin
+          log_plain_line 2 (expand 13 ("- " ^ id ^ ":") ^ msg);
+          let output = phase input in
+          if id = Globals.args.endphase || Globals.args.verbose >= 2 then begin
+            ignore (Print.phase output)
+          end;
+          output
+        end else
+          input
+      in
+      if id = Globals.args.endphase then () else run_phases output tl
+  in
+  run_phases Empty phases
+
+(* Main function, returns exit status
+ * Command-line arguments are stored in lobals.args *)
+let main () =
   try
     try
-      Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
+      parse_args ();
       compile ();
       0
     with

+ 7 - 8
stringify.ml

@@ -1,5 +1,4 @@
 open Types
-open Globals
 
 let tab = "    "
 
@@ -138,19 +137,19 @@ and node2str node =
 
   (* Annotation nodes print more information at higher verbosity, for
    * debugging purposes *)
-  | VarLet (dec, dims, value, _) when args.verbose >= 3 ->
+  | VarLet (dec, dims, value, _) when Globals.args.verbose >= 3 ->
     "<let:" ^ node2str (Assign (nameof dec, dims, value, [])) ^ ">"
-  | VarUse (dec, dims, _)        when args.verbose >= 3 ->
+  | VarUse (dec, dims, _)        when Globals.args.verbose >= 3 ->
     "<use:" ^ node2str (Var (nameof dec, dims, [])) ^ ">"
-  | FunUse (dec, params, _)      when args.verbose >= 3 ->
+  | FunUse (dec, params, _)      when Globals.args.verbose >= 3 ->
     "<use:" ^ node2str (FunCall (nameof dec, params, [])) ^ ">"
-  | Dim (name, _)                when args.verbose >= 3 ->
+  | Dim (name, _)                when Globals.args.verbose >= 3 ->
     "<dim:" ^ name ^ ">"
-  | Arg node                     when args.verbose >= 3 ->
+  | Arg node                     when Globals.args.verbose >= 3 ->
     "<arg:" ^ str node ^ ">"
-  | VarDecs nodes                when args.verbose >= 3 ->
+  | VarDecs nodes                when Globals.args.verbose >= 3 ->
     String.concat "\n" ("// vardecs" :: List.map str nodes)
-  | LocalFuns nodes              when args.verbose >= 3 ->
+  | LocalFuns nodes              when Globals.args.verbose >= 3 ->
     String.concat "\n" ("// localfuns" :: List.map str nodes)
 
   | VarLet (dec, dims, value, _) ->