GNU Octave  4.0.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
pt-select.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2015 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include "error.h"
28 #include "oct-obj.h"
29 #include "ov.h"
30 #include "pt-cmd.h"
31 #include "pt-exp.h"
32 #include "pt-select.h"
33 #include "pt-stmt.h"
34 #include "pt-walk.h"
35 #include "Cell.h"
36 #include "ov-typeinfo.h"
37 
38 // If clauses.
39 
41 {
42  delete expr;
43  delete list;
44  delete lead_comm;
45 }
46 
50 {
51  return new tree_if_clause (expr ? expr->dup (scope, context) : 0,
52  list ? list->dup (scope, context) : 0,
53  lead_comm ? lead_comm->dup () : 0);
54 }
55 
56 void
58 {
59  tw.visit_if_clause (*this);
60 }
61 
62 // List of if commands.
63 
67 {
69 
70  for (const_iterator p = begin (); p != end (); p++)
71  {
72  const tree_if_clause *elt = *p;
73 
74  new_icl->append (elt ? elt->dup (scope, context) : 0);
75  }
76 
77  return new_icl;
78 }
79 
80 void
82 {
83  tw.visit_if_command_list (*this);
84 }
85 
86 // If.
87 
89 {
90  delete list;
91  delete lead_comm;
92  delete trail_comm;
93 }
94 
98 {
99  return new tree_if_command (list ? list->dup (scope, context) : 0,
100  lead_comm ? lead_comm->dup () : 0,
101  trail_comm ? trail_comm->dup () : 0,
102  line (), column ());
103 }
104 
105 void
107 {
108  tw.visit_if_command (*this);
109 }
110 
111 // Switch cases.
112 
114 {
115  delete label;
116  delete list;
117  delete lead_comm;
118 }
119 
120 
121 bool
123 {
124  octave_value label_value = label->rvalue1 ();
125 
126  if (! error_state && label_value.is_defined ())
127  {
128  if (label_value.is_cell ())
129  {
130  Cell cell (label_value.cell_value ());
131 
132  for (octave_idx_type i = 0; i < cell.rows (); i++)
133  {
134  for (octave_idx_type j = 0; j < cell.columns (); j++)
135  {
136  bool match = val.is_equal (cell(i,j));
137 
138  if (error_state)
139  return false;
140  else if (match)
141  return true;
142  }
143  }
144  }
145  else
146  {
147  bool match = val.is_equal (label_value);
148 
149  if (error_state)
150  return false;
151  else
152  return match;
153  }
154  }
155 
156  return false;
157 }
158 
162 {
163  return new tree_switch_case (label ? label->dup (scope, context) : 0,
164  list ? list->dup (scope, context) : 0,
165  lead_comm ? lead_comm->dup () : 0);
166 }
167 
168 void
170 {
171  tw.visit_switch_case (*this);
172 }
173 
174 // List of switch cases.
175 
179 {
181 
182  for (const_iterator p = begin (); p != end (); p++)
183  {
184  const tree_switch_case *elt = *p;
185 
186  new_scl->append (elt ? elt->dup (scope, context) : 0);
187  }
188 
189  return new_scl;
190 }
191 
192 void
194 {
195  tw.visit_switch_case_list (*this);
196 }
197 
198 // Switch.
199 
201 {
202  delete expr;
203  delete list;
204  delete lead_comm;
205  delete trail_comm;
206 }
207 
208 tree_command *
211 {
212  return new tree_switch_command (expr ? expr->dup (scope, context) : 0,
213  list ? list->dup (scope, context) : 0,
214  lead_comm ? lead_comm->dup () : 0,
215  trail_comm ? trail_comm->dup () : 0,
216  line (), column ());
217 }
218 
219 void
221 {
222  tw.visit_switch_command (*this);
223 }
Definition: Cell.h:35
virtual void visit_if_clause(tree_if_clause &)=0
void accept(tree_walker &tw)
Definition: pt-select.cc:169
tree_switch_case_list * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:177
tree_command * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:96
bool label_matches(const octave_value &val)
Definition: pt-select.cc:122
void accept(tree_walker &tw)
Definition: pt-select.cc:106
tree_if_command_list(void)
Definition: pt-select.h:93
bool is_defined(void) const
Definition: ov.h:520
void accept(tree_walker &tw)
Definition: pt-select.cc:220
octave_comment_list * lead_comm
Definition: pt-select.h:152
virtual tree_expression * dup(symbol_table::scope_id, symbol_table::context_id context) const =0
bool is_cell(void) const
Definition: ov.h:529
tree_switch_case * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:160
octave_comment_list * trail_comm
Definition: pt-select.h:155
tree_switch_case_list * list
Definition: pt-select.h:287
tree_switch_command(int l=-1, int c=-1)
Definition: pt-select.h:256
Cell cell_value(void) const
Definition: ov.cc:1566
virtual void visit_if_command(tree_if_command &)=0
tree_if_command(int l=-1, int c=-1)
Definition: pt-select.h:126
tree_statement_list * list
Definition: pt-select.h:206
virtual void visit_switch_case_list(tree_switch_case_list &)=0
void append(const elt_type &s)
Definition: base-list.h:105
~tree_if_clause(void)
Definition: pt-select.cc:40
octave_comment_list * lead_comm
Definition: pt-select.h:209
static llvm::LLVMContext & context
Definition: jit-typeinfo.cc:76
virtual void visit_switch_case(tree_switch_case &)=0
int error_state
Definition: error.cc:101
tree_if_command_list * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:65
~tree_switch_command(void)
Definition: pt-select.cc:200
std::list< tree_if_clause * >::const_iterator const_iterator
Definition: base-list.h:37
void accept(tree_walker &tw)
Definition: pt-select.cc:193
virtual void visit_switch_command(tree_switch_command &)=0
virtual int line(void) const
Definition: pt.h:45
tree_expression * label
Definition: pt-select.h:203
virtual octave_value rvalue1(int nargout=1)
Definition: pt-exp.cc:58
tree_statement_list * list
Definition: pt-select.h:76
tree_expression * expr
Definition: pt-select.h:284
tree_if_clause(int l=-1, int c=-1)
Definition: pt-select.h:43
~tree_if_command(void)
Definition: pt-select.cc:88
void accept(tree_walker &tw)
Definition: pt-select.cc:81
virtual void visit_if_command_list(tree_if_command_list &)=0
octave_comment_list * trail_comm
Definition: pt-select.h:293
tree_expression * expr
Definition: pt-select.h:73
octave_comment_list * lead_comm
Definition: pt-select.h:79
void accept(tree_walker &tw)
Definition: pt-select.cc:57
tree_switch_case(int l=-1, int c=-1)
Definition: pt-select.h:171
static bool match(const std::string &filename_arg, const std::string &path_elt_arg)
Definition: kpse.cc:1738
~tree_switch_case(void)
Definition: pt-select.cc:113
tree_command * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:209
octave_comment_list * lead_comm
Definition: pt-select.h:290
octave_comment_list * dup(void) const
Definition: comment-list.cc:33
bool is_equal(const octave_value &) const
Definition: ov.cc:1547
tree_if_command_list * list
Definition: pt-select.h:149
virtual int column(void) const
Definition: pt.h:47
tree_if_clause * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-select.cc:48
tree_statement_list * dup(symbol_table::scope_id scope, symbol_table::context_id context) const
Definition: pt-stmt.cc:271