Skip to content

Commit

Permalink
add 'rand' module (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
d5 authored Feb 9, 2019
1 parent 9782d87 commit 2b517f3
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 54 deletions.
80 changes: 80 additions & 0 deletions compiler/stdlib/func_typedefs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,60 @@ func FuncARI(fn func() int) *objects.UserFunction {
}
}

// FuncARI64 transform a function of 'func() int64' signature
// into a user function object.
func FuncARI64(fn func() int64) *objects.UserFunction {
return &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 0 {
return nil, objects.ErrWrongNumArguments
}

return &objects.Int{Value: fn()}, nil
},
}
}

// FuncAI64RI64 transform a function of 'func(int64) int64' signature
// into a user function object.
func FuncAI64RI64(fn func(int64) int64) *objects.UserFunction {
return &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}

i1, ok := objects.ToInt64(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}

return &objects.Int{Value: fn(i1)}, nil
},
}
}

// FuncAI64R transform a function of 'func(int64)' signature
// into a user function object.
func FuncAI64R(fn func(int64)) *objects.UserFunction {
return &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}

i1, ok := objects.ToInt64(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}

fn(i1)

return objects.UndefinedValue, nil
},
}
}

// FuncARB transform a function of 'func() bool' signature
// into a user function object.
func FuncARB(fn func() bool) *objects.UserFunction {
Expand Down Expand Up @@ -175,6 +229,32 @@ func FuncARIsE(fn func() ([]int, error)) *objects.UserFunction {
}
}

// FuncAIRIs transform a function of 'func(int) []int' signature
// into a user function object.
func FuncAIRIs(fn func(int) []int) *objects.UserFunction {
return &objects.UserFunction{
Value: func(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 1 {
return nil, objects.ErrWrongNumArguments
}

i1, ok := objects.ToInt(args[0])
if !ok {
return nil, objects.ErrInvalidTypeConversion
}

res := fn(i1)

arr := &objects.Array{}
for _, v := range res {
arr.Value = append(arr.Value, &objects.Int{Value: int64(v)})
}

return arr, nil
},
}
}

// FuncAFRF transform a function of 'func(float64) float64' signature
// into a user function object.
func FuncAFRF(fn func(float64) float64) *objects.UserFunction {
Expand Down
Loading

0 comments on commit 2b517f3

Please sign in to comment.