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

     1  package ast
     2  
     3  import (
     4  	"github.com/arnodel/golua/token"
     5  )
     6  
     7  //
     8  // TabelConstructor
     9  //
    10  
    11  // A TableConstructor is an expression node representing a table literal, e.g.
    12  //
    13  //    { "hello", 4.5, x = 2, [z] = true }
    14  type TableConstructor struct {
    15  	Location
    16  	Fields []TableField
    17  }
    18  
    19  var _ ExpNode = TableConstructor{}
    20  
    21  // NewTableConstructor returns a TableConstructor instance with the given fields.
    22  func NewTableConstructor(opTok, clTok *token.Token, fields []TableField) TableConstructor {
    23  	return TableConstructor{
    24  		Location: LocFromTokens(opTok, clTok),
    25  		Fields:   fields,
    26  	}
    27  }
    28  
    29  // HWrite prints a tree representation of the node.
    30  func (c TableConstructor) HWrite(w HWriter) {
    31  	w.Writef("table")
    32  	w.Indent()
    33  	for _, f := range c.Fields {
    34  		w.Next()
    35  		w.Writef("key: ")
    36  		f.Key.HWrite(w)
    37  		w.Next()
    38  		w.Writef("value: ")
    39  		f.Value.HWrite(w)
    40  	}
    41  	w.Dedent()
    42  }
    43  
    44  // ProcessExp uses the given ExpProcessor to process the receiver.
    45  func (c TableConstructor) ProcessExp(p ExpProcessor) {
    46  	p.ProcessTableConstructorExp(c)
    47  }
    48  
    49  //
    50  // TableField
    51  //
    52  
    53  // A TableField is a (key, value ) pair of expression nodes representing a field
    54  // in a table literal.  There is a special key type called "NoTableKey" for
    55  // fields that do not have a key.
    56  type TableField struct {
    57  	Location
    58  	Key   ExpNode
    59  	Value ExpNode
    60  }
    61  
    62  func NewTableField(key ExpNode, value ExpNode) TableField {
    63  	// If value is a function try to give it a name
    64  	f, ok := value.(Function)
    65  	if ok && f.Name == "" {
    66  		name, ok := key.(String)
    67  		if ok {
    68  			f.Name = string(name.Val)
    69  			value = f
    70  		}
    71  	}
    72  	return TableField{
    73  		Location: MergeLocations(key, value),
    74  		Key:      key,
    75  		Value:    value,
    76  	}
    77  }
    78  
    79  //
    80  // NoTableKey
    81  //
    82  
    83  // NoTableKey is a special expression node that can only be used as the Key of a
    84  // TableField, meaning a field with no key.
    85  type NoTableKey struct {
    86  	Location
    87  }
    88  
    89  // HWrite prints a tree representation of the node.
    90  func (k NoTableKey) HWrite(w HWriter) {
    91  	w.Writef("<no key>")
    92  }
    93  
    94  // ProcessExp uses the given ExpProcessor to process the receiver.  It panics as
    95  // this type is a placeholder that should not be processed as an expression.
    96  func (k NoTableKey) ProcessExp(p ExpProcessor) {
    97  	panic("nothing to process?")
    98  }