Ejemplo n.º 1
0
static void constructor (LexState *ls, expdesc *t) {
  /* constructor -> ?? */
  FuncState *fs = ls->fs;
  int line = ls->linenumber;
  int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);
  struct ConsControl cc;
  cc.na = cc.nh = cc.tostore = 0;
  cc.t = t;
  init_exp(t, VRELOCABLE, pc);
  init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
  luaK_exp2nextreg(ls->fs, t);  /* fix it at stack top (for gc) */
  checknext(ls, '{');
#if LUA_OPTIONAL_COMMA
  for (;;) {
#else
  do {
#endif /* LUA_OPTIONAL_COMMA */
    lua_assert(cc.v.k == VVOID || cc.tostore > 0);
    if (ls->t.token == '}') break;
    closelistfield(fs, &cc);
    switch(ls->t.token) {
      case TK_NAME: {  /* may be listfields or recfields */
        luaX_lookahead(ls);
        if (ls->lookahead.token != '=')  /* expression? */
          listfield(ls, &cc);
        else
          recfield(ls, &cc);
        break;
      }
      case '[': {  /* constructor_item -> recfield */
        recfield(ls, &cc);
        break;
      }
      default: {  /* constructor_part -> listfield */
        listfield(ls, &cc);
        break;
      }
    }
#if LUA_OPTIONAL_COMMA
	if (ls->t.token == ',' || ls->t.token == ';')
		next(ls);
	else if (ls->t.token == '}')
		break;
  }
#else
  } while (testnext(ls, ',') || testnext(ls, ';'));
#endif /* LUA_OPTIONAL_COMMA */
  check_match(ls, '}', '{', line);
  lastlistfield(fs, &cc);
  SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
  SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
}

/* }====================================================================== */



static void parlist (LexState *ls) {
  /* parlist -> [ param { `,' param } ] */
  FuncState *fs = ls->fs;
  Proto *f = fs->f;
  int nparams = 0;
  f->is_vararg = 0;
  if (ls->t.token != ')') {  /* is `parlist' not empty? */
    do {
      switch (ls->t.token) {
        case TK_NAME: {  /* param -> NAME */
          new_localvar(ls, str_checkname(ls), nparams++);
          break;
        }
        case TK_DOTS: {  /* param -> `...' */
          luaX_next(ls);
#if defined(LUA_COMPAT_VARARG)
          /* use `arg' as default name */
          new_localvarliteral(ls, "arg", nparams++);
          f->is_vararg = VARARG_HASARG | VARARG_NEEDSARG;
#endif
          f->is_vararg |= VARARG_ISVARARG;
          break;
        }
        default: luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
      }
    } while (!f->is_vararg && testnext(ls, ','));
  }
  adjustlocalvars(ls, nparams);
  f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG));
  luaK_reserveregs(fs, fs->nactvar);  /* reserve register for parameters */
}
Ejemplo n.º 2
0
static void constructor (LexState *ls, expdesc *t)
{
    /* constructor -> ?? */
    FuncState *fs = GetCurrentFuncState( ls );
    int line = ls->linenumber;
    int pc = luaK_codeABC(fs, OP_NEWTABLE, 0, 0, 0);

    struct ConsControl cc;
    cc.na = cc.nh = cc.tostore = 0;
    cc.t = t;

    init_exp(t, VRELOCABLE, pc);
    init_exp(&cc.v, VVOID, 0);  /* no value (yet) */
    luaK_exp2nextreg(fs, t);  /* fix it at stack top (for gc) */
    checknext(ls, '{');

    do
    {
        lua_assert(cc.v.k == VVOID || cc.tostore > 0);

        if (ls->t.token == '}')
        {
            break;
        }

        closelistfield(fs, &cc);

        switch(ls->t.token)
        {
        case TK_NAME:
        {  /* may be listfields or recfields */
            luaX_lookahead(ls);
            if (ls->lookahead.token != '=')  /* expression? */
            {
                listfield(ls, &cc);
            }
            else
            {
                recfield(ls, &cc);
            }
            break;
        }
        case '[':
        {  /* constructor_item -> recfield */
            recfield(ls, &cc);
            break;
        }
        default:
        {  /* constructor_part -> listfield */
            listfield(ls, &cc);
            break;
        }
        }
    }
    while (testnext(ls, ',') || testnext(ls, ';'));

    check_match(ls, '}', '{', line);
    lastlistfield(fs, &cc);

    SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */
    SETARG_C(fs->f->code[pc], luaO_int2fb(cc.nh));  /* set initial table size */
}