@@ -3,8 +3,6 @@ package builtin
33import (
44 "fmt"
55 "reflect"
6-
7- "github.com/antonmedv/expr/vm/runtime"
86)
97
108var (
@@ -13,16 +11,22 @@ var (
1311)
1412
1513type Function struct {
16- Name string
17- Func func (args ... interface {}) (interface {}, error )
18- Types []reflect.Type
19- Validate func (args []reflect.Type ) (reflect.Type , error )
14+ Name string
15+ Func func (args ... interface {}) (interface {}, error )
16+ Types []reflect.Type
17+ Validate func (args []reflect.Type ) (reflect.Type , error )
18+ BuiltinId int
2019}
2120
21+ const (
22+ Len = iota + 1
23+ Abs
24+ )
25+
2226var Builtins = []* Function {
2327 {
24- Name : "len" ,
25- Func : runtime . Len ,
28+ Name : "len" ,
29+ BuiltinId : Len ,
2630 Validate : func (args []reflect.Type ) (reflect.Type , error ) {
2731 if len (args ) != 1 {
2832 return anyType , fmt .Errorf ("invalid number of arguments for len (expected 1, got %d)" , len (args ))
@@ -35,61 +39,8 @@ var Builtins = []*Function{
3539 },
3640 },
3741 {
38- Name : "abs" ,
39- Func : func (args ... interface {}) (interface {}, error ) {
40- x := args [0 ]
41- switch x .(type ) {
42- case float32 :
43- if x .(float32 ) < 0 {
44- return - x .(float32 ), nil
45- }
46- case float64 :
47- if x .(float64 ) < 0 {
48- return - x .(float64 ), nil
49- }
50- case int :
51- if x .(int ) < 0 {
52- return - x .(int ), nil
53- }
54- case int8 :
55- if x .(int8 ) < 0 {
56- return - x .(int8 ), nil
57- }
58- case int16 :
59- if x .(int16 ) < 0 {
60- return - x .(int16 ), nil
61- }
62- case int32 :
63- if x .(int32 ) < 0 {
64- return - x .(int32 ), nil
65- }
66- case int64 :
67- if x .(int64 ) < 0 {
68- return - x .(int64 ), nil
69- }
70- case uint :
71- if x .(uint ) < 0 {
72- return - x .(uint ), nil
73- }
74- case uint8 :
75- if x .(uint8 ) < 0 {
76- return - x .(uint8 ), nil
77- }
78- case uint16 :
79- if x .(uint16 ) < 0 {
80- return - x .(uint16 ), nil
81- }
82- case uint32 :
83- if x .(uint32 ) < 0 {
84- return - x .(uint32 ), nil
85- }
86- case uint64 :
87- if x .(uint64 ) < 0 {
88- return - x .(uint64 ), nil
89- }
90- }
91- return nil , fmt .Errorf ("invalid argument for abs (type %T)" , x )
92- },
42+ Name : "abs" ,
43+ BuiltinId : Abs ,
9344 Validate : func (args []reflect.Type ) (reflect.Type , error ) {
9445 if len (args ) != 1 {
9546 return anyType , fmt .Errorf ("invalid number of arguments for abs (expected 1, got %d)" , len (args ))
0 commit comments