Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua = operator as print

Tags:

lua

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' */
like image 252
Drealmer Avatar asked Sep 27 '08 08:09

Drealmer


People also ask

What is %f Lua?

In Lua, the following are the available string formatting syntax or string literals for different datatypes: %s : This serves as a placeholder for string values in a formatted string. %d : This will stand in for integer numbers. %f : This is the string literal for float values.

What does ~= mean in Lua?

The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types.

What does print Do in Lua?

Definition of Lua print. Lua print in Lua programming language is an inbuilt function that is used for printing the required statement in the console.

What does operator mean in Lua?

An operator is a symbol that tells the interpreter to perform specific mathematical or logical manipulations. Lua language is rich in built-in operators and provides the following type of operators − Arithmetic Operators. Relational Operators.


2 Answers

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.

like image 191
Hugh Allen Avatar answered Nov 15 '22 11:11

Hugh Allen


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.

like image 38
Arle Nadja Avatar answered Nov 15 '22 11:11

Arle Nadja