Add octal literals

This adds support for octal literals in the compiler.  Additionally, adds three passing tests: one for the most
common case, one that include the capital O version (0O instead of 0o), and one that checks a bad octal is
rejected.  It also adds a feature request for the ability to print formatted output with octals within it.

C does not guarantee that ‘long long’ has at most 64 bits.  It does guarantee that it has at least 64 bits.  The only C type that is guaranteed to hold the value of all other C integer types is uintmax_t - found in header <stdint.h>.  The header <stdint.h> also contains uint64_t and intmax_t.

I am not aware of any implementation that does not support ‘long long’ as a 64-bit type.  But, the C standard does allow it to be more than 64-bits.

Also, the C standard does not guarantee that an implementation is required to support unit64_t.  It’s only supported if the macro UINT64_MAX is defined in that header.

#ifndef UINT64_MAX
#  error uint64_t type is required
#endif

is a common code snippet.

TMacD

From: Brad Chamberlain [mailto:notifications@github.com]
Sent: Monday, September 08, 2014 7:32 PM
To: chapel-lang/chapel
Subject: Re: [chapel] Add octal literals (#348)


In compiler/util/stringutil.cpp:

> @@ -164,14 +164,26 @@ uint64_t binStr2uint64(const char* str) {

>    return val;

>  }

>

> +uint64_t octStr2uint64(const char* str) {

> +  if (!str) {

> +    INT_FATAL("NULL string passed to octStrToUint64()");

> +  }

> +  int len = strlen(str);

> +  if (len < 3 || str[0] != '0' || (str[1] != 'o' && str[1] != 'O')) {

> +    INT_FATAL("Illegal string passed to octStrToUint64()");

> +  }

> +  uint64_t val = strtoll(str+2, NULL, 8);

Does C give any guarantees that a 'long long' will fit in a uint64_t? I'm not sure, but would guess not.

—
Reply to this email directly or view it on GitHub<https://github.com/chapel-lang/chapel/pull/348/files#r17276223>.