Mercurial > hg > octave-lyh
comparison src/pt-arg-list.cc @ 7751:7c020c067a60
F__end__: correctly handle fewer indices than dimensions
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 03 May 2008 22:48:24 -0400 |
parents | 745a8299c2b5 |
children | 71f068b22fcc |
comparison
equal
deleted
inserted
replaced
7750:5c6c6f4803c8 | 7751:7c020c067a60 |
---|---|
92 return true; | 92 return true; |
93 } | 93 } |
94 | 94 |
95 static const octave_value *indexed_object = 0; | 95 static const octave_value *indexed_object = 0; |
96 static int index_position = 0; | 96 static int index_position = 0; |
97 static int num_indices = 0; | |
97 | 98 |
98 DEFCONSTFUN (__end__, , , | 99 DEFCONSTFUN (__end__, , , |
99 "internal function") | 100 "internal function") |
100 { | 101 { |
101 octave_value retval; | 102 octave_value retval; |
102 | 103 |
103 if (indexed_object) | 104 if (indexed_object) |
104 { | 105 { |
105 dim_vector dv = indexed_object->dims (); | 106 dim_vector dv = indexed_object->dims (); |
106 | 107 int ndims = dv.length (); |
107 switch (index_position) | 108 |
108 { | 109 if (num_indices < ndims) |
109 case -1: | 110 { |
110 { | 111 for (int i = num_indices; i < ndims; i++) |
111 octave_idx_type numel = dv.numel (); | 112 dv(num_indices-1) *= dv(i); |
112 | 113 |
113 if (numel < 0) | 114 if (num_indices == 1) |
114 { | 115 { |
115 std::string dv_str = dv.str (); | 116 ndims = 2; |
116 ::error ("invalid use of end: (index 1, dims %s)", | 117 dv.resize (ndims); |
117 dv_str.c_str ()); | 118 dv(1) = 1; |
118 } | 119 } |
119 else | 120 else |
120 retval = numel; | 121 { |
121 } | 122 ndims = num_indices; |
122 break; | 123 dv.resize (ndims); |
123 | 124 } |
124 default: | 125 } |
125 { | 126 |
126 | 127 if (index_position < ndims) |
127 if (index_position < dv.length ()) | 128 retval = dv(index_position); |
128 retval = dv(index_position); | 129 else |
129 else | 130 retval = 1; |
130 retval = 1; | |
131 } | |
132 break; | |
133 } | |
134 } | 131 } |
135 else | 132 else |
136 ::error ("invalid use of end"); | 133 ::error ("invalid use of end"); |
137 | 134 |
138 return retval; | 135 return retval; |
169 for (int k = 0; k < len; k++) | 166 for (int k = 0; k < len; k++) |
170 { | 167 { |
171 if (stash_object) | 168 if (stash_object) |
172 { | 169 { |
173 unwind_protect_int (index_position); | 170 unwind_protect_int (index_position); |
174 | 171 unwind_protect_int (num_indices); |
175 index_position = (len == 1) ? -1 : k; | 172 |
173 index_position = k; | |
174 num_indices = len; | |
176 } | 175 } |
177 | 176 |
178 tree_expression *elt = *p++; | 177 tree_expression *elt = *p++; |
179 | 178 |
180 if (elt) | 179 if (elt) |