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

     1  package ast
     2  
     3  import (
     4  	"github.com/arnodel/golua/ops"
     5  	"github.com/arnodel/golua/token"
     6  )
     7  
     8  // An UnOp is an expression node representing the application of a unary
     9  // operation on an expression.
    10  type UnOp struct {
    11  	Location
    12  	Op      ops.Op
    13  	Operand ExpNode
    14  }
    15  
    16  var _ ExpNode = UnOp{}
    17  
    18  // NewUnOp returns a UnOp instance from the given operator an expression.
    19  func NewUnOp(opTok *token.Token, op ops.Op, exp ExpNode) *UnOp {
    20  	return &UnOp{
    21  		Location: MergeLocations(LocFromToken(opTok), exp),
    22  		Op:       op,
    23  		Operand:  exp,
    24  	}
    25  }
    26  
    27  // ProcessExp uses the given ExpProcessor to process the receiver.
    28  func (u UnOp) ProcessExp(p ExpProcessor) {
    29  	p.ProcessUnOpExp(u)
    30  }
    31  
    32  // HWrite prints a tree representation of the node.
    33  func (u UnOp) HWrite(w HWriter) {
    34  	w.Writef("unop: %s", u.Op)
    35  	w.Indent()
    36  	w.Next()
    37  	u.Operand.HWrite(w)
    38  	w.Dedent()
    39  }
    40  
    41  func (u UnOp) isNumber() bool {
    42  	switch u.Op {
    43  	case ops.OpNeg, ops.OpId:
    44  		return IsNumber(u.Operand)
    45  	}
    46  	return false
    47  }