aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto I <roberto@inf.puc-rio.br>2025-10-30 11:07:01 -0300
committerRoberto I <roberto@inf.puc-rio.br>2025-10-30 11:07:01 -0300
commitd342328e5b24c9b3c6c5b33bfcf9f8534210b8e6 (patch)
tree033163b79dc14b69fc8d7160ae38cf3d9181533d
parent0149b781d438091ce086449101a916e9b4456b4e (diff)
downloadlua-d342328e5b24c9b3c6c5b33bfcf9f8534210b8e6.tar.gz
lua-d342328e5b24c9b3c6c5b33bfcf9f8534210b8e6.tar.bz2
lua-d342328e5b24c9b3c6c5b33bfcf9f8534210b8e6.zip
Vertical bar removed from syntax of vararg table
The syntax 'function foo (a, b, ...arg)' is already used by JavaScript for this same semantics, so it seems natural to use the same notation in Lua.
-rw-r--r--lparser.c4
-rw-r--r--manual/manual.of8
-rw-r--r--testes/locals.lua4
-rw-r--r--testes/memerr.lua4
-rw-r--r--testes/vararg.lua22
5 files changed, 20 insertions, 22 deletions
diff --git a/lparser.c b/lparser.c
index e3538c16..40a30ff6 100644
--- a/lparser.c
+++ b/lparser.c
@@ -1079,8 +1079,8 @@ static void parlist (LexState *ls) {
1079 } 1079 }
1080 case TK_DOTS: { 1080 case TK_DOTS: {
1081 varargk |= PF_ISVARARG; 1081 varargk |= PF_ISVARARG;
1082 luaX_next(ls); 1082 luaX_next(ls); /* skip '...' */
1083 if (testnext(ls, '|')) { 1083 if (ls->t.token == TK_NAME) {
1084 new_varkind(ls, str_checkname(ls), RDKVAVAR); 1084 new_varkind(ls, str_checkname(ls), RDKVAVAR);
1085 varargk |= PF_VAVAR; 1085 varargk |= PF_VAVAR;
1086 } 1086 }
diff --git a/manual/manual.of b/manual/manual.of
index ad273d62..0127df02 100644
--- a/manual/manual.of
+++ b/manual/manual.of
@@ -2354,8 +2354,7 @@ initialized with the argument values:
2354@Produc{ 2354@Produc{
2355@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or 2355@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
2356 varargparam} 2356 varargparam}
2357@producname{varargparam}@producbody{@bnfter{...} 2357@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}}
2358 @bnfopt{@bnfter{|} @bnfNter{Name}}}
2359} 2358}
2360When a Lua function is called, 2359When a Lua function is called,
2361it adjusts its list of @x{arguments} to 2360it adjusts its list of @x{arguments} to
@@ -2396,7 +2395,7 @@ g(5, r()) a=5, b=1, ... -> 2 3
2396} 2395}
2397 2396
2398The presence of a varag table in a variadic function is indicated 2397The presence of a varag table in a variadic function is indicated
2399by the @T{|name} syntax after the three dots. 2398by a name after the three dots.
2400When present, 2399When present,
2401a vararg table behaves like a read-only local variable 2400a vararg table behaves like a read-only local variable
2402with the given name that is initialized with a table. 2401with the given name that is initialized with a table.
@@ -9773,8 +9772,7 @@ and @bnfNter{LiteralString}, see @See{lexical}.)
9773@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or 9772@producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or
9774 varargparam} 9773 varargparam}
9775 9774
9776@producname{varargparam}@producbody{@bnfter{...} 9775@producname{varargparam}@producbody{@bnfter{...} @bnfopt{@bnfNter{Name}}}
9777 @bnfopt{@bnfter{|} @bnfNter{Name}}}
9778 9776
9779@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} 9777@producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}}
9780 9778
diff --git a/testes/locals.lua b/testes/locals.lua
index 5222802f..6cd10547 100644
--- a/testes/locals.lua
+++ b/testes/locals.lua
@@ -310,7 +310,7 @@ do -- testing presence of second argument
310 local function foo (howtoclose, obj, n) 310 local function foo (howtoclose, obj, n)
311 local ca -- copy of 'a' visible inside its close metamethod 311 local ca -- copy of 'a' visible inside its close metamethod
312 do 312 do
313 local a <close> = func2close(function (... | t) 313 local a <close> = func2close(function (...t)
314 assert(select("#", ...) == n) 314 assert(select("#", ...) == n)
315 assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj)) 315 assert(t.n == n and t[1] == ca and (t.n < 2 or t[2] == obj))
316 ca = 15 -- final value to be returned if howtoclose=="scope" 316 ca = 15 -- final value to be returned if howtoclose=="scope"
@@ -910,7 +910,7 @@ do
910 910
911 local extrares -- result from extra yield (if any) 911 local extrares -- result from extra yield (if any)
912 912
913 local function check (body, extra, ...|t) 913 local function check (body, extra, ...t)
914 local co = coroutine.wrap(body) 914 local co = coroutine.wrap(body)
915 if extra then 915 if extra then
916 extrares = co() -- runs until first (extra) yield 916 extrares = co() -- runs until first (extra) yield
diff --git a/testes/memerr.lua b/testes/memerr.lua
index 69d2ef85..2cc8f481 100644
--- a/testes/memerr.lua
+++ b/testes/memerr.lua
@@ -126,13 +126,13 @@ testamem("coroutine creation", function()
126end) 126end)
127 127
128do -- vararg tables 128do -- vararg tables
129 local function pack (... | t) return t end 129 local function pack (...t) return t end
130 local b = testamem("vararg table", function () 130 local b = testamem("vararg table", function ()
131 return pack(10, 20, 30, 40, "hello") 131 return pack(10, 20, 30, 40, "hello")
132 end) 132 end)
133 assert(b.aloc == 3) -- new table uses three memory blocks 133 assert(b.aloc == 3) -- new table uses three memory blocks
134 -- table optimized away 134 -- table optimized away
135 local function sel (n, ...|arg) return arg[n] + arg.n end 135 local function sel (n, ...arg) return arg[n] + arg.n end
136 local b = testamem("optimized vararg table", 136 local b = testamem("optimized vararg table",
137 function () return sel(2.0, 20, 30) end) 137 function () return sel(2.0, 20, 30) end)
138 assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case 138 assert(b.res == 32 and b.aloc == 0) -- no memory needed for this case
diff --git a/testes/vararg.lua b/testes/vararg.lua
index 840c3eee..a01598ff 100644
--- a/testes/vararg.lua
+++ b/testes/vararg.lua
@@ -3,7 +3,7 @@
3 3
4print('testing vararg') 4print('testing vararg')
5 5
6local function f (a, ...|t) 6local function f (a, ...t)
7 local x = {n = select('#', ...), ...} 7 local x = {n = select('#', ...), ...}
8 assert(x.n == t.n) 8 assert(x.n == t.n)
9 for i = 1, x.n do 9 for i = 1, x.n do
@@ -20,7 +20,7 @@ local function c12 (...)
20 return res, 2 20 return res, 2
21end 21end
22 22
23local function vararg (... | t) return t end 23local function vararg (... t) return t end
24 24
25local call = function (f, args) return f(table.unpack(args, 1, args.n)) end 25local call = function (f, args) return f(table.unpack(args, 1, args.n)) end
26 26
@@ -153,8 +153,8 @@ end
153 153
154 154
155do -- vararg parameter used in nested functions 155do -- vararg parameter used in nested functions
156 local function foo (... | tab1) 156 local function foo (...tab1)
157 return function (... | tab2) 157 return function (...tab2)
158 return {tab1, tab2} 158 return {tab1, tab2}
159 end 159 end
160 end 160 end
@@ -165,11 +165,11 @@ do -- vararg parameter used in nested functions
165end 165end
166 166
167do -- vararg parameter is read-only 167do -- vararg parameter is read-only
168 local st, msg = load("return function (... | t) t = 10 end") 168 local st, msg = load("return function (... t) t = 10 end")
169 assert(string.find(msg, "const variable 't'")) 169 assert(string.find(msg, "const variable 't'"))
170 170
171 local st, msg = load[[ 171 local st, msg = load[[
172 local function foo (... | extra) 172 local function foo (...extra)
173 return function (...) extra = nil end 173 return function (...) extra = nil end
174 end 174 end
175 ]] 175 ]]
@@ -179,19 +179,19 @@ end
179 179
180do -- _ENV as vararg parameter 180do -- _ENV as vararg parameter
181 local st, msg = load[[ 181 local st, msg = load[[
182 local function aux (... | _ENV) 182 local function aux (... _ENV)
183 global <const> a 183 global <const> a
184 a = 10 184 a = 10
185 end ]] 185 end ]]
186 assert(string.find(msg, "const variable 'a'")) 186 assert(string.find(msg, "const variable 'a'"))
187 187
188 local function aux (... | _ENV) 188 local function aux (..._ENV)
189 global a; a = 10 189 global a; a = 10
190 return a 190 return a
191 end 191 end
192 assert(aux() == 10) 192 assert(aux() == 10)
193 193
194 local function aux (... | _ENV) 194 local function aux (... _ENV)
195 global a = 10 195 global a = 10
196 return a 196 return a
197 end 197 end
@@ -200,7 +200,7 @@ end
200 200
201 201
202do -- access to vararg parameter 202do -- access to vararg parameter
203 local function notab (keys, t, ... | v) 203 local function notab (keys, t, ...v)
204 for _, k in pairs(keys) do 204 for _, k in pairs(keys) do
205 assert(t[k] == v[k]) 205 assert(t[k] == v[k])
206 end 206 end
@@ -216,7 +216,7 @@ do -- access to vararg parameter
216 assert(m == collectgarbage"count") 216 assert(m == collectgarbage"count")
217 217
218 -- writing to the vararg table 218 -- writing to the vararg table
219 local function foo (... | t) 219 local function foo (...t)
220 t[1] = t[1] + 10 220 t[1] = t[1] + 10
221 return t[1] 221 return t[1]
222 end 222 end