Exemple #1
0
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r) {
  int res;
  if (ttype(l) != ttype(r))
    return luaG_ordererror(L, l, r);
  else if (ttisnumber(l))
    return nvalue(l) < nvalue(r);
  else if (ttisstring(l))
    return luaV_strcmp(tsvalue(l), tsvalue(r)) < 0;
  else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
    return res;
  return luaG_ordererror(L, l, r);
}
Exemple #2
0
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  if (ttype(l) != ttype(r))
    return luaG_ordererror(L, l, r);
  else if (ttisnumber(l))
    return luai_numlt(nvalue(l), nvalue(r));
  else if (ttisstring(l))
    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
  else if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
    return res;
  return luaG_ordererror(L, l, r);
}
Exemple #3
0
int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  if (ttype(l) != ttype(r))
    return luaG_ordererror(L, l, r);
  else if (ttisnumber(l))
    return luai_numle(nvalue(l), nvalue(r));
  else if (ttisstring(l))
    return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
  else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */
    return res;
  else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */
    return !res;
  return luaG_ordererror(L, l, r);
}
Exemple #4
0
static int luaV_lessequal (lua_State *L, const TObject *l, const TObject *r) {
  int res;
  if (ttype(l) != ttype(r))
    return luaG_ordererror(L, l, r);
  else if (ttisnumber(l))
    return nvalue(l) <= nvalue(r);
  else if (ttisstring(l))
    return luaV_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  else if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */
    return res;
  else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */
    return !res;
  return luaG_ordererror(L, l, r);
}
Exemple #5
0
/*
** Main operation less than; return 'l < r'.
*/
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
    return LTnum(l, r);
  else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
    return l_strcmp(tsvalue(l), tsvalue(r)) < 0;
  else if ((res = luaT_callorderTM(L, l, r, TM_LT)) < 0)  /* no metamethod? */
    luaG_ordererror(L, l, r);  /* error */
  return res;
}
Exemple #6
0
int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) {
  if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER)
    return (nvalue(l) < nvalue(r));
  else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING)
    return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
  else {  /* call TM */
    luaD_checkstack(L, 2);
    *top++ = *l;
    *top++ = *r;
    if (!call_binTM(L, top, TM_LT))
      luaG_ordererror(L, top-2);
    L->top--;
    return (ttype(L->top) != LUA_TNIL);
  }
}
Exemple #7
0
/*
** Main operation less than or equal to; return 'l <= r'. If it needs
** a metamethod and there is no '__le', try '__lt', based on
** l <= r iff !(r < l) (assuming a total order). If the metamethod
** yields during this substitution, the continuation has to know
** about it (to negate the result of r<l); bit CIST_LEQ in the call
** status keeps that information.
*/
int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  if (ttisnumber(l) && ttisnumber(r))  /* both operands are numbers? */
    return LEnum(l, r);
  else if (ttisstring(l) && ttisstring(r))  /* both are strings? */
    return l_strcmp(tsvalue(l), tsvalue(r)) <= 0;
  else if ((res = luaT_callorderTM(L, l, r, TM_LE)) >= 0)  /* try 'le' */
    return res;
  else {  /* try 'lt': */
    L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
    res = luaT_callorderTM(L, r, l, TM_LT);
    L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
    if (res < 0)
      luaG_ordererror(L, l, r);
    return !res;  /* result is negated */
  }
}
Exemple #8
0
int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  int tl= ttype(l);

  if (tl == ttype(r)) {
    switch(tl) {
#ifdef LUA_TINT
      case LUA_TINT:
        return ivalue(l) < ivalue(r);
#endif
      case LUA_TNUMBER:   
#ifdef LNUM_COMPLEX
        if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) )
          error_complex( L, l, r );
#endif
        return luai_numlt(nvalue_fast(l), nvalue_fast(r));
      case LUA_TSTRING:   
        return l_strcmp(rawtsvalue(l), rawtsvalue(r)) < 0;
    }
    if ((res = call_orderTM(L, l, r, TM_LT)) != -1)
      return res;
    /* fall through to 'luaG_ordererror()' */
  }
