...

Source file src/cuelang.org/go/cue/op.go

Documentation: cuelang.org/go/cue

     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 cue
    16  
    17  import (
    18  	"cuelang.org/go/cue/token"
    19  	"cuelang.org/go/internal/core/adt"
    20  )
    21  
    22  // Op indicates the operation at the top of an expression tree of the expression
    23  // use to evaluate a value.
    24  type Op = adt.Op
    25  
    26  // Values of Op.
    27  const (
    28  	NoOp Op = adt.NoOp
    29  
    30  	AndOp Op = adt.AndOp
    31  	OrOp  Op = adt.OrOp
    32  
    33  	SelectorOp Op = adt.SelectorOp
    34  	IndexOp    Op = adt.IndexOp
    35  	SliceOp    Op = adt.SliceOp
    36  	CallOp     Op = adt.CallOp
    37  
    38  	BooleanAndOp Op = adt.BoolAndOp
    39  	BooleanOrOp  Op = adt.BoolOrOp
    40  
    41  	EqualOp            Op = adt.EqualOp
    42  	NotOp              Op = adt.NotOp
    43  	NotEqualOp         Op = adt.NotEqualOp
    44  	LessThanOp         Op = adt.LessThanOp
    45  	LessThanEqualOp    Op = adt.LessEqualOp
    46  	GreaterThanOp      Op = adt.GreaterThanOp
    47  	GreaterThanEqualOp Op = adt.GreaterEqualOp
    48  
    49  	RegexMatchOp    Op = adt.MatchOp
    50  	NotRegexMatchOp Op = adt.NotMatchOp
    51  
    52  	AddOp           Op = adt.AddOp
    53  	SubtractOp      Op = adt.SubtractOp
    54  	MultiplyOp      Op = adt.MultiplyOp
    55  	FloatQuotientOp Op = adt.FloatQuotientOp
    56  	IntQuotientOp   Op = adt.IntQuotientOp
    57  	IntRemainderOp  Op = adt.IntRemainderOp
    58  	IntDivideOp     Op = adt.IntDivideOp
    59  	IntModuloOp     Op = adt.IntModuloOp
    60  
    61  	InterpolationOp Op = adt.InterpolationOp
    62  )
    63  
    64  // isCmp reports whether an op is a comparator.
    65  func (op op) isCmp() bool {
    66  	return opEql <= op && op <= opGeq
    67  }
    68  
    69  func (op op) unifyType() (unchecked, ok bool) {
    70  	if op == opUnifyUnchecked {
    71  		return true, true
    72  	}
    73  	return false, op == opUnify
    74  }
    75  
    76  type op uint16
    77  
    78  const (
    79  	opUnknown op = iota
    80  
    81  	opUnify
    82  	opUnifyUnchecked
    83  	opDisjunction
    84  
    85  	opLand
    86  	opLor
    87  	opNot
    88  
    89  	opEql
    90  	opNeq
    91  	opMat
    92  	opNMat
    93  
    94  	opLss
    95  	opGtr
    96  	opLeq
    97  	opGeq
    98  
    99  	opAdd
   100  	opSub
   101  	opMul
   102  	opQuo
   103  	opRem
   104  
   105  	opIDiv
   106  	opIMod
   107  	opIQuo
   108  	opIRem
   109  )
   110  
   111  var opStrings = []string{
   112  	opUnknown: "??",
   113  
   114  	opUnify: "&",
   115  	// opUnifyUnchecked is internal only. Syntactically this is
   116  	// represented as embedding.
   117  	opUnifyUnchecked: "&!",
   118  	opDisjunction:    "|",
   119  
   120  	opLand: "&&",
   121  	opLor:  "||",
   122  	opNot:  "!",
   123  
   124  	opEql:  "==",
   125  	opNeq:  "!=",
   126  	opMat:  "=~",
   127  	opNMat: "!~",
   128  
   129  	opLss: "<",
   130  	opGtr: ">",
   131  	opLeq: "<=",
   132  	opGeq: ">=",
   133  
   134  	opAdd: "+",
   135  	opSub: "-",
   136  	opMul: "*",
   137  	opQuo: "/",
   138  
   139  	opIDiv: "div",
   140  	opIMod: "mod",
   141  	opIQuo: "quo",
   142  	opIRem: "rem",
   143  }
   144  
   145  func (op op) String() string { return opStrings[op] }
   146  
   147  var tokenMap = map[token.Token]op{
   148  	token.OR:  opDisjunction, // |
   149  	token.AND: opUnify,       // &
   150  
   151  	token.ADD: opAdd, // +
   152  	token.SUB: opSub, // -
   153  	token.MUL: opMul, // *
   154  	token.QUO: opQuo, // /
   155  
   156  	token.IDIV: opIDiv, // div
   157  	token.IMOD: opIMod, // mod
   158  	token.IQUO: opIQuo, // quo
   159  	token.IREM: opIRem, // rem
   160  
   161  	token.LAND: opLand, // &&
   162  	token.LOR:  opLor,  // ||
   163  
   164  	token.EQL: opEql, // ==
   165  	token.LSS: opLss, // <
   166  	token.GTR: opGtr, // >
   167  	token.NOT: opNot, // !
   168  
   169  	token.NEQ:  opNeq,  // !=
   170  	token.LEQ:  opLeq,  // <=
   171  	token.GEQ:  opGeq,  // >=
   172  	token.MAT:  opMat,  // =~
   173  	token.NMAT: opNMat, // !~
   174  }
   175  
   176  var opMap = map[op]token.Token{}
   177  
   178  func init() {
   179  	for t, o := range tokenMap {
   180  		opMap[o] = t
   181  	}
   182  }
   183  

View as plain text