ueforth icon indicating copy to clipboard operation
ueforth copied to clipboard

Enter %nnnn and #nnnn numbers

Open MPETREMANN11 opened this issue 9 months ago • 0 comments

It would be interesting to extend the entry of prefixed integers as exists in hexadecimal:

$1s2b3c

to other numbers in binary:

%10101101

or in decimal:

#2024

Simply modify the convert() code

static cell_t convert(const char *pos, cell_t n, cell_t base, cell_t *ret) {
  *ret = 0;
  cell_t negate = 0;
  if (!n) { return 0; }
  if (*pos == '-') { negate = -1; ++pos; --n; }
  if (*pos == '$') { base = 16; ++pos; --n; }
  if (*pos == '%') { base = 2; ++pos; --n; }
  if (*pos == '#') { base = 10; ++pos; --n; }
  for (; n; --n) {
    uintptr_t d = UPPER(*pos) - '0';
    if (d > 9) {
      d -= 7;
      if (d < 10) { return 0; }
    }
    if (d >= (uintptr_t) base) { return 0; }
    *ret = *ret * base + d;
    ++pos;
  }
  if (negate) { *ret = -*ret; }
  return -1;
}

I've adding only two lines:

if (*pos == '%') { base = 2; ++pos; --n; } if (*pos == '#') { base = 10; ++pos; --n; }

MPETREMANN11 avatar Apr 29 '24 14:04 MPETREMANN11