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 |
|
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 "pt-cmd.h" |
|
33 #include "ov.h" |
|
34 #include "oct-lvalue.h" |
|
35 #include "pt-decl.h" |
|
36 #include "pt-exp.h" |
|
37 #include "pt-id.h" |
|
38 #include "pt-walk.h" |
|
39 |
|
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 |
|
57 tree_decl_init_list::eval (tree_decl_elt::eval_fcn f, bool skip_init) |
|
58 { |
|
59 for (Pix p = first (); p != 0; next (p)) |
|
60 { |
|
61 f (*(this->operator () (p)), skip_init); |
|
62 |
|
63 if (error_state) |
|
64 break; |
|
65 } |
|
66 } |
|
67 |
|
68 void |
|
69 tree_decl_init_list::accept (tree_walker& tw) |
|
70 { |
|
71 tw.visit_decl_init_list (*this); |
|
72 } |
|
73 |
|
74 // Base class for declaration commands (global, static). |
|
75 |
|
76 tree_decl_command::~tree_decl_command (void) |
|
77 { |
|
78 delete init_list; |
|
79 } |
|
80 |
|
81 void |
|
82 tree_decl_command::accept (tree_walker& tw) |
|
83 { |
|
84 tw.visit_decl_command (*this); |
|
85 } |
|
86 |
|
87 // Global. |
|
88 |
|
89 static void |
|
90 do_global_init (tree_decl_elt& elt, bool skip_initializer) |
|
91 { |
|
92 tree_identifier *id = elt.ident (); |
|
93 |
|
94 if (id) |
|
95 { |
|
96 id->link_to_global (); |
|
97 |
|
98 tree_expression *expr = elt.expression (); |
|
99 |
|
100 if (expr) |
|
101 { |
|
102 octave_value init_val = expr->rvalue (); |
|
103 |
|
104 octave_lvalue ult = id->lvalue (); |
|
105 |
|
106 ult.assign (octave_value::asn_eq, init_val); |
|
107 } |
|
108 } |
|
109 } |
|
110 |
|
111 void |
|
112 tree_global_command::eval (void) |
|
113 { |
|
114 if (init_list) |
|
115 { |
|
116 init_list->eval (do_global_init, initialized); |
|
117 |
|
118 initialized = true; |
|
119 } |
|
120 |
|
121 if (error_state > 0) |
|
122 ::error ("evaluating global command near line %d, column %d", |
|
123 line (), column ()); |
|
124 } |
|
125 |
|
126 // Static. |
|
127 |
|
128 static void |
|
129 do_static_init (tree_decl_elt& elt, bool) |
|
130 { |
|
131 tree_identifier *id = elt.ident (); |
|
132 |
|
133 if (id) |
|
134 { |
|
135 id->mark_as_static (); |
|
136 |
|
137 tree_expression *expr = elt.expression (); |
|
138 |
|
139 if (expr) |
|
140 { |
|
141 octave_value init_val = expr->rvalue (); |
|
142 |
|
143 octave_lvalue ult = id->lvalue (); |
|
144 |
|
145 ult.assign (octave_value::asn_eq, init_val); |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 void |
|
151 tree_static_command::eval (void) |
|
152 { |
|
153 // Static variables only need to be marked and initialized once. |
|
154 |
|
155 if (init_list && ! initialized) |
|
156 { |
|
157 init_list->eval (do_static_init, initialized); |
|
158 |
|
159 initialized = true; |
|
160 |
|
161 if (error_state > 0) |
|
162 ::error ("evaluating static command near line %d, column %d", |
|
163 line (), column ()); |
|
164 } |
|
165 } |
|
166 |
|
167 /* |
|
168 ;;; Local Variables: *** |
|
169 ;;; mode: C++ *** |
|
170 ;;; End: *** |
|
171 */ |