Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enter %nnnn and #nnnn numbers #67

Open
MPETREMANN11 opened this issue Apr 29, 2024 · 0 comments
Open

Enter %nnnn and #nnnn numbers #67

MPETREMANN11 opened this issue Apr 29, 2024 · 0 comments

Comments

@MPETREMANN11
Copy link

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; }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant