88var (
99 anyType = reflect .TypeOf (new (interface {})).Elem ()
1010 integerType = reflect .TypeOf (0 )
11+ floatType = reflect .TypeOf (float64 (0 ))
1112)
1213
1314type Function struct {
@@ -21,6 +22,8 @@ type Function struct {
2122const (
2223 Len = iota + 1
2324 Abs
25+ Int
26+ Float
2427)
2528
2629var Builtins = []* Function {
@@ -52,4 +55,40 @@ var Builtins = []*Function{
5255 return anyType , fmt .Errorf ("invalid argument for abs (type %s)" , args [0 ])
5356 },
5457 },
58+ {
59+ Name : "int" ,
60+ BuiltinId : Int ,
61+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
62+ if len (args ) != 1 {
63+ return anyType , fmt .Errorf ("invalid number of arguments for int (expected 1, got %d)" , len (args ))
64+ }
65+ switch args [0 ].Kind () {
66+ case reflect .Interface :
67+ return integerType , nil
68+ case reflect .Float32 , reflect .Float64 , reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 , reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 :
69+ return integerType , nil
70+ case reflect .String :
71+ return integerType , nil
72+ }
73+ return anyType , fmt .Errorf ("invalid argument for int (type %s)" , args [0 ])
74+ },
75+ },
76+ {
77+ Name : "float" ,
78+ BuiltinId : Float ,
79+ Validate : func (args []reflect.Type ) (reflect.Type , error ) {
80+ if len (args ) != 1 {
81+ return anyType , fmt .Errorf ("invalid number of arguments for float (expected 1, got %d)" , len (args ))
82+ }
83+ switch args [0 ].Kind () {
84+ case reflect .Interface :
85+ return floatType , nil
86+ case reflect .Float32 , reflect .Float64 , reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 , reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 :
87+ return floatType , nil
88+ case reflect .String :
89+ return floatType , nil
90+ }
91+ return anyType , fmt .Errorf ("invalid argument for float (type %s)" , args [0 ])
92+ },
93+ },
5594}
0 commit comments