lua-users home
lua-l archive

nil as lvalue (was Re: Proposal: Trailing comma in function calls)

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


On Thursday, June 05, 2014 09:50:30 AM Axel Kittenberger wrote:
> > Alternately allow `nil` as a lvalue that acts as the bit bucket.
> 
> While I'd appreciate something like that can we please keep some focus on
> the original proposal? And you know the rule, make a proposal, provide a
> patch.
Actually, I can't say I know that rule. But ignorance of the law is not an 
excuse.
In any case, I hacked a bit and got this to (seemingly) work
 a,nil,c = foo()
nil in a `local` declaration list is going to take a while longer because of 
the optimization that occurs. And I haven't looked at `for` yet. But this is 
what I'm aiming towards:
 stat ::= varlist '=' explist |
 local varnamelist ['=' explist] |
 for varnamelist in explist do block end
 varlist ::= var {',' var}
 var ::= Name | prefixexp '[' exp ']' | prefixexp '.' Name | nil
 varnamelist ::= varname {',' varname}
 varname ::= Name | nil
And the patch right now (against Lua 5.3work2):
diff --git a/src/lcode.c b/src/lcode.c
index 2e8b3fb..6d719ee 100644
--- a/src/lcode.c
+++ b/src/lcode.c
@@ -613,6 +613,10 @@ luaK_storevar
 luaK_codeABC(fs, op, var->u.ind.t, var->u.ind.idx, e);
 break;
 }
+ case VNIL: {
+ freeexp(fs, ex);
+ return;
+ }
 default: {
 lua_assert(0); /* invalid var kind to store */
 break;
diff --git a/src/lparser.c b/src/lparser.c
index 9349149..cec3198 100644
--- a/src/lparser.c
+++ b/src/lparser.c
@@ -871,6 +871,11 @@ primaryexp
 luaK_dischargevars(ls->fs, v);
 return;
 }
+ case TK_NIL: {
+ init_exp(v, VNIL, 0);
+ luaX_next(ls);
+ return;
+ }
 case TK_NAME: {
 singlevar(ls, v);
 return;
@@ -1138,7 +1143,7 @@
 
 static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
 expdesc e;
- check_condition(ls, vkisvar(lh->v.k), "syntax error");
+ check_condition(ls, (vkisvar(lh->v.k) | (lh->v.k==VNIL)), "syntax error");
 if (testnext(ls, ',')) { /* assignment -> ',' suffixedexp assignment */
 struct LHS_assign nv;
 nv.prev = lh;
-- 
tom <telliamed@whoopdedo.org>

AltStyle によって変換されたページ (->オリジナル) /