-
Notifications
You must be signed in to change notification settings - Fork 0
/
and.go
42 lines (32 loc) · 784 Bytes
/
and.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
package main
import (
"fmt"
)
var _ Match = (*AndMatch)(nil)
type AndMatch struct {
SubMatch []Match
}
func (am *AndMatch) AssembleMatch(counter *IDCounter, ruleEndLabel, actionLabel string) ([]string, error) {
andAsm := []string{
"# And",
}
for i, match := range am.SubMatch {
matchAsm, err := match.AssembleMatch(counter, ruleEndLabel, actionLabel)
if err != nil {
return nil, fmt.Errorf("sub-match %d: %w", i, err)
}
andAsm = append(andAsm, matchAsm...)
}
andAsm = append(andAsm, "# End and")
return andAsm, nil
}
func (am *AndMatch) Invert() Match {
// De Morgan 2: ¬(P ∧ Q) ⇔ (¬P ∨ ¬Q)
or := &OrMatch{
SubMatch: make([]Match, len(am.SubMatch)),
}
for i, match := range am.SubMatch {
or.SubMatch[i] = match.Invert()
}
return or
}