view libinterp/parse-tree/pt-eval.h @ 23435:c452180ab672

begin refactoring parse tree evaluator * libinterp/parse-tree/pt-tm-const.cc, libinterp/parse-tree/pt-tm-const.h: New files, extracted from pt-mat.h and pt-mat.cc. * libinterp/parse-tree/module.mk: Update. * interpreter.cc, ov-class.cc, ov-classdef.cc, ov-classdef.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-usr-fcn.cc, oct-parse.in.yy, pt-arg-list.cc, pt-arg-list.h, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-classdef.cc, pt-classdef.h, pt-cmd.cc, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-funcall.cc, pt-funcall.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jit.cc, pt-jump.cc, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-select.cc, pt-select.h, pt-stmt.cc, pt-stmt.h, pt-unop.cc, pt-unop.h: Use tree_evaluator class to walk the parse tree instead of rvalue methods in the parse tree classes. Maintain a stack in the evaluator class to accumulate results. Pass pointer to evaluator to function objects and other classes as needed for evaluation. This is a work in progress that is not yet complete. The ultimate goal is to move all evaluation into the tree_evaluator class instead of spreading that work among many different classes. The global pointer to the current evaluator should also be eliminated.
author John W. Eaton <jwe@octave.org>
date Fri, 21 Apr 2017 18:07:40 -0400
parents 092078913d54
children 21baad6b35c4
line wrap: on
line source

/*

Copyright (C) 2009-2017 John W. Eaton

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
<http://www.gnu.org/licenses/>.

*/

#if ! defined (octave_pt_eval_h)
#define octave_pt_eval_h 1

#include "octave-config.h"

#include <list>
#include <stack>
#include <string>

#include "comment-list.h"
#include "ovl.h"
#include "pt-exp.h"
#include "pt-walk.h"

namespace octave
{
  class tree_decl_elt;
  class tree_expression;

  class interpreter;

  // How to evaluate the code that the parse trees represent.

  class OCTINTERP_API tree_evaluator : public tree_walker
  {
  public:

    template <typename T>
    class value_stack
    {
    public:

      value_stack (void) = default;

      value_stack (const value_stack&) = default;

      value_stack& operator = (const value_stack&) = default;

      ~value_stack (void) = default;

      void push (const T& val) { m_stack.push (val); }

      T pop (void)
      {
        T retval = m_stack.top ();
        m_stack.pop ();
        return retval;
      }

      T top (void) const
      {
        return m_stack.top ();
      }

      void clear (void)
      {
        while (! m_stack.empty ())
          m_stack.pop ();
      }
      
    private:

      std::stack<T> m_stack;
    };

    typedef void (*decl_elt_init_fcn) (tree_decl_elt&);

    tree_evaluator (interpreter *interp_context)
      : m_value_stack (), m_lvalue_list_stack (), m_nargout_stack (),
        m_interp_context (interp_context)
    { }

    // No copying!

    tree_evaluator (const tree_evaluator&) = delete;

    tree_evaluator& operator = (const tree_evaluator&) = delete;

    ~tree_evaluator (void) = default;

    void reset (void);

    void visit_anon_fcn_handle (tree_anon_fcn_handle&);

    void visit_argument_list (tree_argument_list&);

    void visit_binary_expression (tree_binary_expression&);

    void visit_boolean_expression (tree_boolean_expression&);

    void visit_compound_binary_expression (tree_compound_binary_expression&);

    void visit_break_command (tree_break_command&);

    void visit_colon_expression (tree_colon_expression&);

    void visit_continue_command (tree_continue_command&);

    void visit_global_command (tree_global_command&);

    void visit_persistent_command (tree_persistent_command&);

    void visit_decl_elt (tree_decl_elt&);

    void visit_decl_init_list (tree_decl_init_list&);

    void visit_simple_for_command (tree_simple_for_command&);

    void visit_complex_for_command (tree_complex_for_command&);

    void visit_octave_user_script (octave_user_script&);

    void visit_octave_user_function (octave_user_function&);

    void visit_octave_user_function_header (octave_user_function&);

    void visit_octave_user_function_trailer (octave_user_function&);

    void visit_function_def (tree_function_def&);

    void visit_identifier (tree_identifier&);

    void visit_if_clause (tree_if_clause&);

    void visit_if_command (tree_if_command&);

    void visit_if_command_list (tree_if_command_list&);

    void visit_index_expression (tree_index_expression&);

    void visit_matrix (tree_matrix&);

