2982
|
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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
2982
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3156
|
28 #include "defun.h" |
2982
|
29 #include "error.h" |
|
30 #include "pt-cmd.h" |
|
31 #include "ov.h" |
|
32 #include "oct-lvalue.h" |
|
33 #include "pt-decl.h" |
|
34 #include "pt-exp.h" |
|
35 #include "pt-id.h" |
|
36 #include "pt-walk.h" |
3156
|
37 #include "utils.h" |
|
38 #include "variables.h" |
|
39 |
2982
|
40 // Declarations (global, static, etc.). |
|
41 |
|
42 tree_decl_elt::~tree_decl_elt (void) |
|
43 { |
|
44 delete id; |
|
45 delete expr; |
|
46 } |
|
47 |
|
48 void |
|
49 tree_decl_elt::accept (tree_walker& tw) |
|
50 { |
|
51 tw.visit_decl_elt (*this); |
|
52 } |
|
53 |
|
54 // Initializer lists for declaration statements. |
|
55 |
|
56 void |
2989
|
57 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f) |
2982
|
58 { |
4219
|
59 for (iterator p = begin (); p != end (); p++) |
2982
|
60 { |
4219
|
61 tree_decl_elt *elt = *p; |
|
62 |
|
63 f (*elt); |
2982
|
64 |
|
65 if (error_state) |
|
66 break; |
|
67 } |
|
68 } |
|
69 |
|
70 void |
|
71 tree_decl_init_list::accept (tree_walker& tw) |
|
72 { |
|
73 tw.visit_decl_init_list (*this); |
|
74 } |
|
75 |
|
76 // Base class for declaration commands (global, static). |
|
77 |
|
78 tree_decl_command::~tree_decl_command (void) |
|
79 { |
|
80 delete init_list; |
|
81 } |
|
82 |
|
83 void |
|
84 tree_decl_command::accept (tree_walker& tw) |
|
85 { |
|
86 tw.visit_decl_command (*this); |
|
87 } |
|
88 |
|
89 // Global. |
|
90 |
2989
|
91 void |
|
92 tree_global_command::do_init (tree_decl_elt& elt) |
2982
|
93 { |
|
94 tree_identifier *id = elt.ident (); |
|
95 |
|
96 if (id) |
|
97 { |
|
98 id->link_to_global (); |
|
99 |
4009
|
100 if (! error_state) |
|
101 { |
|
102 octave_lvalue ult = id->lvalue (); |
2982
|
103 |
4009
|
104 if (ult.is_undefined ()) |
|
105 { |
|
106 tree_expression *expr = elt.expression (); |
2982
|
107 |
4009
|
108 octave_value init_val; |
3157
|
109 |
4009
|
110 if (expr) |
|
111 init_val = expr->rvalue (); |
4464
|
112 else |
|
113 init_val = Matrix (); |
2982
|
114 |
4009
|
115 ult.assign (octave_value::op_asn_eq, init_val); |
|
116 } |
2982
|
117 } |
|
118 } |
|
119 } |
|
120 |
|
121 void |
|
122 tree_global_command::eval (void) |
|
123 { |
|
124 if (init_list) |
|
125 { |
2989
|
126 init_list->eval (do_init); |
2982
|
127 |
|
128 initialized = true; |
|
129 } |
|
130 |
3965
|
131 if (error_state) |
2982
|
132 ::error ("evaluating global command near line %d, column %d", |
|
133 line (), column ()); |
|
134 } |
|
135 |
|
136 // Static. |
|
137 |
2989
|
138 void |
|
139 tree_static_command::do_init (tree_decl_elt& elt) |
2982
|
140 { |
|
141 tree_identifier *id = elt.ident (); |
|
142 |
|
143 if (id) |
|
144 { |
|
145 id->mark_as_static (); |
|
146 |
4844
|
147 octave_lvalue ult = id->lvalue (); |
2982
|
148 |
4846
|
149 if (ult.is_undefined ()) |
2982
|
150 { |
4844
|
151 tree_expression *expr = elt.expression (); |
|
152 |
|
153 octave_value init_val; |
2982
|
154 |
4844
|
155 if (expr) |
|
156 init_val = expr->rvalue (); |
|
157 else |
|
158 init_val = Matrix (); |
2982
|
159 |
3538
|
160 ult.assign (octave_value::op_asn_eq, init_val); |
2982
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 void |
|
166 tree_static_command::eval (void) |
|
167 { |
|
168 // Static variables only need to be marked and initialized once. |
|
169 |
|
170 if (init_list && ! initialized) |
|
171 { |
2989
|
172 init_list->eval (do_init); |
2982
|
173 |
|
174 initialized = true; |
|
175 |
3965
|
176 if (error_state) |
2982
|
177 ::error ("evaluating static command near line %d, column %d", |
|
178 line (), column ()); |
|
179 } |
|
180 } |
|
181 |
|
182 /* |
|
183 ;;; Local Variables: *** |
|
184 ;;; mode: C++ *** |
|
185 ;;; End: *** |
|
186 */ |