pt-binop.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_tree_binop_h)
00024 #define octave_tree_binop_h 1
00025 
00026 #include <string>
00027 
00028 class tree_walker;
00029 
00030 class octave_value;
00031 class octave_value_list;
00032 class octave_lvalue;
00033 
00034 #include "ov.h"
00035 #include "pt-exp.h"
00036 #include "symtab.h"
00037 
00038 // Binary expressions.
00039 
00040 class
00041 tree_binary_expression : public tree_expression
00042 {
00043 public:
00044 
00045   tree_binary_expression (int l = -1, int c = -1,
00046                           octave_value::binary_op t
00047                             = octave_value::unknown_binary_op)
00048     : tree_expression (l, c), op_lhs (0), op_rhs (0), etype (t),
00049       eligible_for_braindead_shortcircuit (false) { }
00050 
00051   tree_binary_expression (tree_expression *a, tree_expression *b,
00052                           int l = -1, int c = -1,
00053                           octave_value::binary_op t
00054                             = octave_value::unknown_binary_op)
00055     : tree_expression (l, c), op_lhs (a), op_rhs (b), etype (t),
00056       eligible_for_braindead_shortcircuit (false) { }
00057 
00058   ~tree_binary_expression (void)
00059     {
00060       delete op_lhs;
00061       delete op_rhs;
00062     }
00063 
00064   void mark_braindead_shortcircuit (const std::string& file)
00065     {
00066       if (etype == octave_value::op_el_and
00067           || etype == octave_value::op_el_or)
00068         {
00069           if (file.empty ())
00070             warning_with_id ("Octave:possible-matlab-short-circuit-operator",
00071                              "possible Matlab-style short-circuit operator at line %d, column %d",
00072                              line (), column ());
00073           else
00074             warning_with_id ("Octave:possible-matlab-short-circuit-operator",
00075                              "%s: possible Matlab-style short-circuit operator at line %d, column %d",
00076                              file.c_str (), line (), column ());
00077 
00078           eligible_for_braindead_shortcircuit = true;
00079 
00080           op_lhs->mark_braindead_shortcircuit (file);
00081           op_rhs->mark_braindead_shortcircuit (file);
00082         }
00083     }
00084 
00085   bool has_magic_end (void) const
00086     {
00087       return ((op_lhs && op_lhs->has_magic_end ())
00088               || (op_rhs && op_rhs->has_magic_end ()));
00089     }
00090 
00091   bool is_binary_expression (void) const { return true; }
00092 
00093   bool rvalue_ok (void) const { return true; }
00094 
00095   octave_value rvalue1 (int nargout = 1);
00096 
00097   octave_value_list rvalue (int nargout);
00098 
00099   std::string oper (void) const;
00100 
00101   octave_value::binary_op op_type (void) const { return etype; }
00102 
00103   tree_expression *lhs (void) { return op_lhs; }
00104   tree_expression *rhs (void) { return op_rhs; }
00105 
00106   tree_expression *dup (symbol_table::scope_id scope,
00107                         symbol_table::context_id context) const;
00108 
00109   void accept (tree_walker& tw);
00110 
00111 protected:
00112 
00113   // The operands for the expression.
00114   tree_expression *op_lhs;
00115   tree_expression *op_rhs;
00116 
00117 private:
00118 
00119   // The type of the expression.
00120   octave_value::binary_op etype;
00121 
00122   // TRUE if this is an | or & expression in the condition of an IF
00123   // or WHILE statement.
00124   bool eligible_for_braindead_shortcircuit;
00125 
00126   // No copying!
00127 
00128   tree_binary_expression (const tree_binary_expression&);
00129 
00130   tree_binary_expression& operator = (const tree_binary_expression&);
00131 };
00132 
00133 // Boolean expressions.
00134 
00135 class
00136 tree_boolean_expression : public tree_binary_expression
00137 {
00138 public:
00139 
00140   enum type
00141     {
00142       unknown,
00143       bool_and,
00144       bool_or
00145     };
00146 
00147   tree_boolean_expression (int l = -1, int c = -1, type t = unknown)
00148     : tree_binary_expression (l, c), etype (t) { }
00149 
00150   tree_boolean_expression (tree_expression *a, tree_expression *b,
00151                            int l = -1, int c = -1, type t = unknown)
00152     : tree_binary_expression (a, b, l, c), etype (t) { }
00153 
00154   ~tree_boolean_expression (void) { }
00155 
00156   bool is_boolean_expression (void) const { return true; }
00157 
00158   bool rvalue_ok (void) const { return true; }
00159 
00160   octave_value rvalue1 (int nargout = 1);
00161 
00162   octave_value_list rvalue (int nargout);
00163 
00164   std::string oper (void) const;
00165 
00166   type op_type (void) const { return etype; }
00167 
00168   tree_expression *dup (symbol_table::scope_id scope,
00169                         symbol_table::context_id context) const;
00170 
00171 private:
00172 
00173   // The type of the expression.
00174   type etype;
00175 
00176   // No copying!
00177 
00178   tree_boolean_expression (const tree_boolean_expression&);
00179 
00180   tree_boolean_expression& operator = (const tree_boolean_expression&);
00181 };
00182 
00183 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines