-
Notifications
You must be signed in to change notification settings - Fork 3
/
oper.go
80 lines (76 loc) · 2.23 KB
/
oper.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
package glitch
type operator struct {
precedence int
assoc int
f func(uint8, uint8) uint8
}
const (
assocRight = iota
assocLeft
)
var operMap = map[rune]*operator{
'+': &operator{precedence: 4, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x + y
}},
'-': &operator{precedence: 4, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x - y
}},
'|': &operator{precedence: 4, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x | y
}},
'^': &operator{precedence: 4, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x ^ y
}},
'*': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x * y
}},
'/': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
if y == 0 {
return x
} else {
return x / y
}
}},
'%': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
if y == 0 {
return x
} else {
return x % y
}
}},
'<': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x << y
}},
'>': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x >> y
}},
'&': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x & y
}},
':': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
return x &^ y
}},
'#': &operator{precedence: 5, assoc: assocLeft, f: func(x, y uint8) uint8 {
var z uint8 = 1
for y > 0 {
z *= x
y--
}
return z
}},
'?': &operator{precedence: 6, assoc: assocRight, f: func(x, y uint8) uint8 {
if x > y {
return 255
} else {
return 0
}
}},
'@': &operator{precedence: 6, assoc: assocRight, f: func(x, y uint8) uint8 {
fuzz := float64(y)/255.0
return uint8(float64(x) * fuzz)
}},
}
func (o1 *operator) hasPrecedence(o2 *operator) bool {
return (o2.assoc == assocRight && o1.precedence > o2.precedence) ||
(o2.assoc == assocLeft && o1.precedence >= o2.precedence)
}