changeset 28582:dc8de424fc72

use octave_value object to store numeric tokens in the lexer * lex.ll (base_lexer::handle_number): Use octave_value object to store numeric values. (base_lexer::display_token): Use octave_value::print_raw to display value of numeric tokens. * oct-parse.yy (NUM, IMAG_NUM): Replace with single NUMBER token ID. Change all uses. * token.h, token.cc (tok_info::m_num): Use octave_value object instead of double to store numeric values. Update all uses. (token::number): Return octave_value instead of double. Change all uses.
author John W. Eaton <jwe@octave.org>
date Thu, 16 Jul 2020 16:02:45 -0400
parents 562902dc1bb6
children 96e7dc4c2214
files libinterp/parse-tree/lex.ll libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/octave.gperf libinterp/parse-tree/token.cc libinterp/parse-tree/token.h
diffstat 5 files changed, 34 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/lex.ll	Wed Jul 15 10:31:37 2020 -0400
+++ b/libinterp/parse-tree/lex.ll	Thu Jul 16 16:02:45 2020 -0400
@@ -2861,8 +2861,9 @@
       case magic_line_kw:
         {
           int l = m_tok_beg.line ();
-          tok_val = new token (magic_line_kw, static_cast<double> (l),
-                               "", m_tok_beg, m_tok_end);
+          octave_value ov_value (static_cast<double> (l));
+          tok_val = new token (magic_line_kw, ov_value, "",
+                               m_tok_beg, m_tok_end);
         }
         break;
 
@@ -2996,9 +2997,12 @@
 
     update_token_positions (flex_yyleng ());
 
-    push_token (new token (NUM, value, yytxt, m_tok_beg, m_tok_end));
-
-    return count_token_internal (imag ? IMAG_NUM : NUM);
+    octave_value ov_value
+      = imag ? octave_value (Complex (0.0, value)) : octave_value (value);
+
+    push_token (new token (NUMBER, ov_value, yytxt, m_tok_beg, m_tok_end));
+
+    return count_token_internal (NUMBER);
   }
 
   void
@@ -3427,12 +3431,13 @@
       case POW: std::cerr << "POW\n"; break;
       case EPOW: std::cerr << "EPOW\n"; break;
 
-      case NUM:
-      case IMAG_NUM:
+      case NUMBER:
         {
           token *tok_val = current_token ();
-          std::cerr << (tok == NUM ? "NUM" : "IMAG_NUM")
-                    << " [" << tok_val->number () << "]\n";
+          std::cerr << "NUMBER [";
+          octave_value num = tok_val->number ();
+          num.print_raw (std::cerr);
+          std::cerr << "]\n";
         }
         break;
 
--- a/libinterp/parse-tree/oct-parse.yy	Wed Jul 15 10:31:37 2020 -0400
+++ b/libinterp/parse-tree/oct-parse.yy	Thu Jul 16 16:02:45 2020 -0400
@@ -199,7 +199,7 @@
 %token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS
 %token <tok_val> HERMITIAN TRANSPOSE
 %token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW
-%token <tok_val> NUM IMAG_NUM
+%token <tok_val> NUMBER
 %token <tok_val> STRUCT_ELT
 %token <tok_val> NAME
 %token <tok_val> END
@@ -541,10 +541,8 @@
                   { $$ = parser.make_constant (SQ_STRING, $1); }
                 ;
 
-constant        : NUM
-                  { $$ = parser.make_constant (NUM, $1); }
-                | IMAG_NUM
-                  { $$ = parser.make_constant (IMAG_NUM, $1); }
+constant        : NUMBER
+                  { $$ = parser.make_constant (NUMBER, $1); }
                 | string
                   { $$ = $1; }
                 ;
