github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/compiler/codegen/scope.go (about)

     1  package codegen
     2  
     3  type scope struct {
     4  	symbols map[string]link // linkLocal or linkUpval
     5  	labels  map[string]label
     6  
     7  	outer   *scope
     8  	savedSP int
     9  
    10  	doClose bool // generate CLOSE(JMP) op when closeScope called
    11  
    12  	nlocals int // if r >= nlocals then r is tmp variable
    13  
    14  	endPC int
    15  }
    16  
    17  func (s *scope) root() *scope {
    18  	scope := s
    19  	for {
    20  		if scope.outer == nil {
    21  			return scope
    22  		}
    23  
    24  		scope = scope.outer
    25  	}
    26  }
    27  
    28  func (s *scope) resolveLabel(name string) (label, bool) {
    29  	scope := s
    30  	for {
    31  		l, ok := scope.labels[name]
    32  		if ok {
    33  			return l, true
    34  		}
    35  
    36  		scope = scope.outer
    37  		if scope == nil {
    38  			return label{}, false
    39  		}
    40  	}
    41  }
    42  
    43  func (s *scope) declare(name string, l link) {
    44  	s.symbols[name] = l
    45  }
    46  
    47  func (s *scope) resolveLocal(name string) (link, bool) {
    48  	scope := s
    49  	for {
    50  		l, ok := scope.symbols[name]
    51  		if ok {
    52  			return l, true
    53  		}
    54  
    55  		scope = scope.outer
    56  		if scope == nil {
    57  			return link{}, false
    58  		}
    59  	}
    60  }