Skip to content

Commit 5dcb233

Browse files
committed
Move ast.Function to builtin.Function
1 parent ed68836 commit 5dcb233

File tree

7 files changed

+22
-26
lines changed

7 files changed

+22
-26
lines changed

ast/func.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

builtin/builtin.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/expr-lang/expr/ast"
1312
"github.com/expr-lang/expr/vm/runtime"
1413
)
1514

15+
type Function struct {
16+
Name string
17+
Func func(args ...any) (any, error)
18+
Fast func(arg any) any
19+
Types []reflect.Type
20+
Validate func(args []reflect.Type) (reflect.Type, error)
21+
Predicate bool
22+
}
23+
1624
var (
1725
Index map[string]int
1826
Names []string
@@ -27,7 +35,7 @@ func init() {
2735
}
2836
}
2937

30-
var Builtins = []*ast.Function{
38+
var Builtins = []*Function{
3139
{
3240
Name: "all",
3341
Predicate: true,

builtin/lib.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"reflect"
77
"strconv"
88

9-
"github.com/expr-lang/expr/ast"
109
"github.com/expr-lang/expr/vm/runtime"
1110
)
1211

@@ -274,8 +273,8 @@ func Min(args ...any) (any, error) {
274273
return min, nil
275274
}
276275

277-
func bitFunc(name string, fn func(x, y int) (any, error)) *ast.Function {
278-
return &ast.Function{
276+
func bitFunc(name string, fn func(x, y int) (any, error)) *Function {
277+
return &Function{
279278
Name: name,
280279
Func: func(args ...any) (any, error) {
281280
if len(args) != 2 {

checker/checker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type varScope struct {
7171

7272
type info struct {
7373
method bool
74-
fn *ast.Function
74+
fn *builtin.Function
7575

7676
// elem is element type of array or map.
7777
// Arrays created with type []any, but
@@ -819,7 +819,7 @@ func (v *checker) checkBuiltinGet(node *ast.BuiltinNode) (reflect.Type, info) {
819819
return v.error(val, "type %v does not support indexing", t)
820820
}
821821

822-
func (v *checker) checkFunction(f *ast.Function, node ast.Node, arguments []ast.Node) (reflect.Type, info) {
822+
func (v *checker) checkFunction(f *builtin.Function, node ast.Node, arguments []ast.Node) (reflect.Type, info) {
823823
if f.Validate != nil {
824824
args := make([]reflect.Type, len(arguments))
825825
for i, arg := range arguments {

compiler/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (c *compiler) addVariable(name string) int {
144144
}
145145

146146
// emitFunction adds builtin.Function.Func to the program.functions and emits call opcode.
147-
func (c *compiler) emitFunction(fn *ast.Function, argsLen int) {
147+
func (c *compiler) emitFunction(fn *builtin.Function, argsLen int) {
148148
switch argsLen {
149149
case 0:
150150
c.emit(OpCall0, c.addFunction(fn))
@@ -161,7 +161,7 @@ func (c *compiler) emitFunction(fn *ast.Function, argsLen int) {
161161
}
162162

163163
// addFunction adds builtin.Function.Func to the program.functions and returns its index.
164-
func (c *compiler) addFunction(fn *ast.Function) int {
164+
func (c *compiler) addFunction(fn *builtin.Function) int {
165165
if fn == nil {
166166
panic("function is nil")
167167
}

conf/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type Config struct {
2121
Strict bool
2222
ConstFns map[string]reflect.Value
2323
Visitors []ast.Visitor
24-
Functions map[string]*ast.Function
25-
Builtins map[string]*ast.Function
24+
Functions map[string]*builtin.Function
25+
Builtins map[string]*builtin.Function
2626
Disabled map[string]bool // disabled builtins
2727
}
2828

@@ -32,8 +32,8 @@ func CreateNew() *Config {
3232
Optimize: true,
3333
Operators: make(map[string][]string),
3434
ConstFns: make(map[string]reflect.Value),
35-
Functions: make(map[string]*ast.Function),
36-
Builtins: make(map[string]*ast.Function),
35+
Functions: make(map[string]*builtin.Function),
36+
Builtins: make(map[string]*builtin.Function),
3737
Disabled: make(map[string]bool),
3838
}
3939
for _, f := range builtin.Builtins {

expr.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77

88
"github.com/expr-lang/expr/ast"
9+
"github.com/expr-lang/expr/builtin"
910
"github.com/expr-lang/expr/checker"
1011
"github.com/expr-lang/expr/compiler"
1112
"github.com/expr-lang/expr/conf"
@@ -123,7 +124,7 @@ func Function(name string, fn func(params ...any) (any, error), types ...any) Op
123124
}
124125
ts[i] = t
125126
}
126-
c.Functions[name] = &ast.Function{
127+
c.Functions[name] = &builtin.Function{
127128
Name: name,
128129
Func: fn,
129130
Types: ts,

0 commit comments

Comments
 (0)