github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/ast/indexexp.go (about) 1 package ast 2 3 // An IndexExp is an expression node representing indexing, i.e. "Coll[Index]". 4 type IndexExp struct { 5 Location 6 Coll ExpNode 7 Idx ExpNode 8 } 9 10 var _ Var = IndexExp{} 11 12 // NewIndexExp returns an IndexExp instance for the given collection and index. 13 func NewIndexExp(coll ExpNode, idx ExpNode) IndexExp { 14 return IndexExp{ 15 Location: MergeLocations(coll, idx), // TODO: use the "]" for locaion end 16 Coll: coll, 17 Idx: idx, 18 } 19 } 20 21 // ProcessExp uses the given ExpProcessor to process the receiver. 22 func (e IndexExp) ProcessExp(p ExpProcessor) { 23 p.ProcessIndexExp(e) 24 } 25 26 // ProcessVar uses the given VarProcessor to process the receiver. 27 func (e IndexExp) ProcessVar(p VarProcessor) { 28 p.ProcessIndexExpVar(e) 29 } 30 31 // FunctionName returns the function name associated with this expression (i.e. 32 // if it is part of a function definition statement), or an empty string if 33 // there is no sensible name. 34 func (e IndexExp) FunctionName() string { 35 if s, ok := e.Idx.(String); ok { 36 return string(s.Val) 37 } 38 return "" 39 } 40 41 // HWrite prints a tree representation of the node. 42 func (e IndexExp) HWrite(w HWriter) { 43 w.Writef("idx") 44 w.Indent() 45 w.Next() 46 w.Writef("coll: ") 47 e.Coll.HWrite(w) 48 w.Next() 49 w.Writef("at: ") 50 e.Idx.HWrite(w) 51 w.Dedent() 52 }