Beispiel #1
0
/*
  Find variable with given name 'n'. If it is an upvalue, add this
  upvalue into all intermediate functions.
*/
static int singlevaraux (FuncState *fs, TString *n, expdesc *var) {
  if (fs == NULL)  /* no more levels? */
    return VVOID;  /* default is global */
  else {
  	LexState *ls = fs->ls;
    int v = searchvar(fs, n);  /* look up locals at current level */
    if (v >= 0) {  /* found? */
      init_exp(var, VLOCAL, v);  /* variable is local */
      return VLOCAL;
    }
    else {  /* not found as local at current level; try upvalues */
	  if (ls->t.token != '(') {
    	new_localvar(ls, n);
        init_exp(var, VLOCAL, fs->nlocvars - 1);  /* variable is local */
        return VLOCAL;
	  }
      int idx = searchupvalue(fs, n);  /* try existing upvalues */
      if (idx < 0) {  /* not found? */
        if (singlevaraux(fs->prev, n, var) == VVOID) /* try upper levels */
          return VVOID;  /* not found; is a global */
        /* else was LOCAL or UPVAL */
        idx  = newupvalue(fs, n, var);  /* will be a new upvalue */
      }
      init_exp(var, VUPVAL, idx);
      return VUPVAL;
    }
  }
}
Beispiel #2
0
/*
   Find variable with given name 'n'. If it is an upvalue, add this
   upvalue into all intermediate functions.
 */
static int singlevaraux(FuncState *fs, TString *n, expdesc *var, int base)
{
    if (fs == NULL) /* no more levels? */
        return VVOID; /* default is global */
    else
    {
        int v = searchvar(fs, n); /* look up locals at current level */
        if (v >= 0) /* found? */
        {
            init_exp(var, VLOCAL, v); /* variable is local */
            if (!base)
                markupval(fs, v); /* local will be used as an upval */

            return VLOCAL;
        }
        else /* not found as local at current level; try upvalues */
        {
            int idx = searchupvalue(fs, n); /* try existing upvalues */
            if (idx < 0) /* not found? */
            {
                if (singlevaraux(fs->prev, n, var, 0) == VVOID) /* try upper levels */
                    return VVOID; /* not found; is a global */

                /* else was LOCAL or UPVAL */
                idx = newupvalue(fs, n, var); /* will be a new upvalue */
            }

            init_exp(var, VUPVAL, idx);
            return VUPVAL;
        }
    }
}