gpoore / pythontex

A LaTeX package that executes Python and other code in LaTeX documents, and includes the output
880 stars 100 forks source link

Add OCaml support #62

Open romildo opened 9 years ago

romildo commented 9 years ago

It would be very nice if OCaml support is added to PythonTeX .

gpoore commented 9 years ago

Based on your question at TeX.SE, it looks like you want OCaml console emulation.

That should be possible, but a few things will be necessary. Currently, Python is the only language supported for console emulation (all the other languages are only supported for plain code execution). I plan to refactor things for the next release so that it is easier to customize the Python console and also possible to add console support for other languages. Because the machinery is not currently in place for a console that is not Python-based, adding support for OCaml would probably take at least a week or two from my side of things. So this probably can't be done fast, if you are working with a tight deadline. But the necessary structure on the PythonTeX side is coming in the (relatively) near future.

The second thing that is needed for OCaml support is a customizable OCaml REPL. In the case of Python, there is the code module that provides console emulation. Basically, you feed lines of text to this module, and its treats it as input to an interactive session, giving back input interspersed with output. To add OCaml support, I would need something equivalent. I would need a program (let's call it ocamlconsole) that would work as described below.

Suppose we put the following in a temp file tempfile (the leading # could be optional, and we could have lots of different commands in the one file):

# 1 + 2 * 3;;

Then calling ocamlconsole tempfile should write the following to a file (original code, with # added if not in original, interspersed with output).

# 1 + 2 * 3;;
  - : int = 7

There would probably need to be a command-line flag for specifying the name of the output file. So probably something like ocamlconsole -o outfile tempfile would be needed in practice.

If you can provide a program that provides the functionality of ocamlconsole, or point me to a pre-existing REPL emulator for OCaml that can do the same thing, then I will be happy to look into this further as I have time.

romildo commented 9 years ago

I have posted a message in the OCaml mailing list about this issue. See the archives.

andrewray commented 9 years ago

iocaml provides a toplevel repl which can be driven over zmq via json messages. If you have any experience with the ipython tools then this might be a reasonable way to control an ocaml top level. I expect you could probably reuse some ipython code to make this fairly easy and possibly even gain access to the other languages from the ipython world at the same time.