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