Home | History | Annotate | Download | only in Chapter2
      1 (*===----------------------------------------------------------------------===
      2  * Top-Level parsing and JIT Driver
      3  *===----------------------------------------------------------------------===*)
      4 
      5 (* top ::= definition | external | expression | ';' *)
      6 let rec main_loop stream =
      7   match Stream.peek stream with
      8   | None -> ()
      9 
     10   (* ignore top-level semicolons. *)
     11   | Some (Token.Kwd ';') ->
     12       Stream.junk stream;
     13       main_loop stream
     14 
     15   | Some token ->
     16       begin
     17         try match token with
     18         | Token.Def ->
     19             ignore(Parser.parse_definition stream);
     20             print_endline "parsed a function definition.";
     21         | Token.Extern ->
     22             ignore(Parser.parse_extern stream);
     23             print_endline "parsed an extern.";
     24         | _ ->
     25             (* Evaluate a top-level expression into an anonymous function. *)
     26             ignore(Parser.parse_toplevel stream);
     27             print_endline "parsed a top-level expr";
     28         with Stream.Error s ->
     29           (* Skip token for error recovery. *)
     30           Stream.junk stream;
     31           print_endline s;
     32       end;
     33       print_string "ready> "; flush stdout;
     34       main_loop stream
     35