Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit 3aee197

Browse files
committed
refactor eval package into runtime struct to reduce static dependency on the eval package (closes #5)
1 parent 91fb71a commit 3aee197

67 files changed

Lines changed: 650 additions & 577 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

aliases.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import (
88

99
"go.xrstf.de/rudi/pkg/builtin"
1010
"go.xrstf.de/rudi/pkg/coalescing"
11-
"go.xrstf.de/rudi/pkg/eval/functions"
12-
"go.xrstf.de/rudi/pkg/eval/types"
11+
"go.xrstf.de/rudi/pkg/runtime/functions"
12+
"go.xrstf.de/rudi/pkg/runtime/interpreter"
13+
"go.xrstf.de/rudi/pkg/runtime/types"
1314
)
1415

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

4142
// NewContext wraps the document, variables and functions into a Context.
42-
func NewContext(ctx context.Context, doc Document, variables Variables, funcs Functions, coalescer Coalescer) Context {
43-
return types.NewContext(ctx, doc, variables, funcs, coalescer)
43+
func NewContext(runtime types.Runtime, ctx context.Context, doc Document, variables Variables, funcs Functions, coalescer Coalescer) (Context, error) {
44+
if runtime == nil {
45+
runtime = interpreter.New()
46+
}
47+
48+
return types.NewContext(runtime, ctx, doc, variables, funcs, coalescer)
4449
}
4550

4651
// NewFunctions returns an empty set of runtime functions.

cmd/rudi/cmd/console/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"go.xrstf.de/rudi/cmd/rudi/docs"
1414
"go.xrstf.de/rudi/cmd/rudi/options"
1515
"go.xrstf.de/rudi/cmd/rudi/util"
16-
"go.xrstf.de/rudi/pkg/eval/types"
16+
"go.xrstf.de/rudi/pkg/runtime/types"
1717

1818
colorjson "github.com/TylerBrock/colorjson"
1919
"github.com/chzyer/readline"

cmd/rudi/util/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"go.xrstf.de/rudi/cmd/rudi/options"
1212
"go.xrstf.de/rudi/cmd/rudi/types"
1313
"go.xrstf.de/rudi/pkg/coalescing"
14+
"go.xrstf.de/rudi/pkg/runtime/interpreter"
1415
)
1516

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

6970
// No context set here, caller is expected to provide their own (the Rudi context is re-used
7071
// in the console, but the Go context should not be, hence the separation).
71-
//nolint:staticcheck
72-
return rudi.NewContext(nil, document, vars, funcs, coalescer), nil
72+
return rudi.NewContext(interpreter.New(), nil, document, vars, funcs, coalescer)
7373
}

pkg/builtin/coalesce/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package coalesce
66
import (
77
"go.xrstf.de/rudi/pkg/builtin/core"
88
"go.xrstf.de/rudi/pkg/coalescing"
9-
"go.xrstf.de/rudi/pkg/eval/functions"
10-
"go.xrstf.de/rudi/pkg/eval/types"
9+
"go.xrstf.de/rudi/pkg/runtime/functions"
10+
"go.xrstf.de/rudi/pkg/runtime/types"
1111
)
1212

1313
var (

pkg/builtin/compare/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88

99
"go.xrstf.de/rudi/pkg/coalescing"
1010
"go.xrstf.de/rudi/pkg/equality"
11-
"go.xrstf.de/rudi/pkg/eval/functions"
12-
"go.xrstf.de/rudi/pkg/eval/types"
11+
"go.xrstf.de/rudi/pkg/runtime/functions"
12+
"go.xrstf.de/rudi/pkg/runtime/types"
1313
)
1414

1515
var (

pkg/builtin/compare/functions_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
"fmt"
99
"testing"
1010

11-
"go.xrstf.de/rudi/pkg/eval/types"
11+
"go.xrstf.de/rudi/pkg/runtime/interpreter"
12+
"go.xrstf.de/rudi/pkg/runtime/types"
1213
"go.xrstf.de/rudi/pkg/testutil"
1314
)
1415

@@ -391,7 +392,10 @@ func TestInvalidComparisonFunctions(t *testing.T) {
391392
gteFunction,
392393
}
393394

394-
ctx := types.NewContext(context.Background(), types.Document{}, nil, nil, nil)
395+
ctx, err := types.NewContext(interpreter.New(), context.Background(), types.Document{}, nil, nil, nil)
396+
if err != nil {
397+
t.Fatalf("Failed to create context: %v", err)
398+
}
395399

