lua-users home
lua-l archive

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



Changing Lua 5.x syntax:
	
	function → function funcbody
to:
	function → function funcbody | do block end


I proposed this a couple of days ago, to get easier 'switch' syntax (declaring little funcs as data members). Now, I took my first lesson on touching Lua source internally, and it wasn't hard! The following change (lparser.c, line 776 in 5.1w1) does this, and has no side effects whatsoever.

It could be better implemented by changing the 'body' function (additional "no paranthesis" flag) but I wanted to have it all in one place.

Here goes:

    case TK_FUNCTION: {
      next(ls);
      body(ls, v, 0, ls->linenumber);
      return;
    }
>>>
case TK_DO: { //AK: "do ... end" = "function() ... end" (as a value)
      next(ls);
      /* body_noparam ->  chunk END */
      {
      int line= ls->linenumber;
      expdesc *e= v;
      //
      FuncState new_fs;
      open_func(ls, &new_fs);
      new_fs.f->lineDefined = line;
      //check(ls, '(');
      //parlist(ls);
      //check(ls, ')');
      chunk(ls);
      check_match(ls, TK_END, TK_FUNCTION, line);
      close_func(ls);
      pushclosure(ls, &new_fs, e);
      }
      return;
    }  //AK: end of patch :)
<<<
    default: {
      primaryexp(ls, v);
      return;
    }

Attached is a small stupid test piece to see it works, and doesn't affect the do..end usage elsewhere.

-ak

Attachment: testme.lua
Description: Binary data