#include#include "CppInterpreter/Runtime.h" int main() { CppInterpreter::Runtime runtime; runtime.execute("int x = 5;"); CppInterpreter::Context context = runtime.getContext(); // Access the value of variable x std::cout << context.getVariable("x").asInt() << std::endl; return 0; }
#includeIn this example, we define a new function `myFunction()` that takes a `Context` object as a parameter and prints a greeting to the console using the value of the `name` variable in the context. We then create a new `Runtime` object and execute some code to define a `name` variable. We then call `getContext()` to get the current execution context, use `setFunction()` to add our `myFunction()` to the context under the name `greet`, and finally call `execute()` with the code `greet();` to execute our new function. When run, this program should output `Hello, Alice`. Package/Library: `CppInterpreter`#include "CppInterpreter/Runtime.h" void myFunction(CppInterpreter::Context& context) { std::cout << "Hello, " << context.getVariable("name").asString() << std::endl; } int main() { CppInterpreter::Runtime runtime; runtime.execute("string name = \"Alice\";"); // Define a new function and add it to the context CppInterpreter::Context context = runtime.getContext(); context.setFunction("greet", myFunction); // Call the function runtime.execute("greet();"); return 0; }