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 |
|
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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
4203
|
118 bool script_file_executed = false; |
|
119 |
2982
|
120 if (expr->is_identifier ()) |
|
121 { |
|
122 tree_identifier *id = static_cast<tree_identifier *> (expr); |
|
123 |
4203
|
124 id->do_lookup (script_file_executed, true); |
2982
|
125 |
|
126 do_bind_ans = id->is_function (); |
|
127 } |
|
128 else |
3933
|
129 do_bind_ans = (! expr->is_assignment_expression ()); |
2982
|
130 |
4203
|
131 if (! script_file_executed) |
|
132 { |
|
133 retval = expr->rvalue (nargout); |
2982
|
134 |
4203
|
135 if (do_bind_ans && ! (error_state || retval.empty ())) |
|
136 bind_ans (retval(0), pf); |
|
137 } |
2982
|
138 } |
3708
|
139 |
|
140 unwind_protect::run (); |
2982
|
141 } |
|
142 |
|
143 return retval; |
|
144 } |
|
145 |
5861
|
146 tree_statement * |
|
147 tree_statement::dup (symbol_table *sym_tab) |
|
148 { |
|
149 tree_statement *new_stmt = new tree_statement (); |
|
150 |
|
151 new_stmt->cmd = cmd ? cmd->dup (sym_tab) : 0; |
|
152 |
|
153 new_stmt->expr = expr ? expr->dup (sym_tab) : 0; |
|
154 |
|
155 new_stmt->comm = comm ? comm->dup () : 0; |
|
156 |
|
157 new_stmt->print_flag = print_flag; |
|
158 |
|
159 return new_stmt; |
|
160 } |
|
161 |
2982
|
162 void |
|
163 tree_statement::accept (tree_walker& tw) |
|
164 { |
|
165 tw.visit_statement (*this); |
|
166 } |
|
167 |
|
168 octave_value_list |
|
169 tree_statement_list::eval (bool silent, int nargout) |
|
170 { |
|
171 octave_value_list retval; |
|
172 |
5858
|
173 static octave_value_list empty_list; |
|
174 |
2982
|
175 if (error_state) |
|
176 return retval; |
|
177 |
5858
|
178 iterator p = begin (); |
2982
|
179 |
5858
|
180 if (p != end ()) |
|
181 { |
|
182 while (true) |
2982
|
183 { |
5858
|
184 tree_statement *elt = *p++; |
|
185 |
|
186 if (elt) |
|
187 { |
|
188 OCTAVE_QUIT; |
4153
|
189 |
5858
|
190 retval = elt->eval (silent, nargout, function_body); |
|
191 |
|
192 if (error_state) |
|
193 break; |
|
194 |
|
195 if (tree_break_command::breaking |
|
196 || tree_continue_command::continuing) |
|
197 break; |
|
198 |
|
199 if (tree_return_command::returning) |
|
200 break; |
5855
|
201 |
5858
|
202 if (p == end ()) |
|
203 break; |
|
204 else |
|
205 { |
|
206 // Clear preivous values before next statement is |
|
207 // evaluated so that we aren't holding an extra |
|
208 // reference to a value that may be used next. For |
|
209 // example, in code like this: |
|
210 // |
|
211 // X = rand (N); ## refcount for X should be 1 |
|
212 // ## after this statement |
|
213 // |
|
214 // X(idx) = val; ## no extra copy of X should be |
|
215 // ## needed, but we will be faked |
|
216 // ## out if retval is not cleared |
|
217 // ## between statements here |
2982
|
218 |
5858
|
219 retval = empty_list; |
|
220 } |
|
221 } |
|
222 else |
|
223 error ("invalid statement found in statement list!"); |
2982
|
224 } |
|
225 } |
|
226 |
|
227 return retval; |
|
228 } |
|
229 |
3770
|
230 int |
|
231 tree_statement_list::set_breakpoint (int line) |
|
232 { |
|
233 tree_breakpoint tbp (line, tree_breakpoint::set); |
|
234 accept (tbp); |
|
235 |
|
236 return tbp.get_line (); |
|
237 } |
|
238 |
|
239 void |
|
240 tree_statement_list::delete_breakpoint (int line) |
|
241 { |
3895
|
242 if (line < 0) |
|
243 { |
4212
|
244 octave_value_list bp_lst = list_breakpoints (); |
3895
|
245 |
4212
|
246 int len = bp_lst.length (); |
3895
|
247 |
4587
|
248 for (int i = 0; i < len; i++) |
3895
|
249 { |
4587
|
250 tree_breakpoint tbp (i, tree_breakpoint::clear); |
3895
|
251 accept (tbp); |
|
252 } |
|
253 } |
|
254 else |
|
255 { |
|
256 tree_breakpoint tbp (line, tree_breakpoint::clear); |
|
257 accept (tbp); |
|
258 } |
3770
|
259 } |
|
260 |
|
261 octave_value_list |
|
262 tree_statement_list::list_breakpoints (void) |
|
263 { |
|
264 tree_breakpoint tbp (0, tree_breakpoint::list); |
|
265 accept (tbp); |
|
266 |
|
267 return tbp.get_list (); |
|
268 } |
|
269 |
5861
|
270 tree_statement_list * |
|
271 tree_statement_list::dup (symbol_table *sym_tab) |
|
272 { |
|
273 tree_statement_list *new_list = new tree_statement_list (); |
|
274 |
|
275 new_list->function_body = function_body; |
|
276 |
|
277 for (iterator p = begin (); p != end (); p++) |
|
278 { |
|
279 tree_statement *elt = *p; |
|
280 |
|
281 new_list->append (elt ? elt->dup (sym_tab) : 0); |
|
282 } |
|
283 |
|
284 return new_list; |
|
285 } |
|
286 |
2982
|
287 void |
|
288 tree_statement_list::accept (tree_walker& tw) |
|
289 { |
|
290 tw.visit_statement_list (*this); |
|
291 } |
|
292 |
|
293 /* |
|
294 ;;; Local Variables: *** |
|
295 ;;; mode: C++ *** |
|
296 ;;; End: *** |
|
297 */ |