Fixed some bugs.
[llvm/zpu.git] / examples / OCaml-Kaleidoscope / Chapter3 / toplevel.ml
blobd1bf5d4c0c655752bd8b5f1eb1f517fd6f4c4ae8
1 (*===----------------------------------------------------------------------===
2 * Top-Level parsing and JIT Driver
3 *===----------------------------------------------------------------------===*)
5 open Llvm
7 (* top ::= definition | external | expression | ';' *)
8 let rec main_loop stream =
9 match Stream.peek stream with
10 | None -> ()
12 (* ignore top-level semicolons. *)
13 | Some (Token.Kwd ';') ->
14 Stream.junk stream;
15 main_loop stream
17 | Some token ->
18 begin
19 try match token with
20 | Token.Def ->
21 let e = Parser.parse_definition stream in
22 print_endline "parsed a function definition.";
23 dump_value (Codegen.codegen_func e);
24 | Token.Extern ->
25 let e = Parser.parse_extern stream in
26 print_endline "parsed an extern.";
27 dump_value (Codegen.codegen_proto e);
28 | _ ->
29 (* Evaluate a top-level expression into an anonymous function. *)
30 let e = Parser.parse_toplevel stream in
31 print_endline "parsed a top-level expr";
32 dump_value (Codegen.codegen_func e);
33 with Stream.Error s | Codegen.Error s ->
34 (* Skip token for error recovery. *)
35 Stream.junk stream;
36 print_endline s;
37 end;
38 print_string "ready> "; flush stdout;
39 main_loop stream