lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Actually I don't see much the benefit of \$ \ against ".. .." : that
only saves 3 chars, may be confusing with \n in some cases, and may
break existing code. Well you will say that then don't use patch. And I
agree !
Simply I'm just putting warning before someone makes lobbying to include
this in lua.

To give an in on how it may break existing code, I'll give an example. I
already use $ combined with string.gsub to perform multiple replacements
at once in macro generation, eventually with some formatting.
Although I don't have code like this, I may have:
function delfile(dirname, filename)
  macro = "del $dirname\$filename.txt\ndel
$dirname.back\$filename.txt\n"
  generated = string.gsub(macro, "$dirname", string.upper(dirname))
  generated = string.gsub(generated, "$filename",
string.upper(filename))
  os.execute(generated)
end


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Karel Tuma
Sent: Monday, October 23, 2006 4:04 AM
To: Lua List
Subject: [PATCH] Expressions/variables within strings (aka no more '..')

Hello list,

I'm not sure if this wasnt implemented before, but couldn't find it
anywhere so here it is:

print("Hello from \$_LUA_VERSION\ ..")

equals to:

print("Hello from ".._LUA_VERSION.." ..")

the use of escape character was chosen to avoid clashes with existing
scripts, this can be fairly simply modified though. the terminating
(\ again) character is necessary since this is only a syntactic
sugar and we have no idea about parser state in llex.c, i wanted to
keep it simple. not to mention this allows us some cool things to do,
like:

print("blah \$func("foo\$var\bar")\ baz")

thats right the \$ and \ technically equals to ".. and ..", so there
can be any expression allowed between concats, but always only one,
so print("blah \$func() func2()\ baz") is incorrect.

Note that all of this only works with "string" constants, not 'string'
nor [[string]] (again, to keep it simple :)

This patch is part of larger collection of useful patches available at
http://blua.leet.cz/ - sort of netfilter-like patch-o-matic for lua,
have a peek.