public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7573] gccrs: tokenstream: Add conversion for float literals
@ 2024-01-16 17:41 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 17:41 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:eb95910feee269f71cb4ab4aafbf1beecb2f9993

commit r14-7573-geb95910feee269f71cb4ab4aafbf1beecb2f9993
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Fri Apr 28 15:07:33 2023 +0200

    gccrs: tokenstream: Add conversion for float literals
    
    Add the conversion handler for float/double literal tokens to
    tokenstream Literals.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-tokenstream.cc (dispatch_float_literals): Add
            dispatch function for floating point literals.
            (TokenStream::collect): Add call to dispatcher.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast-tokenstream.cc | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/gcc/rust/ast/rust-ast-tokenstream.cc b/gcc/rust/ast/rust-ast-tokenstream.cc
index d77c1cf4149..0a28081e0bd 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.cc
+++ b/gcc/rust/ast/rust-ast-tokenstream.cc
@@ -41,6 +41,32 @@ pop_group (std::vector<ProcMacro::TokenStream> &streams,
   streams.back ().push (tt);
 }
 
+static void
+dispatch_float_literals (ProcMacro::TokenStream &ts, TokenPtr &token)
+{
+  std::string::size_type sz;
+  auto str = token->as_string ();
+  switch (token->get_type_hint ())
+    {
+      case CORETYPE_F32: {
+	auto value = std::stof (str, &sz);
+	bool suffixed = sz == str.length ();
+	ts.push (ProcMacro::TokenTree::make_tokentree (
+	  ProcMacro::Literal::make_f32 (value, suffixed)));
+      }
+      break;
+      case CORETYPE_F64: {
+	auto value = std::stod (str, &sz);
+	bool suffixed = sz == str.length ();
+	ts.push (ProcMacro::TokenTree::make_tokentree (
+	  ProcMacro::Literal::make_f64 (value, suffixed)));
+      }
+      break;
+    default:
+      gcc_unreachable ();
+    }
+}
+
 static void
 dispatch_integer_literals (ProcMacro::TokenStream &ts, TokenPtr &token)
 {
@@ -134,6 +160,9 @@ TokenStream::collect () const
       switch (token->get_id ())
 	{
 	// Literals
+	case FLOAT_LITERAL:
+	  dispatch_float_literals (trees.back (), token);
+	  break;
 	case INT_LITERAL:
 	  dispatch_integer_literals (trees.back (), token);
 	  break;

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 17:41 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 17:41 [gcc r14-7573] gccrs: tokenstream: Add conversion for float literals Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).