#ifdef LUA_TINT
  else if (ttype_ext(l) == ttype_ext(r)) {
    lua_Integer tmp;
      /* Avoid accuracy losing casts: if 'r' is integer by value, do comparisons
       * in integer realm. Only otherwise cast 'l' to FP (which might change its
       * value).
       */
# ifdef LNUM_COMPLEX
    if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
      error_complex( L, l, r );
# endif
    if (tl==LUA_TINT) {  /* l:int, r:num */
      return tt_integer_valued(r,&tmp) ? (ivalue(l) < tmp)
                : luai_numlt( cast_num(ivalue(l)), nvalue_fast(r) );
    } else {  /* l:num, r:int */
      return tt_integer_valued(l,&tmp) ? (tmp < ivalue(r))
                : luai_numlt( nvalue_fast(l), cast_num(ivalue(r)) );
    }
  }
#endif
  return luaG_ordererror(L, l, r);
}
Exemple #9
0
Fichier : ltm.c Projet : lua/lua
int luaT_callorderTM (lua_State *L, const TValue *p1, const TValue *p2,
                      TMS event) {
  if (callbinTM(L, p1, p2, L->top, event))  /* try original event */
    return !l_isfalse(s2v(L->top));
#if defined(LUA_COMPAT_LT_LE)
  else if (event == TM_LE) {
      /* try '!(p2 < p1)' for '(p1 <= p2)' */
      L->ci->callstatus |= CIST_LEQ;  /* mark it is doing 'lt' for 'le' */
      if (callbinTM(L, p2, p1, L->top, TM_LT)) {
        L->ci->callstatus ^= CIST_LEQ;  /* clear mark */
        return l_isfalse(s2v(L->top));
      }
      /* else error will remove this 'ci'; no need to clear mark */
  }
#endif
  luaG_ordererror(L, p1, p2);  /* no metamethod found */
  return 0;  /* to avoid warnings */
}
Exemple #10
0
static int lessequal (lua_State *L, const TValue *l, const TValue *r) {
  int res;
  int tl= ttype(l);

  if (tl == ttype(r)) {
    switch(tl) {
#ifdef LUA_TINT
      case LUA_TINT:
        return ivalue(l) <= ivalue(r);
#endif
      case LUA_TNUMBER:
#ifdef LNUM_COMPLEX
        if ( (nvalue_img_fast(l)!=0) || (nvalue_img_fast(r)!=0) )
          error_complex( L, l, r );
#endif
        return luai_numle(nvalue_fast(l), nvalue_fast(r));
      case LUA_TSTRING:
        return l_strcmp(rawtsvalue(l), rawtsvalue(r)) <= 0;
    }

    if ((res = call_orderTM(L, l, r, TM_LE)) != -1)  /* first try `le' */
      return res;
    else if ((res = call_orderTM(L, r, l, TM_LT)) != -1)  /* else try `lt' */
      return !res;
    /* fall through to 'luaG_ordererror()' */
  }
#ifdef LUA_TINT
  else if (ttype_ext(l) == ttype_ext(r)) {
    lua_Integer tmp;
# ifdef LNUM_COMPLEX
    if ( (nvalue_img(l)!=0) || (nvalue_img(r)!=0) )
      error_complex( L, l, r );
# endif
    if (tl==LUA_TINT) {  /* l:int, r:num */
      return tt_integer_valued(r,&tmp) ? (ivalue(l) <= tmp)
                : luai_numle( cast_num(ivalue(l)), nvalue_fast(r) );
    } else {  /* l:num, r:int */
      return tt_integer_valued(l,&tmp) ? (tmp <= ivalue(r))
                : luai_numle( nvalue_fast(l), cast_num(ivalue(r)) );
    }
  }
#endif
  return luaG_ordererror(L, l, r);
}