Mercurial > octave
comparison src/pt-select.cc @ 2982:20f5cec4f11c
[project @ 1997-05-16 03:29:26 by jwe]
author | jwe |
---|---|
date | Fri, 16 May 1997 03:30:14 +0000 |
parents | |
children | 6cfa474c5b99 |
comparison
equal
deleted
inserted
replaced
2981:38365813950d | 2982:20f5cec4f11c |
---|---|
1 /* | |
2 | |
3 Copyright (C) 1996, 1997 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 2, or (at your option) any | |
10 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, write to the Free | |
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 | |
21 */ | |
22 | |
23 #if defined (__GNUG__) | |
24 #pragma implementation | |
25 #endif | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
31 #include "error.h" | |
32 #include "oct-obj.h" | |
33 #include "ov.h" | |
34 #include "pt-cmd.h" | |
35 #include "pt-exp.h" | |
36 #include "pt-select.h" | |
37 #include "pt-stmt.h" | |
38 #include "pt-walk.h" | |
39 | |
40 // If clauses. | |
41 | |
42 tree_if_clause::~tree_if_clause (void) | |
43 { | |
44 delete expr; | |
45 delete list; | |
46 } | |
47 | |
48 int | |
49 tree_if_clause::eval (void) | |
50 { | |
51 if (is_else_clause () || expr->is_logically_true ("if")) | |
52 { | |
53 if (list) | |
54 list->eval (); | |
55 | |
56 return 1; | |
57 } | |
58 | |
59 return 0; | |
60 } | |
61 | |
62 void | |
63 tree_if_clause::accept (tree_walker& tw) | |
64 { | |
65 tw.visit_if_clause (*this); | |
66 } | |
67 | |
68 // List of if commands. | |
69 | |
70 void | |
71 tree_if_command_list::eval (void) | |
72 { | |
73 for (Pix p = first (); p != 0; next (p)) | |
74 { | |
75 tree_if_clause *t = this->operator () (p); | |
76 | |
77 if (t->eval () || error_state) | |
78 break; | |
79 } | |
80 } | |
81 | |
82 void | |
83 tree_if_command_list::accept (tree_walker& tw) | |
84 { | |
85 tw.visit_if_command_list (*this); | |
86 } | |
87 | |
88 // If. | |
89 | |
90 tree_if_command::~tree_if_command (void) | |
91 { | |
92 delete list; | |
93 } | |
94 | |
95 void | |
96 tree_if_command::eval (void) | |
97 { | |
98 if (list) | |
99 list->eval (); | |
100 | |
101 if (error_state > 0) | |
102 ::error ("evaluating if command near line %d, column %d", | |
103 line (), column ()); | |
104 } | |
105 | |
106 void | |
107 tree_if_command::accept (tree_walker& tw) | |
108 { | |
109 tw.visit_if_command (*this); | |
110 } | |
111 | |
112 // Switch cases. | |
113 | |
114 tree_switch_case::~tree_switch_case (void) | |
115 { | |
116 delete label; | |
117 delete list; | |
118 } | |
119 | |
120 bool | |
121 tree_switch_case::label_matches (const octave_value& val) | |
122 { | |
123 bool retval = false; | |
124 | |
125 octave_value label_value = label->rvalue (); | |
126 | |
127 if (! error_state) | |
128 { | |
129 if (label_value.is_defined ()) | |
130 { | |
131 octave_value tmp = do_binary_op (octave_value::eq, | |
132 val, label_value); | |
133 | |
134 if (! error_state) | |
135 { | |
136 if (tmp.is_defined ()) | |
137 retval = tmp.is_true (); | |
138 else | |
139 eval_error (); | |
140 } | |
141 else | |
142 eval_error (); | |
143 } | |
144 else | |
145 eval_error (); | |
146 } | |
147 else | |
148 eval_error (); | |
149 | |
150 return retval; | |
151 } | |
152 | |
153 int | |
154 tree_switch_case::eval (const octave_value& val) | |
155 { | |
156 int retval = 0; | |
157 | |
158 if (is_default_case () || label_matches (val)) | |
159 { | |
160 if (list) | |
161 list->eval (); | |
162 | |
163 retval = 1; | |
164 } | |
165 | |
166 return retval; | |
167 } | |
168 | |
169 void | |
170 tree_switch_case::eval_error (void) | |
171 { | |
172 ::error ("evaluating switch case label"); | |
173 } | |
174 | |
175 void | |
176 tree_switch_case::accept (tree_walker& tw) | |
177 { | |
178 tw.visit_switch_case (*this); | |
179 } | |
180 | |
181 // List of switch cases. | |
182 | |
183 void | |
184 tree_switch_case_list::eval (const octave_value& val) | |
185 { | |
186 for (Pix p = first (); p != 0; next (p)) | |
187 { | |
188 tree_switch_case *t = this->operator () (p); | |
189 | |
190 if (t->eval (val) || error_state) | |
191 break; | |
192 } | |
193 } | |
194 | |
195 void | |
196 tree_switch_case_list::accept (tree_walker& tw) | |
197 { | |
198 tw.visit_switch_case_list (*this); | |
199 } | |
200 | |
201 // Switch. | |
202 | |
203 tree_switch_command::~tree_switch_command (void) | |
204 { | |
205 delete expr; | |
206 delete list; | |
207 } | |
208 | |
209 void | |
210 tree_switch_command::eval (void) | |
211 { | |
212 if (expr) | |
213 { | |
214 octave_value val = expr->rvalue (); | |
215 | |
216 if (! error_state) | |
217 { | |
218 if (list) | |
219 list->eval (val); | |
220 | |
221 if (error_state) | |
222 eval_error (); | |
223 } | |
224 else | |
225 eval_error (); | |
226 } | |
227 else | |
228 ::error ("missing value in switch command near line %d, column %d", | |
229 line (), column ()); | |
230 } | |
231 | |
232 void | |
233 tree_switch_command::eval_error (void) | |
234 { | |
235 ::error ("evaluating switch command near line %d, column %d", | |
236 line (), column ()); | |
237 } | |
238 | |
239 void | |
240 tree_switch_command::accept (tree_walker& tw) | |
241 { | |
242 tw.visit_switch_command (*this); | |
243 } | |
244 | |
245 /* | |
246 ;;; Local Variables: *** | |
247 ;;; mode: C++ *** | |
248 ;;; End: *** | |
249 */ |