@@ -31,11 +31,9 @@ e.g. A = 3.14 (resolves as A! by the default rules), A$ = "hello", A% = 1
31315b. An extended variable cannot co-exist with other symbols of the same name
3232*/
3333
34- type Key = Option < SubprogramName > ;
35-
3634pub struct Names {
37- data : HashMap < Key , NamesOneLevel > ,
38- current_key : Key ,
35+ data : HashMap < SubprogramName , NamesOneLevel > ,
36+ current_key : SubprogramName ,
3937}
4038
4139/// Stores the data relevant to one level only (i.e. global symbols, or a FUNCTION, or a SUB).
@@ -47,12 +45,12 @@ struct NamesOneLevel(NamesInner, ImplicitVars);
4745
4846impl Names {
4947 pub fn new ( ) -> Self {
50- let mut data: HashMap < Key , NamesOneLevel > = HashMap :: new ( ) ;
48+ let mut data: HashMap < SubprogramName , NamesOneLevel > = HashMap :: new ( ) ;
5149 // insert GLOBAL scope
52- data. insert ( None , NamesOneLevel :: default ( ) ) ;
50+ data. insert ( SubprogramName :: Global , NamesOneLevel :: default ( ) ) ;
5351 Self {
5452 data,
55- current_key : None ,
53+ current_key : SubprogramName :: Global ,
5654 }
5755 }
5856
@@ -79,8 +77,8 @@ impl Names {
7977 /// Returns the global names, but only if we are currently within a sub program.
8078 fn global_names ( & self ) -> Option < & NamesInner > {
8179 match & self . current_key {
82- Some ( _ ) => self . data . get ( & None ) . map ( |x| & x . 0 ) ,
83- None => None ,
80+ SubprogramName :: Global => None ,
81+ _ => self . data . get ( & SubprogramName :: Global ) . map ( |x| & x . 0 ) ,
8482 }
8583 }
8684
@@ -160,39 +158,40 @@ impl Names {
160158
161159 pub fn is_in_function ( & self , function_name : & BareName ) -> bool {
162160 match & self . current_key {
163- Some ( SubprogramName :: Function ( f) ) => f. as_bare_name ( ) == function_name,
161+ SubprogramName :: Function ( f) => f. as_bare_name ( ) == function_name,
164162 _ => false ,
165163 }
166164 }
167165
168166 pub fn is_in_subprogram ( & self ) -> bool {
169- self . current_key . is_some ( )
167+ self . current_key != SubprogramName :: Global
170168 }
171169
172170 pub fn get_name_scope ( & self ) -> NameScope {
173- match & self . current_key {
174- Some ( SubprogramName :: Function ( _) ) => NameScope :: Function ,
175- Some ( SubprogramName :: Sub ( _) ) => NameScope :: Sub ,
176- None => NameScope :: Global ,
177- }
171+ NameScope :: from ( & self . current_key )
178172 }
179173
180174 pub fn push ( & mut self , subprogram_name : SubprogramName ) {
181- let key = Some ( subprogram_name) ;
182175 debug_assert ! (
183- !self . data. contains_key( & key ) ,
176+ !self . data. contains_key( & subprogram_name ) ,
184177 "should not encounter same function/sub twice!"
185178 ) ;
186- self . current_key = key. clone ( ) ;
187- self . data . insert ( key, NamesOneLevel :: default ( ) ) ;
179+ debug_assert_ne ! (
180+ subprogram_name,
181+ SubprogramName :: Global ,
182+ "should not push global scope"
183+ ) ;
184+ self . current_key = subprogram_name. clone ( ) ;
185+ self . data . insert ( subprogram_name, NamesOneLevel :: default ( ) ) ;
188186 }
189187
190188 pub fn pop ( & mut self ) {
191- debug_assert ! (
192- self . current_key. is_some( ) ,
189+ debug_assert_ne ! (
190+ self . current_key,
191+ SubprogramName :: Global ,
193192 "should not pop context from the global level"
194193 ) ;
195- self . current_key = None ;
194+ self . current_key = SubprogramName :: Global ;
196195 }
197196
198197 pub fn find_name_or_shared_in_parent (
@@ -208,7 +207,7 @@ impl Names {
208207
209208 pub fn get_resolved_variable_info (
210209 & self ,
211- subprogram_name : & Option < SubprogramName > ,
210+ subprogram_name : & SubprogramName ,
212211 name : & Name ,
213212 ) -> & VariableInfo {
214213 let one_level = self
@@ -218,11 +217,11 @@ impl Names {
218217 match one_level. 0 . get_variable_info_by_name ( name) {
219218 Some ( i) => i,
220219 None => {
221- if subprogram_name. is_some ( ) {
220+ if subprogram_name != & SubprogramName :: Global {
222221 // try then parent level but only for SHARED
223222 let result = self
224223 . data
225- . get ( & None )
224+ . get ( & SubprogramName :: Global )
226225 . expect ( "Global name scope missing!" )
227226 . 0
228227 . get_variable_info_by_name ( name)
0 commit comments