From cf83adfcd1b069ced4ecada24ce89f211bcab0b7 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 13 Feb 2019 15:16:40 -0700 Subject: [PATCH] Add support for C++17 hexadecimal floating literals --- CHANGES.current | 3 ++ Doc/Manual/CPlusPlus17.html | 15 +++++++ Doc/Manual/Contents.html | 1 + Examples/test-suite/common.mk | 1 + .../test-suite/cpp17_hex_floating_literals.i | 43 +++++++++++++++++++ .../cpp17_hex_floating_literals_runme.py | 28 ++++++++++++ Source/Swig/scanner.c | 21 ++++++++- 7 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 Examples/test-suite/cpp17_hex_floating_literals.i create mode 100644 Examples/test-suite/python/cpp17_hex_floating_literals_runme.py diff --git a/CHANGES.current b/CHANGES.current index 5bc2b4982..e49b7ee7f 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2019-02-13: ZackerySpytz + #1469 Add support for C++17 hexadecimal floating literals. + 2019-02-11: wsfulton [OCaml] #1437 OCaml has been give the 'Experimental' language status. The examples work and most of the test-suite is also working, so it is quite close to being a 'Supported' language. diff --git a/Doc/Manual/CPlusPlus17.html b/Doc/Manual/CPlusPlus17.html index ae3ca5c77..a2346660a 100644 --- a/Doc/Manual/CPlusPlus17.html +++ b/Doc/Manual/CPlusPlus17.html @@ -16,6 +16,7 @@
  • Standard library changes @@ -87,6 +88,20 @@ char a = u8'a'; +

    9.2.3 Hexadecimal floating literals

    + + +

    +C++17 added hexadecimal floating literals. +For example: +

    + +
    +
    +double f = 0xF.68p2;
    +
    +
    +

    9.3 Standard library changes

    diff --git a/Doc/Manual/Contents.html b/Doc/Manual/Contents.html index d30337058..fa1ef9b5c 100644 --- a/Doc/Manual/Contents.html +++ b/Doc/Manual/Contents.html @@ -365,6 +365,7 @@
  • Standard library changes diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index e7e3dd07b..4fb623724 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -163,6 +163,7 @@ CPP_TEST_CASES += \ cpp_static \ cpp_typedef \ cpp14_binary_integer_literals \ + cpp17_hex_floating_literals \ cpp17_nested_namespaces \ cpp17_nspace_nested_namespaces \ cpp17_u8_char_literals \ diff --git a/Examples/test-suite/cpp17_hex_floating_literals.i b/Examples/test-suite/cpp17_hex_floating_literals.i new file mode 100644 index 000000000..dfc1dc0cf --- /dev/null +++ b/Examples/test-suite/cpp17_hex_floating_literals.i @@ -0,0 +1,43 @@ +%module cpp17_hex_floating_literals + +// Tests are designed so that code compiles with C++98 compilers + +%{ +#if __cplusplus >= 201703L +#define CPP17 1 +#endif +%} + +double f1 = 0x.0p1; +double f2 = 0x0.p1; +double f3 = 0x0.0p-1; +double f4 = 0xf.p-1; +double f5 = 0xA.0p1; +double f6 = 0x0.10P+10; +double f7 = 0xb2F.p2; +float f8 = 0x1234AP1F; +float f9 = 0x45A1.D1A2p+10f; + +%{ +#if defined(CPP17) +double f1 = 0x.0p1; +double f2 = 0x0.p1; +double f3 = 0x0.0p-1; +double f4 = 0xf.p-1; +double f5 = 0xA.0p1; +double f6 = 0x0.10P+10; +double f7 = 0xb2F.p2; +float f8 = 0x1234AP1F; +float f9 = 0x45A1.D1A2p+10f; +#else +double f1 = 0.; +double f2 = 0.; +double f3 = 0.; +double f4 = 7.5; +double f5 = 20.; +double f6 = 64.; +double f7 = 11452.; +float f8 = 149140.f; +float f9 = 18253638.f; +#endif +%} diff --git a/Examples/test-suite/python/cpp17_hex_floating_literals_runme.py b/Examples/test-suite/python/cpp17_hex_floating_literals_runme.py new file mode 100644 index 000000000..ed9f4d26b --- /dev/null +++ b/Examples/test-suite/python/cpp17_hex_floating_literals_runme.py @@ -0,0 +1,28 @@ +from cpp17_hex_floating_literals import * + +if cvar.f1 != 0.: + raise RuntimeError + +if cvar.f2 != 0.: + raise RuntimeError + +if cvar.f3 != 0.: + raise RuntimeError + +if cvar.f4 != 7.5: + raise RuntimeError + +if cvar.f5 != 20.: + raise RuntimeError + +if cvar.f6 != 64.: + raise RuntimeError + +if cvar.f7 != 11452.: + raise RuntimeError + +if cvar.f8 != 149140.: + raise RuntimeError + +if cvar.f9 != 18253638.: + raise RuntimeError diff --git a/Source/Swig/scanner.c b/Source/Swig/scanner.c index 24a07e68b..964336621 100644 --- a/Source/Swig/scanner.c +++ b/Source/Swig/scanner.c @@ -1196,6 +1196,10 @@ static int look(Scanner *s) { return SWIG_TOKEN_INT; if (isxdigit(c)) state = 85; + else if (c == '.') /* hexadecimal float */ + state = 860; + else if ((c == 'p') || (c == 'P')) /* hexadecimal float */ + state = 820; else if ((c == 'l') || (c == 'L')) { state = 87; } else if ((c == 'u') || (c == 'U')) { @@ -1220,7 +1224,22 @@ static int look(Scanner *s) { return SWIG_TOKEN_INT; } break; - + case 860: + /* hexadecimal float */ + if ((c = nextchar(s)) == 0) { + Swig_error(cparse_file, cparse_start_line, "Hexadecimal floating literals require an exponent\n"); + return SWIG_TOKEN_ERROR; + } + if (isxdigit(c)) + state = 860; + else if ((c == 'p') || (c == 'P')) + state = 820; + else { + retract(s, 2); + Swig_error(cparse_file, cparse_start_line, "Hexadecimal floating literals require an exponent\n"); + return SWIG_TOKEN_ERROR; + } + break; case 86: /* Rest of floating point number */