Hello, folks.
I’m trying to work with compiler-libs.toplevel
api’s just to understand a little bit more of the REPL possibilities Ocaml can provide.
On the internet, I’ve found this simples function here:
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.err_formatter parsed)
It just receives some ocaml code as string and send the result of the execution to a buffer output. It works like like a charm for some simple stuff. Here’s some example:
eval "1 + 1;;";;
- : int = 2
- : unit = ()
eval "let b = \"my name\";;";;
val b : string = "my name"
- : unit = ()
But for some reason, the execution Toploop.execute_phrase
seems to break when I try to escape multiple things inside a string. For example:
let x = "Hello \"World!\""
it’s a perfect valid string in ocaml with the result val x : string = "Hello \"World!\""
. But trying to do the same thing inside my eval function, like this:
eval "let x = \"Hello \"World!\"\";;"
Breaks at the executation with the error Exception: Typecore.Error (_, _, _).
.
Does anyone has any idea what could be happening?