mardi 8 novembre 2022

I want to efficiently lookup a DSL's function identifier and resolve it to a C++ function. Are my ideas for solving this reasonable?

Sorry, bad programmer here. I am parsing a file containing source code for a simple DSL, I want to know if an idea I have is acceptable, optionally if you could explain how you would go about achieving this then that would also be great.

I have a DSL function that need to take arguments, so I want a way to quickly translate a function identifier to a C++ function/something for real work. I need to handle the case of there being variable numbers of arguments, with few supported basic types (std::uint8_t, std::string_view, float) etc.

The DSL syntax might look something like this:

(setoption "Enable Vsync?" false)

The first idea that came to my mind was this:

I'll use a hash table (let's just say std::unordered_map), holding function names as strings, and storing function pointers/whatever for calling the appropriate functions without having to do any extra work (such as conditional checks).

The obvious problem with this approach is differing function signatures, each function could have any number of arguments.

So the second thing I come up with is the idea of creating function tags (for grouping functions into categories) and performing a switch statement on stored enumerators after narrowing down the selection. With this approach I wouldn't store function pointers, but handle everything at one call site with something like this:

template <class... Args>
void resolve_function_and_perform_a_backflip(const FuncData& fdata, Args&&... args) {
    switch (fdata.FUNCTION_ID) {
    /*cases here for handling IF FUNCTION_ID == FUNCTION_ID_ENUM::SETOPTION*/
    }
    Application::get_instance()->crash_for_no_reason();
}

I would rather have an idea of what is good before I spend my time writing anything, so there is no minimal, reproducible example.

Aucun commentaire:

Enregistrer un commentaire