Browse Source

Code cleanup

Taddeus Kroes 12 năm trước cách đây
mục cha
commit
bf51961a6f
1 tập tin đã thay đổi với 11 bổ sung6 xóa
  1. 11 6
      phases/print.ml

+ 11 - 6
phases/print.ml

@@ -132,23 +132,28 @@ let rec instr2str = function
 let rec print_assembly oc instrs =
   let output_line line =
     output_string oc line;
-    output_char oc '\n';
+    output_char oc '\n'
   in
+
+  (* Print instructions to outfile and collect directives in endbuf list *)
   let endbuf = ref [] in
   let rec trav = function
     | [] -> ()
     | EmptyLine :: tl -> output_line ""; trav tl
     | hd :: tl ->
-      let line = instr2str hd in
       begin
-        if String.length line > 0 && line.[0] = '.' then
-          endbuf := line :: !endbuf
-        else if String.length line > 0 then
-          output_line line
+        match instr2str hd with
+        | ""                       -> ()
+        | line when line.[0] = '.' -> endbuf := line :: !endbuf
+        | line                     -> output_line line
       end;
       trav tl
   in
   trav instrs;
+
+  (* Directives (lines beginning with a '.') are collected in endbuf and are
+   * printed at the end of the file here. The directives are sorted by the first
+   * 7 characters to group directive opcodes *)
   if List.length !endbuf > 1 then begin
     output_line (instr2str (Comment ("globals:")));
     let cmp a b = compare (String.sub b 0 7) (String.sub a 0 7) in