Skip to content

Commit

Permalink
refactor eval package into runtime struct to reduce static dependency…
Browse files Browse the repository at this point in the history
… on the eval package (closes #5)
  • Loading branch information
xrstf committed Dec 24, 2023
1 parent 91fb71a commit 3aee197
Show file tree
Hide file tree
Showing 67 changed files with 650 additions and 577 deletions.
13 changes: 9 additions & 4 deletions aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

"go.xrstf.de/rudi/pkg/builtin"
"go.xrstf.de/rudi/pkg/coalescing"
"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/interpreter"
"go.xrstf.de/rudi/pkg/runtime/types"
)

// Context is the evaluation context for a Rudi program, consisting of
Expand Down Expand Up @@ -39,8 +40,12 @@ type Document = types.Document
type Coalescer = coalescing.Coalescer

// NewContext wraps the document, variables and functions into a Context.
func NewContext(ctx context.Context, doc Document, variables Variables, funcs Functions, coalescer Coalescer) Context {
return types.NewContext(ctx, doc, variables, funcs, coalescer)
func NewContext(runtime types.Runtime, ctx context.Context, doc Document, variables Variables, funcs Functions, coalescer Coalescer) (Context, error) {
if runtime == nil {
runtime = interpreter.New()
}

return types.NewContext(runtime, ctx, doc, variables, funcs, coalescer)
}

// NewFunctions returns an empty set of runtime functions.
Expand Down
2 changes: 1 addition & 1 deletion cmd/rudi/cmd/console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"go.xrstf.de/rudi/cmd/rudi/docs"
"go.xrstf.de/rudi/cmd/rudi/options"
"go.xrstf.de/rudi/cmd/rudi/util"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/types"

colorjson "github.com/TylerBrock/colorjson"
"github.com/chzyer/readline"
Expand Down
4 changes: 2 additions & 2 deletions cmd/rudi/util/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.xrstf.de/rudi/cmd/rudi/options"
"go.xrstf.de/rudi/cmd/rudi/types"
"go.xrstf.de/rudi/pkg/coalescing"
"go.xrstf.de/rudi/pkg/runtime/interpreter"
)

