Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to override fields starlark.Binary and starlark.Unary #429 #430

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions starlark/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,8 +696,15 @@ func setIndex(x, y, z Value) error {
return nil
}

// Unary applies a unary operator (+, -, ~, not) to its operand.
func Unary(op syntax.Token, x Value) (Value, error) {
var (
// Unary is the default implementation of the unary operation and is used by current package. You can override this field.
Unary = UnaryDefault
// Binary is the default implementation of the binary operation and is used by current package. You can override this field.
Binary = BinaryDefault
)

// UnaryDefault applies a unary operator (+, -, ~, not) to its operand.
func UnaryDefault(op syntax.Token, x Value) (Value, error) {
// The NOT operator is not customizable.
if op == syntax.NOT {
return !x.Truth(), nil
Expand All @@ -715,9 +722,9 @@ func Unary(op syntax.Token, x Value) (Value, error) {
return nil, fmt.Errorf("unknown unary op: %s %s", op, x.Type())
}

// Binary applies a strict binary operator (not AND or OR) to its operands.
// BinaryDefault applies a strict binary operator (not AND or OR) to its operands.
// For equality tests or ordered comparisons, use Compare instead.
func Binary(op syntax.Token, x, y Value) (Value, error) {
func BinaryDefault(op syntax.Token, x, y Value) (Value, error) {
switch op {
case syntax.PLUS:
switch x := x.(type) {
Expand Down Expand Up @@ -969,7 +976,7 @@ func Binary(op syntax.Token, x, y Value) (Value, error) {
}

case syntax.NOT_IN:
z, err := Binary(syntax.IN, x, y)
z, err := BinaryDefault(syntax.IN, x, y)
if err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion starlark/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,14 +1248,17 @@ func EqualDepth(x, y Value, depth int) (bool, error) {
return CompareDepth(syntax.EQL, x, y, depth)
}

// Compare is the default implementation of the compare operation and is used by current package. You can override this field.
var Compare = DefaultCompare

// Compare compares two Starlark values.
// The comparison operation must be one of EQL, NEQ, LT, LE, GT, or GE.
// Compare returns an error if an ordered comparison was
// requested for a type that does not support it.
//
// Recursive comparisons by implementations of Value.CompareSameType
// should use CompareDepth to prevent infinite recursion.
func Compare(op syntax.Token, x, y Value) (bool, error) {
func DefaultCompare(op syntax.Token, x, y Value) (bool, error) {
return CompareDepth(op, x, y, CompareLimit)
}

Expand Down