REPL(读取评估打印循环)与 Mathlink (Wolfram Mathematica)
我是 Mathlink 的新手,在将其集成到我的代码中之前,我尝试编写一个小的 REPL 来习惯它。代码如下(省略了不相关的部分,并对 C 和 C++ 的可怕混合感到抱歉):
int main(int argc,char **argv)
{
init_and_openlink(argc,argv);
while(!feof(stdin))
{
int pkt;
char buf[1024];
if(!fgets(buf,1024,stdin))
continue;
MLPutFunction(lp,"EnterTextPacket",1);
MLPutString(lp,buf);
MLEndPacket(lp);
while(((pkt=MLNextPacket(lp),pkt))&&(pkt!=RETURNPKT))
{
MLNewPacket(lp);
if(MLError(lp))
return 1;
}
const char *result;
MLGetString(lp,&result);
printf("%s\n",result);
MLReleaseString(lp,result);
}
return 0;
}
但它似乎根本不起作用。我尝试用单个 MLNextPacket 指令替换 while 循环,但没有成功;我花了几个小时搜索 Mathlink 文档,但那是一团糟!我哪里做错了?
I'm new to Mathlink, and before integrating it in my code I tried to write a small REPL to get accustomed to it. The code is as follows (irrelevent parts omitted, and sorry for the horrible blend of C and C++):
int main(int argc,char **argv)
{
init_and_openlink(argc,argv);
while(!feof(stdin))
{
int pkt;
char buf[1024];
if(!fgets(buf,1024,stdin))
continue;
MLPutFunction(lp,"EnterTextPacket",1);
MLPutString(lp,buf);
MLEndPacket(lp);
while(((pkt=MLNextPacket(lp),pkt))&&(pkt!=RETURNPKT))
{
MLNewPacket(lp);
if(MLError(lp))
return 1;
}
const char *result;
MLGetString(lp,&result);
printf("%s\n",result);
MLReleaseString(lp,result);
}
return 0;
}
but it doesn't seem to work at all. I've tried replacing the while loop with a single MLNextPacket instruction but to no avail; I spent hours searching Mathlink documentation, but that one is a big mess! Where I'm doing it wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EnterTextPacket MathLink 数据包将使内核返回包装在 ReturnTextPacket MathLink 数据包。尝试将 while 循环中的条件更改为:
The EnterTextPacket MathLink packet will make the kernel return the result wrapped in a ReturnTextPacket MathLink packet. Try changing the condition in your while loop to: