1 (*===----------------------------------------------------------------------===
2 * Top-Level parsing and JIT Driver
3 *===----------------------------------------------------------------------===*)
6 open Llvm_executionengine
8 (* top ::= definition | external | expression | ';' *)
9 let rec main_loop the_fpm the_execution_engine stream
=
10 match Stream.peek stream
with
13 (* ignore top-level semicolons. *)
14 | Some
(Token.Kwd '
;'
) ->
16 main_loop the_fpm the_execution_engine stream
22 let e = Parser.parse_definition stream
in
23 print_endline
"parsed a function definition.";
24 dump_value
(Codegen.codegen_func the_fpm
e);
26 let e = Parser.parse_extern stream
in
27 print_endline
"parsed an extern.";
28 dump_value
(Codegen.codegen_proto
e);
30 (* Evaluate a top-level expression into an anonymous function. *)
31 let e = Parser.parse_toplevel stream
in
32 print_endline
"parsed a top-level expr";
33 let the_function = Codegen.codegen_func the_fpm
e in
34 dump_value
the_function;
36 (* JIT the function, returning a function pointer. *)
37 let result = ExecutionEngine.run_function
the_function [||]
38 the_execution_engine
in
40 print_string
"Evaluated to ";
41 print_float
(GenericValue.as_float
Codegen.double_type
result);
43 with Stream.Error s
| Codegen.Error s
->
44 (* Skip token for error recovery. *)
48 print_string
"ready> "; flush stdout
;
49 main_loop the_fpm the_execution_engine stream