Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor builtin.Function and move builtin funcs to builtin package #402

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import (

type Function struct {
Name string
Id int
Func func(args ...interface{}) (interface{}, error)
Builtin1 func(arg interface{}) interface{}
Types []reflect.Type
Validate func(args []reflect.Type) (reflect.Type, error)
}

const (
Len = iota + 1
Abs
Int
Float
)
var Index map[string]int

func init() {
Index = make(map[string]int)
for i, fn := range Builtins {
Index[fn.Name] = i
}
}

var Builtins = map[int]*Function{
Len: {
Name: "len",
Id: Len,
var Builtins = []*Function{
{
Name: "len",
Builtin1: Len,
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for len (expected 1, got %d)", len(args))
Expand All @@ -35,9 +37,9 @@ var Builtins = map[int]*Function{
return anyType, fmt.Errorf("invalid argument for len (type %s)", args[0])
},
},
Abs: {
Name: "abs",
Id: Abs,
{
Name: "abs",
Builtin1: Abs,
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for abs (expected 1, got %d)", len(args))
Expand All @@ -49,9 +51,9 @@ var Builtins = map[int]*Function{
return anyType, fmt.Errorf("invalid argument for abs (type %s)", args[0])
},
},
Int: {
Name: "int",
Id: Int,
{
Name: "int",
Builtin1: Int,
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for int (expected 1, got %d)", len(args))
Expand All @@ -67,9 +69,9 @@ var Builtins = map[int]*Function{
return anyType, fmt.Errorf("invalid argument for int (type %s)", args[0])
},
},
Float: {
Name: "float",
Id: Float,
{
Name: "float",
Builtin1: Float,
Validate: func(args []reflect.Type) (reflect.Type, error) {
if len(args) != 1 {
return anyType, fmt.Errorf("invalid number of arguments for float (expected 1, got %d)", len(args))
Expand Down
169 changes: 169 additions & 0 deletions builtin/func.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package builtin

import (
"fmt"
"reflect"
"strconv"
)

func Len(x interface{}) interface{} {
v := reflect.ValueOf(x)
switch v.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return v.Len()
default:
panic(fmt.Sprintf("invalid argument for len (type %T)", x))
}
}

func Abs(x interface{}) interface{} {
switch x.(type) {
case float32:
if x.(float32) < 0 {
return -x.(float32)
} else {
return x
}
case float64:
if x.(float64) < 0 {
return -x.(float64)
} else {
return x
}
case int:
if x.(int) < 0 {
return -x.(int)
} else {
return x
}
case int8:
if x.(int8) < 0 {
return -x.(int8)
} else {
return x
}
case int16:
if x.(int16) < 0 {
return -x.(int16)
} else {
return x
}
case int32:
if x.(int32) < 0 {
return -x.(int32)
} else {
return x
}
case int64:
if x.(int64) < 0 {
return -x.(int64)
} else {
return x
}
case uint:
if x.(uint) < 0 {
return -x.(uint)
} else {
return x
}
case uint8:
if x.(uint8) < 0 {
return -x.(uint8)
} else {
return x
}
case uint16:
if x.(uint16) < 0 {
return -x.(uint16)
} else {
return x
}
case uint32:
if x.(uint32) < 0 {
return -x.(uint32)
} else {
return x
}
case uint64:
if x.(uint64) < 0 {
return -x.(uint64)
} else {
return x
}
}
panic(fmt.Sprintf("invalid argument for abs (type %T)", x))
}

func Int(x interface{}) interface{} {
switch x := x.(type) {
case float32:
return int(x)
case float64:
return int(x)
case int:
return x
case int8:
return int(x)
case int16:
return int(x)
case int32:
return int(x)
case int64:
return int(x)
case uint:
return int(x)
case uint8:
return int(x)
case uint16:
return int(x)
case uint32:
return int(x)
case uint64:
return int(x)
case string:
i, err := strconv.Atoi(x)
if err != nil {
panic(fmt.Sprintf("invalid operation: int(%s)", x))
}
return i
default:
panic(fmt.Sprintf("invalid operation: int(%T)", x))
}
}

func Float(x interface{}) interface{} {
switch x := x.(type) {
case float32:
return float64(x)
case float64:
return x
case int:
return float64(x)
case int8:
return float64(x)
case int16:
return float64(x)
case int32:
return float64(x)
case int64:
return float64(x)
case uint:
return float64(x)
case uint8:
return float64(x)
case uint16:
return float64(x)
case uint32:
return float64(x)
case uint64:
return float64(x)
case string:
f, err := strconv.ParseFloat(x, 64)
if err != nil {
panic(fmt.Sprintf("invalid operation: float(%s)", x))
}
return f
default:
panic(fmt.Sprintf("invalid operation: float(%T)", x))
}
}
Loading
Loading