gpt4 book ai didi

c++ - C++ 解释器 : Function table storage problem

转载 作者:太空狗 更新时间:2023-10-29 21:08:44 26 4
gpt4 key购买 nike

在我的解释器中,我有内置函数,可以用print exit input 等语言使用。这些函数显然可以从语言内部访问。然后,解释器在 vector 中查找具有正确名称的相应函数,并通过存储有其名称的指针调用它。

所以我将所有这些函数收集在文件中,例如 io.cppstring.cpparithmetic.cpp。但是我必须将每个函数添加到解释器的函数列表中才能找到它。

所以在这些函数文件中我有这样的东西:

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

我会将此打印函数添加到解释器函数列表中:

interpreter.AddFunc( "print", print );
  • 但是我应该在哪里调用 interpreter.AddFunc

我不能把它放在打印函数下面,因为根据 C++ 语法,它必须在函数中。

  • 应在何处以及如何将所有功能添加到列表中?

最佳答案

在每个模块(io、string 等)中,定义一个向解释器注册模块的方法,例如:

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

如果您的模块没有在类中实现,这也可以是一个普通函数。

然后在你的应用程序的主初始化中,调用所有模块的注册方法。

这种方法有助于保持模块化:主应用程序初始化需要知道存在哪些模块,但导出哪些函数的细节留给模块本身。

关于c++ - C++ 解释器 : Function table storage problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com