Skip to content

Commit

Permalink
remove unnecessary context return values
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Jan 3, 2024
1 parent e5caf4b commit abc2162
Show file tree
Hide file tree
Showing 29 changed files with 144 additions and 192 deletions.
22 changes: 10 additions & 12 deletions cmd/rudi/cmd/console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func Run(handler *util.SignalHandler, opts *options.Options, args []string, rudi
continue
}

newCtx, stop, err := processInput(handler, rudiCtx, opts, line)
stop, err := processInput(handler, rudiCtx, opts, line)
if err != nil {
parseErr := &rudi.ParseError{}
if errors.As(err, parseErr) {
Expand All @@ -98,33 +98,31 @@ func Run(handler *util.SignalHandler, opts *options.Options, args []string, rudi
if stop {
break
}

rudiCtx = newCtx
}

fmt.Println()

return nil
}

func processInput(handler *util.SignalHandler, rudiCtx types.Context, opts *options.Options, input string) (newCtx types.Context, stop bool, err error) {
func processInput(handler *util.SignalHandler, rudiCtx types.Context, opts *options.Options, input string) (stop bool, err error) {
if command, exists := replCommands[input]; exists {
return rudiCtx, false, command(rudiCtx, opts)
return false, command(rudiCtx, opts)
}

if prefix := "help "; strings.HasPrefix(input, prefix) {
topicName := strings.TrimPrefix(input, prefix)
return rudiCtx, false, helpTopicCommand(topicName)
return false, helpTopicCommand(topicName)
}

if strings.EqualFold("exit", input) {
return rudiCtx, true, nil
return true, nil
}

// parse input
program, err := rudi.Parse("(repl)", input)
if err != nil {
return rudiCtx, false, err
return false, err
}

// allow to interrupt the statement
Expand All @@ -133,9 +131,9 @@ func processInput(handler *util.SignalHandler, rudiCtx types.Context, opts *opti

handler.SetCancelFn(cancel)

newCtx, evaluated, err := program.RunContext(rudiCtx.WithGoContext(ctx))
evaluated, err := program.RunContext(rudiCtx.WithGoContext(ctx))
if err != nil {
return rudiCtx, false, err
return false, err
}

f := colorjson.NewFormatter()
Expand All @@ -144,10 +142,10 @@ func processInput(handler *util.SignalHandler, rudiCtx types.Context, opts *opti

encoded, err := f.Marshal(evaluated)
if err != nil {
return rudiCtx, false, fmt.Errorf("failed to encode %v: %w", evaluated, err)
return false, fmt.Errorf("failed to encode %v: %w", evaluated, err)
}

fmt.Println(string(encoded))

return newCtx, false, nil
return false, nil
}
2 changes: 1 addition & 1 deletion cmd/rudi/cmd/script/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Run(handler *util.SignalHandler, opts *options.Options, args []string) erro
handler.SetCancelFn(cancel)

// evaluate the script
_, evaluated, err := program.RunContext(rudiCtx.WithGoContext(subCtx))
evaluated, err := program.RunContext(rudiCtx.WithGoContext(subCtx))
if err != nil {
return fmt.Errorf("failed to evaluate script: %w", err)
}
Expand Down
Binary file modified cmd/rudi/docs/data/language.md.gz
Binary file not shown.
37 changes: 15 additions & 22 deletions pkg/builtin/core/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,15 @@ func keepContextCanceled(err error) error {
}

func ifFunction(ctx types.Context, test bool, yes ast.Expression) (any, error) {
if test {
_, result, err := ctx.Runtime().EvalExpression(ctx, yes)
return result, err
}

return nil, nil
return ifElseFunction(ctx, test, yes, ast.Null{})
}

func ifElseFunction(ctx types.Context, test bool, yes, no ast.Expression) (any, error) {
if test {
_, result, err := ctx.Runtime().EvalExpression(ctx, yes)
return result, err
return ctx.Runtime().EvalExpression(ctx, yes)
}

_, result, err := ctx.Runtime().EvalExpression(ctx, no)
return result, err
return ctx.Runtime().EvalExpression(ctx, no)
}

// NB: Variadic functions always require at least 1 argument in Rudi to match.
Expand All @@ -72,7 +65,7 @@ func DoFunction(ctx types.Context, args ...ast.Expression) (any, error) {
)

for _, arg := range args {
ctx, result, err = ctx.Runtime().EvalExpression(ctx, arg)
result, err = ctx.Runtime().EvalExpression(ctx, arg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -102,7 +95,7 @@ func hasFunction(ctx types.Context, arg ast.Expression) (any, error) {
}

// evaluate the base value
_, value, err := ctx.Runtime().EvalExpression(ctx, expr)
value, err := ctx.Runtime().EvalExpression(ctx, expr)
if err != nil {
return nil, err
}
Expand All @@ -127,7 +120,7 @@ func defaultFunction(ctx types.Context, value any, fallback ast.Expression) (any
return value, nil
}

_, value, err = ctx.Runtime().EvalExpression(ctx, fallback)
value, err = ctx.Runtime().EvalExpression(ctx, fallback)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}
Expand All @@ -136,7 +129,7 @@ func defaultFunction(ctx types.Context, value any, fallback ast.Expression) (any
}

func tryFunction(ctx types.Context, test ast.Expression) (any, error) {
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
result, err := ctx.Runtime().EvalExpression(ctx, test)
if err != nil {
return nil, keepContextCanceled(err)
}
Expand All @@ -145,9 +138,9 @@ func tryFunction(ctx types.Context, test ast.Expression) (any, error) {
}

func tryWithFallbackFunction(ctx types.Context, test ast.Expression, fallback ast.Expression) (any, error) {
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
result, err := ctx.Runtime().EvalExpression(ctx, test)
if err != nil {
_, result, err = ctx.Runtime().EvalExpression(ctx, fallback)
result, err = ctx.Runtime().EvalExpression(ctx, fallback)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}
Expand Down Expand Up @@ -197,7 +190,7 @@ func setFunction(ctx types.Context, target ast.Expression, value any) (any, erro
}

// evaluate the target
_, targetValue, err := ctx.Runtime().EvalExpression(ctx, expr)
targetValue, err := ctx.Runtime().EvalExpression(ctx, expr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -235,7 +228,7 @@ func deleteFunction(ctx types.Context, target ast.Expression) (any, error) {
}

// evaluate the target
_, targetValue, err := ctx.Runtime().EvalExpression(ctx, pathed.Pathless())
targetValue, err := ctx.Runtime().EvalExpression(ctx, pathed.Pathless())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -269,15 +262,15 @@ func deleteFunction(ctx types.Context, target ast.Expression) (any, error) {
// All of this applies equally to the delete function, hence both share the same bang handler.
// Since this behaviour makes set different from other regular functions, it needs a custom
// BangHandler.
func overwriteEverythingBangHandler(ctx types.Context, originalArgs []ast.Expression, value any) (types.Context, any, error) {
func overwriteEverythingBangHandler(ctx types.Context, originalArgs []ast.Expression, value any) (any, error) {
if len(originalArgs) == 0 {
return ctx, nil, errors.New("must have at least 1 symbol argument")
return nil, errors.New("must have at least 1 symbol argument")
}

firstArg := originalArgs[0]
symbol, ok := firstArg.(ast.Symbol)
if !ok {
return ctx, nil, fmt.Errorf("must use Symbol as first argument, got %T", firstArg)
return nil, fmt.Errorf("must use Symbol as first argument, got %T", firstArg)
}

// Since set always returns the entire data structure, all we must do here is to
Expand All @@ -289,7 +282,7 @@ func overwriteEverythingBangHandler(ctx types.Context, originalArgs []ast.Expres
ctx.GetDocument().Set(value)
}

return ctx, value, nil
return value, nil
}

func isEmptyFunction(val bool) (any, error) {
Expand Down
58 changes: 15 additions & 43 deletions pkg/builtin/lists/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ func rangeVectorFunction(ctx types.Context, data []any, namingVec ast.Expression
// Do not use separate contexts for each loop iteration, as the loop might build up a counter,
// but only use the loop variables in a shallow scope, where only these two temporary variables
// are laid over the regular scoped/global variables.
scope := ctx.NewShallowScope(vars)

_, result, err = ctx.Runtime().EvalExpression(scope, expr)
result, err = ctx.Runtime().EvalExpression(ctx.NewShallowScope(vars), expr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,7 +106,7 @@ func rangeObjectFunction(ctx types.Context, data map[string]any, namingVec ast.E
vars[loopIndexName] = key
}

_, result, err = ctx.Runtime().EvalExpression(ctx.NewShallowScope(vars), expr)
result, err = ctx.Runtime().EvalExpression(ctx.NewShallowScope(vars), expr)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +116,7 @@ func rangeObjectFunction(ctx types.Context, data map[string]any, namingVec ast.E
}

// itemHandlerFunc works for iterating over vectors as well as over objects.
type itemHandlerFunc func(ctx types.Context, _ any, value any) (types.Context, any, error)
type itemHandlerFunc func(ctx types.Context, _ any, value any) (any, error)

// (map VECTOR identifier)
func mapVectorAnonymousFunction(ctx types.Context, data []any, ident ast.Expression) (any, error) {
Expand All @@ -128,7 +126,7 @@ func mapVectorAnonymousFunction(ctx types.Context, data []any, ident ast.Express
return nil, fmt.Errorf("argument #1: expected identifier, got %T", ident)
}

mapHandler := func(ctx types.Context, _ any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, _ any, value any) (any, error) {
return ctx.Runtime().CallFunction(ctx, identifier, []ast.Expression{types.MakeShim(value)})
}

Expand All @@ -143,7 +141,7 @@ func mapVectorExpressionFunction(ctx types.Context, data []any, namingVec ast.Ex
return nil, fmt.Errorf("argument #1: not a valid naming vector: %w", err)
}

mapHandler := func(ctx types.Context, index any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, index any, value any) (any, error) {
vars := map[string]any{
valueVarName: value,
}
Expand All @@ -160,16 +158,9 @@ func mapVectorExpressionFunction(ctx types.Context, data []any, namingVec ast.Ex

func mapVector(ctx types.Context, data []any, f itemHandlerFunc) (any, error) {
output := make([]any, len(data))
loopCtx := ctx

for i, item := range data {
var (
result any
err error
)

// do not use separate contexts for each loop iteration, as the loop might build up a counter
loopCtx, result, err = f(loopCtx, i, item)
result, err := f(ctx, i, item)
if err != nil {
return nil, err
}
Expand All @@ -188,7 +179,7 @@ func mapObjectAnonymousFunction(ctx types.Context, data map[string]any, ident as
return nil, fmt.Errorf("argument #1: expected identifier, got %T", ident)
}

mapHandler := func(ctx types.Context, _ any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, _ any, value any) (any, error) {
return ctx.Runtime().CallFunction(ctx, identifier, []ast.Expression{types.MakeShim(value)})
}

Expand All @@ -203,7 +194,7 @@ func mapObjectExpressionFunction(ctx types.Context, data map[string]any, namingV
return nil, fmt.Errorf("argument #1: not a valid naming vector: %w", err)
}

mapHandler := func(ctx types.Context, key any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, key any, value any) (any, error) {
vars := map[string]any{
valueVarName: value,
}
Expand All @@ -220,16 +211,9 @@ func mapObjectExpressionFunction(ctx types.Context, data map[string]any, namingV

func mapObject(ctx types.Context, data map[string]any, f itemHandlerFunc) (any, error) {
output := map[string]any{}
loopCtx := ctx

for key, value := range data {
var (
result any
err error
)

// do not use separate contexts for each loop iteration, as the loop might build up a counter
loopCtx, result, err = f(loopCtx, key, value)
result, err := f(ctx, key, value)
if err != nil {
return nil, err
}
Expand All @@ -248,7 +232,7 @@ func filterVectorAnonymousFunction(ctx types.Context, data []any, ident ast.Expr
return nil, fmt.Errorf("argument #1: expected identifier, got %T", ident)
}

mapHandler := func(ctx types.Context, _ any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, _ any, value any) (any, error) {
return ctx.Runtime().CallFunction(ctx, identifier, []ast.Expression{types.MakeShim(value)})
}

Expand All @@ -263,7 +247,7 @@ func filterVectorExpressionFunction(ctx types.Context, data []any, namingVec ast
return nil, fmt.Errorf("argument #1: not a valid naming vector: %w", err)
}

mapHandler := func(ctx types.Context, index any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, index any, value any) (any, error) {
vars := map[string]any{
valueVarName: value,
}
Expand All @@ -280,15 +264,9 @@ func filterVectorExpressionFunction(ctx types.Context, data []any, namingVec ast

func filterVector(ctx types.Context, data []any, f itemHandlerFunc) (any, error) {
output := []any{}
loopCtx := ctx

for i, item := range data {
var (
result any
err error
)

loopCtx, result, err = f(loopCtx, i, item)
result, err := f(ctx, i, item)
if err != nil {
return nil, err
}
Expand All @@ -314,7 +292,7 @@ func filterObjectAnonymousFunction(ctx types.Context, data map[string]any, ident
return nil, fmt.Errorf("argument #1: expected identifier, got %T", ident)
}

mapHandler := func(ctx types.Context, _ any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, _ any, value any) (any, error) {
return ctx.Runtime().CallFunction(ctx, identifier, []ast.Expression{types.MakeShim(value)})
}

Expand All @@ -329,7 +307,7 @@ func filterObjectExpressionFunction(ctx types.Context, data map[string]any, nami
return nil, fmt.Errorf("argument #1: not a valid naming vector: %w", err)
}

mapHandler := func(ctx types.Context, key any, value any) (types.Context, any, error) {
mapHandler := func(ctx types.Context, key any, value any) (any, error) {
vars := map[string]any{
valueVarName: value,
}
Expand All @@ -346,15 +324,9 @@ func filterObjectExpressionFunction(ctx types.Context, data map[string]any, nami

func filterObject(ctx types.Context, data map[string]any, f itemHandlerFunc) (any, error) {
output := map[string]any{}
loopCtx := ctx

for key, value := range data {
var (
result any
err error
)

loopCtx, result, err = f(loopCtx, key, value)
result, err := f(ctx, key, value)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/builtin/logic/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var (

func andFunction(ctx types.Context, args ...ast.Expression) (any, error) {
for i, arg := range args {
_, evaluated, err := ctx.Runtime().EvalExpression(ctx, arg)
evaluated, err := ctx.Runtime().EvalExpression(ctx, arg)
if err != nil {
return nil, fmt.Errorf("argument #%d: %w", i, err)
}
Expand All @@ -41,7 +41,7 @@ func andFunction(ctx types.Context, args ...ast.Expression) (any, error) {

func orFunction(ctx types.Context, args ...ast.Expression) (any, error) {
for i, arg := range args {
_, evaluated, err := ctx.Runtime().EvalExpression(ctx, arg)
evaluated, err := ctx.Runtime().EvalExpression(ctx, arg)
if err != nil {
return nil, fmt.Errorf("argument #%d: %w", i, err)
}
Expand Down
Loading

0 comments on commit abc2162

Please sign in to comment.