-
Notifications
You must be signed in to change notification settings - Fork 0
/
operation.go
89 lines (72 loc) · 1.12 KB
/
operation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package prim
import "unicode/utf8"
type Operation int
const (
// arith
ADD Operation = iota
SUB
MUL
DIV
MOD
// logic and shift/rotate
AND
OR
XOR
LSL
LSR
// compare
EQ
NE
LT
GT
LE
GE
// logic
BAND
BOR
BXOR
// unary
INV
NEG
)
var operationNames = [...]string{
ADD: "+",
SUB: "-",
MUL: "*",
DIV: "/",
MOD: "%",
AND: "&",
OR: "|",
XOR: "^",
LSL: "<<",
LSR: ">>",
EQ: "==",
NE: "!=",
LT: "<",
GT: ">",
LE: "<=",
GE: ">=",
BAND: "&&",
BOR: "||",
BXOR: "~",
INV: "~",
NEG: "!",
}
func (o Operation) String() string {
return operationNames[int(o)]
}
var runeToOperationMap map[rune]Operation
var stringToOperationMap map[string]Operation
func RuneToOperation(r rune) Operation { return runeToOperationMap[r] }
func StringToOperation(s string) Operation { return stringToOperationMap[s] }
func init() {
runeToOperationMap = make(map[rune]Operation)
stringToOperationMap = make(map[string]Operation)
for i, s := range operationNames {
stringToOperationMap[s] = Operation(i)
if len(s) == 1 {
r, _ := utf8.DecodeRuneInString(s)
runeToOperationMap[r] = Operation(i)
}
}
}