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" |
|
39 #include "pt-cmd.h" |
|
40 #include "pt-id.h" |
|
41 #include "pt-idx.h" |
2985
|
42 #include "pt-jump.h" |
2982
|
43 #include "pt-pr-code.h" |
|
44 #include "pt-stmt.h" |
|
45 #include "pt-walk.h" |
3707
|
46 #include "unwind-prot.h" |
2982
|
47 #include "utils.h" |
|
48 #include "variables.h" |
|
49 |
|
50 // If TRUE, turn off printing of results in functions (as if a |
|
51 // semicolon has been appended to each statement). |
|
52 static bool Vsilent_functions; |
|
53 |
3707
|
54 // Pointer to the current statement being executed. |
|
55 tree_statement *curr_statement = 0; |
|
56 |
2982
|
57 // A list of commands to be executed. |
|
58 |
|
59 tree_statement::~tree_statement (void) |
|
60 { |
|
61 delete cmd; |
|
62 delete expr; |
3665
|
63 delete comm; |
2982
|
64 } |
|
65 |
|
66 int |
|
67 tree_statement::line (void) |
|
68 { |
|
69 return cmd ? cmd->line () : (expr ? expr->line () : -1); |
|
70 } |
|
71 |
|
72 int |
|
73 tree_statement::column (void) |
|
74 { |
|
75 return cmd ? cmd->column () : (expr ? expr->column () : -1); |
|
76 } |
|
77 |
|
78 void |
|
79 tree_statement::maybe_echo_code (bool in_function_body) |
|
80 { |
|
81 if (in_function_body |
|
82 && (Vecho_executing_commands & ECHO_FUNCTIONS)) |
|
83 { |
|
84 tree_print_code tpc (octave_stdout, Vps4); |
|
85 |
|
86 accept (tpc); |
|
87 } |
|
88 } |
|
89 |
|
90 octave_value_list |
|
91 tree_statement::eval (bool silent, int nargout, bool in_function_body) |
|
92 { |
|
93 octave_value_list retval; |
|
94 |
|
95 bool pf = silent ? false : print_flag; |
|
96 |
|
97 if (cmd || expr) |
|
98 { |
3708
|
99 unwind_protect_ptr (curr_statement); |
|
100 curr_statement = this; |
|
101 |
2982
|
102 maybe_echo_code (in_function_body); |
|
103 |
|
104 if (cmd) |
|
105 cmd->eval (); |
|
106 else |
|
107 { |
|
108 expr->set_print_flag (pf); |
|
109 |
|
110 // XXX FIXME XXX -- maybe all of this should be packaged in |
|
111 // one virtual function that returns a flag saying whether |
|
112 // or not the expression will take care of binding ans and |
|
113 // printing the result. |
|
114 |
|
115 bool do_bind_ans = false; |
|
116 |
|
117 if (expr->is_identifier ()) |
|
118 { |
|
119 bool script_file_executed = false; |
|
120 |
|
121 tree_identifier *id = static_cast<tree_identifier *> (expr); |
|
122 |
|
123 id->do_lookup (script_file_executed, false); |
|
124 |
|
125 do_bind_ans = id->is_function (); |
|
126 } |
|
127 else |
|
128 do_bind_ans = (! (expr->is_indirect_ref () |
|
129 || expr->is_assignment_expression ())); |
|
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 |
|
187 void |
|
188 tree_statement_list::accept (tree_walker& tw) |
|
189 { |
|
190 tw.visit_statement_list (*this); |
|
191 } |
|
192 |
|
193 static int |
|
194 silent_functions (void) |
|
195 { |
|
196 Vsilent_functions = check_preference ("silent_functions"); |
|
197 |
|
198 return 0; |
|
199 } |
|
200 |
|
201 void |
|
202 symbols_of_pt_stmt (void) |
|
203 { |
3258
|
204 DEFVAR (silent_functions, 0.0, silent_functions, |
3371
|
205 "-*- texinfo -*-\n\ |
|
206 @defvr {Built-in Variable} silent_functions\n\ |
|
207 If the value of @code{silent_functions} is nonzero, internal output\n\ |
|
208 from a function is suppressed. Otherwise, the results of expressions\n\ |
|
209 within a function body that are not terminated with a semicolon will\n\ |
|
210 have their values printed. The default value is 0.\n\ |
|
211 \n\ |
|
212 For example, if the function\n\ |
|
213 \n\ |
|
214 @example\n\ |
|
215 function f ()\n\ |
|
216 2 + 2\n\ |
|
217 endfunction\n\ |
|
218 @end example\n\ |
|
219 \n\ |
|
220 @noindent\n\ |
|
221 is executed, Octave will either print @samp{ans = 4} or nothing\n\ |
|
222 depending on the value of @code{silent_functions}.\n\ |
|
223 @end defvr"); |
2982
|
224 } |
|
225 |
|
226 /* |
|
227 ;;; Local Variables: *** |
|
228 ;;; mode: C++ *** |
|
229 ;;; End: *** |
|
230 */ |