C++中的解释器:函数表存储问题
在我的解释器中,我有语言中可用的内置函数,例如 print
exit
input
等。 显然可以从语言内部访问这些函数。然后,解释器在向量中查找具有正确名称的相应函数,并通过与其名称一起存储的指针来调用它。
因此,我将所有这些函数收集在 io.cpp
、string.cpp
、arithmetic.cpp
等文件中。但我必须将每个函数添加到解释器中的函数列表中才能找到它。
因此,在这些函数文件中,我有如下内容:
void print( arg )
{
cout << arg.ToString;
}
我会将此打印函数添加到解释器函数列表中:
interpreter.AddFunc( "print", print );
- 但是我应该在哪里调用
interpreter.AddFunc
?
我不能只是将它放在 print 函数下面,因为它必须位于根据 C++ 语法的函数中。
- 所有功能应该在哪里以及如何添加到列表中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在每个模块(io、string 等)中,定义一个向解释器注册模块的方法,例如:
如果您的模块未在类中实现,这也可以是普通函数。
然后在应用程序的主初始化中,调用所有模块的register方法。
这种方法有助于保持模块化:主应用程序初始化需要知道存在哪些模块,但导出哪些函数的详细信息则留给模块本身。
In each module (io, string, etc.), define a method that registers the module with the interpreter, e.g.:
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.
最简单的方法是保留函数名称到函数指针的
映射
,并在程序启动时加载该映射。您已经将函数链接到解释器可执行文件中,因此在调用main()
时可以访问它们。您还可以提出一个方案,其中函数在动态库(
.dll
或.so
取决于平台)中进行配置,并且一些配置文件将函数名称映射到库/入口点。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 timemain()
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.每个口译员都包含所有内容吗?如果是这样,我建议将其添加到构造函数(假设解释器是一个对象)或 init 方法中。
如果没有,您可能需要考虑在您的语言中添加“include”类型指令。然后当你遇到 include 指令时就这样做。
Is everything included in every interpreter? If so, I would recommend adding it either in a constructor (assuming the interpreter is an object) or an init method.
If not, you may want to consider adding an "include" type directive in your language. Then you do it when you encounter the include directive.