github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/ast/forstat.go (about) 1 package ast 2 3 import ( 4 "github.com/arnodel/golua/token" 5 ) 6 7 // ForStat represents the statement node "for Var = Start, Stop, Step do Body end". 8 type ForStat struct { 9 Location 10 Var Name 11 Start ExpNode 12 Stop ExpNode 13 Step ExpNode 14 Body BlockStat 15 } 16 17 var _ Stat = ForStat{} 18 19 // NewForStat returns a ForStat from the given parts. 20 func NewForStat(startTok, endTok *token.Token, itervar Name, params []ExpNode, body BlockStat) *ForStat { 21 return &ForStat{ 22 Location: LocFromTokens(startTok, endTok), 23 Var: itervar, 24 Start: params[0], 25 Stop: params[1], 26 Step: params[2], 27 Body: body, 28 } 29 } 30 31 // HWrite prints a tree representation of the node. 32 func (s ForStat) HWrite(w HWriter) { 33 w.Writef("for %s", s.Var) 34 w.Indent() 35 if s.Start != nil { 36 w.Next() 37 w.Writef("Start: ") 38 s.Start.HWrite(w) 39 } 40 if s.Stop != nil { 41 w.Next() 42 w.Writef("Stop: ") 43 s.Stop.HWrite(w) 44 } 45 if s.Step != nil { 46 w.Next() 47 w.Writef("Step: ") 48 s.Step.HWrite(w) 49 } 50 w.Next() 51 s.Body.HWrite(w) 52 w.Dedent() 53 } 54 55 // ProcessStat uses the given StatProcessor to process the receiver. 56 func (s ForStat) ProcessStat(p StatProcessor) { 57 p.ProcessForStat(s) 58 }