Пример #1
0
static void chunk (LexState *ls) {
  /* chunk -> { stat [`;'] } */
  int islast = 0;
  enterlevel(ls);
  while (!islast && !block_follow(ls->t.token)) {
    islast = statement(ls);
    testnext(ls, ';');
    lua_assert(ls->fs->freereg >= ls->fs->nactvar);
    ls->fs->freereg = ls->fs->nactvar;  /* free registers */
  }
  leavelevel(ls);
}
Пример #2
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, '{');
  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 */
}
Пример #3
0
static int statement (LexState *ls) {
  int line = ls->linenumber;  /* may be needed for error messages */
  switch (ls->t.token) {
    case TK_IF: {  /* stat -> ifstat */
      ifstat(ls, line);
      return 0;
    }
    case TK_WHILE: {  /* stat -> whilestat */
      whilestat(ls, line);
      return 0;
    }
    case TK_DO: {  /* stat -> DO block END */
      luaX_next(ls);  /* skip DO */
      block(ls);
      check_match(ls, TK_END, TK_DO, line);
      return 0;
    }
    case TK_FOR: {  /* stat -> forstat */
      forstat(ls, line);
      return 0;
    }
    case TK_REPEAT: {  /* stat -> repeatstat */
      repeatstat(ls, line);
      return 0;
    }
    case TK_FUNCTION: {
      funcstat(ls, line);  /* stat -> funcstat */
      return 0;
    }
    case TK_LOCAL: {  /* stat -> localstat */
      luaX_next(ls);  /* skip LOCAL */
      if (testnext(ls, TK_FUNCTION))  /* local function? */
        localfunc(ls);
      else
        localstat(ls);
      return 0;
    }
    case TK_RETURN: {  /* stat -> retstat */
      retstat(ls);
      return 1;  /* must be last statement */
    }
    case TK_BREAK: {  /* stat -> breakstat */
      luaX_next(ls);  /* skip BREAK */
      breakstat(ls);
      return 1;  /* must be last statement */
    }
    default: {
      exprstat(ls);
      return 0;  /* to avoid warnings */
    }
  }
}
Пример #4
0
static void check_match(ktap_lexstate *ls, int what, int who, int where)
{
	if (!testnext(ls, what)) {
		if (where == ls->linenumber)
			error_expected(ls, what);
		else {
			lex_syntaxerror(ls, ktapc_sprintf(
					"%s expected (to close %s at line %d)",
					lex_token2str(ls, what),
					lex_token2str(ls, who), where));
		}
	}
}
Пример #5
0
static int explist1(LexState* ls, expdesc* v)
{
	/* explist1 -> expr { `,' expr } */
	int n = 1;  /* at least one expression */
	expr(ls, v);
	while (testnext(ls, ','))
	{
		luaK_exp2nextreg(ls->fs, v);
		expr(ls, v);
		n++;
	}
	return n;
}
Пример #6
0
static void assignment (LexState *ls, struct LHS_assign *lh, int nvars)
{
    FuncState *fs = GetCurrentFuncState( ls );
    expdesc e;

    check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED, "syntax error");

    if (testnext(ls, ','))
    {  /* assignment -> `,' primaryexp assignment */
        struct LHS_assign nv;
        nv.prev = lh;

        primaryexp(ls, &nv.v);

        if (nv.v.k == VLOCAL)
        {
            check_conflict(ls, lh, &nv.v);
        }

        luaY_checklimit(fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls, "variables in assignment");

        assignment(ls, &nv, nvars+1);
    }
    else
    {  /* assignment -> `=' explist1 */
        int nexps;
        checknext(ls, '=');
        nexps = explist1(ls, &e);

        if (nexps != nvars)
        {
            adjust_assign(ls, nvars, nexps, &e);

            if (nexps > nvars)
            {
                fs->freereg -= nexps - nvars;  /* remove extra values */
            }
        }
        else
        {
            luaK_setoneret(fs, &e);  /* close last expression */
            luaK_storevar(fs, &lh->v, &e);
            return;  /* avoid default */
        }
    }

    init_exp(&e, VNONRELOC, fs->freereg-1);  /* default assignment */

    luaK_storevar(fs, &lh->v, &e);
}
Пример #7
0
static void check_match(LexState* ls, int what, int who, int where)
{
	if (!testnext(ls, what))
	{
		if (where == ls->linenumber)
			error_expected(ls, what);
		else
		{
			luaX_syntaxerror(ls, luaO_pushfstring(ls->L,
							      LUA_QS " expected (to close " LUA_QS " at line %d)",
							      luaX_token2str(ls, what), luaX_token2str(ls, who), where));
		}
	}
}
Пример #8
0
static void parlist (LexState *ls) {
  /* parlist -> [ param { `,' param } ] */
  int nparams = 0;
  int dots = 0;
  if (ls->t.token != ')') {  /* is `parlist' not empty? */
    do {
      switch (ls->t.token) {
        case TK_DOTS: dots = 1; next(ls); break;
        case TK_NAME: new_localvar(ls, str_checkname(ls), nparams++); break;
        default: luaX_syntaxerror(ls, "<name> or `...' expected");
      }
    } while (!dots && testnext(ls, ','));
  }
  code_params(ls, nparams, dots);
}
Пример #9
0
static void assignment (LexState *ls, struct LHS_assign *lh, int nvars, BinOpr *opr) {
  expdesc e;
  check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
                      "syntax error");
  if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */
    struct LHS_assign nv;
    nv.prev = lh;
    primaryexp(ls, &nv.v);
    if (nv.v.k == VLOCAL)
      check_conflict(ls, lh, &nv.v);
    assignment(ls, &nv, nvars+1, opr);
  }
  else {  /* assignment -> `=' explist1 */
    int nexps;

	*opr = OPR_NOBINOPR;
	switch(ls->t.token)
	{
	case '=': break;
	case TK_CONCATASSIGN: *opr = OPR_CONCAT; break;
	case TK_ADDASSIGN: *opr = OPR_ADD; break;
	case TK_SUBASSIGN: *opr = OPR_SUB; break;
	case TK_MULASSIGN: *opr = OPR_MUL; break;
	case TK_DIVASSIGN: *opr = OPR_DIV; break;
	case TK_POWASSIGN: *opr = OPR_POW; break;
	case TK_MODASSIGN: *opr = OPR_MOD; break;
	default:
		luaX_syntaxerror(ls, "unexpected symbol");
		break;
	};
	luaX_next(ls);
    nexps = explist1(ls, &e);
    if (nexps != nvars) {
      adjust_assign(ls, nvars, nexps, &e);
      if (nexps > nvars)
        ls->fs->freereg -= nexps - nvars;  /* remove extra values */
    }
    else {
      luaK_setoneret(ls->fs, &e);  /* close last expression */
      luaK_storevar(ls->fs, &lh->v, &e, *opr);
      return;  /* avoid default */
    }
  }
  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
  luaK_storevar(ls->fs, &lh->v, &e, *opr);
}
Пример #10
0
static void chunk (LexState *ls)
{
    /* chunk -> { stat [`;'] } */
    int islast = 0;
    callstack_ref cref( *ls->L );

    while (!islast && !block_follow(ls->t.token))
    {
        islast = statement(ls);
        testnext(ls, ';');

        FuncState *fs = GetCurrentFuncState( ls );

        lua_assert(fs->f->maxstacksize >= fs->freereg && fs->freereg >= fs->nactvar);

        fs->freereg = fs->nactvar;  /* free registers */
    }
}
Пример #11
0
static void forlist (LexState *ls, TString *indexname) {
  /* forlist -> NAME {,NAME} IN explist1 DO body */
  FuncState *fs = ls->fs;
  expdesc e;
  int nvars = 0;
  int line;
  int base = fs->freereg;
  new_localvarstr(ls, "(for generator)", nvars++);
  new_localvarstr(ls, "(for state)", nvars++);
  new_localvar(ls, indexname, nvars++);
  while (testnext(ls, ','))
    new_localvar(ls, str_checkname(ls), nvars++);
  check(ls, TK_IN);
  line = ls->linenumber;
  adjust_assign(ls, nvars, explist1(ls, &e), &e);
  luaK_checkstack(fs, 3);  /* extra space to call generator */
  luaK_codeAsBx(fs, OP_TFORPREP, base, NO_JUMP);
  forbody(ls, base, line, nvars, 0);
}
Пример #12
0
static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  expdesc e;
  check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
                      "syntax error");
  if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */
    struct LHS_assign nv;
    nv.prev = lh;
    primaryexp(ls, &nv.v);
    if (nv.v.k == VLOCAL)
      check_conflict(ls, lh, &nv.v);
    luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
                    "variables in assignment");
    assignment(ls, &nv, nvars+1);
  }
  else {  /* assignment -> `=' explist1 */
    int nexps;
      
      /* hook for inc_assignment */
      if(nvars==1) {
          switch(ls->t.token) {
              case '+': case '-': case '*': case '/': case TK_CONCAT:
                  inc_assignment(ls,lh);
                  /* If you're using Shook's table unpack patch, return 0 here.*/
                  return;
          }
      }
      
    checknext(ls, '=');
    nexps = explist1(ls, &e);
    if (nexps != nvars) {
      adjust_assign(ls, nvars, nexps, &e);
      if (nexps > nvars)
        ls->fs->freereg -= nexps - nvars;  /* remove extra values */
    }
    else {
      luaK_setoneret(ls->fs, &e);  /* close last expression */
      luaK_storevar(ls->fs, &lh->v, &e);
      return;  /* avoid default */
    }
  }
  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
  luaK_storevar(ls->fs, &lh->v, &e);
}
Пример #13
0
static void fornum (LexState *ls, TString *varname, int line) {
  /* fornum -> NAME = exp1,exp1[,exp1] forbody */
  FuncState *fs = ls->fs;
  int base = fs->freereg;
  new_localvarliteral(ls, "(for index)", 0);
  new_localvarliteral(ls, "(for limit)", 1);
  new_localvarliteral(ls, "(for step)", 2);
  new_localvar(ls, varname, 3);
  checknext(ls, '=');
  exp1(ls);  /* initial value */
  checknext(ls, ',');
  exp1(ls);  /* limit */
  if (testnext(ls, ','))
    exp1(ls);  /* optional step */
  else {  /* default step = 1 */
    luaK_codeABx(fs, OP_LOADK, fs->freereg, luaK_numberK(fs, 1));
    luaK_reserveregs(fs, 1);
  }
  forbody(ls, base, line, 1, 1);
}
Пример #14
0
static void parlist (LexState *ls)
{
    /* parlist -> [ param { `,' param } ] */
    FuncState *fs = GetCurrentFuncState( ls );
    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);
                f->is_vararg |= VARARG_ISVARARG;
                break;
            }
            default:
            {
                luaX_syntaxerror(ls, "<name> or " LUA_QL("...") " expected");
                break;
            }
            }
        }
        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 */
}
Пример #15
0
static void forlist (LexState *ls, TString *indexname) {
  /* forlist -> NAME {,NAME} IN explist1 forbody */
  FuncState *fs = ls->fs;
  expdesc e;
  int nvars = 0;
  int line;
  int base = fs->freereg;
  /* create control variables */
  new_localvarliteral(ls, "(for generator)", nvars++);
  new_localvarliteral(ls, "(for state)", nvars++);
  new_localvarliteral(ls, "(for control)", nvars++);
  /* create declared variables */
  new_localvar(ls, indexname, nvars++);
  while (testnext(ls, ','))
    new_localvar(ls, str_checkname(ls), nvars++);
  checknext(ls, TK_IN);
  line = ls->linenumber;
  adjust_assign(ls, 3, explist1(ls, &e), &e);
  luaK_checkstack(fs, 3);  /* extra space to call generator */
  forbody(ls, base, line, nvars - 3, 0);
}
Пример #16
0
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 */
}
Пример #17
0
static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) {
  expdesc e;
  check_condition(ls, VLOCAL <= lh->v.k && lh->v.k <= VINDEXED,
                      "syntax error");
  if (testnext(ls, ',')) {  /* assignment -> `,' primaryexp assignment */
    struct LHS_assign nv;
    nv.prev = lh;
    primaryexp(ls, &nv.v);
    if (nv.v.k == VLOCAL)
      check_conflict(ls, lh, &nv.v);
    luaY_checklimit(ls->fs, nvars, LUAI_MAXCCALLS - ls->L->nCcalls,
                    "variables in assignment");
    assignment(ls, &nv, nvars+1);
  }
  else {  /* assignment -> `=' explist1 */
    int nexps;
#if LUA_MUTATION_OPERATORS
    luaX_next(ls); /* consume `=' token. */
#else
    checknext(ls, '=');
#endif /* LUA_MUTATION_OPERATORS */
    nexps = explist1(ls, &e);
    if (nexps != nvars) {
      adjust_assign(ls, nvars, nexps, &e);
      if (nexps > nvars)
        ls->fs->freereg -= nexps - nvars;  /* remove extra values */
    }
    else {
      luaK_setoneret(ls->fs, &e);  /* close last expression */
      luaK_storevar(ls->fs, &lh->v, &e);
      return;  /* avoid default */
    }
  }
  init_exp(&e, VNONRELOC, ls->fs->freereg-1);  /* default assignment */
  luaK_storevar(ls->fs, &lh->v, &e);
}
Пример #18
0
static void check (LexState *ls, int c) {
  if (!testnext(ls, c))
    error_expected(ls, c);
}
Пример #19
0
static int statement (LexState *ls) {
  int line = ls->linenumber;  /* may be needed for error messages */
  switch (ls->t.token) {
    case TK_IF: {  /* stat -> ifstat */
      ifstat(ls, line);
      return 0;
    }
    case TK_WHILE: {  /* stat -> whilestat */
      whilestat(ls, line);
      return 0;
    }
    case TK_DO: {  /* stat -> DO block END */
      luaX_next(ls);  /* skip DO */
      block(ls);
      check_match(ls, TK_END, TK_DO, line);
      return 0;
    }
    case TK_FOR: {  /* stat -> forstat */
      forstat(ls, line);
      return 0;
    }
    case TK_REPEAT: {  /* stat -> repeatstat */
      repeatstat(ls, line);
      return 0;
    }
    case TK_FUNCTION: {
      funcstat(ls, line);  /* stat -> funcstat */
      return 0;
    }
    case TK_LOCAL: {  /* stat -> localstat */
      luaX_next(ls);  /* skip LOCAL */
      if (testnext(ls, TK_FUNCTION))  /* local function? */
        localfunc(ls);
      else
        localstat(ls);
      return 0;
    }
    case TK_RETURN: {  /* stat -> retstat */
      retstat(ls);
      return 1;  /* must be last statement */
    }
    case TK_BREAK: {  /* stat -> breakstat */
      luaX_next(ls);  /* skip BREAK */
      if (testnext(ls, TK_NUMBER)) { /* multi scope 'break n' */
        breakstat(ls, ls->t.seminfo.r);
      } else breakstat(ls, 1);
      return 1;  /* must be last statement */
    }
    case TK_CONTINUE: {  /* stat -> continuestat */
      luaX_next(ls);  /* skip CONTINUE */
      continuestat(ls);
      return 1;	  /* must be last statement */
    }
    case TK_INC: {  /* added 0.5.2.*/
      luaX_next(ls);
      changevalue(ls, OP_ADD);
      return 0;
    }
    case TK_DEC: {  /* added 0.5.2.*/
      luaX_next(ls);
      changevalue(ls, OP_SUB);
      return 0;
    }
    case TK_CASE: {  /* added 0.5.2.*/
      selectstat(ls, line);
      return 0;
    }
    default: {
      exprstat(ls);
      return 0;  /* to avoid warnings */
    }
  }
}
Пример #20
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 */
}