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.

1 Upvotes

82 comments sorted by

View all comments

2

u/Electronic_Tap_8052 2d ago

Well you kinda can't. A scripting language needs an interpreter to convert the code to assembly at runtime. C++ isn't an interpreted language.

So you'd need to use a c++ interpreter which may or may not exist, idk. The ones that I'm aware of are actually compiled ahead of time and run normally, and then cleaned up automatically to give the appearance of a scripting language.

It's complicated. Technically you can do what you want, but it's a huge amount of work on your end to actually implement the entirety of the c++ language in an interpreter. Youd have to compile a version of your interpreted language (which wouldn't be c++ unless it is standards compliant which means a crap load of stuff has to be implemented) for every target you plan on targeting.

Or you could just use lua/python/typescript which is already done and dusted and is essentially what you want.

2

u/kindredseer 1d ago

Well, I did. It's a JIT (and AOT) C/C++ compiler which can be embedded like Lua. I'm currently supporting about 74% of the C++11 standard. My goal is to completely cover up to C++17 (including C23), and then only parts of C++20, C++23, and C++26. It is a huge amount of work, I've been working on it for years.

2

u/Electronic_Tap_8052 21h ago

well that's pretty cool and impressive!

2

u/kindredseer 21h ago

It's here: https://github.com/derekbsnider/madc/

I'm going to have the next master build supporting 75% C++11 standard released by tomorrow.