2887
|
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 "error.h" |
|
32 #include "oct-obj.h" |
2979
|
33 #include "oct-lvalue.h" |
2900
|
34 #include "pager.h" |
2887
|
35 #include "pt-const.h" |
|
36 #include "pt-id.h" |
|
37 #include "pt-walk.h" |
2900
|
38 #include "symtab.h" |
2887
|
39 #include "utils.h" |
2956
|
40 #include "variables.h" |
2887
|
41 |
|
42 // Symbols from the symbol table. |
|
43 |
|
44 string |
|
45 tree_identifier::name (void) const |
|
46 { |
3523
|
47 std::string retval; |
2887
|
48 if (sym) |
|
49 retval = sym->name (); |
|
50 return retval; |
|
51 } |
|
52 |
|
53 tree_identifier * |
|
54 tree_identifier::define (octave_function *f, unsigned int sym_type) |
|
55 { |
|
56 int status = sym->define (f, sym_type); |
|
57 return status ? this : 0; |
|
58 } |
|
59 |
|
60 void |
3523
|
61 tree_identifier::document (const std::string& s) |
2887
|
62 { |
|
63 if (sym) |
|
64 sym->document (s); |
|
65 } |
|
66 |
|
67 bool |
|
68 tree_identifier::is_defined (void) |
|
69 { |
|
70 return (sym && sym->is_defined ()); |
|
71 } |
|
72 |
2971
|
73 bool |
|
74 tree_identifier::is_function (void) |
|
75 { |
|
76 return (sym && sym->is_function ()); |
|
77 } |
|
78 |
2887
|
79 void |
|
80 tree_identifier::eval_undefined_error (void) |
|
81 { |
|
82 int l = line (); |
|
83 int c = column (); |
|
84 |
|
85 if (l == -1 && c == -1) |
|
86 ::error ("`%s' undefined", name ().c_str ()); |
|
87 else |
|
88 ::error ("`%s' undefined near line %d column %d", |
|
89 name ().c_str (), l, c); |
|
90 } |
|
91 |
|
92 // Try to find a definition for an identifier. Here's how: |
|
93 // |
|
94 // * If the identifier is already defined and is a function defined |
|
95 // in an function file that has been modified since the last time |
|
96 // we parsed it, parse it again. |
|
97 // |
|
98 // * If the identifier is not defined, try to find a builtin |
|
99 // variable or an already compiled function with the same name. |
|
100 // |
|
101 // * If the identifier is still undefined, try looking for an |
|
102 // function file to parse. |
|
103 // |
|
104 // * On systems that support dynamic linking, we prefer .oct files |
|
105 // over .m files. |
|
106 |
2971
|
107 octave_value |
2887
|
108 tree_identifier::do_lookup (bool& script_file_executed, bool exec_script) |
|
109 { |
2971
|
110 static octave_value foo; |
|
111 |
2887
|
112 script_file_executed = lookup (sym, exec_script); |
|
113 |
2971
|
114 return script_file_executed ? foo : sym->def (); |
2887
|
115 } |
|
116 |
|
117 void |
|
118 tree_identifier::link_to_global (void) |
|
119 { |
|
120 if (sym) |
|
121 link_to_global_variable (sym); |
|
122 } |
|
123 |
|
124 void |
|
125 tree_identifier::mark_as_static (void) |
|
126 { |
|
127 if (sym) |
|
128 sym->mark_as_static (); |
|
129 } |
|
130 |
|
131 void |
|
132 tree_identifier::mark_as_formal_parameter (void) |
|
133 { |
|
134 if (sym) |
|
135 sym->mark_as_formal_parameter (); |
|
136 } |
|
137 |
|
138 octave_value_list |
2971
|
139 tree_identifier::rvalue (int nargout) |
2887
|
140 { |
|
141 octave_value_list retval; |
|
142 |
|
143 if (error_state) |
|
144 return retval; |
|
145 |
|
146 bool script_file_executed = false; |
|
147 |
2971
|
148 octave_value val = do_lookup (script_file_executed); |
2887
|
149 |
|
150 if (! script_file_executed) |
|
151 { |
2971
|
152 if (val.is_defined ()) |
2887
|
153 { |
2971
|
154 // XXX GAGME XXX -- this would be cleaner if we required |
|
155 // parens to indicate function calls. |
|
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. |
2887
|
164 |
2971
|
165 if (val.is_function () && ! is_postfix_indexed ()) |
|
166 { |
|
167 octave_value_list tmp_args; |
2887
|
168 |
2971
|
169 retval = val.do_index_op (nargout, tmp_args); |
2887
|
170 } |
|
171 else |
2971
|
172 { |
|
173 if (print_result () && nargout == 0) |
|
174 val.print_with_name (octave_stdout, name ()); |
|
175 |
|
176 retval = val; |
|
177 } |
2887
|
178 } |
|
179 else |
|
180 eval_undefined_error (); |
|
181 } |
|
182 |
|
183 return retval; |
|
184 } |
|
185 |
2971
|
186 octave_value |
|
187 tree_identifier::rvalue (void) |
|
188 { |
|
189 octave_value retval; |
|
190 |
|
191 octave_value_list tmp = rvalue (1); |
|
192 |
|
193 if (! tmp.empty ()) |
|
194 retval = tmp(0); |
|
195 |
|
196 return retval; |
|
197 } |
|
198 |
2979
|
199 octave_lvalue |
2971
|
200 tree_identifier::lvalue (void) |
|
201 { |
|
202 return sym->variable_reference (); |
|
203 } |
|
204 |
2887
|
205 void |
|
206 tree_identifier::accept (tree_walker& tw) |
|
207 { |
|
208 tw.visit_identifier (*this); |
|
209 } |
|
210 |
|
211 /* |
|
212 ;;; Local Variables: *** |
|
213 ;;; mode: C++ *** |
|
214 ;;; End: *** |
|
215 */ |