Ver Fonte

Added interface for Main module and generalised main phase function in a type

Taddeus Kroes há 12 anos atrás
pai
commit
b8ec1c056c

+ 1 - 1
Makefile

@@ -3,7 +3,7 @@ GLOBALS := types globals stringify util
 PHASES := load parse print desug context typecheck dimreduce boolop extern \
 	constprop index assemble peephole output
 SOURCES := $(addsuffix .mli,$(GLOBALS)) $(addsuffix .ml,$(GLOBALS)) \
-	lexer.mll parser.mly \
+	lexer.mll parser.mly main.mli \
 	$(patsubst %,phases/%.mli,$(PHASES)) $(patsubst %,phases/%.ml,$(PHASES)) \
 	main.ml
 PRE_TARGETS := types.ml $(addsuffix .cmi,$(GLOBALS))

+ 5 - 2
main.ml

@@ -4,9 +4,13 @@ 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
 
+(* List of all phases, which will be executed in the order defined here. *)
 let phases = [
   ("load", Load.phase, always,
    "Load input file");
@@ -36,8 +40,7 @@ let phases = [
    "Output assembly");
 ]
 
-(* Compile CVC file to assembly code
- * in_channel -> int -> repr *)
+(* Compile CVC file to assembly code *)
 let compile () =
   let rec run_phases input = function
     | [] -> ()

+ 20 - 0
main.mli

@@ -0,0 +1,20 @@
+(** Main module, parses command-line arguments and cycles through phases. *)
+(** [Pervasives.exit (]{!main} [())] is executed at the bottom of the file. *)
+
+(** Main function of a phase. Each phase exports a function of this signature
+    that is called by the {!main}. *)
+type phase_func = Types.intermediate -> Types.intermediate
+
+(** List of all phases as
+    [(identifier, phase_function, condition, description)]. [identifier] is used
+    for the [-upto] command-line argument, which is saved in
+    {!Globals.args}([.endphase]). [description] is used for logging, and for the
+    usage message of [-upto]. *)
+val phases : (string * phase_func * (unit -> bool) * string) list
+
+(** Main function, returns exit status. Parses command-line arguments, saving
+    them in {!Globals.args}. Then all phases are executed successively. Run
+    [./civicc -help] in a terminal to see the available command-line options.
+    This function catches the exceptions defined in {!Types}, and prints error
+    messages accordingly. In case of error, 1 is returned, otherwise 0. *)
+val main : unit -> int

+ 1 - 1
phases/assemble.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/boolop.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/constprop.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/context.mli

@@ -1,3 +1,3 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func
 
 val analyse_context : Types.node -> Types.node

+ 1 - 1
phases/desug.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/dimreduce.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/extern.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/index.mli

@@ -2,4 +2,4 @@
 val tag_index : Types.node -> Types.node
 
 (**  *)
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/load.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/output.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/parse.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/peephole.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/print.mli

@@ -1,3 +1,3 @@
 val print_assembly : out_channel -> Types.instr list -> unit
 
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func

+ 1 - 1
phases/typecheck.mli

@@ -1 +1 @@
-val phase : Types.intermediate -> Types.intermediate
+val phase : Main.phase_func