cuelang.org/go@v0.10.1/internal/core/adt/op.go (about)

     1  // Copyright 2018 The CUE Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package adt
    16  
    17  import "cuelang.org/go/cue/token"
    18  
    19  // Op indicates the operation at the top of an expression tree of the expression
    20  // use to evaluate a value.
    21  type Op int
    22  
    23  //go:generate go run golang.org/x/tools/cmd/stringer -type=Op -linecomment
    24  
    25  // Values of Op.
    26  const (
    27  	NoOp Op = iota
    28  
    29  	AndOp // &
    30  	OrOp  // |
    31  
    32  	SelectorOp // .
    33  	IndexOp    // []
    34  	SliceOp    // [:]
    35  	CallOp     // ()
    36  
    37  	BoolAndOp // &&
    38  	BoolOrOp  // ||
    39  
    40  	EqualOp        // ==
    41  	NotOp          // !
    42  	NotEqualOp     // !=
    43  	LessThanOp     // <
    44  	LessEqualOp    // <=
    45  	GreaterThanOp  // >
    46  	GreaterEqualOp // >=
    47  
    48  	MatchOp    // =~
    49  	NotMatchOp // !~
    50  
    51  	AddOp           // +
    52  	SubtractOp      // -
    53  	MultiplyOp      // *
    54  	FloatQuotientOp // /
    55  	IntQuotientOp   // quo
    56  	IntRemainderOp  // rem
    57  	IntDivideOp     // div
    58  	IntModuloOp     // mod
    59  
    60  	InterpolationOp // \()
    61  )
    62  
    63  // OpFromToken converts a token.Token to an Op.
    64  func OpFromToken(t token.Token) Op {
    65  	return tokenMap[t]
    66  }
    67  
    68  // Token returns the token.Token corresponding to the Op.
    69  func (op Op) Token() token.Token {
    70  	return opMap[op]
    71  }
    72  
    73  var tokenMap = map[token.Token]Op{
    74  	token.OR:  OrOp,  // |
    75  	token.AND: AndOp, // &
    76  
    77  	token.ADD: AddOp,           // +
    78  	token.SUB: SubtractOp,      // -
    79  	token.MUL: MultiplyOp,      // *
    80  	token.QUO: FloatQuotientOp, // /
    81  
    82  	token.IDIV: IntDivideOp,    // div
    83  	token.IMOD: IntModuloOp,    // mod
    84  	token.IQUO: IntQuotientOp,  // quo
    85  	token.IREM: IntRemainderOp, // rem
    86  
    87  	token.LAND: BoolAndOp, // &&
    88  	token.LOR:  BoolOrOp,  // ||
    89  
    90  	token.EQL: EqualOp,       // ==
    91  	token.LSS: LessThanOp,    // <
    92  	token.GTR: GreaterThanOp, // >
    93  	token.NOT: NotOp,         // !
    94  
    95  	token.NEQ:  NotEqualOp,     // !=
    96  	token.LEQ:  LessEqualOp,    // <=
    97  	token.GEQ:  GreaterEqualOp, // >=
    98  	token.MAT:  MatchOp,        // =~
    99  	token.NMAT: NotMatchOp,     // !~
   100  }
   101  
   102  var opMap = map[Op]token.Token{}
   103  
   104  func init() {
   105  	for t, o := range tokenMap {
   106  		opMap[o] = t
   107  	}
   108  }