blob: 214e3c811a71ca7416708931fd4688d8babf2286 [file] [log] [blame]
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +01001// 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
15package cue
16
17import "cuelang.org/go/cue/token"
18
Marcel van Lohuizen0519f252019-05-06 18:06:48 +020019// Op indicates the operation at the top of an expression tree of the expression
20// use to evaluate a value.
21type Op int
22
23// Values of Op.
24const (
25 NoOp Op = iota
26
27 AndOp
28 OrOp
29
30 SelectorOp
31 IndexOp
32 SliceOp
33 CallOp
34
35 BooleanAndOp
36 BooleanOrOp
37
38 EqualOp
39 NotOp
40 NotEqualOp
41 LessThanOp
42 LessThanEqualOp
43 GreaterThanOp
44 GreaterThanEqualOp
45
46 RegexMatchOp
47 NotRegexMatchOp
48
49 AddOp
50 SubtractOp
51 MultiplyOp
52 FloatQuotientOp
53 FloatRemainOp
54 IntQuotientOp
55 IntRemainderOp
56 IntDivideOp
57 IntModuloOp
58
59 InterpolationOp
60)
61
62var opToOp = map[op]Op{
63 opUnify: AndOp,
64 opDisjunction: OrOp,
65 opLand: BooleanAndOp,
66 opLor: BooleanOrOp,
67 opEql: EqualOp,
68 opNot: NotOp,
69 opNeq: NotEqualOp,
70 opLss: LessThanOp,
71 opLeq: LessThanEqualOp,
72 opGtr: GreaterThanOp,
73 opGeq: GreaterThanEqualOp,
74 opMat: RegexMatchOp,
75 opNMat: NotRegexMatchOp,
76 opAdd: AddOp,
77 opSub: SubtractOp,
78 opMul: MultiplyOp,
79 opQuo: FloatQuotientOp,
80 opRem: FloatRemainOp,
81 opIQuo: IntQuotientOp,
82 opIRem: IntRemainderOp,
83 opIDiv: IntDivideOp,
84 opIMod: IntModuloOp,
85}
86
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +010087func opIn(op op, anyOf ...op) bool {
88 for _, o := range anyOf {
89 if o == op {
90 return true
91 }
92 }
93 return false
94}
95
96// isCmp reports whether an op is a comparator.
97func (op op) isCmp() bool {
98 return opEql <= op && op <= opGeq
99}
100
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100101type op uint16
102
103const (
104 opUnknown op = iota
105
106 opUnify
107 opDisjunction
108
109 opLand
110 opLor
111 opNot
112
113 opEql
114 opNeq
Marcel van Lohuizen706e69c2019-02-11 18:21:14 +0100115 opMat
116 opNMat
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100117
118 opLss
119 opGtr
120 opLeq
121 opGeq
122
123 opAdd
124 opSub
125 opMul
126 opQuo
127 opRem
128
129 opIDiv
130 opIMod
131 opIQuo
132 opIRem
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100133)
134
135var opStrings = []string{
136 opUnknown: "??",
137
138 opUnify: "&",
139 opDisjunction: "|",
140
141 opLand: "&&",
142 opLor: "||",
143 opNot: "!",
144
Marcel van Lohuizen706e69c2019-02-11 18:21:14 +0100145 opEql: "==",
146 opNeq: "!=",
147 opMat: "=~",
148 opNMat: "!~",
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100149
150 opLss: "<",
151 opGtr: ">",
152 opLeq: "<=",
153 opGeq: ">=",
154
155 opAdd: "+",
156 opSub: "-",
157 opMul: "*",
158 opQuo: "/",
159 opRem: "%",
160
161 opIDiv: "div",
162 opIMod: "mod",
163 opIQuo: "quo",
164 opIRem: "rem",
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100165}
166
167func (op op) String() string { return opStrings[op] }
168
169var tokenMap = map[token.Token]op{
Marcel van Lohuizenbedcf0c2019-02-22 18:00:00 +0100170 token.OR: opDisjunction, // |
171 token.AND: opUnify, // &
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100172
173 token.ADD: opAdd, // +
174 token.SUB: opSub, // -
175 token.MUL: opMul, // *
176 token.QUO: opQuo, // /
177 token.REM: opRem, // %
178
179 token.IDIV: opIDiv, // div
180 token.IMOD: opIMod, // mod
181 token.IQUO: opIQuo, // quo
182 token.IREM: opIRem, // rem
183
184 token.LAND: opLand, // &&
185 token.LOR: opLor, // ||
186
187 token.EQL: opEql, // ==
188 token.LSS: opLss, // <
189 token.GTR: opGtr, // >
190 token.NOT: opNot, // !
191
Marcel van Lohuizen706e69c2019-02-11 18:21:14 +0100192 token.NEQ: opNeq, // !=
193 token.LEQ: opLeq, // <=
194 token.GEQ: opGeq, // >=
195 token.MAT: opMat, // =~
196 token.NMAT: opNMat, // !~
Marcel van Lohuizen17157ea2018-12-11 10:41:10 +0100197}
198
199var opMap = map[op]token.Token{}
200
201func init() {
202 for t, o := range tokenMap {
203 opMap[o] = t
204 }
205}