lua-users home
lua-l archive

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


I've realised I'll have to go back to the original 5.1.3 rc sources
to make a useful patch, which'd be heaps of work from my position.
(I'm not using lua from a C environment - so it would either involve
migrating 5.1.3 over to delphi or downloading and setting up another ide)
So I might just post here the changes to 5.1.2, apologies to the list 
for those not interested.

Any bug fixes to any bugs I haven't noticed would be -greatly- appreciated.

And yes, it's not a proper diff. Sorry. Only one function has been
changed though (and one added).

>Something that I always desired (and would not be intrusive) was to allow commas
to be also accepted before the items, and not only after:
Fairly easy one, in function "constructor" in "lparser.c"

  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
  checknext(ls, '{');
+ testnext(ls, ',') || testnext(ls, ';');
  do {
	lua_assert(cc.v.k == VVOID || cc.tostore > 0);

I'm sure luiz and others will have much more efficient ways, 
but this was my learn-by-experimenting way:

New function, below "recfield" in "lparser.c"

static void body (LexState *ls, expdesc *e, int needself, int line);
static void funcfield (LexState *ls, struct ConsControl *cc) {
  FuncState *fs = ls->fs;
  int reg = fs->freereg;
  expdesc key, val;
  int rkkey;
  checknext(ls, TK_FUNCTION);
  checkname(ls, &key);
  luaY_checklimit(fs, cc->nh, MAX_INT, "items in a constructor");
  cc->nh++;
  rkkey = luaK_exp2RK(fs, &key);
  body(ls, &val, 1 /* set to 0 to disable implicit self */, ls->linenumber);
  luaK_codeABC(fs, OP_SETTABLE, cc->t->u.s.info, rkkey, luaK_exp2RK(fs, &val));
  fs->freereg = reg;
}

Function "constructor" in "lparser.c"
(I've double ++ for optional commas)

  FuncState *fs = ls->fs;
  int line = ls->linenumber;
++int optionalcomma;

  do {
      lua_assert(cc.v.k == VVOID || cc.tostore > 0);
      if (ls->t.token == '}') break;
++    optionalcomma = 0;
      closelistfield(fs, &cc);

      case '[': {  /* constructor_item -> recfield */
        recfield(ls, &cc);
        break;
      }
+     case TK_FUNCTION: {
+       luaX_lookahead(ls);
+       if (ls->lookahead.token == TK_NAME) {
+         funcfield(ls, &cc);
++        optionalcomma = 1
+         break;
+       } /* else fallthrough */
+     }
      default: {  /* constructor_part -> listfield */
        listfield(ls, &cc);
        break;
      }
    }
-- } while (testnext(ls, ',') || testnext(ls, ';'));
++ } while (testnext(ls, ',') || testnext(ls, ';') || optionalcomma);
  check_match(ls, '}', '{', line);

I hope I've done it correctly, there may supposed to be
a luaK_fixline inside funcfield, to do with error messages.

- Alex