2982
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1996, 1997, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
|
4 2006, 2007 John W. Eaton |
2982
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
2982
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
2982
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
4153
|
28 #include "quit.h" |
|
29 |
2982
|
30 #include "defun.h" |
|
31 #include "error.h" |
|
32 #include "ov.h" |
|
33 #include "oct-lvalue.h" |
|
34 #include "input.h" |
|
35 #include "pager.h" |
3770
|
36 #include "pt-bp.h" |
2982
|
37 #include "pt-cmd.h" |
|
38 #include "pt-id.h" |
|
39 #include "pt-idx.h" |
2985
|
40 #include "pt-jump.h" |
2982
|
41 #include "pt-pr-code.h" |
|
42 #include "pt-stmt.h" |
|
43 #include "pt-walk.h" |
3707
|
44 #include "unwind-prot.h" |
2982
|
45 #include "utils.h" |
|
46 #include "variables.h" |
|
47 |
3707
|
48 // Pointer to the current statement being executed. |
|
49 tree_statement *curr_statement = 0; |
|
50 |
4975
|
51 // Pointer to the current statement being executed in the calling function. |
|
52 tree_statement *curr_caller_statement = 0; |
|
53 |
2982
|
54 // A list of commands to be executed. |
|
55 |
|
56 tree_statement::~tree_statement (void) |
|
57 { |
|
58 delete cmd; |
|
59 delete expr; |
3665
|
60 delete comm; |
2982
|
61 } |
|
62 |
|
63 int |
|
64 tree_statement::line (void) |
|
65 { |
|
66 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
|
67 } |
|
68 |
|
69 int |
|
70 tree_statement::column (void) |
|
71 { |
|
72 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
|
73 } |
|
74 |
|
75 void |
|
76 tree_statement::maybe_echo_code (bool in_function_body) |
|
77 { |
|
78 if (in_function_body |
|
79 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
|
80 { |
5794
|
81 tree_print_code tpc (octave_stdout, VPS4); |
2982
|
82 |
|
83 accept (tpc); |
|
84 } |
|
85 } |
|
86 |
|
87 octave_value_list |
|
88 tree_statement::eval (bool silent, int nargout, bool in_function_body) |
|
89 { |
|
90 octave_value_list retval; |
|
91 |
|
92 bool pf = silent ? false : print_flag; |
|
93 |
|
94 if (cmd || expr) |
|
95 { |
3708
|
96 unwind_protect_ptr (curr_statement); |
|
97 curr_statement = this; |
|
98 |
2982
|
99 maybe_echo_code (in_function_body); |
|
100 |
|
101 if (cmd) |
|
102 cmd->eval (); |
|
103 else |
|
104 { |
|
105 expr->set_print_flag (pf); |
|
106 |
5775
|
107 // FIXME -- maybe all of this should be packaged in |
2982
|
108 // one virtual function that returns a flag saying whether |
|
109 // or not the expression will take care of binding ans and |
|
110 // printing the result. |
|
111 |
5775
|
112 // FIXME -- it seems that we should just have to |
4203
|
113 // call expr->rvalue () and that should take care of |
|
114 // everything, binding ans as necessary? |
|
115 |
2982
|
116 bool do_bind_ans = false; |
|
117 |
|
118 if (expr->is_identifier ()) |
|
119 { |
6253
|
120 tree_identifier *id = dynamic_cast<tree_identifier *> (expr); |
2982
|
121 |
7336
|
122 do_bind_ans = (! id->is_variable ()); |
2982
|
123 } |
|
124 else |
3933
|
125 do_bind_ans = (! expr->is_assignment_expression ()); |
2982
|
126 |
7336
|
127 retval = expr->rvalue (nargout); |
2982
|
128 |
7336
|
129 if (do_bind_ans && ! (error_state || retval.empty ())) |
|
130 bind_ans (retval(0), pf); |
2982
|
131 } |
3708
|
132 |
|
133 unwind_protect::run (); |
2982
|
134 } |
|
135 |
|
136 return retval; |
|
137 } |
|
138 |
5861
|
139 tree_statement * |
7336
|
140 tree_statement::dup (symbol_table::scope_id scope) |
5861
|
141 { |
|
142 tree_statement *new_stmt = new tree_statement (); |
|
143 |
7336
|
144 new_stmt->cmd = cmd ? cmd->dup (scope) : 0; |
5861
|
145 |
7336
|
146 new_stmt->expr = expr ? expr->dup (scope) : 0; |
5861
|
147 |
|
148 new_stmt->comm = comm ? comm->dup () : 0; |
|
149 |
|
150 new_stmt->print_flag = print_flag; |
|
151 |
|
152 return new_stmt; |
|
153 } |
|
154 |
2982
|
155 void |
|
156 tree_statement::accept (tree_walker& tw) |
|
157 { |
|
158 tw.visit_statement (*this); |
|
159 } |
|
160 |
|
161 octave_value_list |
|
162 tree_statement_list::eval (bool silent, int nargout) |
|
163 { |
|
164 octave_value_list retval; |
|
165 |
5858
|
166 static octave_value_list empty_list; |
|
167 |
2982
|
168 if (error_state) |
|
169 return retval; |
|
170 |
5858
|
171 iterator p = begin (); |
2982
|
172 |
5858
|
173 if (p != end ()) |
|
174 { |
|
175 while (true) |
2982
|
176 { |
5858
|
177 tree_statement *elt = *p++; |
|
178 |
|
179 if (elt) |
|
180 { |
|
181 OCTAVE_QUIT; |
4153
|
182 |
5858
|
183 retval = elt->eval (silent, nargout, function_body); |
|
184 |
|
185 if (error_state) |
|
186 break; |
|
187 |
|
188 if (tree_break_command::breaking |
|
189 || tree_continue_command::continuing) |
|
190 break; |
|
191 |
|
192 if (tree_return_command::returning) |
|
193 break; |
5855
|
194 |
5858
|
195 if (p == end ()) |
|
196 break; |
|
197 else |
|
198 { |
|
199 // Clear preivous values before next statement is |
|
200 // evaluated so that we aren't holding an extra |
|
201 // reference to a value that may be used next. For |
|
202 // example, in code like this: |
|
203 // |
|
204 // X = rand (N); ## refcount for X should be 1 |
|
205 // ## after this statement |
|
206 // |
|
207 // X(idx) = val; ## no extra copy of X should be |
|
208 // ## needed, but we will be faked |
|
209 // ## out if retval is not cleared |
|
210 // ## between statements here |
2982
|
211 |
5858
|
212 retval = empty_list; |
|
213 } |
|
214 } |
|
215 else |
|
216 error ("invalid statement found in statement list!"); |
2982
|
217 } |
|
218 } |
|
219 |
|
220 return retval; |
|
221 } |
|
222 |
3770
|
223 int |
|
224 tree_statement_list::set_breakpoint (int line) |
|
225 { |
|
226 tree_breakpoint tbp (line, tree_breakpoint::set); |
|
227 accept (tbp); |
|
228 |
|
229 return tbp.get_line (); |
|
230 } |
|
231 |
|
232 void |
|
233 tree_statement_list::delete_breakpoint (int line) |
|
234 { |
3895
|
235 if (line < 0) |
|
236 { |
4212
|
237 octave_value_list bp_lst = list_breakpoints (); |
3895
|
238 |
4212
|
239 int len = bp_lst.length (); |
3895
|
240 |
4587
|
241 for (int i = 0; i < len; i++) |
3895
|
242 { |
4587
|
243 tree_breakpoint tbp (i, tree_breakpoint::clear); |
3895
|
244 accept (tbp); |
|
245 } |
|
246 } |
|
247 else |
|
248 { |
|
249 tree_breakpoint tbp (line, tree_breakpoint::clear); |
|
250 accept (tbp); |
|
251 } |
3770
|
252 } |
|
253 |
|
254 octave_value_list |
|
255 tree_statement_list::list_breakpoints (void) |
|
256 { |
|
257 tree_breakpoint tbp (0, tree_breakpoint::list); |
|
258 accept (tbp); |
|
259 |
|
260 return tbp.get_list (); |
|
261 } |
|
262 |
5861
|
263 tree_statement_list * |
7336
|
264 tree_statement_list::dup (symbol_table::scope_id scope) |
5861
|
265 { |
|
266 tree_statement_list *new_list = new tree_statement_list (); |
|
267 |
|
268 new_list->function_body = function_body; |
|
269 |
|
270 for (iterator p = begin (); p != end (); p++) |
|
271 { |
|
272 tree_statement *elt = *p; |
|
273 |
7336
|
274 new_list->append (elt ? elt->dup (scope) : 0); |
5861
|
275 } |
|
276 |
|
277 return new_list; |
|
278 } |
|
279 |
2982
|
280 void |
|
281 tree_statement_list::accept (tree_walker& tw) |
|
282 { |
|
283 tw.visit_statement_list (*this); |
|
284 } |
|
285 |
|
286 /* |
|
287 ;;; Local Variables: *** |
|
288 ;;; mode: C++ *** |
|
289 ;;; End: *** |
|
290 */ |