@@ -2583,18 +2581,9 @@
 
     switch (op)
       {
-      case NUM:
+      case NUMBER:
         {
-          octave_value tmp (tok_val->number ());
-          retval = new tree_constant (tmp, l, c);
-          retval->stash_original_text (tok_val->text_rep ());
-        }
-        break;
-
-      case IMAG_NUM:
-        {
-          octave_value tmp (Complex (0.0, tok_val->number ()));
-          retval = new tree_constant (tmp, l, c);
+          retval = new tree_constant (tok_val->number (), l, c);
           retval->stash_original_text (tok_val->text_rep ());
         }
         break;
--- a/libinterp/parse-tree/octave.gperf	Wed Jul 15 10:31:37 2020 -0400
+++ b/libinterp/parse-tree/octave.gperf	Thu Jul 16 16:02:45 2020 -0400
@@ -129,4 +129,4 @@
 unwind_protect_cleanup, CLEANUP, unwind_protect_cleanup_kw
 while, WHILE, while_kw
 __FILE__, DQ_STRING, magic_file_kw
-__LINE__, NUM, magic_line_kw
+__LINE__, NUMBER, magic_line_kw
--- a/libinterp/parse-tree/token.cc	Wed Jul 15 10:31:37 2020 -0400
+++ b/libinterp/parse-tree/token.cc	Thu Jul 16 16:02:45 2020 -0400
@@ -62,11 +62,11 @@
       m_tok_info (s), m_orig_text ()
   { }
 
-  token::token (int tv, double d, const std::string& s, const filepos& beg_pos,
-                const filepos& end_pos)
+  token::token (int tv, const octave_value& val, const std::string& s,
+                const filepos& beg_pos, const filepos& end_pos)
     : m_maybe_cmd (false), m_tspc (false), m_beg_pos (beg_pos),
-      m_end_pos (end_pos), m_tok_val (tv), m_type_tag (double_token),
-      m_tok_info (d), m_orig_text (s)
+      m_end_pos (end_pos), m_tok_val (tv), m_type_tag (numeric_token),
+      m_tok_info (val), m_orig_text (s)
   { }
 
   token::token (int tv, end_tok_type t, const filepos& beg_pos,
@@ -114,11 +114,11 @@
     return m_tok_info.m_sr->name ();
   }
 
-  double
+  octave_value
   token::number (void) const
   {
-    assert (m_type_tag == double_token);
-    return m_tok_info.m_num;
+    assert (m_type_tag == numeric_token);
+    return *m_tok_info.m_num;
   }
 
   token::token_type
--- a/libinterp/parse-tree/token.h	Wed Jul 15 10:31:37 2020 -0400
+++ b/libinterp/parse-tree/token.h	Thu Jul 16 16:02:45 2020 -0400
@@ -31,6 +31,7 @@
 #include <string>
 
 #include "filepos.h"
+#include "ov.h"
 #include "symrec.h"
 
 namespace octave
@@ -44,7 +45,7 @@
       generic_token,
       keyword_token,
       string_token,
-      double_token,
+      numeric_token,
       ettype_token,
       sym_rec_token,
       scls_name_token,
@@ -80,8 +81,8 @@
     token (int tv, const std::string& s, const filepos& beg_pos,
            const filepos& end_pos);
 
-    token (int tv, double d, const std::string& s, const filepos& beg_pos,
-           const filepos& end_pos);
+    token (int tv, const octave_value& val, const std::string& s,
+           const filepos& beg_pos, const filepos& end_pos);
 
     token (int tv, end_tok_type t, const filepos& beg_pos,
            const filepos& end_pos);
@@ -131,7 +132,7 @@
 
     std::string text (void) const;
     std::string symbol_name (void) const;
-    double number (void) const;
+    octave_value number (void) const;
     token_type ttype (void) const;
     end_tok_type ettype (void) const;
     symbol_record sym_rec (void) const;
@@ -162,7 +163,7 @@
 
       tok_info (const std::string& str) : m_str (new std::string (str)) { }
 
-      tok_info (double num) : m_num (num) { }
+      tok_info (const octave_value& num) : m_num (new octave_value (num)) { }
 
       tok_info (end_tok_type et) : m_et (et) { }
 
@@ -182,7 +183,7 @@
 
       std::string *m_str;
 
-      double m_num;
+      octave_value *m_num;
 
       end_tok_type m_et;