blob: 6383290ab6ba87eae392c34b1265eb08c40f3cd0 [file] [log] [blame]
Marcel van Lohuizen6a088302020-07-03 14:42:05 +02001// 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 adt
16
17import "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.
21type Op int
22
23func (o Op) String() string {
24 return opToString[o]
25}
26
27// Values of Op.
28const (
29 NoOp Op = iota
30
31 AndOp
32 OrOp
33
34 SelectorOp
35 IndexOp
36 SliceOp
37 CallOp
38
39 BoolAndOp
40 BoolOrOp
41
42 EqualOp
43 NotOp
44 NotEqualOp
45 LessThanOp
46 LessEqualOp
47 GreaterThanOp
48 GreaterEqualOp
49
50 MatchOp
51 NotMatchOp
52
53 AddOp
54 SubtractOp
55 MultiplyOp
56 FloatQuotientOp
57 IntQuotientOp
58 IntRemainderOp
59 IntDivideOp
60 IntModuloOp
61
62 InterpolationOp
63)
64
65var opToString = map[Op]string{
66 AndOp: "&",
67 OrOp: "|",
68 BoolAndOp: "&&",
69 BoolOrOp: "||",
70 EqualOp: "==",
71 NotOp: "!",
72 NotEqualOp: "!=",
73 LessThanOp: "<",
74 LessEqualOp: "<=",
75 GreaterThanOp: ">",
76 GreaterEqualOp: ">=",
77 MatchOp: "=~",
78 NotMatchOp: "!~",
79 AddOp: "+",
80 SubtractOp: "-",
81 MultiplyOp: "*",
82 FloatQuotientOp: "/",
83 IntQuotientOp: "quo",
84 IntRemainderOp: "rem",
85 IntDivideOp: "div",
86 IntModuloOp: "mod",
87
88 SelectorOp: ".",
89 IndexOp: "[]",
90 SliceOp: "[:]",
91 CallOp: "()",
92
93 InterpolationOp: `\()`,
94}
95
96// OpFromToken converts a token.Token to an Op.
97func OpFromToken(t token.Token) Op {
98 return tokenMap[t]
99}
100
101// Token returns the token.Token corresponding to the Op.
102func (op Op) Token() token.Token {
103 return opMap[op]
104}
105
106var tokenMap = map[token.Token]Op{
107 token.OR: OrOp, // |
108 token.AND: AndOp, // &
109
110 token.ADD: AddOp, // +
111 token.SUB: SubtractOp, // -
112 token.MUL: MultiplyOp, // *
113 token.QUO: FloatQuotientOp, // /
114
115 token.IDIV: IntDivideOp, // div
116 token.IMOD: IntModuloOp, // mod
117 token.IQUO: IntQuotientOp, // quo
118 token.IREM: IntRemainderOp, // rem
119
120 token.LAND: BoolAndOp, // &&
121 token.LOR: BoolOrOp, // ||
122
123 token.EQL: EqualOp, // ==
124 token.LSS: LessThanOp, // <
125 token.GTR: GreaterThanOp, // >
126 token.NOT: NotOp, // !
127
128 token.NEQ: NotEqualOp, // !=
129 token.LEQ: LessEqualOp, // <=
130 token.GEQ: GreaterEqualOp, // >=
131 token.MAT: MatchOp, // =~
132 token.NMAT: NotMatchOp, // !~
133}
134
135var opMap = map[Op]token.Token{}
136
137func init() {
138 for t, o := range tokenMap {
139 opMap[o] = t
140 }
141}