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" |
|
32 #include "oct-obj.h" |
|
33 #include "pager.h" |
|
34 #include "ov.h" |
|
35 #include "pt-colon.h" |
|
36 #include "pt-pr-code.h" |
|
37 #include "pt-walk.h" |
|
38 |
|
39 // Colon expressions. |
|
40 |
|
41 tree_colon_expression * |
|
42 tree_colon_expression::append (tree_expression *t) |
|
43 { |
|
44 tree_colon_expression *retval = 0; |
|
45 |
|
46 if (op_base) |
|
47 { |
|
48 if (op_limit) |
|
49 { |
|
50 if (op_increment) |
|
51 ::error ("invalid colon expression"); |
|
52 else |
|
53 { |
|
54 // Stupid syntax: |
|
55 // |
|
56 // base : limit |
|
57 // base : increment : limit |
|
58 |
|
59 op_increment = op_limit; |
|
60 op_limit = t; |
|
61 } |
|
62 } |
|
63 else |
|
64 op_limit = t; |
|
65 |
|
66 retval = this; |
|
67 } |
|
68 else |
|
69 ::error ("invalid colon expression"); |
|
70 |
|
71 return retval; |
|
72 } |
|
73 |
|
74 octave_value_list |
|
75 tree_colon_expression::rvalue (int nargout) |
|
76 { |
|
77 octave_value_list retval; |
|
78 |
|
79 if (nargout > 1) |
|
80 error ("invalid number of output arguments for colon expression"); |
|
81 else |
|
82 retval = rvalue (); |
|
83 |
|
84 return retval; |
|
85 } |
|
86 |
|
87 octave_value |
|
88 tree_colon_expression::rvalue (void) |
|
89 { |
|
90 octave_value retval; |
|
91 |
|
92 if (error_state || ! op_base || ! op_limit) |
|
93 return retval; |
|
94 |
|
95 octave_value tmp = op_base->rvalue (); |
|
96 |
|
97 if (tmp.is_undefined ()) |
|
98 { |
|
99 eval_error ("invalid null value in colon expression"); |
|
100 return retval; |
|
101 } |
|
102 |
|
103 double xbase = tmp.double_value (); |
|
104 |
|
105 if (error_state) |
|
106 { |
|
107 eval_error ("colon expression elements must be scalars"); |
|
108 return retval; |
|
109 } |
|
110 |
|
111 tmp = op_limit->rvalue (); |
|
112 |
|
113 if (tmp.is_undefined ()) |
|
114 { |
|
115 eval_error ("invalid null value in colon expression"); |
|
116 return retval; |
|
117 } |
|
118 |
|
119 double xlimit = tmp.double_value (); |
|
120 |
|
121 if (error_state) |
|
122 { |
|
123 eval_error ("colon expression elements must be scalars"); |
|
124 return retval; |
|
125 } |
|
126 |
|
127 double xinc = 1.0; |
|
128 |
|
129 if (op_increment) |
|
130 { |
|
131 tmp = op_increment->rvalue (); |
|
132 |
|
133 if (tmp.is_undefined ()) |
|
134 { |
|
135 eval_error ("invalid null value in colon expression"); |
|
136 return retval; |
|
137 } |
|
138 |
|
139 xinc = tmp.double_value (); |
|
140 |
|
141 if (error_state) |
|
142 { |
|
143 eval_error ("colon expression elements must be scalars"); |
|
144 return retval; |
|
145 } |
|
146 } |
|
147 |
|
148 retval = octave_value (xbase, xlimit, xinc); |
|
149 |
|
150 if (error_state) |
|
151 { |
|
152 if (error_state) |
|
153 eval_error (); |
|
154 |
|
155 return octave_value (); |
|
156 } |
|
157 |
|
158 return retval; |
|
159 } |
|
160 |
|
161 void |
|
162 tree_colon_expression::eval_error (const string& s) |
|
163 { |
|
164 if (error_state > 0) |
|
165 { |
|
166 if (! s.empty ()) |
|
167 ::error ("%s", s.c_str ()); |
|
168 |
|
169 ::error ("evaluating colon expression near line %d column %d", |
|
170 line (), column ()); |
|
171 } |
|
172 } |
|
173 |
|
174 void |
|
175 tree_colon_expression::accept (tree_walker& tw) |
|
176 { |
|
177 tw.visit_colon_expression (*this); |
|
178 } |
|
179 |
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |