Mercurial > octave-nkf
annotate src/pt-unop.cc @ 10614:d1194069e58c
optimize code handling ++,--
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 08 May 2010 15:15:22 +0200 |
parents | 57a59eae83cc |
children | fd0a3ac60b0e |
rev | line source |
---|---|
2980 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 2000, 2001, 2002, 2004, 2005, 2006, |
8920 | 4 2007, 2008, 2009 John W. Eaton |
2980 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2980 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2980 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "error.h" | |
29 #include "oct-obj.h" | |
30 #include "oct-lvalue.h" | |
31 #include "ov.h" | |
3770 | 32 #include "pt-bp.h" |
2980 | 33 #include "pt-unop.h" |
34 #include "pt-walk.h" | |
35 | |
3203 | 36 // Unary expressions. |
37 | |
3536 | 38 std::string |
3203 | 39 tree_unary_expression::oper (void) const |
40 { | |
41 return octave_value::unary_op_as_string (etype); | |
42 } | |
43 | |
2980 | 44 // Prefix expressions. |
45 | |
46 octave_value_list | |
47 tree_prefix_expression::rvalue (int nargout) | |
48 { | |
49 octave_value_list retval; | |
50 | |
51 if (nargout > 1) | |
52 error ("prefix operator `%s': invalid number of output arguments", | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
53 oper () . c_str ()); |
2980 | 54 else |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
55 retval = rvalue1 (nargout); |
2980 | 56 |
57 return retval; | |
58 } | |
59 | |
60 octave_value | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
61 tree_prefix_expression::rvalue1 (int) |
2980 | 62 { |
63 octave_value retval; | |
64 | |
65 if (error_state) | |
66 return retval; | |
67 | |
68 if (op) | |
69 { | |
3539 | 70 if (etype == octave_value::op_incr || etype == octave_value::op_decr) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
71 { |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
72 octave_lvalue ref = op->lvalue (); |
3962 | 73 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
74 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
75 { |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
76 ref.do_unary_op (etype); |
3203 | 77 |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
78 if (! error_state) |
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
79 retval = ref.value (); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
80 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
81 } |
3203 | 82 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
83 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
84 octave_value val = op->rvalue1 (); |
2980 | 85 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
86 if (! error_state && val.is_defined ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
87 { |
9887
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
88 // Attempt to do the operation in-place if it is unshared |
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
89 // (a temporary expression). |
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
90 if (val.get_count () == 1) |
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
91 retval = val.do_non_const_unary_op (etype); |
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
92 else |
e3bd1569a68c
in-place operation of unary ops on temporary expressions
Jaroslav Hajek <highegg@gmail.com>
parents:
9716
diff
changeset
|
93 retval = ::do_unary_op (etype, val); |
2980 | 94 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 retval = octave_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
97 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 } |
2980 | 99 } |
100 | |
101 return retval; | |
102 } | |
103 | |
5861 | 104 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
105 tree_prefix_expression::dup (symbol_table::scope_id scope, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
106 symbol_table::context_id context) const |
5861 | 107 { |
108 tree_prefix_expression *new_pe | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
109 = new tree_prefix_expression (op ? op->dup (scope, context) : 0, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
110 line (), column (), etype); |
5861 | 111 |
112 new_pe->copy_base (*this); | |
113 | |
114 return new_pe; | |
115 } | |
116 | |
2980 | 117 void |
118 tree_prefix_expression::accept (tree_walker& tw) | |
119 { | |
120 tw.visit_prefix_expression (*this); | |
121 } | |
122 | |
123 // Postfix expressions. | |
124 | |
125 octave_value_list | |
126 tree_postfix_expression::rvalue (int nargout) | |
127 { | |
128 octave_value_list retval; | |
129 | |
130 if (nargout > 1) | |
131 error ("postfix operator `%s': invalid number of output arguments", | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
132 oper () . c_str ()); |
2980 | 133 else |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
134 retval = rvalue1 (nargout); |
2980 | 135 |
136 return retval; | |
137 } | |
138 | |
139 octave_value | |
8658
73c4516fae10
New evaluator and debugger derived from tree-walker class
John W. Eaton <jwe@octave.org>
parents:
8011
diff
changeset
|
140 tree_postfix_expression::rvalue1 (int) |
2980 | 141 { |
142 octave_value retval; | |
143 | |
144 if (error_state) | |
145 return retval; | |
146 | |
147 if (op) | |
148 { | |
3539 | 149 if (etype == octave_value::op_incr || etype == octave_value::op_decr) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
150 { |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
151 octave_lvalue ref = op->lvalue (); |
3962 | 152 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
153 if (! error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
154 { |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
155 retval = ref.value (); |
3203 | 156 |
10614
d1194069e58c
optimize code handling ++,--
Jaroslav Hajek <highegg@gmail.com>
parents:
10315
diff
changeset
|
157 ref.do_unary_op (etype); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
158 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
159 } |
3203 | 160 else |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
161 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
162 octave_value val = op->rvalue1 (); |
2980 | 163 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
164 if (! error_state && val.is_defined ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
165 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
166 retval = ::do_unary_op (etype, val); |
2980 | 167 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
168 if (error_state) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
169 retval = octave_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
170 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
171 } |
2980 | 172 } |
173 | |
174 return retval; | |
175 } | |
176 | |
5861 | 177 tree_expression * |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
178 tree_postfix_expression::dup (symbol_table::scope_id scope, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
179 symbol_table::context_id context) const |
5861 | 180 { |
181 tree_postfix_expression *new_pe | |
7767
71f068b22fcc
scope and context fixes for function handles
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
182 = new tree_postfix_expression (op ? op->dup (scope, context) : 0, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
183 line (), column (), etype); |
5861 | 184 |
185 new_pe->copy_base (*this); | |
186 | |
187 return new_pe; | |
188 } | |
189 | |
2980 | 190 void |
191 tree_postfix_expression::accept (tree_walker& tw) | |
192 { | |
193 tw.visit_postfix_expression (*this); | |
194 } |