Skip to content

Commit 2eb6a03

Browse files
committed
Refactor builtin funcs
1 parent 9ca99f0 commit 2eb6a03

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

builtin/builtin.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ import (
55
"reflect"
66
)
77

8-
var (
9-
anyType = reflect.TypeOf(new(interface{})).Elem()
10-
integerType = reflect.TypeOf(0)
11-
floatType = reflect.TypeOf(float64(0))
12-
)
13-
148
type Function struct {
159
Name string
10+
Id int
1611
Func func(args ...interface{}) (interface{}, error)
17-
Opcode int
1812
Types []reflect.Type
1913
Validate func(args []reflect.Type) (reflect.Type, error)
2014
}
@@ -28,8 +22,8 @@ const (
2822

2923
var Builtins = map[int]*Function{
3024
Len: {
31-
Name: "len",
32-
Opcode: Len,
25+
Name: "len",
26+
Id: Len,
3327
Validate: func(args []reflect.Type) (reflect.Type, error) {
3428
if len(args) != 1 {
3529
return anyType, fmt.Errorf("invalid number of arguments for len (expected 1, got %d)", len(args))
@@ -42,8 +36,8 @@ var Builtins = map[int]*Function{
4236
},
4337
},
4438
Abs: {
45-
Name: "abs",
46-
Opcode: Abs,
39+
Name: "abs",
40+
Id: Abs,
4741
Validate: func(args []reflect.Type) (reflect.Type, error) {
4842
if len(args) != 1 {
4943
return anyType, fmt.Errorf("invalid number of arguments for abs (expected 1, got %d)", len(args))
@@ -56,8 +50,8 @@ var Builtins = map[int]*Function{
5650
},
5751
},
5852
Int: {
59-
Name: "int",
60-
Opcode: Int,
53+
Name: "int",
54+
Id: Int,
6155
Validate: func(args []reflect.Type) (reflect.Type, error) {
6256
if len(args) != 1 {
6357
return anyType, fmt.Errorf("invalid number of arguments for int (expected 1, got %d)", len(args))
@@ -74,8 +68,8 @@ var Builtins = map[int]*Function{
7468
},
7569
},
7670
Float: {
77-
Name: "float",
78-
Opcode: Float,
71+
Name: "float",
72+
Id: Float,
7973
Validate: func(args []reflect.Type) (reflect.Type, error) {
8074
if len(args) != 1 {
8175
return anyType, fmt.Errorf("invalid number of arguments for float (expected 1, got %d)", len(args))
@@ -92,10 +86,3 @@ var Builtins = map[int]*Function{
9286
},
9387
},
9488
}
95-
96-
func kind(t reflect.Type) reflect.Kind {
97-
if t == nil {
98-
return reflect.Invalid
99-
}
100-
return t.Kind()
101-
}

builtin/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package builtin
2+
3+
import "reflect"
4+
5+
var (
6+
anyType = reflect.TypeOf(new(interface{})).Elem()
7+
integerType = reflect.TypeOf(0)
8+
floatType = reflect.TypeOf(float64(0))
9+
)
10+
11+
func kind(t reflect.Type) reflect.Kind {
12+
if t == nil {
13+
return reflect.Invalid
14+
}
15+
return t.Kind()
16+
}

compiler/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ func (c *compiler) CallNode(node *ast.CallNode) {
563563
c.compile(arg)
564564
}
565565
if node.Func != nil {
566-
if node.Func.Opcode > 0 {
567-
c.emit(OpBuiltin, node.Func.Opcode)
566+
if node.Func.Id > 0 {
567+
c.emit(OpBuiltin, node.Func.Id)
568568
return
569569
}
570570
switch len(node.Arguments) {

0 commit comments

Comments
 (0)