lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 01/12/14 12:33 PM, Ico Doornekamp wrote:
With proper integer support and binary operators in place in Lua 5.3, I would
like to propose something like the following to allow integers to be expressed
in binary format in Lua code as 0b[10]+:

--- lua-5.3.0-beta/src/lobject.c	2014-10-17 18:28:21.000000000 +0200
+++ lua-5.3.0-beta-ico/src/lobject.c	2014-12-01 15:13:29.557580628 +0100
@@ -280,6 +280,14 @@ static const char *l_str2int (const char
        empty = 0;
      }
    }
+  else if (s[0] == '0' &&
+      (s[1] == 'b' || s[1] == 'B')) {  /* bin? */
+    s += 2;  /* skip '0b' */
+    for (; *s == '0' || *s == '1'; s++) {
+      a = a * 2 + luaO_hexavalue(*s);
+      empty = 0;
+    }
+  }
    else {  /* decimal */
      for (; lisdigit(cast_uchar(*s)); s++) {
        a = a * 10 + luaO_hexavalue(cast_uchar(*s));


Examples:

Lua 5.3.0 (beta)  Copyright (C) 1994-2014 Lua.org, PUC-Rio
0b10000000
128
0b01
1
0b11111111111111111111111111111111111
34359738367

As the above patch is small and unintrusive, would inclusion in Lua 5.3 be
feasible and/or usable?

By the way, I *love* the new hexadecimal 'P' binary exponent!

What about 0b1111_1111_1111_1111 etc? (looks cleaner)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.