Sep 232013

More than a mouthful snippet!

The September 22nd Snippets post included mention of an update to programming language RiscLua, bringing it up to version 5.70, which current developer Gavin Wraith announced in August.

Gavin has since been in touch with a follow-up to his original announcement, explaining that to his chagrin he had discovered, a mere week or so ago, that one of the links on the download page was still pointing to version 5.60 – so anyone who attempted to download version 5.70 prior to then will have actually downloaded the previous version.

The RISCOSitory Snippets post was only yesterday, so anyone who ventured to the RiscLua website after reading RISCOSitory will have found the right version waiting for them – which leads to an obvious question: Why mention it again?

The answer is that in his email, Gavin went on to explain in much more detail about the change that turned version 5.60 into 5.70, and that explanation is worth posting in full. So, in Gavin’s words:

The only difference between the versions is that now you can use update operators; i.e. expressions like x + = y instead of x = x + y. The same applies to any of the operations:

  • .. string concatenation
  • + addition
  • – subtraction
  • * multiplication
  • / integer division
  • ^ power
  • % modulo
  • | logical or
  • & logical and
  • ^^ logical exclusive or
  • << shift left
  • >> logical shift right

However, this only applies to single assignments, not multi-assignments. You cannot, for example, replace x,y = x+a,y+b by x,y += a,b. That would raise an error. On the other hand, if you implement vectors as tables with metatables that overload addition using the __add event (so that + can be used for vector addition), then there should be no problem with writing x + = y for a vector assignment x = x + y.

Note that blank spaces, even newlines, can come between the operator and the equality sign.

In some circumstances update operators can make for shorter compiled code. They can certainly mean less typing. So

x[1 + f(a,b)<<c] = y

is better than

x[1 + f(a,b)<<c] = x[1 + f(a,b)<<c] + y

though I think you would have to be oblivious to the mantra “never evaluate the same expression twice in the same program” to perpetrate such a horror. A sensible person (using a previous version of RiscLua) would have introduced an auxiliary variable and written

local z = 1 + f(a,b)<<c
x[z] = x[z] + y

but in version 5.70 the auxiliary variable is no longer needed; in effect it is introduced automatically by the compiler.

Update operators are not a feature of standard Lua. They have been introduced in RiscLua because BBC Basic has them for addition and subtraction, and so may be reasonably familiar.