1*9880d681SAndroid Build Coastguard Worker(*===----------------------------------------------------------------------=== 2*9880d681SAndroid Build Coastguard Worker * Top-Level parsing and JIT Driver 3*9880d681SAndroid Build Coastguard Worker *===----------------------------------------------------------------------===*) 4*9880d681SAndroid Build Coastguard Worker 5*9880d681SAndroid Build Coastguard Workeropen Llvm 6*9880d681SAndroid Build Coastguard Workeropen Llvm_executionengine 7*9880d681SAndroid Build Coastguard Worker 8*9880d681SAndroid Build Coastguard Worker(* top ::= definition | external | expression | ';' *) 9*9880d681SAndroid Build Coastguard Workerlet rec main_loop the_fpm the_execution_engine stream = 10*9880d681SAndroid Build Coastguard Worker match Stream.peek stream with 11*9880d681SAndroid Build Coastguard Worker | None -> () 12*9880d681SAndroid Build Coastguard Worker 13*9880d681SAndroid Build Coastguard Worker (* ignore top-level semicolons. *) 14*9880d681SAndroid Build Coastguard Worker | Some (Token.Kwd ';') -> 15*9880d681SAndroid Build Coastguard Worker Stream.junk stream; 16*9880d681SAndroid Build Coastguard Worker main_loop the_fpm the_execution_engine stream 17*9880d681SAndroid Build Coastguard Worker 18*9880d681SAndroid Build Coastguard Worker | Some token -> 19*9880d681SAndroid Build Coastguard Worker begin 20*9880d681SAndroid Build Coastguard Worker try match token with 21*9880d681SAndroid Build Coastguard Worker | Token.Def -> 22*9880d681SAndroid Build Coastguard Worker let e = Parser.parse_definition stream in 23*9880d681SAndroid Build Coastguard Worker print_endline "parsed a function definition."; 24*9880d681SAndroid Build Coastguard Worker dump_value (Codegen.codegen_func the_fpm e); 25*9880d681SAndroid Build Coastguard Worker | Token.Extern -> 26*9880d681SAndroid Build Coastguard Worker let e = Parser.parse_extern stream in 27*9880d681SAndroid Build Coastguard Worker print_endline "parsed an extern."; 28*9880d681SAndroid Build Coastguard Worker dump_value (Codegen.codegen_proto e); 29*9880d681SAndroid Build Coastguard Worker | _ -> 30*9880d681SAndroid Build Coastguard Worker (* Evaluate a top-level expression into an anonymous function. *) 31*9880d681SAndroid Build Coastguard Worker let e = Parser.parse_toplevel stream in 32*9880d681SAndroid Build Coastguard Worker print_endline "parsed a top-level expr"; 33*9880d681SAndroid Build Coastguard Worker let the_function = Codegen.codegen_func the_fpm e in 34*9880d681SAndroid Build Coastguard Worker dump_value the_function; 35*9880d681SAndroid Build Coastguard Worker 36*9880d681SAndroid Build Coastguard Worker (* JIT the function, returning a function pointer. *) 37*9880d681SAndroid Build Coastguard Worker let result = ExecutionEngine.run_function the_function [||] 38*9880d681SAndroid Build Coastguard Worker the_execution_engine in 39*9880d681SAndroid Build Coastguard Worker 40*9880d681SAndroid Build Coastguard Worker print_string "Evaluated to "; 41*9880d681SAndroid Build Coastguard Worker print_float (GenericValue.as_float Codegen.double_type result); 42*9880d681SAndroid Build Coastguard Worker print_newline (); 43*9880d681SAndroid Build Coastguard Worker with Stream.Error s | Codegen.Error s -> 44*9880d681SAndroid Build Coastguard Worker (* Skip token for error recovery. *) 45*9880d681SAndroid Build Coastguard Worker Stream.junk stream; 46*9880d681SAndroid Build Coastguard Worker print_endline s; 47*9880d681SAndroid Build Coastguard Worker end; 48*9880d681SAndroid Build Coastguard Worker print_string "ready> "; flush stdout; 49*9880d681SAndroid Build Coastguard Worker main_loop the_fpm the_execution_engine stream 50