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

proc: add min and max builtins #3530

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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
53 changes: 53 additions & 0 deletions pkg/proc/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,8 @@ var supportedBuiltins = map[string]func([]*Variable, []ast.Expr) (*Variable, err
"complex": complexBuiltin,
"imag": imagBuiltin,
"real": realBuiltin,
"min": minBuiltin,
"max": maxBuiltin,
}

func capBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
Expand Down Expand Up @@ -1662,6 +1664,57 @@ func realBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
return newConstant(constant.Real(arg.Value), arg.mem), nil
}

func minBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
return minmaxBuiltin("min", token.LSS, args, nodeargs)
}

func maxBuiltin(args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
return minmaxBuiltin("max", token.GTR, args, nodeargs)
}

func minmaxBuiltin(name string, op token.Token, args []*Variable, nodeargs []ast.Expr) (*Variable, error) {
var best *Variable

for i := range args {
if args[i].Kind == reflect.String {
args[i].loadValue(loadFullValueLongerStrings)
} else {
args[i].loadValue(loadFullValue)
}

if args[i].Unreadable != nil {
return nil, fmt.Errorf("could not load %q: %v", exprToString(nodeargs[i]), args[i].Unreadable)
}
if args[i].FloatSpecial != 0 {
return nil, errOperationOnSpecialFloat
}

if best == nil {
best = args[i]
continue
}

_, err := negotiateType(op, args[i], best)
if err != nil {
return nil, err
}

v, err := compareOp(op, args[i], best)
if err != nil {
return nil, err
}

if v {
best = args[i]
}
}

if best == nil {
return nil, fmt.Errorf("not enough arguments to %s", name)
}
return best, nil
}

// Evaluates expressions <subexpr>.<field name> where subexpr is not a package name
func (scope *EvalScope) evalStructSelector(op *evalop.Select, stack *evalStack) {
xv := stack.pop()
Expand Down
7 changes: 7 additions & 0 deletions pkg/proc/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,13 @@ func getEvalExpressionTestCases() []varTest {
{"real(cpx1)", false, "1", "1", "", nil},
{"imag(3i)", false, "3", "3", "", nil},
{"real(4)", false, "4", "4", "", nil},
{"max(1, 2, 3)", false, "3", "3", "", nil},
{`max("one", "two", "three")`, false, `"two"`, `"two"`, "", nil},
{`min("one", "two", "three")`, false, `"one"`, `"one"`, "", nil},
{"max(s1[0], s1[1], s1[2])", false, `"two"`, `"two"`, "string", nil},
{"min(s1[0], s1[1], s1[2])", false, `"one"`, `"one"`, "string", nil},
{`max(s1[0], "two", s1[2])`, false, `"two"`, `"two"`, "", nil},
{`min(s1[0], "two", s1[2])`, false, `"one"`, `"one"`, "string", nil},

// nil
{"nil", false, "nil", "nil", "", nil},
Expand Down