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 |
|
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 <SLList.h> |
|
32 |
|
33 #include "defun.h" |
|
34 #include "error.h" |
|
35 #include "ov.h" |
|
36 #include "oct-lvalue.h" |
|
37 #include "input.h" |
|
38 #include "pager.h" |
3770
|
39 #include "pt-bp.h" |
2982
|
40 #include "pt-cmd.h" |
|
41 #include "pt-id.h" |
|
42 #include "pt-idx.h" |
2985
|
43 #include "pt-jump.h" |
2982
|
44 #include "pt-pr-code.h" |
|
45 #include "pt-stmt.h" |
|
46 #include "pt-walk.h" |
3707
|
47 #include "unwind-prot.h" |
2982
|
48 #include "utils.h" |
|
49 #include "variables.h" |
|
50 |
|
51 // If TRUE, turn off printing of results in functions (as if a |
|
52 // semicolon has been appended to each statement). |
|
53 static bool Vsilent_functions; |
|
54 |
3707
|
55 // Pointer to the current statement being executed. |
|
56 tree_statement *curr_statement = 0; |
|
57 |
2982
|
58 // A list of commands to be executed. |
|
59 |
|
60 tree_statement::~tree_statement (void) |
|
61 { |
|
62 delete cmd; |
|
63 delete expr; |
3665
|
64 delete comm; |
2982
|
65 } |
|
66 |
|
67 int |
|
68 tree_statement::line (void) |
|
69 { |
|
70 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
|
71 } |
|
72 |
|
73 int |
|
74 tree_statement::column (void) |
|
75 { |
|
76 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
|
77 } |
|
78 |
|
79 void |
|
80 tree_statement::maybe_echo_code (bool in_function_body) |
|
81 { |
|
82 if (in_function_body |
|
83 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
|
84 { |
|
85 tree_print_code tpc (octave_stdout, Vps4); |
|
86 |
|
87 accept (tpc); |
|
88 } |
|
89 } |
|
90 |
|
91 octave_value_list |
|
92 tree_statement::eval (bool silent, int nargout, bool in_function_body) |
|
93 { |
|
94 octave_value_list retval; |
|
95 |
|
96 bool pf = silent ? false : print_flag; |
|
97 |
|
98 if (cmd || expr) |
|
99 { |
3708
|
100 unwind_protect_ptr (curr_statement); |
|
101 curr_statement = this; |
|
102 |
2982
|
103 maybe_echo_code (in_function_body); |
|
104 |
|
105 if (cmd) |
|
106 cmd->eval (); |
|
107 else |
|
108 { |
|
109 expr->set_print_flag (pf); |
|
110 |
|
111 // XXX FIXME XXX -- maybe all of this should be packaged in |
|
112 // one virtual function that returns a flag saying whether |
|
113 // or not the expression will take care of binding ans and |
|
114 // printing the result. |
|
115 |
|
116 bool do_bind_ans = false; |
|
117 |
|
118 if (expr->is_identifier ()) |
|
119 { |
|
120 bool script_file_executed = false; |
|
121 |
|
122 tree_identifier *id = static_cast<tree_identifier *> (expr); |
|
123 |
|
124 id->do_lookup (script_file_executed, false); |
|
125 |
|
126 do_bind_ans = id->is_function (); |
|
127 } |
|
128 else |
3933
|
129 do_bind_ans = (! expr->is_assignment_expression ()); |
2982
|
130 |
|
131 retval = expr->rvalue (nargout); |
|
132 |
|
133 if (do_bind_ans && ! (error_state || retval.empty ())) |
|
134 bind_ans (retval(0), pf); |
|
135 } |
3708
|
136 |
|
137 unwind_protect::run (); |
2982
|
138 } |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 void |
|
144 tree_statement::accept (tree_walker& tw) |
|
145 { |
|
146 tw.visit_statement (*this); |
|
147 } |
|
148 |
|
149 octave_value_list |
|
150 tree_statement_list::eval (bool silent, int nargout) |
|
151 { |
|
152 octave_value_list retval; |
|
153 |
|
154 if (error_state) |
|
155 return retval; |
|
156 |
|
157 for (Pix p = first (); p != 0; next (p)) |
|
158 { |
|
159 tree_statement *elt = this->operator () (p); |
|
160 |
|
161 if (elt) |
|
162 { |
|
163 bool silent_flag = |
|
164 silent ? true : (function_body ? Vsilent_functions : false); |
|
165 |
|
166 retval = elt->eval (silent_flag, nargout, function_body); |
|
167 |
|
168 if (error_state) |
|
169 break; |
|
170 |
2985
|
171 if (tree_break_command::breaking |
|
172 || tree_continue_command::continuing) |
2982
|
173 break; |
|
174 |
2985
|
175 if (tree_return_command::returning) |
2982
|
176 break; |
|
177 } |
|
178 else |
|
179 error ("invalid statement found in statement list!"); |
3707
|
180 |
|
181 |
2982
|
182 } |
|
183 |
|
184 return retval; |
|
185 } |
|
186 |
3770
|
187 int |
|
188 tree_statement_list::set_breakpoint (int line) |
|
189 { |
|
190 tree_breakpoint tbp (line, tree_breakpoint::set); |
|
191 accept (tbp); |
|
192 |
|
193 return tbp.get_line (); |
|
194 } |
|
195 |
|
196 void |
|
197 tree_statement_list::delete_breakpoint (int line) |
|
198 { |
3895
|
199 if (line < 0) |
|
200 { |
|
201 octave_value_list lst = list_breakpoints (); |
|
202 |
|
203 int len = lst.length (); |
|
204 |
|
205 for (int line = 0; line < len; line++) |
|
206 { |
|
207 tree_breakpoint tbp (line, tree_breakpoint::clear); |
|
208 accept (tbp); |
|
209 } |
|
210 } |
|
211 else |
|
212 { |
|
213 tree_breakpoint tbp (line, tree_breakpoint::clear); |
|
214 accept (tbp); |
|
215 } |
3770
|
216 } |
|
217 |
|
218 octave_value_list |
|
219 tree_statement_list::list_breakpoints (void) |
|
220 { |
|
221 tree_breakpoint tbp (0, tree_breakpoint::list); |
|
222 accept (tbp); |
|
223 |
|
224 return tbp.get_list (); |
|
225 } |
|
226 |
2982
|
227 void |
|
228 tree_statement_list::accept (tree_walker& tw) |
|
229 { |
|
230 tw.visit_statement_list (*this); |
|
231 } |
|
232 |
|
233 static int |
|
234 silent_functions (void) |
|
235 { |
|
236 Vsilent_functions = check_preference ("silent_functions"); |
|
237 |
|
238 return 0; |
|
239 } |
|
240 |
|
241 void |
|
242 symbols_of_pt_stmt (void) |
|
243 { |
3258
|
244 DEFVAR (silent_functions, 0.0, silent_functions, |
3371
|
245 "-*- texinfo -*-\n\ |
|
246 @defvr {Built-in Variable} silent_functions\n\ |
|
247 If the value of @code{silent_functions} is nonzero, internal output\n\ |
|
248 from a function is suppressed. Otherwise, the results of expressions\n\ |
|
249 within a function body that are not terminated with a semicolon will\n\ |
|
250 have their values printed. The default value is 0.\n\ |
|
251 \n\ |
|
252 For example, if the function\n\ |
|
253 \n\ |
|
254 @example\n\ |
|
255 function f ()\n\ |
|
256 2 + 2\n\ |
|
257 endfunction\n\ |
|
258 @end example\n\ |
|
259 \n\ |
|
260 @noindent\n\ |
|
261 is executed, Octave will either print @samp{ans = 4} or nothing\n\ |
|
262 depending on the value of @code{silent_functions}.\n\ |
|
263 @end defvr"); |
2982
|
264 } |
|
265 |
|
266 /* |
|
267 ;;; Local Variables: *** |
|
268 ;;; mode: C++ *** |
|
269 ;;; End: *** |
|
270 */ |