-
Notifications
You must be signed in to change notification settings - Fork 15
/
types_util.go
132 lines (127 loc) · 2.39 KB
/
types_util.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
package fmr
import (
"fmt"
"math/big"
)
func metaEqual(m1, m2 interface{}) bool {
if m1 == nil && m2 == nil {
return true
}
if m1 != nil && m2 != nil {
if Debug {
fmt.Println("In Equal:", m1, m2)
}
switch m1.(type) {
// meta for (any)
case map[string]int:
t1 := m1.(map[string]int)
t2, ok2 := m2.(map[string]int)
if ok2 && len(t1) == len(t2) {
for k, v := range t1 {
if Debug {
fmt.Println(k, v)
}
if w, ok := t2[k]; !ok || v != w {
if Debug {
fmt.Println(v, w, ok)
}
return false
}
}
return true
}
// meta for terminal text
case string:
s1 := m1.(string)
s2, ok := m2.(string)
if ok && s1 == s2 {
return true
}
}
}
return false
}
// Equal func for Term
func (t *Term) Equal(t1 *Term) bool {
if t == nil && t1 == nil {
return true
}
if t == nil || t1 == nil {
return false
}
if t.Value != t1.Value || t.Type != t1.Type {
return false
}
return metaEqual(t.Meta, t1.Meta)
}
// Equal func for RuleBody
func (r *RuleBody) Equal(rb *RuleBody) bool {
if rb == nil && r == nil {
return true
}
if rb == nil || r == nil {
return false
}
if len(rb.Terms) != len(r.Terms) {
return false
}
for i, term := range rb.Terms {
if !term.Equal(r.Terms[i]) {
return false
}
}
return r.F.Equal(rb.F)
}
// Equal func for FMR
func (f *FMR) Equal(fmr *FMR) bool {
if f == nil && fmr == nil {
return true
}
if !(f != nil && fmr != nil) {
return false
}
if f.Fn != fmr.Fn {
return false
}
if len(f.Args) != len(fmr.Args) {
return false
}
for i, arg := range fmr.Args {
if arg.Type != f.Args[i].Type {
return false
}
switch arg.Type {
case "string":
s1, ok1 := arg.Value.(string)
s2, ok2 := f.Args[i].Value.(string)
if !ok1 || !ok2 || s1 != s2 {
return false
}
case "int":
s1, ok1 := arg.Value.(*big.Int)
s2, ok2 := f.Args[i].Value.(*big.Int)
if !ok1 || !ok2 || s1.Cmp(s2) != 0 {
return false
}
case "float":
s1, ok1 := arg.Value.(*big.Float)
s2, ok2 := f.Args[i].Value.(*big.Float)
if !ok1 || !ok2 || s1.Cmp(s2) != 0 {
return false
}
case "index":
s1, ok1 := arg.Value.(int)
s2, ok2 := f.Args[i].Value.(int)
if !ok1 || !ok2 || s1 != s2 {
return false
}
case "func":
s1, ok1 := arg.Value.(*FMR)
s2, ok2 := f.Args[i].Value.(*FMR)
if !ok1 || !ok2 || !s1.Equal(s2) {
return false
}
}
}
return true
}