func SetupRudiContext(opts *options.Options, fileNames []string, fileContents []any) (rudi.Context, error) {
Expand Down Expand Up @@ -68,6 +69,5 @@ func SetupRudiContext(opts *options.Options, fileNames []string, fileContents []

// No context set here, caller is expected to provide their own (the Rudi context is re-used
// in the console, but the Go context should not be, hence the separation).
//nolint:staticcheck
return rudi.NewContext(nil, document, vars, funcs, coalescer), nil
return rudi.NewContext(interpreter.New(), nil, document, vars, funcs, coalescer)
}
4 changes: 2 additions & 2 deletions pkg/builtin/coalesce/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package coalesce
import (
"go.xrstf.de/rudi/pkg/builtin/core"
"go.xrstf.de/rudi/pkg/coalescing"
"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pkg/builtin/compare/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"go.xrstf.de/rudi/pkg/coalescing"
"go.xrstf.de/rudi/pkg/equality"
"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down
13 changes: 10 additions & 3 deletions pkg/builtin/compare/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"fmt"
"testing"

"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/interpreter"
"go.xrstf.de/rudi/pkg/runtime/types"
"go.xrstf.de/rudi/pkg/testutil"
)

Expand Down Expand Up @@ -391,7 +392,10 @@ func TestInvalidComparisonFunctions(t *testing.T) {
gteFunction,
}

ctx := types.NewContext(context.Background(), types.Document{}, nil, nil, nil)
ctx, err := types.NewContext(interpreter.New(), context.Background(), types.Document{}, nil, nil, nil)
if err != nil {
t.Fatalf("Failed to create context: %v", err)
}

for _, tc := range testcases {
for _, f := range funcs {
Expand Down Expand Up @@ -463,7 +467,10 @@ func TestComparisonFunctions(t *testing.T) {
},
}

ctx := types.NewContext(context.Background(), types.Document{}, nil, nil, nil)
ctx, err := types.NewContext(interpreter.New(), context.Background(), types.Document{}, nil, nil, nil)
if err != nil {
t.Fatalf("Failed to create context: %v", err)
}

for _, tc := range testcases {
t.Run("", func(t *testing.T) {
Expand Down
40 changes: 20 additions & 20 deletions pkg/builtin/core/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

"go.xrstf.de/rudi/pkg/coalescing"
"go.xrstf.de/rudi/pkg/deepcopy"
"go.xrstf.de/rudi/pkg/eval"
"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/lang/ast"
"go.xrstf.de/rudi/pkg/pathexpr"
genericpathexpr "go.xrstf.de/rudi/pkg/pathexpr"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/pathexpr"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down Expand Up @@ -45,7 +45,7 @@ func keepContextCanceled(err error) error {

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

Expand All @@ -54,11 +54,11 @@ func ifFunction(ctx types.Context, test bool, yes ast.Expression) (any, error) {

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

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

Expand All @@ -73,7 +73,7 @@ func DoFunction(ctx types.Context, args ...ast.Expression) (any, error) {
)

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

// pre-evaluate the path
evaluatedPath, err := eval.EvalPathExpression(ctx, pathExpr)
evaluatedPath, err := pathexpr.Eval(ctx, pathExpr)
if err != nil {
return nil, fmt.Errorf("invalid path expression: %w", err)
}

// evaluate the base value
_, value, err := eval.EvalExpression(ctx, expr)
_, value, err := ctx.Runtime().EvalExpression(ctx, expr)
if err != nil {
return nil, err
}

_, err = eval.TraverseEvaluatedPathExpression(value, *evaluatedPath)
_, err = pathexpr.Traverse(value, *evaluatedPath)
if err != nil {
return false, keepContextCanceled(err)
}
Expand All @@ -161,7 +161,7 @@ func defaultFunction(ctx types.Context, value any, fallback ast.Expression) (any
return value, nil
}

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

func tryFunction(ctx types.Context, test ast.Expression) (any, error) {
_, result, err := eval.EvalExpression(ctx, test)
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
if err != nil {
return nil, keepContextCanceled(err)
}
Expand All @@ -179,9 +179,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 := eval.EvalExpression(ctx, test)
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
if err != nil {
_, result, err = eval.EvalExpression(ctx, fallback)
_, result, err = ctx.Runtime().EvalExpression(ctx, fallback)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}
Expand All @@ -206,7 +206,7 @@ func setFunction(ctx types.Context, target, value ast.Expression) (any, error) {
}

// discard any context changes within the newValue expression
_, newValue, err := eval.EvalExpression(ctx, value)
_, newValue, err := ctx.Runtime().EvalExpression(ctx, value)
if err != nil {
return nil, fmt.Errorf("argument #1: %w", err)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func deleteFunction(ctx types.Context, expr ast.Expression) (any, error) {
}

// pre-evaluate the path
pathExpr, err := eval.EvalPathExpression(ctx, symbol.PathExpression)
pathExpr, err := pathexpr.Eval(ctx, symbol.PathExpression)
if err != nil {
return nil, fmt.Errorf("argument #0: invalid path expression: %w", err)
}
Expand All @@ -261,7 +261,7 @@ func deleteFunction(ctx types.Context, expr ast.Expression) (any, error) {
}

// delete the desired path in the value
updatedValue, err := pathexpr.Delete(currentValue, pathexpr.FromEvaluatedPath(*pathExpr))
updatedValue, err := genericpathexpr.Delete(currentValue, genericpathexpr.FromEvaluatedPath(*pathExpr))
if err != nil {
return nil, fmt.Errorf("cannot delete %s in %T: %w", pathExpr, currentValue, err)
}
Expand All @@ -285,7 +285,7 @@ func deleteBangHandler(ctx types.Context, originalArgs []ast.Expression, value a
// if the symbol has a path to traverse, do so
if sym.PathExpression != nil {
// pre-evaluate the path expression
pathExpr, err := eval.EvalPathExpression(ctx, sym.PathExpression)
pathExpr, err := pathexpr.Eval(ctx, sym.PathExpression)
if err != nil {
return ctx, nil, fmt.Errorf("argument #0: invalid path expression: %w", err)
}
Expand All @@ -303,7 +303,7 @@ func deleteBangHandler(ctx types.Context, originalArgs []ast.Expression, value a
}

// apply the path expression
updatedValue, err = pathexpr.Delete(currentValue, pathexpr.FromEvaluatedPath(*pathExpr))
updatedValue, err = genericpathexpr.Delete(currentValue, genericpathexpr.FromEvaluatedPath(*pathExpr))
if err != nil {
return ctx, nil, fmt.Errorf("cannot set value in %T at %s: %w", currentValue, pathExpr, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/builtin/core/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package core
import (
"testing"

"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/types"
"go.xrstf.de/rudi/pkg/testutil"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/builtin/datetime/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package datetime
import (
"time"

"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pkg/builtin/encoding/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"encoding/json"
"fmt"

"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pkg/builtin/hashing/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"hash"
"io"

"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down
25 changes: 12 additions & 13 deletions pkg/builtin/lists/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ package lists
import (
"fmt"

"go.xrstf.de/rudi/pkg/eval"
"go.xrstf.de/rudi/pkg/eval/functions"
"go.xrstf.de/rudi/pkg/eval/types"
"go.xrstf.de/rudi/pkg/lang/ast"
"go.xrstf.de/rudi/pkg/runtime/functions"
"go.xrstf.de/rudi/pkg/runtime/types"
)

var (
Expand Down Expand Up @@ -79,7 +78,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
loopCtx = loopCtx.WithVariables(vars)

loopCtx, result, err = eval.EvalExpression(loopCtx, expr)
loopCtx, result, err = ctx.Runtime().EvalExpression(loopCtx, expr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -114,7 +113,7 @@ func rangeObjectFunction(ctx types.Context, data map[string]any, namingVec ast.E
// do not use separate contexts for each loop iteration, as the loop might build up a counter
loopCtx = loopCtx.WithVariables(vars)

loopCtx, result, err = eval.EvalExpression(loopCtx, expr)
loopCtx, result, err = ctx.Runtime().EvalExpression(loopCtx, expr)
if err != nil {
return nil, err
}
Expand All @@ -135,7 +134,7 @@ func mapVectorAnonymousFunction(ctx types.Context, data []any, ident ast.Express
}

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

return mapVector(ctx, data, mapHandler)
Expand All @@ -160,7 +159,7 @@ func mapVectorExpressionFunction(ctx types.Context, data []any, namingVec ast.Ex

ctx = ctx.WithVariables(vars)

return eval.EvalExpression(ctx, expr)
return ctx.Runtime().EvalExpression(ctx, expr)
}

return mapVector(ctx, data, mapHandler)
Expand Down Expand Up @@ -197,7 +196,7 @@ func mapObjectAnonymousFunction(ctx types.Context, data map[string]any, ident as
}

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

return mapObject(ctx, data, mapHandler)
Expand All @@ -222,7 +221,7 @@ func mapObjectExpressionFunction(ctx types.Context, data map[string]any, namingV

ctx = ctx.WithVariables(vars)

return eval.EvalExpression(ctx, expr)
return ctx.Runtime().EvalExpression(ctx, expr)
}

return mapObject(ctx, data, mapHandler)
Expand Down Expand Up @@ -259,7 +258,7 @@ func filterVectorAnonymousFunction(ctx types.Context, data []any, ident ast.Expr
}

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

return filterVector(ctx, data, mapHandler)
Expand All @@ -284,7 +283,7 @@ func filterVectorExpressionFunction(ctx types.Context, data []any, namingVec ast

ctx = ctx.WithVariables(vars)

return eval.EvalExpression(ctx, expr)
return ctx.Runtime().EvalExpression(ctx, expr)
}

return filterVector(ctx, data, mapHandler)
Expand Down Expand Up @@ -327,7 +326,7 @@ func filterObjectAnonymousFunction(ctx types.Context, data map[string]any, ident
}

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

return filterObject(ctx, data, mapHandler)
Expand All @@ -352,7 +351,7 @@ func filterObjectExpressionFunction(ctx types.Context, data map[string]any, nami

ctx = ctx.WithVariables(vars)

return eval.EvalExpression(ctx, expr)
return ctx.Runtime().EvalExpression(ctx, expr)
}

return filterObject(ctx, data, mapHandler)
Expand Down
Loading

0 comments on commit 3aee197

Please sign in to comment.