luaexpressionstatements

What exactly are expressions, and statements in Lua?


what really are expressions, and statements in Lua? its often not that clear due to things like the vararg expression '...' and that nil/true/false are also expressions.

and are table accesses expressions? it seems to make sense, because you cant just write a table access in to the file and expect it too run.


Solution

  • The Complete Syntax of Lua has the formal specification. stat is statements and exp is expressions. Anyway, here's the summary of the specific bits you asked about:

    what really are expressions, and statements in Lua?

    Basically, expressions are anything that evaluates to a value, and statements are things that get executed and can be separated by semicolons, and are usually each on their own line.

    its often not that clear due to things like the vararg expression '...' and that nil/true/false are also expressions.

    You're right about those things being expressions. It doesn't make anything unclear, though.

    and are table accesses expressions?

    Yes. They're covered by var ::= Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name, which is part of exp via prefixexp.