Skip to content

Instantly share code, notes, and snippets.

@scemama
Created October 26, 2015 22:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scemama/a4003f8a1b923745a141 to your computer and use it in GitHub Desktop.
Save scemama/a4003f8a1b923745a141 to your computer and use it in GitHub Desktop.
Evaluate a string in OCaml
(* http://stackoverflow.com/questions/33291754/eval-a-string-in-ocaml *)
#require "compiler-libs" (* Assuming you're using utop, if compiling then this is the package you need *)
let eval code =
let as_buf = Lexing.from_string code in
let parsed = !Toploop.parse_toplevel_phrase as_buf in
ignore (Toploop.execute_phrase true Format.std_formatter parsed)
eval "let () = print_endline \"hello\";;"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment