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

     1  package ast
     2  
     3  // AssignStat represents an assignment var1, ..., varN = src1, ..., srcM.
     4  //
     5  // M and N do not have to be the same
     6  type AssignStat struct {
     7  	Location
     8  	Dest []Var
     9  	Src  []ExpNode
    10  }
    11  
    12  // NewAssignStat makes a new AssignStat.
    13  func NewAssignStat(dst []Var, src []ExpNode) AssignStat {
    14  
    15  	// Give a name to functions defined here if possible
    16  	for i, v := range src {
    17  		if i >= len(dst) {
    18  			break
    19  		}
    20  		f, ok := v.(Function)
    21  		if ok && f.Name == "" {
    22  			fName := dst[i].FunctionName()
    23  			if fName != "" {
    24  				f.Name = fName
    25  				src[i] = f
    26  			}
    27  		}
    28  	}
    29  	return AssignStat{
    30  		Location: MergeLocations(dst[0], src[len(src)-1]),
    31  		Dest:     dst,
    32  		Src:      src,
    33  	}
    34  }
    35  
    36  // HWrite prints the AST in tree form.
    37  func (s AssignStat) HWrite(w HWriter) {
    38  	w.Writef("assign")
    39  	w.Indent()
    40  	for i, v := range s.Dest {
    41  		w.Next()
    42  		w.Writef("dst_%d: ", i)
    43  		v.HWrite(w)
    44  	}
    45  	for i, n := range s.Src {
    46  		w.Next()
    47  		w.Writef("src_%d: ", i)
    48  		n.HWrite(w)
    49  	}
    50  	w.Dedent()
    51  }
    52  
    53  // ProcessStat uses the given StatProcessor to process the receiver.
    54  func (s AssignStat) ProcessStat(p StatProcessor) {
    55  	p.ProcessAssignStat(s)
    56  }