changeset 33112:4cae6d1e5122 bytecode-interpreter

maint: merge default to bytecode-interpreter
author John W. Eaton <jwe@octave.org>
date Thu, 29 Feb 2024 13:55:38 -0500
parents 613a23f79a1f (current diff) 8bb7f5f8e35c (diff)
children 9acadc2d8336
files libinterp/parse-tree/module.mk
diffstat 4 files changed, 110 insertions(+), 201 deletions(-) [+]
line wrap: on
line diff
--- a/doc/interpreter/genpropdoc.m	Thu Feb 29 01:40:25 2024 -0500
+++ b/doc/interpreter/genpropdoc.m	Thu Feb 29 13:55:38 2024 -0500
@@ -1877,7 +1877,7 @@
 a 2-D matrix where each element's value determines that pixel's color \
 according to the current colormap, or as a 3-D array where the third \
 dimension contains separate red, blue, and green components for each pixel.  \
-For RGB arrays, the values that map to minimum to a maximum color value \
+For RGB arrays, the values that map to minimum and maximum color value \
 depend on the class of @qcode{\"cdata\"}.  Floating point and logical values \
 range from 0 to 1 while integer value range from @code{intmin} to \
 @code{intmax} for that integer class.";
--- a/libinterp/parse-tree/module.mk	Thu Feb 29 01:40:25 2024 -0500
+++ b/libinterp/parse-tree/module.mk	Thu Feb 29 13:55:38 2024 -0500
@@ -93,8 +93,7 @@
   %reldir%/pt-tm-const.cc \
   %reldir%/pt-unop.cc \
   %reldir%/pt-walk.cc \
-  %reldir%/pt.cc \
-  %reldir%/token.cc
+  %reldir%/pt.cc
 
 ## Special rules for sources which must be built before rest of compilation.
 
--- a/libinterp/parse-tree/token.cc	Thu Feb 29 01:40:25 2024 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,143 +0,0 @@
-////////////////////////////////////////////////////////////////////////
-//
-// Copyright (C) 1993-2024 The Octave Project Developers
-//
-// See the file COPYRIGHT.md in the top-level directory of this
-// distribution or <https://octave.org/copyright/>.
-//
-// This file is part of Octave.
-//
-// Octave is free software: you can redistribute it and/or modify it
-// under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Octave is distributed in the hope that it will be useful, but
-// WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Octave; see the file COPYING.  If not, see
-// <https://www.gnu.org/licenses/>.
-//
-////////////////////////////////////////////////////////////////////////
-
-#if defined (HAVE_CONFIG_H)
-#  include "config.h"
-#endif
-
-#include <cassert>
-
-#include "error.h"
-#include "token.h"
-
-OCTAVE_BEGIN_NAMESPACE(octave)
-
-token::token (int tv, 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 (generic_token),
-    m_tok_info (), m_orig_text ()
-{ }
-
-token::token (int tv, bool is_kw, 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 (is_kw ? keyword_token : generic_token), m_tok_info (),
-    m_orig_text ()
-{ }
-
-token::token (int tv, const char *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 (string_token),
-    m_tok_info (s), m_orig_text ()
-{ }
-
-token::token (int tv, 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 (string_token),
-    m_tok_info (s), m_orig_text ()
-{ }
-
-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 (numeric_token),
-    m_tok_info (val), m_orig_text (s)
-{ }
-
-token::token (int tv, end_tok_type t, 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 (ettype_token),
-    m_tok_info (t), m_orig_text ()
-{ }
-
-token::token (int tv, const std::string& meth, const std::string& cls,
-              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 (scls_name_token),
-    m_tok_info (meth, cls), m_orig_text ()
-{ }
-
-token::~token ()
-{
-  if (m_type_tag == string_token)
-    delete m_tok_info.m_str;
-  else if (m_type_tag == numeric_token)
-    delete m_tok_info.m_num;
-  else if (m_type_tag == scls_name_token)
-    delete m_tok_info.m_superclass_info;
-}
-
-std::string
-token::text () const
-{
-  panic_if (m_type_tag != string_token);
-  return *m_tok_info.m_str;
-}
-
-octave_value
-token::number () const
-{
-  panic_if (m_type_tag != numeric_token);
-  return *m_tok_info.m_num;
-}
-
-token::token_type
-token::ttype () const
-{
-  return m_type_tag;
-}
-
-token::end_tok_type
-token::ettype () const
-{
-  panic_if (m_type_tag != ettype_token);
-  return m_tok_info.m_et;
-}
-
-std::string
-token::superclass_method_name () const
-{
-  panic_if (m_type_tag != scls_name_token);
-  return m_tok_info.m_superclass_info->m_method_name;
-}
-
-std::string
-token::superclass_class_name () const
-{
-  panic_if (m_type_tag != scls_name_token);
-  return m_tok_info.m_superclass_info->m_class_name;
-}
-
-std::string
-token::text_rep () const
-{
-  return m_orig_text;
-}
-
-OCTAVE_END_NAMESPACE(octave)
--- a/libinterp/parse-tree/token.h	Thu Feb 29 01:40:25 2024 -0500
+++ b/libinterp/parse-tree/token.h	Thu Feb 29 13:55:38 2024 -0500
@@ -29,7 +29,9 @@
 #include "octave-config.h"
 
 #include <string>
+#include <variant>
 
+#include "error.h"
 #include "filepos.h"
 #include "ov.h"
 
@@ -69,29 +71,55 @@
     while_end,
   };
 
-  token (int tv, const filepos& beg_pos, const filepos& end_pos);
+  token (int tv, 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 (generic_token),
+      m_tok_info (), m_orig_text ()
+  { }
 
-  token (int tv, bool is_keyword, const filepos& beg_pos
-         , const filepos& end_pos);
+  token (int tv, bool is_kw, 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 (is_kw ? keyword_token : generic_token), m_tok_info (),
+      m_orig_text ()
+  { }
 
   token (int tv, const char *s, const filepos& beg_pos,
-         const filepos& end_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 (string_token),
+      m_tok_info (s), m_orig_text ()
+  { }
 
   token (int tv, const std::string& s, const filepos& beg_pos,
-         const filepos& end_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 (string_token),
+      m_tok_info (s), m_orig_text ()
+  { }
 
   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);
+         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 (numeric_token),
+      m_tok_info (val), m_orig_text (s)
+  { }
 
-  token (int tv, const std::string& mth, const std::string& cls,
-         const filepos& beg_pos, const filepos& end_pos);
+  token (int tv, end_tok_type t, 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 (ettype_token),
+      m_tok_info (t), m_orig_text ()
+  { }
 
-  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (token)
+  token (int tv, const std::string& meth, const std::string& cls,
+         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 (scls_name_token),
+      m_tok_info (meth, cls), m_orig_text ()
+  { }
 
-  ~token ();
+  OCTAVE_DEFAULT_COPY_MOVE_DELETE (token)
 
   void mark_may_be_command () { m_maybe_cmd = true; }
   bool may_be_command () const { return m_maybe_cmd; }
@@ -119,15 +147,15 @@
 
   bool isstring () const { return m_type_tag == string_token; }
 
-  std::string text () const;
-  octave_value number () const;
-  token_type ttype () const;
-  end_tok_type ettype () const;
+  std::string text () const { return m_tok_info.text (); }
+  octave_value number () const { return m_tok_info.number (); }
+  token_type ttype () const { return m_type_tag; }
+  end_tok_type ettype () const { return m_tok_info.ettype (); }
 
-  std::string superclass_method_name () const;
-  std::string superclass_class_name () const;
+  std::string superclass_method_name () const { return m_tok_info.superclass_method_name (); }
+  std::string superclass_class_name () const { return m_tok_info.superclass_class_name (); }
 
-  std::string text_rep () const;
+  std::string text_rep () const { return m_orig_text; }
 
 private:
 
@@ -142,57 +170,82 @@
 
   token_type m_type_tag;
 
-  union tok_info
+  class superclass_info
   {
-    tok_info () { }
-
-    tok_info (const char *s) : m_str (new std::string (s)) { }
+  public:
 
-    tok_info (const std::string& str) : m_str (new std::string (str)) { }
-
-    tok_info (const octave_value& num) : m_num (new octave_value (num)) { }
-
-    tok_info (end_tok_type et) : m_et (et) { }
-
-    tok_info (const std::string& meth, const std::string& cls)
-      : m_superclass_info (new superclass_info (meth, cls))
+    superclass_info (const std::string& meth, const std::string& cls)
+      : m_method_name (meth), m_class_name (cls)
     { }
 
-    OCTAVE_DISABLE_COPY_MOVE (tok_info)
+    OCTAVE_DEFAULT_COPY_MOVE_DELETE (superclass_info)
 
-    ~tok_info () { }
+    std::string method_name () const { return m_method_name; }
+
+    std::string class_name () const { return m_class_name; }
 
-    std::string *m_str;
+  private:
+
+    //--------
 
-    octave_value *m_num;
+    // The name of the method to call.  This is the text before the
+    // "@" and may be of the form "object.method".
+    std::string m_method_name;
 
-    end_tok_type m_et;
+    // The name of the superclass.  This is the text after the "@"
+    // and may be of the form "object.method".
+    std::string m_class_name;
+  };
+
+  typedef std::variant<std::string, octave_value, end_tok_type, superclass_info> tok_info_variant;
 
-    struct superclass_info
-    {
-    public:
-      superclass_info (const std::string& meth, const std::string& cls)
-        : m_method_name (meth), m_class_name (cls)
-      { }
+  class tok_info : public tok_info_variant
+  {
+  public:
+
+    tok_info (const char *s) : tok_info_variant (std::string (s)) { }
+
+    tok_info (const std::string& str) : tok_info_variant (str) { }
+
+    tok_info (const octave_value& num) : tok_info_variant (octave_value (num)) { }
+
+    tok_info (end_tok_type et) : tok_info_variant (et) { }
 
-      superclass_info () = delete;
+    tok_info (const std::string& meth, const std::string& cls) : tok_info_variant (superclass_info (meth, cls)) { }
 
-      OCTAVE_DISABLE_COPY_MOVE (superclass_info)
+    OCTAVE_DEFAULT_CONSTRUCT_COPY_MOVE_DELETE (tok_info)
 
-      ~superclass_info () = default;
+    std::string text () const
+    {
+      panic_unless (std::holds_alternative<std::string> (*this));
+      return std::get<std::string> (*this);
+    }
 
-      //--------
+    octave_value number () const
+    {
+      panic_unless (std::holds_alternative<octave_value> (*this));
+      return std::get<octave_value> (*this);
+    }
 
-      // The name of the method to call.  This is the text before the
-      // "@" and may be of the form "object.method".
-      std::string m_method_name;
+    token::end_tok_type ettype () const
+    {
+      panic_unless (std::holds_alternative<end_tok_type> (*this));
+      return std::get<end_tok_type> (*this);
+    }
 
-      // The name of the superclass.  This is the text after the "@"
-      // and may be of the form "object.method".
-      std::string m_class_name;
-    };
+    std::string
+    superclass_method_name () const
+    {
+      panic_unless (std::holds_alternative<superclass_info> (*this));
+      return std::get<superclass_info> (*this).method_name ();
+    }
 
-    superclass_info *m_superclass_info;
+    std::string
+    superclass_class_name () const
+    {
+      panic_unless (std::holds_alternative<superclass_info> (*this));
+      return std::get<superclass_info> (*this).class_name ();
+    }
   };
 
   tok_info m_tok_info;