2980
|
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" |
3930
|
32 #include "oct-map.h" |
2980
|
33 #include "oct-obj.h" |
|
34 #include "oct-lvalue.h" |
|
35 #include "ov.h" |
3930
|
36 #include "pager.h" |
2982
|
37 #include "pt-arg-list.h" |
3930
|
38 #include "pt-bp.h" |
2980
|
39 #include "pt-idx.h" |
|
40 #include "pt-walk.h" |
3930
|
41 #include "utils.h" |
|
42 #include "variables.h" |
2980
|
43 |
|
44 // Index expressions. |
|
45 |
3546
|
46 tree_index_expression::tree_index_expression (tree_expression *e, |
|
47 tree_argument_list *lst, |
3929
|
48 int l, int c, type t) |
3215
|
49 : tree_expression (l, c), expr (e), list (lst), |
3929
|
50 itype (t), arg_nm (lst ? lst->get_arg_names () : string_vector ()) { } |
3215
|
51 |
3930
|
52 tree_index_expression::tree_index_expression (tree_expression *e, |
|
53 const std::string& n, |
|
54 int l = -1, int c = -1) |
|
55 : tree_expression (l, c), expr (e), list (0), itype (dot), |
|
56 arg_nm (n) { } |
|
57 |
2980
|
58 tree_index_expression::~tree_index_expression (void) |
|
59 { |
|
60 delete expr; |
|
61 delete list; |
|
62 } |
|
63 |
2991
|
64 // This is useful for printing the name of the variable in an indexed |
|
65 // assignment. |
|
66 |
3536
|
67 std::string |
2991
|
68 tree_index_expression::name (void) const |
|
69 { |
3930
|
70 // ??? FIXME ??? |
|
71 std::string xname = expr->name (); |
|
72 |
|
73 return (! dot || xname == "<unknown>") ? xname : xname + "." + arg_nm(0); |
2991
|
74 } |
|
75 |
2980
|
76 octave_value_list |
|
77 tree_index_expression::rvalue (int nargout) |
|
78 { |
|
79 octave_value_list retval; |
|
80 |
|
81 if (error_state) |
|
82 return retval; |
|
83 |
|
84 octave_value tmp = expr->rvalue (); |
|
85 |
|
86 if (! error_state) |
|
87 { |
3930
|
88 if (itype == dot) |
2980
|
89 { |
3930
|
90 MAYBE_DO_BREAKPOINT; |
|
91 |
|
92 if (nargout > 1) |
|
93 error ("invalid number of output arguments for structure reference"); |
|
94 else |
|
95 { |
|
96 octave_value_list tmp = expr->rvalue (nargout); |
|
97 |
|
98 if (tmp.empty ()) |
|
99 eval_error (); |
|
100 else |
|
101 { |
|
102 octave_value val = tmp(0).do_struct_elt_index_op (arg_nm(0)); |
|
103 |
|
104 if (print_result () && nargout == 0 && val.is_defined ()) |
|
105 { |
|
106 // ??? FIXME ??? |
|
107 |
|
108 std::string xname = name (); |
|
109 |
|
110 if (xname == "<unknown>") |
|
111 bind_ans (val, true); |
|
112 else |
|
113 val.print_with_name (octave_stdout, xname); |
|
114 } |
2980
|
115 |
3930
|
116 retval = val; |
|
117 } |
|
118 } |
|
119 } |
|
120 else if (itype == paren || itype == brace) |
|
121 { |
|
122 octave_value_list args; |
|
123 |
|
124 if (list) |
|
125 args = list->convert_to_const_vector (); |
|
126 |
|
127 if (! error_state) |
|
128 { |
|
129 if (! args.empty ()) |
|
130 args.stash_name_tags (arg_nm); |
|
131 |
|
132 // XXX FIXME XXX -- is this the right thing to do? |
|
133 if (tmp.is_constant ()) |
|
134 retval = tmp.do_index_op (args); |
|
135 else |
|
136 retval = tmp.do_multi_index_op (nargout, args); |
|
137 } |
2980
|
138 else |
3930
|
139 eval_error (); |
2980
|
140 } |
3930
|
141 #if 0 |
2980
|
142 else |
3930
|
143 panic_impossible (); |
|
144 #endif |
2980
|
145 } |
|
146 else |
|
147 eval_error (); |
|
148 |
|
149 return retval; |
|
150 } |
|
151 |
|
152 octave_value |
|
153 tree_index_expression::rvalue (void) |
|
154 { |
|
155 octave_value retval; |
|
156 |
|
157 octave_value_list tmp = rvalue (1); |
|
158 |
|
159 if (! tmp.empty ()) |
|
160 retval = tmp(0); |
|
161 |
|
162 return retval; |
|
163 } |
|
164 |
|
165 octave_lvalue |
|
166 tree_index_expression::lvalue (void) |
|
167 { |
|
168 octave_lvalue retval; |
|
169 |
|
170 if (! error_state) |
|
171 { |
3930
|
172 if (itype == dot) |
|
173 { |
|
174 octave_lvalue tmp = expr->lvalue (); |
2980
|
175 |
3930
|
176 if (tmp.is_undefined () || ! tmp.is_map ()) |
|
177 tmp.define (Octave_map ()); |
|
178 |
|
179 retval = tmp.struct_elt_ref (arg_nm(0)); |
|
180 } |
|
181 else if (itype == paren || itype == brace) |
2980
|
182 { |
3930
|
183 retval = expr->lvalue (); |
|
184 |
|
185 if (! error_state) |
|
186 { |
|
187 octave_value_list args; |
2980
|
188 |
3930
|
189 if (list) |
|
190 args = list->convert_to_const_vector (); |
2980
|
191 |
3930
|
192 retval.set_index (args); |
|
193 } |
|
194 #if 0 |
|
195 else |
|
196 panic_impossible (); |
|
197 #endif |
2980
|
198 } |
|
199 } |
|
200 |
|
201 return retval; |
|
202 } |
|
203 |
|
204 void |
|
205 tree_index_expression::eval_error (void) |
|
206 { |
|
207 if (error_state > 0) |
|
208 { |
|
209 int l = line (); |
|
210 int c = column (); |
|
211 |
3930
|
212 const char *type_str; |
|
213 |
|
214 if (itype == dot) |
|
215 type_str = "structure reference operator"; |
|
216 else if (list) |
|
217 type_str = "index expression"; |
|
218 else |
|
219 type_str = "expression"; |
|
220 |
2980
|
221 if (l != -1 && c != -1) |
3930
|
222 ::error ("evaluating %s near line %d, column %d", type_str, l, c); |
2980
|
223 else |
3930
|
224 ::error ("evaluating %s", type_str); |
2980
|
225 } |
|
226 } |
|
227 |
|
228 void |
|
229 tree_index_expression::accept (tree_walker& tw) |
|
230 { |
|
231 tw.visit_index_expression (*this); |
|
232 } |
|
233 |
|
234 /* |
|
235 ;;; Local Variables: *** |
|
236 ;;; mode: C++ *** |
|
237 ;;; End: *** |
|
238 */ |