I'm working on a program about graphs. That program for now can read, save graphs from and to files, allows to modify them at runtime (dynamic graphs). Later i will add visualization window, for now it's all command line. But before adding the graphics, i've to work on the real purpose of the program: I want to let the user write an algorithm, load it in the program, so the program will run that algorithm, and finally respond with some informations like: % of nodes visited, average visits per node, time required, time/nodes amount time/arcs amount and stuff like that.
I need it to be platform independent; i ended up thinking i'd need to write an interpreter.
But i want to allow much freedom to the algorithm files, almost as if they were actual c++ code. Well no need for includes, fancy inheritance etcc, i dont think an algorithm implementation needs that. But i'd need stuff like functions, variables, loops, pointers, different types etcc.
Also as a requirement a valid algorithm file must have two functions with specific names.
Is there any "simplified c-like" c++ interpreter code i can use as base to start with? (or lex/yacc files although i know very very very little little about them).
Or is there something else i can do?
Maybe a way to let the user write their own code in c++, compile it like a library, load that library from my program at runtime and call the function?
Or let the user write a stand alone program that somehow (and tell me how please) can call interfaces from my software to access the graph?
__________________________________
I dont know if i explained wrong, i really have no idea how to explain it better. To put it in other terms, i need to run an algorithm written by someone else reading its code from outside (a file) at runtime; said code would access some interface to my graph class.
Callbacks? I need my compiled program to run someone else's code at runtimeā¦ I really totally fail at seeing how the concept of callbacks even remotely touches my questions.
You could use libclang to compile and load C++ code at run time, achieving native performance. Additionally, the host program obtain callable pointers to named functions in the compiled module and the compiled module can call back into the host (via pointers passed).
I have a sample you can use, if you want: https://github.com/Helios-vmg/Borderless/tree/master/src/plugin-core/Cpp
The problem is that libclang is a fairly large dependency. On Windows in particular it's difficult to get working because C++ headers are not included with the OS. Also LLVM is somewhat annoying to compile on Windows.
Otherwise, I recommend Lua, preferably with luajit.
I have not stitched it into any of my own projects though, I have only used it in existing projects. Other programs, such as GNURadio or Blender, come wrapped up with a version of Python as their scripting language of choice.
In this example, declare_c_function() does two things: it declares in the namespace of the guest program a function of the given name, and it associates with that function a callback that is called when the guest calls that function. The specific parameters that these kinds of functions take may change depending on the language. For example, a static language may require passing the parameter types for the declared function as a string, so it can validate that the guest calls the function correctly.
The callback will usually use its interpreter instance parameter to obtain the parameters that the guest passed. For example
1 2
auto first = get_int(instance, 0);
auto second = get_string(instance, 1);
Lua in this particular regard is somewhat annoying because of its use of a stack structure to do parameter passing.
All embedded languages explain their C interface in the documentation, so don't worry too much about this.