@@ -90,6 +90,12 @@ Context *Context::getParent() const {
9090 return parent;
9191}
9292
93+ Context *Context::getGlobalContext () {
94+ Context *ctx = this ;
95+ while (ctx->parent != nullptr ) {ctx = ctx->parent ;}
96+ return ctx;
97+ }
98+
9399const std::string &Context::getName () const {
94100 return name;
95101}
@@ -98,12 +104,13 @@ void Context::addVariable(Variable *variable) {
98104 variables.emplace_back (variable);
99105}
100106
101- Variable *Context::getVariable (const std::string &varName) {
107+ Variable *Context::getVariable (const std::string &varName, bool global ) {
102108 for (auto &var : variables) {
103109 if (var->name == varName) return var.get ();
104110 }
105111
106- return nullptr ;
112+ if (!global || parent == nullptr ) return nullptr ;
113+ return getGlobalContext ()->getVariable (varName);
107114}
108115
109116void Context::addProcedure (std::unique_ptr<Procedure> &&procedure) {
@@ -142,17 +149,18 @@ void Context::addArray(std::unique_ptr<Array> &&array) {
142149 arrays.emplace_back (std::move (array));
143150}
144151
145- Array *Context::getArray (const std::string &arrayName) {
152+ Array *Context::getArray (const std::string &arrayName, bool global ) {
146153 for (auto &array : arrays) {
147154 if (arrayName == array->name ) {
148155 return array.get ();
149156 }
150157 }
151158
152- return nullptr ;
159+ if (!global || parent == nullptr ) return nullptr ;
160+ return getGlobalContext ()->getArray (arrayName);
153161}
154162
155- PSC::DataType Context::getType (const Token &token) {
163+ PSC::DataType Context::getType (const Token &token, bool global ) {
156164 if (token.type == TokenType::DATA_TYPE) {
157165 if (token.value == " INTEGER" ) return PSC::DataType (PSC::DataType::INTEGER);
158166 else if (token.value == " REAL" ) return PSC::DataType (PSC::DataType::REAL);
@@ -161,15 +169,15 @@ PSC::DataType Context::getType(const Token &token) {
161169 else if (token.value == " STRING" ) return PSC::DataType (PSC::DataType::STRING);
162170 else std::abort ();
163171 } else if (token.type == TokenType::IDENTIFIER) {
164- auto *enumDefinition = getEnumDefinition (token.value );
172+ auto *enumDefinition = getEnumDefinition (token.value , global );
165173 if (enumDefinition != nullptr )
166174 return PSC::DataType (PSC::DataType::ENUM, &enumDefinition->name );
167175
168- auto *pointerDefinition = getPointerDefinition (token.value );
176+ auto *pointerDefinition = getPointerDefinition (token.value , global );
169177 if (pointerDefinition != nullptr )
170178 return PSC::DataType (PSC::DataType::POINTER, &pointerDefinition->name );
171179
172- auto *compositeDefinition = getCompositeDefinition (token.value );
180+ auto *compositeDefinition = getCompositeDefinition (token.value , global );
173181 if (compositeDefinition != nullptr )
174182 return PSC::DataType (PSC::DataType::COMPOSITE, &compositeDefinition->name );
175183
@@ -178,19 +186,19 @@ PSC::DataType Context::getType(const Token &token) {
178186 std::abort ();
179187}
180188
181- bool Context::isIdentifierType (const Token &identifier) {
182- PSC::DataType dataType = getType (identifier);
189+ bool Context::isIdentifierType (const Token &identifier, bool global ) {
190+ PSC::DataType dataType = getType (identifier, global );
183191 if (dataType != PSC::DataType::NONE)
184192 return true ;
185193
186- std::unique_ptr<PSC::Enum> enumEl (getEnumElement (identifier.value ));
194+ std::unique_ptr<PSC::Enum> enumEl (getEnumElement (identifier.value , global ));
187195 if (enumEl.get () != nullptr )
188196 return true ;
189197
190198 return false ;
191199}
192200
193- Enum *Context::getEnumElement (const std::string &value) {
201+ Enum *Context::getEnumElement (const std::string &value, bool global ) {
194202 for (auto &definition : enums) {
195203 for (size_t i = 0 ; i < definition->values .size (); i++) {
196204 if (definition->values [i] == value) {
@@ -200,7 +208,7 @@ Enum *Context::getEnumElement(const std::string &value) {
200208 }
201209 }
202210 }
203- if (parent != nullptr ) return parent ->getEnumElement (value);
211+ if (global && parent != nullptr ) return getGlobalContext () ->getEnumElement (value);
204212 return nullptr ;
205213}
206214
@@ -216,26 +224,26 @@ void Context::createCompositeDefinition(CompositeTypeDefinition &&definition) {
216224 composites.emplace_back (std::make_unique<CompositeTypeDefinition>(std::move (definition)));
217225}
218226
219- const EnumTypeDefinition *Context::getEnumDefinition (const std::string &name) {
227+ const EnumTypeDefinition *Context::getEnumDefinition (const std::string &name, bool global ) {
220228 for (const auto &e : enums) {
221229 if (e->name == name) return e.get ();
222230 }
223- if (parent != nullptr ) return parent ->getEnumDefinition (name);
231+ if (global && parent != nullptr ) return getGlobalContext () ->getEnumDefinition (name);
224232 return nullptr ;
225233}
226234
227- const PointerTypeDefinition *Context::getPointerDefinition (const std::string &name) {
235+ const PointerTypeDefinition *Context::getPointerDefinition (const std::string &name, bool global ) {
228236 for (const auto &p : pointers) {
229237 if (p->name == name) return p.get ();
230238 }
231- if (parent != nullptr ) return parent ->getPointerDefinition (name);
239+ if (global && parent != nullptr ) return getGlobalContext () ->getPointerDefinition (name);
232240 return nullptr ;
233241}
234242
235- const CompositeTypeDefinition *Context::getCompositeDefinition (const std::string &name) {
243+ const CompositeTypeDefinition *Context::getCompositeDefinition (const std::string &name, bool global ) {
236244 for (const auto &c : composites) {
237245 if (c->name == name) return c.get ();
238246 }
239- if (parent != nullptr ) return parent ->getCompositeDefinition (name);
247+ if (global && parent != nullptr ) return getGlobalContext () ->getCompositeDefinition (name);
240248 return nullptr ;
241249}
0 commit comments