-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup API, get rid of LiteralFunctions again
- Loading branch information
Showing
16 changed files
with
263 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/args_consumer.go → pkg/eval/functions/args_consumer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/args_consumer_test.go → pkg/eval/functions/args_consumer_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/args_matcher.go → pkg/eval/functions/args_matcher.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/args_matcher_test.go → pkg/eval/functions/args_matcher_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-FileCopyrightText: 2023 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package functions | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.xrstf.de/rudi/pkg/coalescing" | ||
"go.xrstf.de/rudi/pkg/eval/types" | ||
) | ||
|
||
type Builder struct { | ||
forms []form | ||
coalescer coalescing.Coalescer | ||
bangHandler BangHandlerFunc | ||
description string | ||
} | ||
|
||
func NewBuilder(forms ...any) *Builder { | ||
b := &Builder{ | ||
forms: []form{}, | ||
} | ||
|
||
for _, f := range forms { | ||
b.AddForm(f) | ||
} | ||
|
||
return b | ||
} | ||
|
||
func (b *Builder) AddForm(form any) *Builder { | ||
funcForm, err := newForm(form) | ||
if err != nil { | ||
panic(fmt.Sprintf("invalid form: %v", err)) | ||
} | ||
|
||
b.forms = append(b.forms, funcForm) | ||
return b | ||
} | ||
|
||
func (b *Builder) WithDescription(s string) *Builder { | ||
b.description = s | ||
return b | ||
} | ||
|
||
func (b *Builder) WithCoalescer(c coalescing.Coalescer) *Builder { | ||
b.coalescer = c | ||
return b | ||
} | ||
|
||
func (b *Builder) WithBangHandler(h BangHandlerFunc) *Builder { | ||
b.bangHandler = h | ||
return b | ||
} | ||
|
||
func (b *Builder) Build() types.Function { | ||
f := newRegularFunction(b.forms, b.coalescer, b.description) | ||
|
||
if b.bangHandler != nil { | ||
return &extendedFunction{ | ||
regularFunction: f, | ||
bangHandler: b.bangHandler, | ||
} | ||
} | ||
|
||
return &f | ||
} |
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/expression.go → pkg/eval/functions/expression.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pkg/eval/util/native/form_test.go → pkg/eval/functions/form_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// SPDX-FileCopyrightText: 2023 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package functions | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"go.xrstf.de/rudi/pkg/coalescing" | ||
"go.xrstf.de/rudi/pkg/eval" | ||
"go.xrstf.de/rudi/pkg/eval/types" | ||
"go.xrstf.de/rudi/pkg/lang/ast" | ||
) | ||
|
||
type regularFunction struct { | ||
forms []form | ||
coalescer coalescing.Coalescer | ||
description string | ||
} | ||
|
||
var _ types.Function = ®ularFunction{} | ||
|
||
func newRegularFunction(forms []form, coalescer coalescing.Coalescer, description string) regularFunction { | ||
return regularFunction{ | ||
forms: forms, | ||
coalescer: coalescer, | ||
description: description, | ||
} | ||
} | ||
|
||
func (b *regularFunction) Description() string { | ||
return b.description | ||
} | ||
|
||
func (b *regularFunction) Evaluate(ctx types.Context, args []ast.Expression) (any, error) { | ||
cachedArgs := convertArgs(args) | ||
|
||
if b.coalescer != nil { | ||
ctx = ctx.WithCoalescer(b.coalescer) | ||
} | ||
|
||
for i, form := range b.forms { | ||
matched, err := form.Match(ctx, cachedArgs) | ||
if err != nil { | ||
return nil, fmt.Errorf("form#%d: %w", i, err) | ||
} | ||
|
||
if matched { | ||
return form.Call(ctx) | ||
} | ||
} | ||
|
||
return nil, errors.New("none of the available forms matched the given expressions") | ||
} | ||
|
||
type BangHandlerFunc func(ctx types.Context, sym ast.Symbol, value any) (types.Context, any, error) | ||
|
||
type extendedFunction struct { | ||
regularFunction | ||
|
||
bangHandler BangHandlerFunc | ||
} | ||
|
||
var ( | ||
_ types.Function = &extendedFunction{} | ||
_ eval.BangHandler = &extendedFunction{} | ||
) | ||
|
||
func (f *extendedFunction) BangHandler(ctx types.Context, sym ast.Symbol, value any) (types.Context, any, error) { | ||
return f.bangHandler(ctx, sym, value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.