# HG changeset patch # User John W. Eaton # Date 1362870291 18000 # Node ID 70f465930546be9e7cc5cf6964f76e301dd21d45 # Parent c31fd42f9000493ecc36050485ae7b98615b9992 rearrange class heirarchy for tree_cell and tree_matrix * pt-array-list.h, pt-array-list.cc: New files. (tree_array_list): New class. * pt-cell.h, pt-cell.cc (tree_cell): Derive from tree_array_list. * pt-mat.h, pt-mat.cc (tree_matrix): Derive from tree_array_list. * oct-parse.in.yy (octave_parser::finish_array_list): New function adapted from octave_parser::finish_matrix. (octave_parser::finish_matrix, octave_parser::finish_cell): Call finish_array_list to do the work. * pt-arg-list.h: Include symtab.h. * base-list.h: Include cstdlib. diff --git a/libinterp/parse-tree/module.mk b/libinterp/parse-tree/module.mk --- a/libinterp/parse-tree/module.mk +++ b/libinterp/parse-tree/module.mk @@ -21,6 +21,7 @@ PARSE_TREE_INC = \ parse-tree/pt-all.h \ parse-tree/pt-arg-list.h \ + parse-tree/pt-array-list.h \ parse-tree/pt-assign.h \ parse-tree/pt-binop.h \ parse-tree/pt-bp.h \ @@ -52,6 +53,7 @@ PARSE_TREE_SRC = \ parse-tree/pt-arg-list.cc \ + parse-tree/pt-array-list.cc \ parse-tree/pt-assign.cc \ parse-tree/pt-binop.cc \ parse-tree/pt-bp.cc \ diff --git a/libinterp/parse-tree/oct-parse.in.yy b/libinterp/parse-tree/oct-parse.in.yy --- a/libinterp/parse-tree/oct-parse.in.yy +++ b/libinterp/parse-tree/oct-parse.in.yy @@ -2956,12 +2956,12 @@ return row; } -// Finish building a matrix list. +// Finish building an array_list. tree_expression * -octave_parser::finish_matrix (tree_matrix *m) +octave_parser::finish_array_list (tree_array_list *array_list) { - tree_expression *retval = m; + tree_expression *retval = array_list; unwind_protect frame; @@ -2974,24 +2974,25 @@ discard_error_messages = true; discard_warning_messages = true; - if (m->all_elements_are_constant ()) + if (array_list->all_elements_are_constant ()) { - octave_value tmp = m->rvalue1 (); + octave_value tmp = array_list->rvalue1 (); if (! (error_state || warning_state)) { tree_constant *tc_retval - = new tree_constant (tmp, m->line (), m->column ()); + = new tree_constant (tmp, array_list->line (), + array_list->column ()); std::ostringstream buf; tree_print_code tpc (buf); - m->accept (tpc); + array_list->accept (tpc); tc_retval->stash_original_text (buf.str ()); - delete m; + delete array_list; retval = tc_retval; } @@ -3000,12 +3001,20 @@ return retval; } +// Finish building a matrix list. + +tree_expression * +octave_parser::finish_matrix (tree_matrix *m) +{ + return finish_array_list (m); +} + // Finish building a cell list. tree_expression * octave_parser::finish_cell (tree_cell *c) { - return finish_matrix (c); + return finish_array_list (c); } void diff --git a/libinterp/parse-tree/parse.h b/libinterp/parse-tree/parse.h --- a/libinterp/parse-tree/parse.h +++ b/libinterp/parse-tree/parse.h @@ -41,6 +41,7 @@ class tree; class tree_anon_fcn_handle; class tree_argument_list; +class tree_array_list; class tree_cell; class tree_colon_expression; class tree_command; @@ -330,6 +331,10 @@ // Validate argument list forming a matrix or cell row. tree_argument_list *validate_matrix_row (tree_argument_list *row); + // Finish building an array_list (common action for finish_matrix + // and finish_cell). + tree_expression *finish_array_list (tree_array_list *a); + // Finish building a matrix list. tree_expression *finish_matrix (tree_matrix *m); diff --git a/libinterp/parse-tree/pt-arg-list.h b/libinterp/parse-tree/pt-arg-list.h --- a/libinterp/parse-tree/pt-arg-list.h +++ b/libinterp/parse-tree/pt-arg-list.h @@ -33,6 +33,7 @@ #include "str-vec.h" #include "base-list.h" +#include "symtab.h" // Argument lists. Used to hold the list of expressions that are the // arguments in a function call or index expression. diff --git a/libinterp/parse-tree/pt-array-list.cc b/libinterp/parse-tree/pt-array-list.cc new file mode 100644 --- /dev/null +++ b/libinterp/parse-tree/pt-array-list.cc @@ -0,0 +1,108 @@ +/* + +Copyright (C) 2013 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 +. + +*/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include + +#include "error.h" +#include "pt-array-list.h" + +tree_array_list::~tree_array_list (void) +{ + while (! empty ()) + { + iterator p = begin (); + delete *p; + erase (p); + } +} + +bool +tree_array_list::all_elements_are_constant (void) const +{ + for (const_iterator p = begin (); p != end (); p++) + { + octave_quit (); + + tree_argument_list *elt = *p; + + if (! elt->all_elements_are_constant ()) + return false; + } + + return true; +} + +bool +tree_array_list::has_magic_end (void) const +{ + for (const_iterator p = begin (); p != end (); p++) + { + octave_quit (); + + tree_argument_list *elt = *p; + + if (elt && elt->has_magic_end ()) + return true; + } + + return false; +} + +void +tree_array_list::copy_base (const tree_array_list& array_list) +{ + tree_expression::copy_base (array_list); +} + +void +tree_array_list::copy_base (const tree_array_list& array_list, + symbol_table::scope_id scope, + symbol_table::context_id context) +{ + for (const_iterator p = array_list.begin (); p != array_list.end (); p++) + { + const tree_argument_list *elt = *p; + + append (elt ? elt->dup (scope, context) : 0); + } + + copy_base (*this); +} + +tree_expression * +tree_array_list::dup (symbol_table::scope_id scope, + symbol_table::context_id context) const +{ + panic_impossible (); + return 0; +} + +void +tree_array_list::accept (tree_walker&) +{ + panic_impossible (); +} + diff --git a/libinterp/parse-tree/pt-array-list.h b/libinterp/parse-tree/pt-array-list.h new file mode 100644 --- /dev/null +++ b/libinterp/parse-tree/pt-array-list.h @@ -0,0 +1,72 @@ +/* + +Copyright (C) 2013 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 +. + +*/ + +#if !defined (octave_tree_array_list_h) +#define octave_tree_array_list_h 1 + +#include "base-list.h" +#include "pt-arg-list.h" +#include "pt-exp.h" +#include "symtab.h" + +// Base class for cell arrays and matrices. + +class +tree_array_list : public tree_expression, + public octave_base_list +{ +public: + + tree_array_list (tree_argument_list *row = 0, int l = -1, int c = -1) + : tree_expression (l, c), octave_base_list () + { + if (row) + append (row); + } + + ~tree_array_list (void); + + bool all_elements_are_constant (void) const; + + bool has_magic_end (void) const; + + void copy_base (const tree_array_list& array_list); + + void copy_base (const tree_array_list& array_list, + symbol_table::scope_id scope, + symbol_table::context_id context); + + tree_expression *dup (symbol_table::scope_id scope, + symbol_table::context_id context) const; + + void accept (tree_walker& tw); + +private: + + // No copying! + + tree_array_list (const tree_array_list&); + + tree_array_list& operator = (const tree_array_list&); +}; + +#endif diff --git a/libinterp/parse-tree/pt-cell.cc b/libinterp/parse-tree/pt-cell.cc --- a/libinterp/parse-tree/pt-cell.cc +++ b/libinterp/parse-tree/pt-cell.cc @@ -27,17 +27,12 @@ #include #include "Cell.h" -#include "defun.h" -#include "error.h" #include "oct-obj.h" #include "pt-arg-list.h" -#include "pt-bp.h" #include "pt-exp.h" #include "pt-cell.h" #include "pt-walk.h" -#include "utils.h" #include "ov.h" -#include "variables.h" octave_value tree_cell::rvalue1 (int) @@ -107,14 +102,7 @@ { tree_cell *new_cell = new tree_cell (0, line (), column ()); - for (const_iterator p = begin (); p != end (); p++) - { - const tree_argument_list *elt = *p; - - new_cell->append (elt ? elt->dup (scope, context) : 0); - } - - new_cell->copy_base (*this); + new_cell->copy_base (*this, scope, context); return new_cell; } diff --git a/libinterp/parse-tree/pt-cell.h b/libinterp/parse-tree/pt-cell.h --- a/libinterp/parse-tree/pt-cell.h +++ b/libinterp/parse-tree/pt-cell.h @@ -37,12 +37,13 @@ // General cells. class -tree_cell : public tree_matrix +tree_cell : public tree_array_list { public: tree_cell (tree_argument_list *row = 0, int l = -1, int c = -1) - : tree_matrix (row, l, c) { } + : tree_array_list (row, l, c) + { } ~tree_cell (void) { } diff --git a/libinterp/parse-tree/pt-mat.cc b/libinterp/parse-tree/pt-mat.cc --- a/libinterp/parse-tree/pt-mat.cc +++ b/libinterp/parse-tree/pt-mat.cc @@ -655,48 +655,6 @@ ok = ! error_state; } -tree_matrix::~tree_matrix (void) -{ - while (! empty ()) - { - iterator p = begin (); - delete *p; - erase (p); - } -} - -bool -tree_matrix::has_magic_end (void) const -{ - for (const_iterator p = begin (); p != end (); p++) - { - octave_quit (); - - tree_argument_list *elt = *p; - - if (elt && elt->has_magic_end ()) - return true; - } - - return false; -} - -bool -tree_matrix::all_elements_are_constant (void) const -{ - for (const_iterator p = begin (); p != end (); p++) - { - octave_quit (); - - tree_argument_list *elt = *p; - - if (! elt->all_elements_are_constant ()) - return false; - } - - return true; -} - octave_value_list tree_matrix::rvalue (int nargout) { @@ -1174,14 +1132,7 @@ { tree_matrix *new_matrix = new tree_matrix (0, line (), column ()); - for (const_iterator p = begin (); p != end (); p++) - { - const tree_argument_list *elt = *p; - - new_matrix->append (elt ? elt->dup (scope, context) : 0); - } - - new_matrix->copy_base (*this); + new_matrix->copy_base (*this, scope, context); return new_matrix; } diff --git a/libinterp/parse-tree/pt-mat.h b/libinterp/parse-tree/pt-mat.h --- a/libinterp/parse-tree/pt-mat.h +++ b/libinterp/parse-tree/pt-mat.h @@ -32,6 +32,7 @@ class tree_walker; #include "base-list.h" +#include "pt-array-list.h" #include "pt-exp.h" #include "symtab.h" @@ -39,23 +40,15 @@ // other matrices, variables, and functions. class -tree_matrix : public tree_expression, - public octave_base_list +tree_matrix : public tree_array_list { public: tree_matrix (tree_argument_list *row = 0, int l = -1, int c = -1) - : tree_expression (l, c) - { - if (row) - append (row); - } + : tree_array_list (row, l, c) + { } - ~tree_matrix (void); - - bool has_magic_end (void) const; - - bool all_elements_are_constant (void) const; + ~tree_matrix (void) { } bool rvalue_ok (void) const { return true; } diff --git a/liboctave/util/base-list.h b/liboctave/util/base-list.h --- a/liboctave/util/base-list.h +++ b/liboctave/util/base-list.h @@ -23,6 +23,8 @@ #if !defined (octave_base_list_h) #define octave_base_list_h 1 +#include + #include template