Lua =运算符作为打印

时间:2020-03-06 14:49:16  来源:igfitidea点击:

在Lua中,使用=运算符而不使用l值似乎等同于print(r-value),下面是在Lua独立解释器中运行的一些示例:

> = a
nil
> a = 8
> = a
8
> = 'hello'
hello
> = print
function: 003657C8

等等...

我的问题是:在哪里可以找到=运算符的用法的详细说明?它是如何工作的?是否暗指特殊的默认l值?我想我问题的根源在于我不知道在Google键入什么来查找有关它的信息:-)

编辑:

感谢回答,我们是对的,它是解释器的功能。愚蠢的问题,因为我不知道是什么原因让我完全忽略了显而易见的原因。我应该避免在早上喝咖啡前发布:-)为了完整起见,这是解释器中处理此问题的代码:

while ((status = loadline(L)) != -1) {
  if (status == 0) status = docall(L, 0, 0);
  report(L, status);
  if (status == 0 && lua_gettop(L) > 0) {  /* any result to print? */
    lua_getglobal(L, "print");
    lua_insert(L, 1);
    if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
      l_message(progname, lua_pushfstring(L,
                           "error calling " LUA_QL("print") " (%s)",
                           lua_tostring(L, -1)));
  }
}

编辑2:

实际上,关于将值压入堆栈的整个技巧都在" pushline"函数中:

if (firstline && b[0] == '=')  /* first line starts with `=' ? */
  lua_pushfstring(L, "return %s", b+1);  /* change it to `return' */

解决方案

我认为这必须是独立解释器的功能。我无法将lua编译成任何东西。

我不会将其称为解释器仅返回语句结果的功能。这是他的工作,不是吗?

赋值不是在Lua中返回某些内容的表达式,就像在C中那样返回。

引用手册页:

In interactive mode ... If a line starts with '=', then lua displays the values of all the expressions in the remainder of the line. The expressions must be separated by commas.