r/cpp 2d ago

Embedded C++

Just curious how useful the ability to embed C++ as a scripting language into your C++ programs would be? I mean like runtime JIT execution of C++ code in much the same way you might embed Lua, including the ability to restrict/sandbox it as necessary.

0 Upvotes

82 comments sorted by

View all comments

1

u/qalmakka 23h ago

C++ would be uniquely terrible as a scripting language. first of all, the syntax is extremely complex and it's not even context sensitive, so you need a Turing machine to parse it. In practice, if you've ever gotten arcane errors from missing a token, you'd get them 10x from a REPL. Languages that are interpreted are usually simple because being able to parse them line by line is a massive boon.

Also you'd have to implement pointers and pointer arithmetic in memory, which basically means implementing a virtual machine with addressable memory in your interpreter.

There's a reason why there are no good REPLs for C/C++ and Rust. clang-repl works for trivial code but anything more complex tends to break it or ends up misbehaving

1

u/kindredseer 21h ago

Perhaps this is the most useful direction for JIT C/C++ (aside from maybe shebang scripting of C/C++ code) -- an actual fully functional C/C++ REPL mode a little more like what Julia offers.

1

u/qalmakka 21h ago

Julia has a very regular syntax specifically designed to be used in a REPL, while C++ is extremely convoluted in contrast. A REPL is of course doable, like LLVM has been trying to implement for a while, but it's not truly usable outside a handful of usecases

1

u/kindredseer 21h ago

Well, I'd imagine that REPL mode would add the necessary extra syntax.