forked from tomnomnom/gf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import "strings" | ||
|
||
type evaluator interface { | ||
Eval(content string) bool | ||
} | ||
type sexp struct { | ||
op string | ||
args []evaluator | ||
} | ||
|
||
func (s sexp) Eval(content string) bool { | ||
switch s.op { | ||
case "not": | ||
if len(s.args) == 0 { | ||
return true | ||
} | ||
return !s.args[0].Eval(content) | ||
|
||
case "and": | ||
for _, a := range s.args { | ||
if !a.Eval(content) { | ||
return false | ||
} | ||
} | ||
return true | ||
|
||
case "or": | ||
for _, a := range s.args { | ||
if a.Eval(content) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
return false | ||
} | ||
|
||
type matcher string | ||
|
||
func (m matcher) Eval(content string) bool { | ||
return strings.Contains(content, string(m)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import "testing" | ||
|
||
func TestSexp(t *testing.T) { | ||
|
||
sexp := sexp{ | ||
op: "and", | ||
args: []evaluator{ | ||
sexp{op: "or", args: []evaluator{matcher("too"), matcher("boo")}}, | ||
sexp{op: "not", args: []evaluator{matcher("crumble")}}, | ||
}, | ||
} | ||
|
||
if sexp.Eval("footle boolte") != true { | ||
t.Errorf("expected true, have false") | ||
} | ||
|
||
if sexp.Eval("boo crumble") != false { | ||
t.Errorf("expected false, have true") | ||
} | ||
} | ||
|
||
func TestSexpNot(t *testing.T) { | ||
|
||
sexp := sexp{ | ||
op: "not", | ||
args: []evaluator{matcher("footle")}, | ||
} | ||
|
||
if sexp.Eval("boolte tootle") != true { | ||
t.Errorf("expected true, have false") | ||
} | ||
|
||
if sexp.Eval("footle mcdootle") != false { | ||
t.Errorf("expected false, have true") | ||
} | ||
} |