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

Moved command-line arguments to global record

Taddeus Kroes 12 жил өмнө
parent
commit
180700fd3e

+ 3 - 3
Makefile

@@ -1,9 +1,9 @@
 RESULT := civicc
 PHASES := load parse print desug context_analysis expand_dims typecheck \
 	dim_reduce bool_op
-SOURCES := ast.ml util.mli util.ml lexer.mll parser.mly stringify.mli \
-	stringify.ml $(patsubst %,phases/%.ml,$(PHASES)) main.ml
-PRE_TARGETS := ast.cmi ast.o util.cmi util.o
+SOURCES := ast.ml stringify.mli stringify.ml util.mli util.ml lexer.mll \
+	parser.mly $(patsubst %,phases/%.ml,$(PHASES)) main.ml
+PRE_TARGETS := ast.cmi ast.o stringify.cmi stringify.o util.cmi util.o
 LIBS := str unix
 
 OCAMLFLAGS := -g

+ 4 - 4
ast.ml

@@ -65,10 +65,10 @@ type args = {
 
 (* intermediate representations between phases *)
 type intermediate =
-    | Args of args
-    | FileContent of string * string * args
-    | Ast of node * args
-    | Assembly of string list * args
+    | Empty
+    | FileContent of string * string
+    | Ast of node
+    | Assembly of string list
 
 (* exceptions *)
 exception LocError of loc * string

+ 4 - 9
main.ml

@@ -5,12 +5,12 @@ open Util
 
 (* Compile CVC file to assembly code
  * in_channel -> int -> repr *)
-let compile args =
+let compile () =
     let rec run_phases input = function
         | [] -> ()
         | h::t -> run_phases (h input) t
     in
-    run_phases (Args args) [
+    run_phases Empty [
         Load.phase;
         (*Print.phase;*)
         Parse.phase;
@@ -38,14 +38,9 @@ let compile args =
     ]
 
 (* Main function, returns exit status
+ * Command-line arguments are stored in Util.args
  * () -> int *)
 let main () =
-    let args = {
-        infile = None;
-        outfile = None;
-        verbose = 2;
-        cpp = true;
-    } in
     let args_spec = [
         ("-o", Arg.String (fun s -> args.outfile <- Some s),
             "Output file (defaults to foo.s for foo.cvc)");
@@ -59,7 +54,7 @@ let main () =
     try
         try
             Arg.parse args_spec (fun s -> args.infile <- Some s) usage;
-            compile args;
+            compile ();
             0
         with
         (*| InvalidNode ->

+ 1 - 1
phases/bool_op.ml

@@ -25,5 +25,5 @@ let rec bool_op = function
 let rec phase input =
     prerr_endline "- Convert bool operations";
     match input with
-    | Ast (node, args) -> Ast (bool_op node, args)
+    | Ast node -> Ast (bool_op node)
     | _ -> raise (InvalidInput "bool operations")

+ 1 - 2
phases/context_analysis.ml

@@ -175,6 +175,5 @@ let analyse_context args program =
 let rec phase input =
     prerr_endline "- Context analysis";
     match input with
-    | Ast (node, args) ->
-        Ast (analyse_context args node, args)
+    | Ast node -> Ast (analyse_context args node)
     | _ -> raise (InvalidInput "context analysis")

+ 1 - 2
phases/desug.ml

@@ -163,6 +163,5 @@ let rec array_init = function
 let rec phase input =
     prerr_endline "- Desugaring";
     match input with
-    | Ast (node, args) ->
-        Ast (for_to_while (array_init (var_init node)), args)
+    | Ast node -> Ast (for_to_while (array_init (var_init node)))
     | _ -> raise (InvalidInput "desugar")

+ 1 - 1
phases/dim_reduce.ml

@@ -29,5 +29,5 @@ and dim_reduce = function
 let rec phase input =
     prerr_endline "- Array dimension reduction";
     match input with
-    | Ast (node, args) -> Ast (dim_reduce node, args)
+    | Ast node -> Ast (dim_reduce node)
     | _ -> raise (InvalidInput "dimension reduction")

+ 1 - 1
phases/expand_dims.ml

@@ -42,5 +42,5 @@ let rec expand_dims = function
 let rec phase input =
     prerr_endline "- Expand array dimensions";
     match input with
-    | Ast (node, args) -> Ast (expand_dims node, args)
+    | Ast node -> Ast (expand_dims node)
     | _ -> raise (InvalidInput "expand dimensions")

+ 4 - 3
phases/load.ml

@@ -1,4 +1,5 @@
 open Ast
+open Util
 
 (* Unix command to call for C preprocessor:
  * -nostdinc        : don't include from C stdlib
@@ -30,7 +31,7 @@ let input_buffered ic chunksize =
 let phase ir =
     prerr_endline "- Load input file";
     match ir with
-    | Args args ->
+    | Empty ->
         let display_name = match args.infile with
             | Some filename -> filename
             | None -> "<stdin>"
@@ -53,11 +54,11 @@ let phase ir =
 
             (* Read preprocessed code from cpp's stdout *)
             let preprocessed = input_buffered cpp_out bufsize in
-            FileContent (display_name, preprocessed, args)
+            FileContent (display_name, preprocessed)
         else
             let content = match args.infile with
                 | Some filename -> input_all (open_in filename)
                 | None -> input_buffered stdin bufsize
             in
-            FileContent (display_name, content, args)
+            FileContent (display_name, content)
     | _ -> raise (InvalidInput "load")

+ 2 - 2
phases/parse.ml

@@ -19,11 +19,11 @@ let parse_with_error lexbuf =
 let phase input =
     prerr_endline "- Parse input";
     match input with
-    | FileContent (display_name, content, args) ->
+    | FileContent (display_name, content) ->
         let lexbuf = Lexing.from_string content in
         lexbuf.lex_curr_p <- { lexbuf.lex_curr_p with pos_fname = display_name };
         let ast = parse_with_error lexbuf in
         (match ast with
             | None -> raise (CompileError "no syntax tree was constructed")
-            | Some node -> Ast (node, args))
+            | Some node -> Ast node)
     | _ -> raise (InvalidInput "parse")

+ 3 - 2
phases/print.ml

@@ -1,8 +1,9 @@
 open Ast
 open Stringify
+open Util
 
 let phase = function
-    | Ast (node, args) as input ->
+    | Ast node as input ->
         if args.verbose >= 2 then (
             prerr_endline "--------------------------------------------------";
             prerr_endline (node2str node);
@@ -10,7 +11,7 @@ let phase = function
         );
         input
 
-    | FileContent (display_name, content, args) as input ->
+    | FileContent (display_name, content) as input ->
         if args.verbose >= 2 then (
             prerr_endline "--------------------------------------------------";
             prerr_endline (display_name ^ ":\n");

+ 1 - 1
phases/typecheck.ml

@@ -204,5 +204,5 @@ let rec prune_types = function
 let rec phase input =
     prerr_endline "- Type checking";
     match input with
-    | Ast (node, args) -> Ast (prune_types (typecheck node), args)
+    | Ast node -> Ast (prune_types (typecheck node))
     | _ -> raise (InvalidInput "typecheck")

+ 26 - 1
util.ml

@@ -2,8 +2,33 @@ open Printf
 open Lexing
 open Ast
 
-let var_counter = ref 0
+(* Default config *)
+let verbosity_default = 2  (* TODO: set to 1 when done with debugging *)
+let verbosity_debug   = 3
+
+(* Commandline args are stored in a global struct
+ * (yes, it IS dirty, but I'd rather have this than having to pass [args] around
+ * everywhere) *)
+let args = {
+    infile  = None;
+    outfile = None;
+    verbose = verbosity_default;
+    cpp     = true;
+}
+
+(* Logging functions *)
+
+let log_line verbosity line =
+    if args.verbose >= verbosity then prerr_endline line
 
+let log_node verbosity node = log_line verbosity (Stringify.node2str node)
+
+let dbg_line = log_line verbosity_debug
+
+let dbg_node = log_node verbosity_debug
+
+(* Variable generation *)
+let var_counter = ref 0
 let fresh_var prefix =
     var_counter := !var_counter + 1;
     prefix ^ "$" ^ string_of_int !var_counter

+ 9 - 0
util.mli

@@ -1,3 +1,12 @@
+(* Global record with command-line arguments *)
+val args : Ast.args
+
+(* Logging functions, they print to stderr and consider the verbosity flag *)
+val log_line : int -> string -> unit
+val log_node : int -> Ast.node -> unit
+val dbg_line : string -> unit
+val dbg_node : Ast.node -> unit
+
 (* Generate a fresh variable from a given prefix, e.g. "counter" -> "counter$1"  *)
 val fresh_var : string -> string