-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegers.go
178 lines (142 loc) · 3.48 KB
/
integers.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"errors"
"math/big"
"math/rand"
"strconv"
"time"
)
type BoiFuncInt struct {
interpreter *BoiInterpreter
}
func (f BoiFuncInt) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
sum := new(big.Int)
sum.SetUint64(0)
for _, arg := range args {
data := arg.data
value, err := strconv.ParseUint(string(data), 10, 64)
if err != nil {
return err
}
tmp := new(big.Int)
tmp.SetUint64(value)
sum.Add(sum, tmp)
}
context.variables["exit"] = BoiVar{sum.Bytes()}
return nil
}
type BoiFuncAdd struct {
interpreter *BoiInterpreter
}
func (f BoiFuncAdd) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
sum := new(big.Int)
sum.SetUint64(0)
for _, arg := range args {
tmp := new(big.Int)
tmp.SetBytes(arg.data)
sum.Add(sum, tmp)
}
context.variables["exit"] = BoiVar{sum.Bytes()}
return nil
}
type BoiFuncSub struct {
interpreter *BoiInterpreter
}
func (f BoiFuncSub) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
sum := new(big.Int)
sum.SetBytes(args[0].data)
for _, arg := range args[1:] {
tmp := new(big.Int)
tmp.SetBytes(arg.data)
sum.Sub(sum, tmp)
}
context.variables["exit"] = BoiVar{sum.Bytes()}
return nil
}
type BoiFuncDiv struct {
interpreter *BoiInterpreter
}
func (f BoiFuncDiv) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
sum := new(big.Int)
sum.SetBytes(args[0].data)
for _, arg := range args[1:] {
tmp := new(big.Int)
tmp.SetBytes(arg.data)
sum.Div(sum, tmp)
}
context.variables["exit"] = BoiVar{sum.Bytes()}
return nil
}
type BoiFuncMul struct {
interpreter *BoiInterpreter
}
func (f BoiFuncMul) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
sum := new(big.Int)
sum = sum.SetUint64(1)
for _, arg := range args {
tmp := new(big.Int)
tmp.SetBytes(arg.data)
sum.Mul(sum, tmp)
}
context.variables["exit"] = BoiVar{sum.Bytes()}
return nil
}
type BoiFuncDec struct {
interpreter *BoiInterpreter
}
func (f BoiFuncDec) Do(args []BoiVar) error {
context := f.interpreter.subContext()
defer f.interpreter.returnContext()
value := new(big.Int)
value = value.SetUint64(0)
if len(args) > 0 {
value = value.SetBytes(args[0].data)
}
output := []byte(value.String())
context.variables["exit"] = BoiVar{output}
return nil
}
func BoiFuncLess(context *BoiContext, args []BoiVar) (BoiVar, error) {
if len(args) < 2 {
return BoiVar{}, errors.New("< requires at least 2 parameters")
}
for i := 0; i < len(args)-1; i++ {
a, b := new(big.Int), new(big.Int)
a = a.SetBytes(args[i].data)
b = b.SetBytes(args[i+1].data)
if a.Cmp(b) >= 0 {
return BoiVar{[]byte("false")}, nil
}
}
return BoiVar{[]byte("true")}, nil
}
func BoiFuncIsEven(context *BoiContext, args []BoiVar) (BoiVar, error) {
if len(args) != 1 {
return BoiVar{}, errors.New("IsEven can only take one value (for now)")
}
// always seed random
rand.Seed(time.Now().UTC().UnixNano())
probabilityOfWrongAnswer := rand.Intn(100)
value := new(big.Int)
value = value.SetBytes(args[0].data)
valueInt := value.Uint64()
even := valueInt%2 == 0
if rand.Intn(100) < probabilityOfWrongAnswer {
even = !even
}
probabilityOfEven := probabilityOfWrongAnswer
if even {
probabilityOfEven = 100 - probabilityOfWrongAnswer
}
return BoiVar{[]byte{byte(probabilityOfEven)}}, nil
}