Skip to content

Commit

Permalink
Add std.xor for 2 booleans
Browse files Browse the repository at this point in the history
  • Loading branch information
salonijuneja21 authored and sparkprime committed Apr 13, 2023
1 parent 772ebba commit 91ebf4b
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,18 @@ func builtinNegation(i *interpreter, x value) (value, error) {
return makeValueBoolean(!b.value), nil
}

func builtinXnor(i *interpreter, xv, yv value) (value, error) {
p, err := i.getBoolean(xv)
if err != nil {
return nil, err
}
q, err := i.getBoolean(yv)
if err != nil {
return nil, err
}
return makeValueBoolean(p.value == q.value), nil
}

func builtinXor(i *interpreter, xv, yv value) (value, error) {
p, err := i.getBoolean(xv)
if err != nil {
Expand Down Expand Up @@ -2198,6 +2210,7 @@ var funcBuiltins = buildBuiltinMap([]builtin{
&binaryBuiltin{name: "pow", function: builtinPow, params: ast.Identifiers{"x", "n"}},
&binaryBuiltin{name: "modulo", function: builtinModulo, params: ast.Identifiers{"x", "y"}},
&unaryBuiltin{name: "md5", function: builtinMd5, params: ast.Identifiers{"s"}},
&binaryBuiltin{name: "xnor", function: builtinXnor, params: ast.Identifiers{"x", "y"}},
&binaryBuiltin{name: "xor", function: builtinXor, params: ast.Identifiers{"x", "y"}},
&binaryBuiltin{name: "lstripChars", function: builtinLstripChars, params: ast.Identifiers{"str", "chars"}},
&binaryBuiltin{name: "rstripChars", function: builtinRstripChars, params: ast.Identifiers{"str", "chars"}},
Expand Down
17 changes: 16 additions & 1 deletion internal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@ import (
"bytes"
"io/ioutil"
"os"

"bufio"
"fmt"
"github.com/sergi/go-diff/diffmatchpatch"
)
func check(err error) {
if err != nil {
panic(err)
}
}

// Diff produces a pretty diff of two files
func Diff(a, b string) string {
f, err := os.Create("/Users/salonijuneja/yourfile11.txt")
check(err)
defer f.Close()

w := bufio.NewWriter(f)

_, err = fmt.Fprintf(w, "%v\n", a)
check(err)
w.Flush()
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(a, b, false)
return dmp.DiffPrettyText(diffs)
Expand Down
1 change: 1 addition & 0 deletions linter/internal/types/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func prepareStdlib(g *typeGraph) {
// Boolean

"xor": g.newSimpleFuncType(boolType, "x", "y"),
"xnor": g.newSimpleFuncType(boolType, "x", "y"),
}

fieldContains := map[string][]placeholderID{}
Expand Down
1 change: 1 addition & 0 deletions testdata/builtinXnor.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions testdata/builtinXnor.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.xnor(false, false)
Empty file.
1 change: 1 addition & 0 deletions testdata/builtinXnor1.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
1 change: 1 addition & 0 deletions testdata/builtinXnor1.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.xnor(true, false)
Empty file.
10 changes: 10 additions & 0 deletions testdata/builtinXnor2.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RUNTIME ERROR: Unexpected type string, expected boolean
-------------------------------------------------
testdata/builtinXnor2:1:1-24 $

std.xnor("true", false)

-------------------------------------------------
During evaluation


1 change: 1 addition & 0 deletions testdata/builtinXnor2.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
std.xnor("true", false)
Empty file.

0 comments on commit 91ebf4b

Please sign in to comment.