Qt 4.8
Namespaces | Macros | Typedefs | Functions
qscriptlexer.cpp File Reference
#include "qscriptlexer_p.h"
#include "qscriptgrammar_p.h"
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Namespaces

 QScript
 

Macros

#define shiftWindowsLineBreak()
 

Typedefs

typedef double qsreal
 

Functions

qsreal QScript::integerFromString (const char *buf, int size, int radix)
 
Q_CORE_EXPORT double qstrtod (const char *s00, char const **se, bool *ok)
 

Macro Definition Documentation

◆ shiftWindowsLineBreak

#define shiftWindowsLineBreak ( )
Value:
do { \
if (((current == '\r') && (next1 == '\n')) \
|| ((current == '\n') && (next1 == '\r'))) { \
shift(1); \
} \
} \
while (0)

Definition at line 36 of file qscriptlexer.cpp.

Referenced by QScript::Lexer::lex().

Typedef Documentation

◆ qsreal

typedef double qsreal

Definition at line 45 of file qscriptlexer.cpp.

Function Documentation

◆ qstrtod()

Q_CORE_EXPORT double qstrtod ( const char *  s00,
char const **  se,
bool *  ok 
)

Definition at line 1570 of file qlocale_tools.cpp.

Referenced by QScript::Lexer::lex().

