/* Parse a number literal. */ static void lex_number(LexState *ls) { StrScanFmt fmt; TValue *tv = &ls->tokenval; int c, xp = 'e'; if (ls->current == '-' || ls->current == '+') { save_and_next(ls); } if ((c = ls->current) == '0') { save_and_next(ls); if ((ls->current | 0x20) == 'x') xp = 'p'; } while (lj_char_isident(ls->current) || ls->current == '.' || ((ls->current == '-' || ls->current == '+') && (c | 0x20) == xp)) { c = ls->current; save_and_next(ls); } save(ls, '\0'); fmt = lj_strscan_scan((const uint8_t *)ls->sb.buf, tv, (LJ_DUALNUM ? STRSCAN_OPT_TOINT : STRSCAN_OPT_TONUM) | (LJ_HASFFI ? (STRSCAN_OPT_LL|STRSCAN_OPT_IMAG) : 0)); ls->token = TK_number; if (LJ_DUALNUM && fmt == STRSCAN_INT) { setitype(tv, LJ_TISNUM); } else if (fmt == STRSCAN_NUM) { /* Already in correct format. */ #if LJ_HASFFI } else if (fmt != STRSCAN_ERROR) { lua_State *L = ls->L; GCcdata *cd; lua_assert(fmt == STRSCAN_I64 || fmt == STRSCAN_U64 || fmt == STRSCAN_IMAG); if (!ctype_ctsG(G(L))) { ptrdiff_t oldtop = savestack(L, L->top); luaopen_ffi(L); /* Load FFI library on-demand. */ L->top = restorestack(L, oldtop); } if (fmt == STRSCAN_IMAG) { cd = lj_cdata_new_(L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); ((double *)cdataptr(cd))[0] = 0; ((double *)cdataptr(cd))[1] = numV(tv); } else { cd = lj_cdata_new_(L, fmt==STRSCAN_I64 ? CTID_INT64 : CTID_UINT64, 8); *(uint64_t *)cdataptr(cd) = tv->u64; } lj_parse_keepcdata(ls, tv, cd); #endif } else { lua_assert(fmt == STRSCAN_ERROR); lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); } }
static int bit_result64(lua_State *L, CTypeID id, uint64_t x) { GCcdata *cd = lj_cdata_new_(L, id, 8); *(uint64_t *)cdataptr(cd) = x; setcdataV(L, L->base-1, cd); return FFH_RES(1); }
/* Parse a number literal. */ static void lex_number(LexState *ls, TValue *tv) { int c; lua_assert(lj_char_isdigit(ls->current)); do { c = ls->current; save_and_next(ls); } while (lj_char_isident(ls->current) || ls->current == '.' || ((ls->current == '-' || ls->current == '+') && ((c & ~0x20) == 'E' || (c & ~0x20) == 'P'))); #if LJ_HASFFI c &= ~0x20; if ((c == 'I' || c == 'L' || c == 'U') && !ctype_ctsG(G(ls->L))) lex_loadffi(ls->L); if (c == 'I') /* Parse imaginary part of complex number. */ ls->sb.n--; #endif save(ls, '\0'); #if LJ_HASFFI if ((c == 'L' || c == 'U') && lex_number64(ls, tv)) { /* Parse 64 bit int. */ return; } else #endif if (lj_str_numconv(ls->sb.buf, tv)) { #if LJ_HASFFI if (c == 'I') { /* Return cdata holding a complex number. */ GCcdata *cd = lj_cdata_new_(ls->L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); ((double *)cdataptr(cd))[0] = 0; ((double *)cdataptr(cd))[1] = numberVnum(tv); lj_parse_keepcdata(ls, tv, cd); } #endif if (LJ_DUALNUM && tvisnum(tv)) { int32_t k = lj_num2int(numV(tv)); if ((lua_Number)k == numV(tv)) /* -0 cannot end up here. */ setintV(tv, k); } return; } lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); }
/* Parse 64 bit integer. */ static int lex_number64(LexState *ls, TValue *tv) { uint64_t n = 0; uint8_t *p = (uint8_t *)ls->sb.buf; CTypeID id = CTID_INT64; GCcdata *cd; int numl = 0; if (p[0] == '0' && (p[1] & ~0x20) == 'X') { /* Hexadecimal. */ p += 2; if (!lj_char_isxdigit(*p)) return 0; do { n = n*16 + (*p & 15); if (!lj_char_isdigit(*p)) n += 9; p++; } while (lj_char_isxdigit(*p)); } else { /* Decimal. */ if (!lj_char_isdigit(*p)) return 0; do { n = n*10 + (*p - '0'); p++; } while (lj_char_isdigit(*p)); } for (;;) { /* Parse suffixes. */ if ((*p & ~0x20) == 'U') id = CTID_UINT64; else if ((*p & ~0x20) == 'L') numl++; else break; p++; } if (numl != 2 || *p != '\0') return 0; /* Return cdata holding a 64 bit integer. */ cd = lj_cdata_new_(ls->L, id, 8); *(uint64_t *)cdataptr(cd) = n; lj_parse_keepcdata(ls, tv, cd); return 1; /* Ok. */ }