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