1571 {
1572  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1573  e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1574  const char *s, *s0, *s1;
1575  double aadj, aadj1, adj, rv, rv0;
1576  Long L;
1577  ULong y, z;
1578  Bigint *bb1, *bd0;
1579  Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
1580 
1581  /*
1582  #ifndef KR_headers
1583  const char decimal_point = localeconv()->decimal_point[0];
1584  #else
1585  const char decimal_point = '.';
1586  #endif */
1587  if (ok != 0)
1588  *ok = true;
1589 
1590  const char decimal_point = '.';
1591 
1592  sign = nz0 = nz = 0;
1593  rv = 0.;
1594 
1595 
1596  for(s = s00; isspace(uchar(*s)); s++)
1597  ;
1598 
1599  if (*s == '-') {
1600  sign = 1;
1601  s++;
1602  } else if (*s == '+') {
1603  s++;
1604  }
1605 
1606  if (*s == '\0') {
1607  s = s00;
1608  goto ret;
1609  }
1610 
1611  if (*s == '0') {
1612  nz0 = 1;
1613  while(*++s == '0') ;
1614  if (!*s)
1615  goto ret;
1616  }
1617  s0 = s;
1618  y = z = 0;
1619  for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1620  if (nd < 9)
1621  y = 10*y + c - '0';
1622  else if (nd < 16)
1623  z = 10*z + c - '0';
1624  nd0 = nd;
1625  if (c == decimal_point) {
1626  c = *++s;
1627  if (!nd) {
1628  for(; c == '0'; c = *++s)
1629  nz++;
1630  if (c > '0' && c <= '9') {
1631  s0 = s;
1632  nf += nz;
1633  nz = 0;
1634  goto have_dig;
1635  }
1636  goto dig_done;
1637  }
1638  for(; c >= '0' && c <= '9'; c = *++s) {
1639  have_dig:
1640  nz++;
1641  if (c -= '0') {
1642  nf += nz;
1643  for(i = 1; i < nz; i++)
1644  if (nd++ < 9)
1645  y *= 10;
1646  else if (nd <= DBL_DIG + 1)
1647  z *= 10;
1648  if (nd++ < 9)
1649  y = 10*y + c;
1650  else if (nd <= DBL_DIG + 1)
1651  z = 10*z + c;
1652  nz = 0;
1653  }
1654  }
1655  }
1656  dig_done:
1657  e = 0;
1658  if (c == 'e' || c == 'E') {
1659  if (!nd && !nz && !nz0) {
1660  s = s00;
1661  goto ret;
1662  }
1663  s00 = s;
1664  esign = 0;
1665  switch(c = *++s) {
1666  case '-':
1667  esign = 1;
1668  case '+':
1669  c = *++s;
1670  }
1671  if (c >= '0' && c <= '9') {
1672  while(c == '0')
1673  c = *++s;
1674  if (c > '0' && c <= '9') {
1675  L = c - '0';
1676  s1 = s;
1677  while((c = *++s) >= '0' && c <= '9')
1678  L = 10*L + c - '0';
1679  if (s - s1 > 8 || L > 19999)
1680  /* Avoid confusion from exponents
1681  * so large that e might overflow.
1682  */
1683  e = 19999; /* safe for 16 bit ints */
1684  else
1685  e = int(L);
1686  if (esign)
1687  e = -e;
1688  }
1689  else
1690  e = 0;
1691  }
1692  else
1693  s = s00;
1694  }
1695  if (!nd) {
1696  if (!nz && !nz0)
1697  s = s00;
1698  goto ret;
1699  }
1700  e1 = e -= nf;
1701 
1702  /* Now we have nd0 digits, starting at s0, followed by a
1703  * decimal point, followed by nd-nd0 digits. The number we're
1704  * after is the integer represented by those digits times
1705  * 10**e */
1706 
1707  if (!nd0)
1708  nd0 = nd;
1709  k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1710  rv = y;
1711  if (k > 9)
1712 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
1713  {
1714  // work around a bug on 64 bit IRIX gcc
1715  double *t = (double *) tens;
1716  rv = t[k - 9] * rv + z;
1717  }
1718 #else
1719  rv = tens[k - 9] * rv + z;
1720 #endif
1721 
1722  bd0 = 0;
1723  if (nd <= DBL_DIG
1724 #ifndef RND_PRODQUOT
1725  && FLT_ROUNDS == 1
1726 #endif
1727  ) {
1728  if (!e)
1729  goto ret;
1730  if (e > 0) {
1731  if (e <= Ten_pmax) {
1732 #ifdef VAX
1733  goto vax_ovfl_check;
1734 #else
1735  /* rv = */ rounded_product(rv, tens[e]);
1736  goto ret;
1737 #endif
1738  }
1739  i = DBL_DIG - nd;
1740  if (e <= Ten_pmax + i) {
1741  /* A fancier test would sometimes let us do
1742  * this for larger i values.
1743  */
1744  e -= i;
1745  rv *= tens[i];
1746 #ifdef VAX
1747  /* VAX exponent range is so narrow we must
1748  * worry about overflow here...
1749  */
1750  vax_ovfl_check:
1751  setWord0(&rv, getWord0(rv) - P*Exp_msk1);
1752  /* rv = */ rounded_product(rv, tens[e]);
1753  if ((getWord0(rv) & Exp_mask)
1754  > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1755  goto ovfl;
1756  setWord0(&rv, getWord0(rv) + P*Exp_msk1);
1757 #else
1758  /* rv = */ rounded_product(rv, tens[e]);
1759 #endif
1760  goto ret;
1761  }
1762  }
1763 #ifndef Inaccurate_Divide
1764  else if (e >= -Ten_pmax) {
1765  /* rv = */ rounded_quotient(rv, tens[-e]);
1766  goto ret;
1767  }
1768 #endif
1769  }
1770  e1 += nd - k;
1771 
1772  /* Get starting approximation = rv * 10**e1 */
1773 
1774  if (e1 > 0) {
1775  if ((i = e1 & 15) != 0)
1776  rv *= tens[i];
1777  if (e1 &= ~15) {
1778  if (e1 > DBL_MAX_10_EXP) {
1779  ovfl:
1780  // errno = ERANGE;
1781  if (ok != 0)
1782  *ok = false;
1783 #ifdef __STDC__
1784  rv = HUGE_VAL;
1785 #else
1786  /* Can't trust HUGE_VAL */
1787 #ifdef IEEE_Arith
1788  setWord0(&rv, Exp_mask);
1789  setWord1(&rv, 0);
1790 #else
1791  setWord0(&rv, Big0);
1792  setWord1(&rv, Big1);
1793 #endif
1794 #endif
1795  if (bd0)
1796  goto retfree;
1797  goto ret;
1798  }
1799  if (e1 >>= 4) {
1800  for(j = 0; e1 > 1; j++, e1 >>= 1)
1801  if (e1 & 1)
1802  rv *= bigtens[j];
1803  /* The last multiplication could overflow. */
1804  setWord0(&rv, getWord0(rv) - P*Exp_msk1);
1805  rv *= bigtens[j];
1806  if ((z = getWord0(rv) & Exp_mask)
1807  > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1808  goto ovfl;
1809  if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1810  /* set to largest number */
1811  /* (Can't trust DBL_MAX) */
1812  setWord0(&rv, Big0);
1813  setWord1(&rv, Big1);
1814  }
1815  else
1816  setWord0(&rv, getWord0(rv) + P*Exp_msk1);
1817  }
1818 
1819  }
1820  }
1821  else if (e1 < 0) {
1822  e1 = -e1;
1823  if ((i = e1 & 15) != 0)
1824  rv /= tens[i];
1825  if (e1 &= ~15) {
1826  e1 >>= 4;
1827  if (e1 >= 1 << n_bigtens)
1828  goto undfl;
1829  for(j = 0; e1 > 1; j++, e1 >>= 1)
1830  if (e1 & 1)
1831  rv *= tinytens[j];
1832  /* The last multiplication could underflow. */
1833  rv0 = rv;
1834  rv *= tinytens[j];
1835  if (rv == g_double_zero)
1836  {
1837  rv = 2.*rv0;
1838  rv *= tinytens[j];
1839  if (rv == g_double_zero)
1840  {
1841  undfl:
1842  rv = 0.;
1843  // errno = ERANGE;
1844  if (ok != 0)
1845  *ok = false;
1846  if (bd0)
1847  goto retfree;
1848  goto ret;
1849  }
1850  setWord0(&rv, Tiny0);
1851  setWord1(&rv, Tiny1);
1852  /* The refinement below will clean
1853  * this approximation up.
1854  */
1855  }
1856  }
1857  }
1858 
1859  /* Now the hard part -- adjusting rv to the correct value.*/
1860 
1861  /* Put digits into bd: true value = bd * 10^e */
1862 
1863  bd0 = s2b(s0, nd0, nd, y);
1864 
1865  for(;;) {
1866  bd = Balloc(bd0->k);
1867  Bcopy(bd, bd0);
1868  bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1869  bs = i2b(1);
1870 
1871  if (e >= 0) {
1872  bb2 = bb5 = 0;
1873  bd2 = bd5 = e;
1874  }
1875  else {
1876  bb2 = bb5 = -e;
1877  bd2 = bd5 = 0;
1878  }
1879  if (bbe >= 0)
1880  bb2 += bbe;
1881  else
1882  bd2 -= bbe;
1883  bs2 = bb2;
1884 #ifdef Sudden_Underflow
1885 #ifdef IBM
1886  j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1887 #else
1888  j = P + 1 - bbbits;
1889 #endif
1890 #else
1891  i = bbe + bbbits - 1; /* logb(rv) */
1892  if (i < Emin) /* denormal */
1893  j = bbe + (P-Emin);
1894  else
1895  j = P + 1 - bbbits;
1896 #endif
1897  bb2 += j;
1898  bd2 += j;
1899  i = bb2 < bd2 ? bb2 : bd2;
1900  if (i > bs2)
1901  i = bs2;
1902  if (i > 0) {
1903  bb2 -= i;
1904  bd2 -= i;
1905  bs2 -= i;
1906  }
1907  if (bb5 > 0) {
1908  bs = pow5mult(bs, bb5);
1909  bb1 = mult(bs, bb);
1910  Bfree(bb);
1911  bb = bb1;
1912  }
1913  if (bb2 > 0)
1914  bb = lshift(bb, bb2);
1915  if (bd5 > 0)
1916  bd = pow5mult(bd, bd5);
1917  if (bd2 > 0)
1918  bd = lshift(bd, bd2);
1919  if (bs2 > 0)
1920  bs = lshift(bs, bs2);
1921  delta = diff(bb, bd);
1922  dsign = delta->sign;
1923  delta->sign = 0;
1924  i = cmp(delta, bs);
1925  if (i < 0) {
1926  /* Error is less than half an ulp -- check for
1927  * special case of mantissa a power of two.
1928  */
1929  if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask)
1930  break;
1931  delta = lshift(delta,Log2P);
1932  if (cmp(delta, bs) > 0)
1933  goto drop_down;
1934  break;
1935  }
1936  if (i == 0) {
1937  /* exactly half-way between */
1938  if (dsign) {
1939  if ((getWord0(rv) & Bndry_mask1) == Bndry_mask1
1940  && getWord1(rv) == 0xffffffff) {
1941  /*boundary case -- increment exponent*/
1942  setWord0(&rv, (getWord0(rv) & Exp_mask)
1943  + Exp_msk1
1944 #ifdef IBM
1945  | Exp_msk1 >> 4
1946 #endif
1947  );
1948  setWord1(&rv, 0);
1949  break;
1950  }
1951  }
1952  else if (!(getWord0(rv) & Bndry_mask) && !getWord1(rv)) {
1953  drop_down:
1954  /* boundary case -- decrement exponent */
1955 #ifdef Sudden_Underflow
1956  L = getWord0(rv) & Exp_mask;
1957 #ifdef IBM
1958  if (L < Exp_msk1)
1959 #else
1960  if (L <= Exp_msk1)
1961 #endif
1962  goto undfl;
1963  L -= Exp_msk1;
1964 #else
1965  L = (getWord0(rv) & Exp_mask) - Exp_msk1;
1966 #endif
1967  setWord0(&rv, L | Bndry_mask1);
1968  setWord1(&rv, 0xffffffff);
1969 #ifdef IBM
1970  goto cont;
1971 #else
1972  break;
1973 #endif
1974  }
1975 #ifndef ROUND_BIASED
1976  if (!(getWord1(rv) & LSB))
1977  break;
1978 #endif
1979  if (dsign)
1980  rv += ulp(rv);
1981 #ifndef ROUND_BIASED
1982  else {
1983  rv -= ulp(rv);
1984 #ifndef Sudden_Underflow
1985  if (rv == g_double_zero)
1986  goto undfl;
1987 #endif
1988  }
1989 #endif
1990  break;
1991  }
1992  if ((aadj = ratio(delta, bs)) <= 2.) {
1993  if (dsign)
1994  aadj = aadj1 = 1.;
1995  else if (getWord1(rv) || getWord0(rv) & Bndry_mask) {
1996 #ifndef Sudden_Underflow
1997  if (getWord1(rv) == Tiny1 && !getWord0(rv))
1998  goto undfl;
1999 #endif
2000  aadj = 1.;
2001  aadj1 = -1.;
2002  }
2003  else {
2004  /* special case -- power of FLT_RADIX to be */
2005  /* rounded down... */
2006 
2007  if (aadj < 2./FLT_RADIX)
2008  aadj = 1./FLT_RADIX;
2009  else
2010  aadj *= 0.5;
2011  aadj1 = -aadj;
2012  }
2013  }
2014  else {
2015  aadj *= 0.5;
2016  aadj1 = dsign ? aadj : -aadj;
2017 #ifdef Check_FLT_ROUNDS
2018  switch(FLT_ROUNDS) {
2019  case 2: /* towards +infinity */
2020  aadj1 -= 0.5;
2021  break;
2022  case 0: /* towards 0 */
2023  case 3: /* towards -infinity */
2024  aadj1 += 0.5;
2025  }
2026 #else
2027  if (FLT_ROUNDS == 0)
2028  aadj1 += 0.5;
2029 #endif
2030  }
2031  y = getWord0(rv) & Exp_mask;
2032 
2033  /* Check for overflow */
2034 
2035  if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
2036  rv0 = rv;
2037  setWord0(&rv, getWord0(rv) - P*Exp_msk1);
2038  adj = aadj1 * ulp(rv);
2039  rv += adj;
2040  if ((getWord0(rv) & Exp_mask) >=
2041  Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
2042  if (getWord0(rv0) == Big0 && getWord1(rv0) == Big1)
2043  goto ovfl;
2044  setWord0(&rv, Big0);
2045  setWord1(&rv, Big1);
2046  goto cont;
2047  }
2048  else
2049  setWord0(&rv, getWord0(rv) + P*Exp_msk1);
2050  }
2051  else {
2052 #ifdef Sudden_Underflow
2053  if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1) {
2054  rv0 = rv;
2055  setWord0(&rv, getWord0(rv) + P*Exp_msk1);
2056  adj = aadj1 * ulp(rv);
2057  rv += adj;
2058 #ifdef IBM
2059  if ((getWord0(rv) & Exp_mask) < P*Exp_msk1)
2060 #else
2061  if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1)
2062 #endif
2063  {
2064  if (getWord0(rv0) == Tiny0
2065  && getWord1(rv0) == Tiny1)
2066  goto undfl;
2067  setWord0(&rv, Tiny0);
2068  setWord1(&rv, Tiny1);
2069  goto cont;
2070  }
2071  else
2072  setWord0(&rv, getWord0(rv) - P*Exp_msk1);
2073  }
2074  else {
2075  adj = aadj1 * ulp(rv);
2076  rv += adj;
2077  }
2078 #else
2079  /* Compute adj so that the IEEE rounding rules will
2080  * correctly round rv + adj in some half-way cases.
2081  * If rv * ulp(rv) is denormalized (i.e.,
2082  * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2083  * trouble from bits lost to denormalization;
2084  * example: 1.2e-307 .
2085  */
2086  if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
2087  aadj1 = int(aadj + 0.5);
2088  if (!dsign)
2089  aadj1 = -aadj1;
2090  }
2091  adj = aadj1 * ulp(rv);
2092  rv += adj;
2093 #endif
2094  }
2095  z = getWord0(rv) & Exp_mask;
2096  if (y == z) {
2097  /* Can we stop now? */
2098  L = Long(aadj);
2099  aadj -= L;
2100  /* The tolerances below are conservative. */
2101  if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask) {
2102  if (aadj < .4999999 || aadj > .5000001)
2103  break;
2104  }
2105  else if (aadj < .4999999/FLT_RADIX)
2106  break;
2107  }
2108  cont:
2109  Bfree(bb);
2110  Bfree(bd);
2111  Bfree(bs);
2112  Bfree(delta);
2113  }
2114  retfree:
2115  Bfree(bb);
2116  Bfree(bd);
2117  Bfree(bs);
2118  Bfree(bd0);
2119  Bfree(delta);
2120  ret:
2121  if (se)
2122  *se = s;
2123  return sign ? -rv : rv;
2124 }
#define Big1
#define Emin
#define Big0
#define rounded_product(a, b)
unsigned char c[8]
Definition: qnumeric_p.h:62
#define DBL_DIG
Definition: qvariant.cpp:69
#define Bndry_mask1
static Bigint * d2b(double d, int *e, int *bits)
#define LSB
static ULong getWord1(const NEEDS_VOLATILE double x)
static Bigint * i2b(int i)
static Bigint * lshift(Bigint *b, int k)
static double ratio(Bigint *a, Bigint *b)
#define ULong
static int sign(int x)
#define Tiny0
unsigned char uchar
Definition: qglobal.h:994
static const double tens[]
static Bigint * mult(Bigint *a, Bigint *b)
#define Ten_pmax
static Bigint * diff(Bigint *a, Bigint *b)
#define n_bigtens
static Bigint * Balloc(int k)
#define Bcopy(x, y)
#define Exp_msk1
#define Exp_mask
static const double bigtens[]
static ULong getWord0(const NEEDS_VOLATILE double x)
#define Bndry_mask
#define Bias
#define rounded_quotient(a, b)
static void setWord1(NEEDS_VOLATILE double *x, ULong l)
static void setWord0(NEEDS_VOLATILE double *x, ULong l)
static Bigint * pow5mult(Bigint *b, int k)
#define Log2P
static void Bfree(Bigint *v)
static Bigint * s2b(const char *s, int nd0, int nd, ULong y9)
#define Long
#define Tiny1
static const double tinytens[]
static int cmp(Bigint *a, Bigint *b)
#define P
static double g_double_zero
static double ulp(double x)