/
boolean.go
65 lines (54 loc) · 1.09 KB
/
boolean.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
package macros
import (
. "github.com/kocircuit/kocircuit/lang/go/eval"
"github.com/kocircuit/kocircuit/lang/go/runtime"
)
func init() {
RegisterEvalGateAt("", "And", new(EvalAnd))
RegisterEvalGateAt("", "Or", new(EvalOr))
RegisterEvalGateAt("", "Xor", new(EvalXor))
RegisterEvalGateAt("", "Not", new(EvalNot))
}
type EvalAnd struct {
Series []bool `ko:"name=series,monadic"`
}
func (and *EvalAnd) Play(ctx *runtime.Context) bool {
r := true
for _, x := range and.Series {
r = r && x
}
return r
}
type EvalOr struct {
Series []bool `ko:"name=series,monadic"`
}
func (or *EvalOr) Play(ctx *runtime.Context) bool {
r := false
for _, x := range or.Series {
r = r || x
}
return r
}
type EvalXor struct {
Series []bool `ko:"name=series,monadic"`
}
func (xor *EvalXor) Play(ctx *runtime.Context) bool {
var q uint
for _, x := range xor.Series {
q ^= boolUint(x)
}
return q != 0
}
func boolUint(b bool) uint {
if b {
return 1
} else {
return 0
}
}
type EvalNot struct {
Bool bool `ko:"name=bool,monadic"`
}
func (not *EvalNot) Play(ctx *runtime.Context) bool {
return !not.Bool
}