Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreter in C++: Function table storage problem

In my interpreter I have built-in functions available in the language like print exit input, etc. These functions can obviously be accessed from inside the language. The interpreter then looks for the corresponding function with the right name in a vector and calls it via a pointer stored with its name.

So I gather all these functions in files like io.cpp, string.cpp, arithmetic.cpp. But I have to add every function to the function list in the interpreter in order for it to be found.

So in these function files I have things like:

void print( arg )
{
     cout << arg.ToString;
}

I'd add this print function to the interpreter function list with:

interpreter.AddFunc( "print", print );
  • But where should I call the interpreter.AddFunc?

I can't just put it there below the print function as it has to be in a function according to the C++ syntax.

  • Where and how should all the functions be added to the list?
like image 316
sub Avatar asked Nov 23 '25 12:11

sub


2 Answers

In each module (io, string, etc.), define a method that registers the module with the interpreter, e.g.:

void IOModule::Register(Interpreter &interpreter) {
    interpreter.AddFunc( "print", print );
    //...
}

This can also be a normal function if your module is not implemented in a class.

Then in your application's main initialization, call the register method of all modules.

This approach helps keep things modular: The main application initialization needs to know which modules exist, but the details of which functions are exported are left to the module itself.

like image 196
interjay Avatar answered Nov 26 '25 00:11

interjay


The simplest is to keep a map of function names to function pointers and load that at program startup. You already have the functions linked into the interpreter executable, so they are accessible at the time main() is called.

You can also come up with a scheme where functions are defiled in the dynamic libraries (.dll or .so depending on the platform) and some configuration file maps function names to libraries/entry points.

like image 33
Nikolai Fetissov Avatar answered Nov 26 '25 01:11

Nikolai Fetissov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!