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
	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: {
      body(ls, v, 0, ls->linenumber);
case TK_DO: { //AK: "do ... end" = "function() ... end" (as a value)
      /* 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, '(');
      //check(ls, ')');
      check_match(ls, TK_END, TK_FUNCTION, line);
      pushclosure(ls, &new_fs, e);
    }  //AK: end of patch :)
    default: {
      primaryexp(ls, v);

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


Attachment: testme.lua
Description: Binary data