comparison src/pt-id.cc @ 2971:f2be17e6f1ea

[project @ 1997-05-15 18:55:44 by jwe]
author jwe
date Thu, 15 May 1997 18:55:47 +0000
parents 194b50e4725b
children a3556d2adec9
comparison
equal deleted inserted replaced
2970:b9e64477f703 2971:f2be17e6f1ea
28 #include <config.h> 28 #include <config.h>
29 #endif 29 #endif
30 30
31 #include "error.h" 31 #include "error.h"
32 #include "oct-obj.h" 32 #include "oct-obj.h"
33 #include "oct-fcn.h"
34 #include "oct-sym.h"
35 #include "oct-var-ref.h" 33 #include "oct-var-ref.h"
36 #include "pager.h" 34 #include "pager.h"
37 #include "pt-const.h" 35 #include "pt-const.h"
38 #include "pt-id.h" 36 #include "pt-id.h"
39 #include "pt-walk.h" 37 #include "pt-walk.h"
70 tree_identifier::is_defined (void) 68 tree_identifier::is_defined (void)
71 { 69 {
72 return (sym && sym->is_defined ()); 70 return (sym && sym->is_defined ());
73 } 71 }
74 72
73 bool
74 tree_identifier::is_function (void)
75 {
76 return (sym && sym->is_function ());
77 }
78
75 void 79 void
76 tree_identifier::eval_undefined_error (void) 80 tree_identifier::eval_undefined_error (void)
77 { 81 {
78 int l = line (); 82 int l = line ();
79 int c = column (); 83 int c = column ();
98 // function file to parse. 102 // function file to parse.
99 // 103 //
100 // * On systems that support dynamic linking, we prefer .oct files 104 // * On systems that support dynamic linking, we prefer .oct files
101 // over .m files. 105 // over .m files.
102 106
103 octave_symbol * 107 octave_value
104 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script) 108 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script)
105 { 109 {
110 static octave_value foo;
111
106 script_file_executed = lookup (sym, exec_script); 112 script_file_executed = lookup (sym, exec_script);
107 113
108 octave_symbol *retval = 0; 114 return script_file_executed ? foo : sym->def ();
109
110 if (! script_file_executed)
111 retval = sym->def ();
112
113 return retval;
114 } 115 }
115 116
116 void 117 void
117 tree_identifier::link_to_global (void) 118 tree_identifier::link_to_global (void)
118 { 119 {
132 { 133 {
133 if (sym) 134 if (sym)
134 sym->mark_as_formal_parameter (); 135 sym->mark_as_formal_parameter ();
135 } 136 }
136 137
137 octave_value 138 octave_value_list
138 tree_identifier::eval (bool print) 139 tree_identifier::rvalue (int nargout)
139 { 140 {
140 octave_value retval; 141 octave_value_list retval;
141 142
142 if (error_state) 143 if (error_state)
143 return retval; 144 return retval;
144 145
145 bool script_file_executed = false; 146 bool script_file_executed = false;
146 147
147 octave_symbol *object_to_eval = do_lookup (script_file_executed); 148 octave_value val = do_lookup (script_file_executed);
148 149
149 if (! script_file_executed) 150 if (! script_file_executed)
150 { 151 {
151 if (object_to_eval) 152 if (val.is_defined ())
152 { 153 {
153 int nargout = maybe_do_ans_assign ? 0 : 1; 154 // XXX GAGME XXX -- this would be cleaner if we required
154 155 // parens to indicate function calls.
155 if (nargout) 156 //
157 // If this identifier refers to a function, we need to know
158 // whether it is indexed so that we can do the same thing
159 // for `f' and `f()'. If the index is present, return the
160 // function object and let tree_index_expression::rvalue
161 // handle indexing. Otherwise, arrange to call the function
162 // here, so that we don't return the function definition as
163 // a value.
164
165 if (val.is_function () && ! is_postfix_indexed ())
156 { 166 {
157 octave_value_list tmp_args; 167 octave_value_list tmp_args;
158 octave_value_list tmp = object_to_eval->eval (nargout, tmp_args); 168
159 169 retval = val.do_index_op (nargout, tmp_args);
160 if (tmp.length () > 0)
161 retval = tmp(0);
162 } 170 }
163 else 171 else
164 retval = object_to_eval->eval (); 172 {
173 if (print_result () && nargout == 0)
174 val.print_with_name (octave_stdout, name ());
175
176 retval = val;
177 }
165 } 178 }
166 else 179 else
167 eval_undefined_error (); 180 eval_undefined_error ();
168 } 181 }
169 182
170 if (! error_state)
171 {
172 if (retval.is_defined ())
173 {
174 if (maybe_do_ans_assign && ! object_to_eval->is_constant ())
175 bind_ans (retval, print);
176 else if (print)
177 retval.print_with_name (octave_stdout, name ());
178 }
179 else if (object_to_eval && object_to_eval->is_constant ())
180 eval_undefined_error ();
181 }
182
183 return retval; 183 return retval;
184 } 184 }
185 185
186 octave_value_list 186 octave_value
187 tree_identifier::eval (bool print, int nargout, const octave_value_list& args) 187 tree_identifier::rvalue (void)
188 { 188 {
189 octave_value_list retval; 189 octave_value retval;
190 190
191 if (error_state) 191 octave_value_list tmp = rvalue (1);
192 return retval; 192
193 193 if (! tmp.empty ())
194 bool script_file_executed = false; 194 retval = tmp(0);
195
196 octave_symbol *object_to_eval = do_lookup (script_file_executed);
197
198 if (! script_file_executed)
199 {
200 if (object_to_eval)
201 {
202 if (maybe_do_ans_assign && nargout == 1)
203 {
204 // Don't count the output arguments that we create
205 // automatically.
206
207 nargout = 0;
208
209 retval = object_to_eval->eval (nargout, args);
210
211 if (retval.length () > 0 && retval(0).is_defined ())
212 bind_ans (retval(0), print);
213 }
214 else
215 retval = object_to_eval->eval (nargout, args);
216 }
217 else
218 eval_undefined_error ();
219 }
220 195
221 return retval; 196 return retval;
222 } 197 }
223 198
199 octave_variable_reference
200 tree_identifier::lvalue (void)
201 {
202 return sym->variable_reference ();
203 }
204
224 void 205 void
225 tree_identifier::accept (tree_walker& tw) 206 tree_identifier::accept (tree_walker& tw)
226 { 207 {
227 tw.visit_identifier (*this); 208 tw.visit_identifier (*this);
228 }
229
230 octave_value
231 tree_identifier::value (void) const
232 {
233 return sym->variable_value ();
234 }
235
236 octave_variable_reference
237 tree_identifier::reference (void)
238 {
239 return sym->variable_reference ();
240 } 209 }
241 210
242 /* 211 /*
243 ;;; Local Variables: *** 212 ;;; Local Variables: ***
244 ;;; mode: C++ *** 213 ;;; mode: C++ ***