tags:

views:

372

answers:

4

In Lua, using the = operator without an l-value seems to be equivalent to a print(r-value), here are a few examples run in the Lua standalone interpreter:

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

And so on...

My question is : where can I find a detailed description of this use for the = operator? How does it work? Is it by implying a special default l-value? I guess the root of my problem is that I have no clue what to type in Google to find info about it :-)

edit:

Thanks for the answers, you are right it's a feature of the interpreter. Silly question, for I don't know which reason I completely overlooked the obvious. I should avoid posting before the morning coffee :-) For completeness, here is the code dealing with this in the interpreter:

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)));
  }
}

edit2:

To be really complete, the whole trick about pushing values on the stack is in the "pushline" function:

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

I think that must be a feature of the stand alone interpreter. I can't make that work on anything I have compiled lua into.

Arle Nadja
A: 

I wouldn't call it a feature - the interpreter just returns the result of the statement. It's his job, isn't it?

unexist
but why doesn't it barf at the wrong syntax?
Vinko Vrsalovic
Shouldn't it print 8 on "a = 8" then, too? I know many scriping consoles do that (python, irb, perl -d -e 0 etc.), but I just tried it LUA doesn't print the value of all statements, just "= ...".
jkramer
A: 

Assignment isn't an expression that returns something in lua like it is in C.

Arle Nadja
+3  A: 

Quoting the man page:

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.

Hugh Allen