11use rusty_common:: { AtPos , CaseInsensitiveString , Position , Positioned } ;
2- use rusty_linter:: { Context , Names , SubprogramName } ;
2+ use rusty_linter:: core:: { LinterContext , ScopeName } ;
3+ use rusty_linter:: names:: Names ;
34use rusty_parser:: {
45 Assignment , BareName , BuiltInFunction , BuiltInSub , DimVar , Expression , ExpressionType , FileHandle , FunctionImplementation , GlobalStatement , HasExpressionType , Name , Parameter , Program , Statement , Statements , SubImplementation , TypeQualifier , UserDefinedTypes
56} ;
@@ -11,10 +12,8 @@ use crate::instruction_generator::subprogram_info::{
1112 SubprogramInfoCollector , SubprogramInfoRepository
1213} ;
1314
14- pub fn unwrap_linter_context ( linter_context : Context ) -> ( Names , UserDefinedTypes ) {
15- let ( pre_linter_result, linter_names) = linter_context. into ( ) ;
16- let user_defined_types = pre_linter_result. into ( ) ;
17- ( linter_names, user_defined_types)
15+ pub fn unwrap_linter_context ( linter_context : LinterContext ) -> ( Names , UserDefinedTypes ) {
16+ ( linter_context. names , linter_context. user_defined_types )
1817}
1918
2019/// Generates instructions for the given program.
@@ -181,7 +180,7 @@ pub enum Instruction {
181180 PushUnnamedByRef ,
182181
183182 PushStack ,
184- PushStaticStack ( SubprogramName ) ,
183+ PushStaticStack ( ScopeName ) ,
185184 PopStack ,
186185
187186 EnqueueToReturnStack ( usize ) ,
@@ -264,7 +263,7 @@ pub struct InstructionGenerator {
264263 pub instructions : Vec < InstructionPos > ,
265264 pub statement_addresses : Vec < usize > ,
266265 pub subprogram_info_repository : SubprogramInfoRepository ,
267- pub current_subprogram : Option < SubprogramName > ,
266+ pub current_subprogram : ScopeName ,
268267 pub linter_names : Names ,
269268}
270269
@@ -274,7 +273,7 @@ impl InstructionGenerator {
274273 instructions : vec ! [ ] ,
275274 statement_addresses : vec ! [ ] ,
276275 subprogram_info_repository,
277- current_subprogram : None ,
276+ current_subprogram : ScopeName :: Global ,
278277 linter_names,
279278 }
280279 }
@@ -375,7 +374,7 @@ impl InstructionGenerator {
375374 let qualifier = function_name
376375 . qualifier ( )
377376 . expect ( "Expected qualified function name" ) ;
378- self . mark_current_subprogram ( SubprogramName :: Function ( function_name) , pos) ;
377+ self . mark_current_subprogram ( ScopeName :: Function ( function_name) , pos) ;
379378 // set default value
380379 self . push ( Instruction :: AllocateBuiltIn ( qualifier) , pos) ;
381380 self . subprogram_body ( body, pos) ;
@@ -397,16 +396,21 @@ impl InstructionGenerator {
397396 body,
398397 ..
399398 } = sub_implementation;
400- self . mark_current_subprogram ( SubprogramName :: Sub ( name) , pos) ;
399+ self . mark_current_subprogram ( ScopeName :: Sub ( name) , pos) ;
401400 self . subprogram_body ( body, pos) ;
402401 }
403402
404- fn mark_current_subprogram ( & mut self , subprogram_name : SubprogramName , pos : Position ) {
403+ fn mark_current_subprogram ( & mut self , scope_name : ScopeName , pos : Position ) {
404+ debug_assert_ne ! (
405+ scope_name,
406+ ScopeName :: Global ,
407+ "should not mark global scope"
408+ ) ;
405409 self . push (
406- Instruction :: Label ( Self :: format_subprogram_label ( & subprogram_name ) ) ,
410+ Instruction :: Label ( Self :: format_subprogram_label ( & scope_name ) ) ,
407411 pos,
408412 ) ;
409- self . current_subprogram = Some ( subprogram_name ) ;
413+ self . current_subprogram = scope_name ;
410414 }
411415
412416 fn subprogram_body ( & mut self , block : Statements , pos : Position ) {
@@ -469,20 +473,23 @@ impl InstructionGenerator {
469473 self . statement_addresses . push ( self . instructions . len ( ) ) ;
470474 }
471475
472- pub fn format_subprogram_label ( subprogram_name : & SubprogramName ) -> BareName {
473- let s: String = match subprogram_name {
474- SubprogramName :: Function ( function_name) => {
476+ pub fn format_subprogram_label ( scope_name : & ScopeName ) -> BareName {
477+ let s: String = match scope_name {
478+ ScopeName :: Function ( function_name) => {
475479 let mut s: String = String :: new ( ) ;
476480 s. push_str ( ":fun:" ) ;
477481 s. push_str ( & function_name. to_string ( ) ) ;
478482 s
479483 }
480- SubprogramName :: Sub ( sub_name) => {
484+ ScopeName :: Sub ( sub_name) => {
481485 let mut s: String = String :: new ( ) ;
482486 s. push_str ( ":sub:" ) ;
483487 s. push_str ( sub_name. as_ref ( ) ) ;
484488 s
485489 }
490+ ScopeName :: Global => {
491+ panic ! ( "Should not generate label for global scope" )
492+ }
486493 } ;
487494 BareName :: new ( s)
488495 }
0 commit comments