[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: bug/feature: 'if 1then print"valid syntax!"end' is valid syntax
- From: Charles Heywood <vandor2012@...>
- Date: Mon, 08 Aug 2016 23:16:09 +0000
It's not really a bug. Variables can't start with a number so when it detects something incompatible with a number it reads it as a variable.
(I strongly suspect this is known and I think I've seen this before
somewhere but couldn't find it in the archive... so here goes (again?):)
Lua 5.3 and 5.2 accept this (Lua 5.1/LuaJIT don't):
_=0_=1._=2local y x=3goto foo x=4while y do end x=5repeat x=6return
7until x::foo::if 8then x=9print'"valid" syntax!'end
Specifically, any char that cannot be part of the number implicitly ends
the token. (That is, the expected ones (space/symbols) *plus* [g-zG-Z_]
with the exception of (1) [xX] in initial '0x' or '0X' (but after two
digits like 00x or 00X it's a "valid" boundary again), and (2) [pP] for
hex literals where an exponent might follow.) Compare
llex.c/read_numeral  for the details.
While it exists, it's a nice code golfing tool:
m=1n=1repeat print(m/n)m,n=m+n,m until m<0
Whether it's worth break^Wfixing is debatable... the simplest fix is
clearly "just don't write it like that!"