    void visit_cell (tree_cell&);

    void visit_multi_assignment (tree_multi_assignment&);

    void visit_no_op_command (tree_no_op_command&);

    void visit_constant (tree_constant&);

    void visit_fcn_handle (tree_fcn_handle&);

    void visit_funcall (tree_funcall&);

    void visit_parameter_list (tree_parameter_list&);

    void visit_postfix_expression (tree_postfix_expression&);

    void visit_prefix_expression (tree_prefix_expression&);

    void visit_return_command (tree_return_command&);

    void visit_return_list (tree_return_list&);

    void visit_simple_assignment (tree_simple_assignment&);

    void visit_statement (tree_statement&);

    void visit_statement_list (tree_statement_list&);

    void visit_switch_case (tree_switch_case&);

    void visit_switch_case_list (tree_switch_case_list&);

    void visit_switch_command (tree_switch_command&);

    void visit_try_catch_command (tree_try_catch_command&);

    void do_unwind_protect_cleanup_code (tree_statement_list *list);

    void visit_unwind_protect_command (tree_unwind_protect_command&);

    void visit_while_command (tree_while_command&);

    void visit_do_until_command (tree_do_until_command&);

    static void reset_debug_state (void);

    static bool statement_printing_enabled (void);

    // If > 0, stop executing at the (N-1)th stopping point, counting
    //         from the the current execution point in the current frame.
    //
    // If < 0, stop executing at the next possible stopping point.
    static int dbstep_flag;

    // The number of the stack frame we are currently debugging.
    static size_t current_frame;

    static bool debug_mode;

    static bool quiet_breakpoint_flag;

    // Possible types of evaluation contexts.
    enum stmt_list_type
    {
      function,  // function body
      script,    // script file
      other      // command-line input or eval string
    };

    // The context for the current evaluation.
    static stmt_list_type statement_context;

    // TRUE means we are evaluating some kind of looping construct.
    static bool in_loop_command;

    octave_value evaluate (tree_expression *expr, int nargout = 1,
                           const std::list<octave_lvalue> *lvalue_list = 0)
    {
      m_nargout_stack.push (nargout);
      m_lvalue_list_stack.push (lvalue_list);

      expr->accept (*this);

      m_nargout_stack.pop ();
      m_lvalue_list_stack.pop ();

      octave_value_list tmp = m_value_stack.pop ();

      return tmp.empty () ? octave_value () : tmp(0);
    }

    octave_value_list
    evaluate_n (tree_expression *expr, int nargout = 1,
                const std::list<octave_lvalue> *lvalue_list = 0)
    {
      m_nargout_stack.push (nargout);
      m_lvalue_list_stack.push (lvalue_list);

      expr->accept (*this);

      m_nargout_stack.pop ();
      m_lvalue_list_stack.pop ();

      return m_value_stack.pop ();
    }

    octave_value evaluate (tree_decl_elt *);

    void
    initialize_undefined_parameter_list_elements
      (tree_parameter_list *param_list, const std::string& warnfor,
       int nargout, const octave_value& val);

    void define_parameter_list_from_arg_vector
      (tree_parameter_list *param_list, const octave_value_list& args);

    void undefine_parameter_list (tree_parameter_list *param_list);

    octave_value_list
    convert_parameter_list_to_const_vector
      (tree_parameter_list *param_list, int nargout, const Cell& varargout);

    bool eval_decl_elt (tree_decl_elt *elt);

    bool switch_case_label_matches (tree_switch_case *expr,
                                    const octave_value& val);

  private:

    void do_global_init (octave::tree_decl_elt& elt);

    void do_static_init (octave::tree_decl_elt& elt);

    void do_breakpoint (tree_statement& stmt) const;

    void do_breakpoint (bool is_breakpoint,
                        bool is_end_of_fcn_or_script = false) const;

    virtual octave_value
    do_keyboard (const octave_value_list& args = octave_value_list ()) const;

    bool is_logically_true (tree_expression *expr, const char *warn_for);

    value_stack<octave_value_list> m_value_stack;

    value_stack<const std::list<octave_lvalue>*> m_lvalue_list_stack;

    value_stack<int> m_nargout_stack;

    interpreter *m_interp_context;
  };
}

// Maximum nesting level for functions, scripts, or sourced files called
// recursively.
extern int Vmax_recursion_depth;

#if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)

OCTAVE_DEPRECATED ("use 'octave::tree_evaluator' instead")
typedef octave::tree_evaluator tree_evaluator;

#endif

#endif