-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanip.go
91 lines (73 loc) · 1.38 KB
/
manip.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
package boolal
const (
truth = "■"
falsity = "□"
)
func Truth() UnaryOp {
return Var(truth)
}
func Falsity() UnaryOp {
return Var(falsity)
}
func asUnaryOp(term any) UnaryOp {
switch v := term.(type) {
case string:
return UnaryOp{Var: v}
case UnaryOp:
return v
case *Expr:
if v.Rhs == nil {
return v.Lhs
} else {
return UnaryOp{Al: v}
}
default:
panic("invalid value")
}
}
func asUnaryOps(term any, terms []any) []UnaryOp {
vs := make([]UnaryOp, len(terms)+1)
vs[0] = asUnaryOp(term)
for i, t := range terms {
vs[i+1] = asUnaryOp(t)
}
return vs
}
func Var(v string) UnaryOp {
return UnaryOp{Var: v}
}
func Not(term any) UnaryOp {
op := asUnaryOp(term)
if op.Name == "" {
op.Name = "!"
} else {
op.Name = ""
}
return op
}
func And(lhs any, rhs any, terms ...any) *Expr {
return &Expr{
Lhs: asUnaryOp(lhs),
Rhs: []BinaryOp{{And: asUnaryOps(rhs, terms)}},
}
}
func Or(lhs any, rhs any, terms ...any) *Expr {
return &Expr{
Lhs: asUnaryOp(lhs),
Rhs: []BinaryOp{{Or: asUnaryOps(rhs, terms)}},
}
}
func (a *Expr) concat(b BinaryOp) {
if a.Rhs == nil {
a.Rhs = make([]BinaryOp, 0, 1)
}
a.Rhs = append(a.Rhs, b)
}
func (a *Expr) And(term any, terms ...any) *Expr {
a.concat(BinaryOp{And: asUnaryOps(term, terms)})
return a
}
func (a *Expr) Or(term any, terms ...any) *Expr {
a.concat(BinaryOp{Or: asUnaryOps(term, terms)})
return a
}