1

I've started long ago to work on a dynamic graph visualizer, editor and algorithm testing platform (graphs with nodes and arcs, not the other kinds). For the algorithm testing platform i need to let the user write a script or call a script from a file, which will interact with the graph currently loaded. The visualizer would do things like light up nodes while they're being visited by the script algorithm, adding some artificial delay, in order to visualize the algorithm navigating and doing stuff. Scripts would also be secondly used to add third party features that i could either make available as pre-existing scripts in the program folder OR just integrate inside the program in c++ once they're tested and working.

All my searches for an interpreter to embed in my program sent me to lua; then i started handwriting my own recursive descent parser for my own C-like syntax scripting language (which i planned to use a subset of C++ grammar so that any code written in my scripting language can be copy-pasted in any C++ code. It was an interesting crazy idea which i don't regret at all, I have scopes, functions, cycles, gotos, typesafe variables, expressions.

But now that i'm approaching the addition of classes, class methods, inheritance (some default classes would be necessary to interface scripts to the program), i realized it's going to take A LOT of time and effort. A bit too much for a personal project of an ungraduated student with exams to study for… but still i whish to complete this project.

The self-imposed requirement of the scripts being 100% compatible with C++ was all but necessary, it would have been just a little nice extra thing, which i can do without.

Now the question is, is there an alternative to lua with a c-like syntax that supports all i've already done plus classes and inheritance? (being able to add custom "classes" that interface scripts to the program is mandatory)

(i can't assume the user to have a full c++ compiler installed so i cant just compile their "script" at runtime as a dll to load and call it, although i whish i could)

11
  • Can you load a C++ compiler as part of your app? Otherwise I would just stick with lua. Commented Apr 25, 2019 at 18:30
  • 2
    Maybe this will work for you: chaiscript.com Commented Apr 25, 2019 at 18:33
  • Emscripten maybe? Commented Apr 25, 2019 at 18:33
  • Javascript? It is somewhat C-like... :-P en.wikipedia.org/wiki/List_of_ECMAScript_engines Commented Apr 25, 2019 at 18:36
  • This seems to be an implementaion of C++ scripting: github.com/tapika/cppscriptcore Commented Apr 25, 2019 at 18:47

2 Answers 2

5

Just-in-time compilation of C++

Parsing C++ is hard. Heck, parsing C is hard. It's difficult to get it right, and there are a lot of edge cases. Thankfully, there are a few libraries out there which can take code and even compile it for you.

libclang

libclang provides a lot of facilities for parsing c++. It's a good, clean library, and it'll parse anything the clang compiler itself will parse. This article here is a good starter

libclang provides a JIT compilation tool that allows you to write and compile C++ at runtime. See this blog post here for a overview of what it does and how to use it. It's very general, very powerful, and user-written code should be fast.

GCC also provides a library called libgccjit for just-in-time compilation during the runtime of a program. libgccjit is a C library, but there's also a C++ wrapper provided by the library maintainers. It can compile abstract syntax trees and link them at runtime, although it's still in Alpha mode.

cppast

If you don't want to use libclang, there's also a library under development called cppast, which is a C++ parser which will give you an abstract syntax tree representation of your c++ code. Unfortunately, it won't parse function bodies.

Other tools

If anyone knows any other libraries for compiling or interpreting C++ at runtime, I encourage them to update this post, or comment them so I can update it!

Sign up to request clarification or add additional context in comments.

3 Comments

Parsing C++ is hard - this is a serious understatement.
There's also Runtime Compiled C++ and alternatives: github.com/RuntimeCompiledCPlusPlus/RuntimeCompiledCPlusPlus/…
i will try this route as well, but compared to using chaiscript (the answer i accepted) letting the user script interface with classes existing in the program would be a mess… i think. Actually i've no idea how i could do that.
2

Here is something that lets you embed a C-like scripting language in your application (and a bunch of other cool things):

http://chaiscript.com/

There is lots of documentation:

https://codedocs.xyz/ChaiScript/ChaiScript/

1 Comment

It doesn't "let me use C++ as a scripting language", it offers a c-like syntax scripting parser that can be embedded in a C++ program. That said it's close enough to my needs, it looks like integrating interfaces to my classes is really easy which was my most important requirement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.