-
Notifications
You must be signed in to change notification settings - Fork 0
/
oplogic.go
105 lines (94 loc) · 1.41 KB
/
oplogic.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package bcl
func binopNumeric(op opcode, a, b value) value {
switch va := a.(type) {
case int:
switch vb := b.(type) {
case int:
switch op {
case opEQ:
return va == vb
case opLT:
return va < vb
case opGT:
return va > vb
case opADD:
return va + vb
case opSUB:
return va - vb
case opMUL:
return va * vb
case opDIV:
return va / vb
}
case float64:
ca := float64(va)
switch op {
case opEQ:
return ca == vb
case opLT:
return ca < vb
case opGT:
return ca > vb
case opADD:
return ca + vb
case opSUB:
return ca - vb
case opMUL:
return ca * vb
case opDIV:
return ca / vb
}
}
case float64:
var cb float64
switch vb := b.(type) {
case float64:
cb = vb
case int:
cb = float64(vb)
}
switch op {
case opEQ:
return va == cb
case opLT:
return va < cb
case opGT:
return va > cb
case opADD:
return va + cb
case opSUB:
return va - cb
case opMUL:
return va * cb
case opDIV:
return va / cb
}
}
return nil
}
func unopNumeric(op opcode, a value) value {
switch va := a.(type) {
case int:
switch op {
case opNEG:
return -va
}
case float64:
switch op {
case opNEG:
return -va
}
}
return nil
}
func binopString(op opcode, a, b string) value {
switch op {
case opLT:
return a < b
case opGT:
return a > b
case opADD:
return a + b
}
return nil
}