github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/ast/forinstat.go (about)

     1  package ast
     2  
     3  import (
     4  	"github.com/arnodel/golua/token"
     5  )
     6  
     7  // ForInStat is a statement node representing the "for Vars in Params do Body end"
     8  // statement.
     9  type ForInStat struct {
    10  	Location
    11  	Vars   []Name
    12  	Params []ExpNode
    13  	Body   BlockStat
    14  }
    15  
    16  var _ Stat = ForInStat{}
    17  
    18  // NewForInStat returns a ForInStat instance from the given parts.
    19  func NewForInStat(startTok, endTok *token.Token, itervars []Name, params []ExpNode, body BlockStat) *ForInStat {
    20  	return &ForInStat{
    21  		Location: LocFromTokens(startTok, endTok),
    22  		Vars:     itervars,
    23  		Params:   params,
    24  		Body:     body,
    25  	}
    26  }
    27  
    28  // ProcessStat uses the given StatProcessor to process the receiver.
    29  func (s ForInStat) ProcessStat(p StatProcessor) {
    30  	p.ProcessForInStat(s)
    31  }
    32  
    33  // HWrite prints a tree representation of the node.
    34  func (s ForInStat) HWrite(w HWriter) {
    35  	w.Writef("for in")
    36  	w.Indent()
    37  	for i, v := range s.Vars {
    38  		w.Next()
    39  		w.Writef("var_%d: ", i)
    40  		v.HWrite(w)
    41  	}
    42  	for i, p := range s.Params {
    43  		w.Next()
    44  		w.Writef("param_%d", i)
    45  		p.HWrite(w)
    46  	}
    47  	w.Next()
    48  	w.Writef("Body: ")
    49  	s.Body.HWrite(w)
    50  	w.Dedent()
    51  }