Cling: an interactive C++ interpreter, built on top of Clang and LLVM
github.com
github.com
Years of physicists time has been wasted on CINT, and many more years on ROOT itself.
I'm sure Cling is much better, and it looks cool on it's own, but I know ROOT isn't. Luckily I only have to dig in the source code for ROOT once a year or so (the last time was figuring out they finally got around to adding LZMA compression to ROOT files when things started breaking at work).
I used to think PyRoot was the way forward. Eventually, ROOT will be burned at the stake and everyone will over with Julia.
Just as info for others.
This is the model followed by environments for Common Lisp, Eiffel, JVM, ML languages, .NET, Dylan, Erlang.
Best of both worlds, JIT/Interpreted for development and JIT/AOT for production.
http://webcache.googleusercontent.com/search?q=cache:xZ37OrB...
A more interesting issue for REPLs is how expression- or statement-oriented the language is (c.f. Martin Odersky's concerns about the upcoming Java REPL). In Java/CXX it's much more likely for a function to want to change an external object, in which case you'll need to set up the environment, run the code, check the environment for the change, for every line you write. That's much more likely to be the nail in the coffin for this.
Regarding the JIT aspect, if you are like me, you will be in awe when you encounter the single operation
f_ptr = engine->getPointerToFunction(f_def)
It turns a function definition (in LLVM IR) into an actual function pointer that you can invoke just like any other C/C++ function.[1] http://llvm.org/docs/tutorial/LangImpl1.html#tutorial-introd...
In principle, you can use any compiler of your choice to fake this: Just create a dynamic library for each line that has been entered into the REPL that you immediately load and call into.
Now, 'all' you need to do is cut out the middleman and directly output properly relocated code to memory. There are compilers that can do so. Clang/LLVM is one one of them. TinyCC is another.
For a less confusing explanation, see https://en.wikipedia.org/wiki/Position-independent_code
(Also: no, it's not really implemented by using one offset register for each .so file in the process.)
Another way would be to operate at block- instead of line-level and just not allow local variable to be shared between blocks. Ie
int i = 5 ↵
printf("%i", i) ↵
would fail, but { ↵
int i = 5; ↵
printf("%i", i); ↵
} ↵
would not.Looking into http://root.cern.ch/drupal/content/requested-cling-features: There is still "bug 11297: STL: vector of pair works if compiled, but not if interpreted" Which is something so basic that it has to work.
Apparently the bug report was created in 2005, and it's currently closed with "Won't Fix".
I'd really like a C++ REPL, but I am not hopeful for Cling.
That's the whole point: By using Clang instead of a custom toolchain, you get a production-grade, standards-compliant C++ environment.
One use is for C++ "scripts". Clang replaces the older "C interpreter CINT" in ROOT (which is mainly a data analysis framework for particle physics). CINT was horrible because it was "not quite C++", and encouraged bad behavior. You could swap . and -> for example; objects that were saved to a file you loaded appear automatically in scope (without prior assignment), templates were a bit iffy and so on. You could run many .cpp files as CINT scripts, but not the other way around. So current best practice is to avoid the"interpreter" and write proper programs that link to libROOT. Cling seems to be much more sane.
I don't know if you need something like that, but one thing Cling/CINT gives you is reflection/introspection, which is pretty neat. I think it's also somehow used to get generate bindings for other languages. Oh, and having a Repl is pretty nice. I use it e.g. for quick plots:
$ root filename.root
[0] tree->Draw("mass")
(where "tree" is a data structure automatically loaded from filename.root containing data points, and the command makes a histogram of "mass")Which is everyday for some people. Moreover, some of us want to use C++ :]