396400
for _, tc := range testcases {
397401
for _, f := range funcs {
@@ -463,7 +467,10 @@ func TestComparisonFunctions(t *testing.T) {
463467
},
464468
}
465469

466-
ctx := types.NewContext(context.Background(), types.Document{}, nil, nil, nil)
470+
ctx, err := types.NewContext(interpreter.New(), context.Background(), types.Document{}, nil, nil, nil)
471+
if err != nil {
472+
t.Fatalf("Failed to create context: %v", err)
473+
}
467474

468475
for _, tc := range testcases {
469476
t.Run("", func(t *testing.T) {

pkg/builtin/core/functions.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010

1111
"go.xrstf.de/rudi/pkg/coalescing"
1212
"go.xrstf.de/rudi/pkg/deepcopy"
13-
"go.xrstf.de/rudi/pkg/eval"
14-
"go.xrstf.de/rudi/pkg/eval/functions"
15-
"go.xrstf.de/rudi/pkg/eval/types"
1613
"go.xrstf.de/rudi/pkg/lang/ast"
17-
"go.xrstf.de/rudi/pkg/pathexpr"
14+
genericpathexpr "go.xrstf.de/rudi/pkg/pathexpr"
15+
"go.xrstf.de/rudi/pkg/runtime/functions"
16+
"go.xrstf.de/rudi/pkg/runtime/pathexpr"
17+
"go.xrstf.de/rudi/pkg/runtime/types"
1818
)
1919

2020
var (
@@ -45,7 +45,7 @@ func keepContextCanceled(err error) error {
4545

4646
func ifFunction(ctx types.Context, test bool, yes ast.Expression) (any, error) {
4747
if test {
48-
_, result, err := eval.EvalExpression(ctx, yes)
48+
_, result, err := ctx.Runtime().EvalExpression(ctx, yes)
4949
return result, err
5050
}
5151

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

5555
func ifElseFunction(ctx types.Context, test bool, yes, no ast.Expression) (any, error) {
5656
if test {
57-
_, result, err := eval.EvalExpression(ctx, yes)
57+
_, result, err := ctx.Runtime().EvalExpression(ctx, yes)
5858
return result, err
5959
}
6060

61-
_, result, err := eval.EvalExpression(ctx, no)
61+
_, result, err := ctx.Runtime().EvalExpression(ctx, no)
6262
return result, err
6363
}
6464

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

7575
for _, arg := range args {
76-
tupleCtx, result, err = eval.EvalExpression(tupleCtx, arg)
76+
tupleCtx, result, err = ctx.Runtime().EvalExpression(tupleCtx, arg)
7777
if err != nil {
7878
return nil, err
7979
}
@@ -130,18 +130,18 @@ func hasFunction(ctx types.Context, arg ast.Expression) (any, error) {
130130
}
131131

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

138138
// evaluate the base value
139-
_, value, err := eval.EvalExpression(ctx, expr)
139+
_, value, err := ctx.Runtime().EvalExpression(ctx, expr)
140140
if err != nil {
141141
return nil, err
142142
}
143143

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

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

172172
func tryFunction(ctx types.Context, test ast.Expression) (any, error) {
173-
_, result, err := eval.EvalExpression(ctx, test)
173+
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
174174
if err != nil {
175175
return nil, keepContextCanceled(err)
176176
}
@@ -179,9 +179,9 @@ func tryFunction(ctx types.Context, test ast.Expression) (any, error) {
179179
}
180180

181181
func tryWithFallbackFunction(ctx types.Context, test ast.Expression, fallback ast.Expression) (any, error) {
182-
_, result, err := eval.EvalExpression(ctx, test)
182+
_, result, err := ctx.Runtime().EvalExpression(ctx, test)
183183
if err != nil {
184-
_, result, err = eval.EvalExpression(ctx, fallback)
184+
_, result, err = ctx.Runtime().EvalExpression(ctx, fallback)
185185
if err != nil {
186186
return nil, fmt.Errorf("argument #1: %w", err)
187187
}
@@ -206,7 +206,7 @@ func setFunction(ctx types.Context, target, value ast.Expression) (any, error) {
206206
}
207207

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

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

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

305305
// apply the path expression
306-
updatedValue, err = pathexpr.Delete(currentValue, pathexpr.FromEvaluatedPath(*pathExpr))
306+
updatedValue, err = genericpathexpr.Delete(currentValue, genericpathexpr.FromEvaluatedPath(*pathExpr))
307307
if err != nil {
308308
return ctx, nil, fmt.Errorf("cannot set value in %T at %s: %w", currentValue, pathExpr, err)
309309
}

pkg/builtin/core/functions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package core
66
import (
77
"testing"
88

9-
"go.xrstf.de/rudi/pkg/eval/types"
9+
"go.xrstf.de/rudi/pkg/runtime/types"
1010
"go.xrstf.de/rudi/pkg/testutil"
1111
)
1212

pkg/builtin/datetime/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package datetime
66
import (
77
"time"
88

9-
"go.xrstf.de/rudi/pkg/eval/functions"
10-
"go.xrstf.de/rudi/pkg/eval/types"
9+
"go.xrstf.de/rudi/pkg/runtime/functions"
10+
"go.xrstf.de/rudi/pkg/runtime/types"
1111
)
1212

1313
var (

pkg/builtin/encoding/functions.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"encoding/json"
99
"fmt"
1010

11-
"go.xrstf.de/rudi/pkg/eval/functions"
12-
"go.xrstf.de/rudi/pkg/eval/types"
11+
"go.xrstf.de/rudi/pkg/runtime/functions"
12+
"go.xrstf.de/rudi/pkg/runtime/types"
1313
)
1414

1515
var (

0 commit comments

Comments
 (0)