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

     1  package ast
     2  
     3  // A BlockStat is a statement node that represents a block of statements,
     4  // optionally ending in a return statement (if Return is not a nil slice - note
     5  // that a bare return is encoded as a non-nil slice of length 0).
     6  type BlockStat struct {
     7  	Location
     8  	Stats  []Stat
     9  	Return []ExpNode
    10  }
    11  
    12  var _ Stat = BlockStat{}
    13  
    14  // NewBlockStat returns a BlockStat instance conatining the given stats and
    15  // return statement.
    16  func NewBlockStat(stats []Stat, rtn []ExpNode) BlockStat {
    17  	return BlockStat{
    18  		// TODO: set Location
    19  		Stats:  stats,
    20  		Return: rtn,
    21  	}
    22  }
    23  
    24  // HWrite prints a tree representation of the node.
    25  func (s BlockStat) HWrite(w HWriter) {
    26  	w.Writef("block")
    27  	w.Indent()
    28  	for _, stat := range s.Stats {
    29  		w.Next()
    30  		stat.HWrite(w)
    31  	}
    32  	if s.Return != nil {
    33  		w.Next()
    34  		w.Writef("return")
    35  		w.Indent()
    36  		for _, val := range s.Return {
    37  			w.Next()
    38  			val.HWrite(w)
    39  		}
    40  		w.Dedent()
    41  	}
    42  	w.Dedent()
    43  }
    44  
    45  // ProcessStat uses the given StatProcessor to process the receiver.
    46  func (s BlockStat) ProcessStat(p StatProcessor) {
    47  	p.ProcessBlockStat(s)
    48  }