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

     1  package ops
     2  
     3  // Op represents a Lua binary or unary operator.  It also encode its precedence.
     4  type Op uint
     5  
     6  //go:generate stringer -type=Op
     7  
     8  // OpOr is a logical or (precedence 0)
     9  const OpOr Op = 0 + iota<<8
    10  
    11  // OpAnd is a logical and (precedence 1)
    12  const OpAnd Op = 1 + iota<<8
    13  
    14  // Precedence 2 binary operators
    15  const (
    16  	OpLt Op = 2 + iota<<8
    17  	OpLeq
    18  	OpGt
    19  	OpGeq
    20  	OpEq
    21  	OpNeq
    22  )
    23  
    24  // OpBitOr is bitwise or (precedence 3)
    25  const OpBitOr Op = 3 + iota<<8
    26  
    27  // OpBitXor is bitwise exclusive or (precedence 4)
    28  const OpBitXor Op = 4 + iota<<8
    29  
    30  // OpBitAnd is bitwise and (precedence 5)
    31  const OpBitAnd Op = 5 + iota<<8
    32  
    33  // Precedence 6 binary operators (bitwise shifts)
    34  const (
    35  	OpShiftL Op = 6 + iota<<8
    36  	OpShiftR
    37  )
    38  
    39  // OpConcat is the concatenate operator (precedence 7)
    40  const OpConcat Op = 7 + iota<<8
    41  
    42  // Precendence 8 binary operators (add / subtract)
    43  const (
    44  	OpAdd Op = 8 + iota<<8
    45  	OpSub
    46  )
    47  
    48  // Precedence 9 binary operators (multiplication / division / modulo)
    49  const (
    50  	OpMul Op = 9 + iota<<8
    51  	OpDiv
    52  	OpFloorDiv
    53  	OpMod
    54  )
    55  
    56  // Unary operators have precedence 10
    57  const (
    58  	OpNeg Op = 10 + iota<<8
    59  	OpNot
    60  	OpLen
    61  	OpBitNot
    62  	OpId
    63  )
    64  
    65  // OpPow (power) is special, precendence 10.
    66  const OpPow Op = 11 + iota<<8
    67  
    68  // Precedence returns the precedence of an operator (higher means binds more
    69  // tightly).
    70  func (op Op) Precedence() int {
    71  	return int(op & 0xff)
    72  }
    73  
    74  // Type returns the type of operator (which coincides with the precedence atm).
    75  func (op Op) Type() Op {
    76  	return op & 0xff
    77  }