Search code examples
lua

Lua '=' expected near '+'


I get an error while using the ZeroBrain IDE studio with Lua code.

a = 15
a + 2
a ^ 2
math.pi / 4

I am new to lua's programming syntax so I am not sure what is wrong with the code. When I run it in the stand-alone interpreter line by line it works fine, but while running it as a chunk I encounter the error in the topic.


Solution

  • Some interactive interpreters (including the one included in ZeroBrane Studio) have two roles: the accept and execute statements, but can also accept and evaluate expressions, which are not valid to be included in Lua code by themselves.

    In other words, you can write if not a then a = 1 end and then 2*a in the interpreter (and it should return 2), but you can't do the same thing in the Lua code (as 2*a is not a valid statement) and you'll have to write something like print(2*a) to make it valid.