type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Abs ...
|
||||
func Abs(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"abs", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
value := i.Value
|
||||
if value < 0 {
|
||||
value = value * -1
|
||||
}
|
||||
return &object.Integer{Value: value}
|
||||
i := args[0].(*object.Integer)
|
||||
value := i.Value
|
||||
if value < 0 {
|
||||
value = value * -1
|
||||
}
|
||||
return newError("argument to `abs` not supported, got %s", args[0].Type())
|
||||
return &object.Integer{Value: value}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Args ...
|
||||
// Args ...
|
||||
func Args(args ...object.Object) object.Object {
|
||||
if err := typing.Check(
|
||||
"args", args,
|
||||
typing.ExactArgs(0),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
elements := make([]object.Object, len(object.Arguments))
|
||||
for i, arg := range object.Arguments {
|
||||
elements[i] = &object.String{Value: arg}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -9,17 +12,12 @@ import (
|
||||
|
||||
// Assert ...
|
||||
func Assert(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.BOOLEAN_OBJ {
|
||||
return newError("argument #1 to `assert` must be BOOLEAN, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
if args[1].Type() != object.STRING_OBJ {
|
||||
return newError("argument #2 to `assert` must be STRING, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"assert", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.BOOLEAN_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if !args[0].(*object.Boolean).Value {
|
||||
|
||||
@@ -3,18 +3,20 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Bin ...
|
||||
// Bin ...
|
||||
func Bin(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"bin", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
return &object.String{Value: fmt.Sprintf("0b%s", strconv.FormatInt(i.Value, 2))}
|
||||
}
|
||||
return newError("argument to `bin` not supported, got %s", args[0].Type())
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("0b%s", strconv.FormatInt(i.Value, 2))}
|
||||
}
|
||||
|
||||
@@ -1,40 +1,18 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Bool ...
|
||||
func Bool(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
||||
if err := typing.Check(
|
||||
"bool", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
case *object.Null:
|
||||
return &object.Boolean{Value: false}
|
||||
case *object.Boolean:
|
||||
return arg
|
||||
case *object.Integer:
|
||||
if arg.Value == 0 {
|
||||
return &object.Boolean{Value: false}
|
||||
}
|
||||
return &object.Boolean{Value: true}
|
||||
case *object.String:
|
||||
if len(arg.Value) > 0 {
|
||||
return &object.Boolean{Value: true}
|
||||
}
|
||||
return &object.Boolean{Value: false}
|
||||
case *object.Array:
|
||||
if len(arg.Elements) > 0 {
|
||||
return &object.Boolean{Value: true}
|
||||
}
|
||||
return &object.Boolean{Value: false}
|
||||
case *object.Hash:
|
||||
if len(arg.Pairs) > 0 {
|
||||
return &object.Boolean{Value: true}
|
||||
}
|
||||
return &object.Boolean{Value: false}
|
||||
|
||||
default:
|
||||
return newError("argument to `bool` not supported, got=%s", args[0].Type())
|
||||
}
|
||||
return &object.Boolean{Value: args[0].Bool()}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,19 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Chr ...
|
||||
func Chr(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"chr", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
return &object.String{Value: fmt.Sprintf("%c", rune(i.Value))}
|
||||
}
|
||||
return newError("argument to `chr` not supported, got %s", args[0].Type())
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("%c", rune(i.Value))}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Divmod ...
|
||||
func Divmod(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"divmod", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Integer); ok {
|
||||
if b, ok := args[1].(*object.Integer); ok {
|
||||
elements := make([]object.Object, 2)
|
||||
elements[0] = &object.Integer{Value: a.Value / b.Value}
|
||||
elements[1] = &object.Integer{Value: a.Value % b.Value}
|
||||
return &object.Array{Elements: elements}
|
||||
} else {
|
||||
return newError("expected argument #2 to `divmod` to be `int` got=%s", args[1].Type())
|
||||
}
|
||||
} else {
|
||||
return newError("expected argument #1 to `divmod` to be `int` got=%s", args[0].Type())
|
||||
}
|
||||
a := args[0].(*object.Integer)
|
||||
b := args[1].(*object.Integer)
|
||||
elements := make([]object.Object, 2)
|
||||
elements[0] = &object.Integer{Value: a.Value / b.Value}
|
||||
elements[1] = &object.Integer{Value: a.Value % b.Value}
|
||||
return &object.Array{Elements: elements}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Exit ...
|
||||
func Exit(args ...object.Object) object.Object {
|
||||
if err := typing.Check(
|
||||
"exit", args,
|
||||
typing.RangeOfArgs(0, 1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
var status int
|
||||
if len(args) == 1 {
|
||||
if args[0].Type() != object.INTEGER_OBJ {
|
||||
return newError("argument to `exit` must be INTEGER, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
status = int(args[0].(*object.Integer).Value)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -9,22 +12,16 @@ import (
|
||||
|
||||
// FFI ...
|
||||
func FFI(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"ffi", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #1 to `ffi` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
name := arg.Value
|
||||
|
||||
arg, ok = args[1].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #2 to `ffi` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
symbol := arg.Value
|
||||
name := args[0].(*object.String).Value
|
||||
symbol := args[1].(*object.String).Value
|
||||
|
||||
p, err := plugin.Open(fmt.Sprintf("%s.so", name))
|
||||
if err != nil {
|
||||
@@ -36,5 +33,5 @@ func FFI(args ...object.Object) object.Object {
|
||||
return newError("error finding symbol: %s", err)
|
||||
}
|
||||
|
||||
return &object.Builtin{Name: symbol, Fn: v.(func(...object.Object) object.Object)}
|
||||
return &object.Builtin{Name: symbol, Fn: v.(object.BuiltinFunction)}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"sort"
|
||||
)
|
||||
|
||||
@@ -11,19 +12,29 @@ import (
|
||||
|
||||
// Find ...
|
||||
func Find(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"find", args,
|
||||
typing.ExactArgs(2),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
// find substring in string
|
||||
if haystack, ok := args[0].(*object.String); ok {
|
||||
if needle, ok := args[1].(*object.String); ok {
|
||||
index := strings.Index(haystack.Value, needle.Value)
|
||||
return &object.Integer{Value: int64(index)}
|
||||
} else {
|
||||
return newError("expected arg #2 to be `str` got got=%T", args[1])
|
||||
if err := typing.Check(
|
||||
"find", args,
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
} else if haystack, ok := args[0].(*object.Array); ok {
|
||||
|
||||
needle := args[1].(*object.String)
|
||||
index := strings.Index(haystack.Value, needle.Value)
|
||||
return &object.Integer{Value: int64(index)}
|
||||
}
|
||||
|
||||
// find in array
|
||||
if haystack, ok := args[0].(*object.Array); ok {
|
||||
needle := args[1].(object.Comparable)
|
||||
i := sort.Search(len(haystack.Elements), func(i int) bool {
|
||||
return needle.Compare(haystack.Elements[i]) == 0
|
||||
@@ -32,7 +43,10 @@ func Find(args ...object.Object) object.Object {
|
||||
return &object.Integer{Value: int64(i)}
|
||||
}
|
||||
return &object.Integer{Value: -1}
|
||||
} else {
|
||||
return newError("expected arg #1 to be `str` or `array` got got=%T", args[0])
|
||||
}
|
||||
|
||||
return newError(
|
||||
"TypeError: find() expected argument #1 to be `array` or `str` got `%s`",
|
||||
args[0].Type(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// First ...
|
||||
func First(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `first` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"first", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
|
||||
@@ -1,16 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// HashOf ...
|
||||
func HashOf(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"hash", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if hash, ok := args[0].(object.Hashable); ok {
|
||||
return &object.Integer{Value: int64(hash.HashKey().Value)}
|
||||
}
|
||||
return newError("argument #1 to `hash()` is not hashable: %s", args[0].Inspect())
|
||||
|
||||
return newError("TypeError: hash() expected argument #1 to be hashable")
|
||||
}
|
||||
|
||||
@@ -3,18 +3,20 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Hex ...
|
||||
func Hex(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"hex", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
return &object.String{Value: fmt.Sprintf("0x%s", strconv.FormatInt(i.Value, 16))}
|
||||
}
|
||||
return newError("argument to `hex` not supported, got %s", args[0].Type())
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("0x%s", strconv.FormatInt(i.Value, 16))}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,16 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// IdOf ...
|
||||
func IdOf(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"id", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg := args[0]
|
||||
@@ -32,7 +35,7 @@ func IdOf(args ...object.Object) object.Object {
|
||||
return &object.String{Value: fmt.Sprintf("%p", c)}
|
||||
} else if b, ok := arg.(*object.Builtin); ok {
|
||||
return &object.String{Value: fmt.Sprintf("%p", b)}
|
||||
} else {
|
||||
return newError("argument 31 to `id()` unsupported got=%T", arg.Type())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -11,15 +14,17 @@ import (
|
||||
|
||||
// Input ...
|
||||
func Input(args ...object.Object) object.Object {
|
||||
if len(args) > 0 {
|
||||
obj, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError(
|
||||
"argument to `input` not supported, got %s",
|
||||
args[0].Type(),
|
||||
)
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, obj.Value)
|
||||
if err := typing.Check(
|
||||
"input", args,
|
||||
typing.RangeOfArgs(0, 1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if len(args) == 1 {
|
||||
prompt := args[0].(*object.String).Value
|
||||
fmt.Fprintf(os.Stdout, prompt)
|
||||
}
|
||||
|
||||
buffer := bufio.NewReader(os.Stdin)
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import "strconv"
|
||||
|
||||
// Int ...
|
||||
func Int(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
||||
if err := typing.Check(
|
||||
"int", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
@@ -23,9 +29,8 @@ func Int(args ...object.Object) object.Object {
|
||||
if err != nil {
|
||||
return newError("could not parse string to int: %s", err)
|
||||
}
|
||||
|
||||
return &object.Integer{Value: n}
|
||||
default:
|
||||
return newError("argument to `int` not supported, got=%s", args[0].Type())
|
||||
return &object.Integer{}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -8,22 +11,19 @@ import (
|
||||
|
||||
// Join ...
|
||||
func Join(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"join", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.ARRAY_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if arr, ok := args[0].(*object.Array); ok {
|
||||
if sep, ok := args[1].(*object.String); ok {
|
||||
a := make([]string, len(arr.Elements))
|
||||
for i, el := range arr.Elements {
|
||||
a[i] = el.String()
|
||||
}
|
||||
return &object.String{Value: strings.Join(a, sep.Value)}
|
||||
} else {
|
||||
return newError("expected arg #2 to be `str` got got=%T", args[1])
|
||||
}
|
||||
} else {
|
||||
return newError("expected arg #1 to be `array` got got=%T", args[0])
|
||||
arr := args[0].(*object.Array)
|
||||
sep := args[1].(*object.String)
|
||||
a := make([]string, len(arr.Elements))
|
||||
for i, el := range arr.Elements {
|
||||
a[i] = el.String()
|
||||
}
|
||||
return &object.String{Value: strings.Join(a, sep.Value)}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Last ...
|
||||
func Last(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `last` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"last", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
|
||||
@@ -1,23 +1,21 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import "unicode/utf8"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Len ...
|
||||
func Len(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"len", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
case *object.Array:
|
||||
return &object.Integer{Value: int64(len(arg.Elements))}
|
||||
case *object.String:
|
||||
return &object.Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
|
||||
default:
|
||||
return newError("argument to `len` not supported, got %s",
|
||||
args[0].Type())
|
||||
if size, ok := args[0].(object.Sizeable); ok {
|
||||
return &object.Integer{Value: int64(size.Len())}
|
||||
}
|
||||
return newError("TypeError: object of type '%s' has no len()", args[0].Type())
|
||||
}
|
||||
|
||||
@@ -1,18 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import "strings"
|
||||
|
||||
// Lower ...
|
||||
func Lower(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"lower", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if str, ok := args[0].(*object.String); ok {
|
||||
return &object.String{Value: strings.ToLower(str.Value)}
|
||||
}
|
||||
return newError("expected `str` argument to `lower` got=%T", args[0])
|
||||
str := args[0].(*object.String)
|
||||
return &object.String{Value: strings.ToLower(str.Value)}
|
||||
}
|
||||
|
||||
@@ -2,28 +2,30 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Max ...
|
||||
func Max(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"max", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Array); ok {
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
if i, ok := e.(*object.Integer); ok {
|
||||
xs = append(xs, int(i.Value))
|
||||
} else {
|
||||
return newError("item #%d not an `int` got=%s", n, e.Type())
|
||||
}
|
||||
a := args[0].(*object.Array)
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
if i, ok := e.(*object.Integer); ok {
|
||||
xs = append(xs, int(i.Value))
|
||||
} else {
|
||||
return newError("item #%d not an `int` got=%s", n, e.Type())
|
||||
}
|
||||
sort.Ints(xs)
|
||||
return &object.Integer{Value: int64(xs[len(xs)-1])}
|
||||
}
|
||||
return newError("argument #1 to `max` expected to be `array` got=%T", args[0].Type())
|
||||
sort.Ints(xs)
|
||||
return &object.Integer{Value: int64(xs[len(xs)-1])}
|
||||
}
|
||||
|
||||
@@ -2,28 +2,30 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Min ...
|
||||
func Min(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"min", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Array); ok {
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
if i, ok := e.(*object.Integer); ok {
|
||||
xs = append(xs, int(i.Value))
|
||||
} else {
|
||||
return newError("item #%d not an `int` got=%s", n, e.Type())
|
||||
}
|
||||
a := args[0].(*object.Array)
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
if i, ok := e.(*object.Integer); ok {
|
||||
xs = append(xs, int(i.Value))
|
||||
} else {
|
||||
return newError("item #%d not an `int` got=%s", n, e.Type())
|
||||
}
|
||||
sort.Ints(xs)
|
||||
return &object.Integer{Value: int64(xs[0])}
|
||||
}
|
||||
return newError("argument #1 to `min` expected to be `array` got=%T", args[0].Type())
|
||||
sort.Ints(xs)
|
||||
return &object.Integer{Value: int64(xs[0])}
|
||||
}
|
||||
|
||||
@@ -3,18 +3,20 @@ package builtins
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Oct ...
|
||||
func Oct(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"oct", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if i, ok := args[0].(*object.Integer); ok {
|
||||
return &object.String{Value: fmt.Sprintf("0%s", strconv.FormatInt(i.Value, 8))}
|
||||
}
|
||||
return newError("argument to `oct` not supported, got %s", args[0].Type())
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("0%s", strconv.FormatInt(i.Value, 8))}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Ord ...
|
||||
func Ord(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"ord", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if s, ok := args[0].(*object.String); ok {
|
||||
if len(s.Value) == 1 {
|
||||
return &object.Integer{Value: int64(s.Value[0])}
|
||||
}
|
||||
return newError("`ord()` expected a character but got string of length %d", len(s.Value))
|
||||
s := args[0].(*object.String)
|
||||
if len(s.Value) == 1 {
|
||||
return &object.Integer{Value: int64(s.Value[0])}
|
||||
}
|
||||
return newError("argument to `ord` not supported, got %s", args[0].Type())
|
||||
return newError(
|
||||
"TypeError: ord() expected a single character `str` got=%s",
|
||||
s.Inspect(),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Pop ...
|
||||
func Pop(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `pop` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"pop", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
if length == 0 {
|
||||
return newError("cannot pop from an empty array")
|
||||
return newError("IndexError: pop from an empty array")
|
||||
}
|
||||
|
||||
element := arr.Elements[length-1]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
func pow(x, y int64) int64 {
|
||||
p := int64(1)
|
||||
@@ -16,19 +19,16 @@ func pow(x, y int64) int64 {
|
||||
|
||||
// Pow ...
|
||||
func Pow(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"pow", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if x, ok := args[0].(*object.Integer); ok {
|
||||
if y, ok := args[1].(*object.Integer); ok {
|
||||
value := pow(x.Value, y.Value)
|
||||
return &object.Integer{Value: value}
|
||||
} else {
|
||||
return newError("expected argument #2 to `pow` to be `int` got=%s", args[1].Type())
|
||||
}
|
||||
} else {
|
||||
return newError("expected argument #1 to `pow` to be `int` got=%s", args[0].Type())
|
||||
}
|
||||
x := args[0].(*object.Integer)
|
||||
y := args[1].(*object.Integer)
|
||||
value := pow(x.Value, y.Value)
|
||||
return &object.Integer{Value: value}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,23 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Print ...
|
||||
func Print(args ...object.Object) object.Object {
|
||||
for _, arg := range args {
|
||||
fmt.Println(arg.String())
|
||||
if err := typing.Check(
|
||||
"print", args,
|
||||
typing.MinimumArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
fmt.Println(args[0].String())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,28 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Push ...
|
||||
func Push(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `push` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"push", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
newElements := make([]object.Object, length+1)
|
||||
copy(newElements, arr.Elements)
|
||||
if immutable, ok := args[1].(object.Immutable); ok {
|
||||
newElements[length] = immutable.Clone()
|
||||
} else {
|
||||
newElements[length] = args[1]
|
||||
}
|
||||
|
||||
return &object.Array{Elements: newElements}
|
||||
newArray := arr.Copy()
|
||||
newArray.Append(args[1])
|
||||
return newArray
|
||||
}
|
||||
|
||||
@@ -1,27 +1,25 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Read ...
|
||||
func Read(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"read", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument to `read` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
|
||||
filename := arg.Value
|
||||
data, err := os.ReadFile(filename)
|
||||
filename := args[0].(*object.String).Value
|
||||
data, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return newError("error reading file: %s", err)
|
||||
return newError("IOError: error reading from file %s: %s", filename, err)
|
||||
}
|
||||
|
||||
return &object.String{Value: string(data)}
|
||||
|
||||
@@ -1,25 +1,22 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Rest ...
|
||||
func Rest(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != object.ARRAY_OBJ {
|
||||
return newError("argument to `rest` must be array, got %s",
|
||||
args[0].Type())
|
||||
if err := typing.Check(
|
||||
"rest", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arr := args[0].(*object.Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
newElements := make([]object.Object, length-1, length-1)
|
||||
copy(newElements, arr.Elements[1:length])
|
||||
return &object.Array{Elements: newElements}
|
||||
}
|
||||
|
||||
return nil
|
||||
newArray := arr.Copy()
|
||||
newArray.PopLeft()
|
||||
return newArray
|
||||
}
|
||||
|
||||
@@ -2,19 +2,21 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Reversed ...
|
||||
func Reversed(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"reversed", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Array); ok {
|
||||
newArray := a.Copy()
|
||||
newArray.Reverse()
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `reversed` expected to be `array` got=%T", args[0].Type())
|
||||
arr := args[0].(*object.Array)
|
||||
newArray := arr.Copy()
|
||||
newArray.Reverse()
|
||||
return newArray
|
||||
}
|
||||
|
||||
@@ -2,20 +2,22 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Sorted ...
|
||||
func Sorted(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"sort", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Array); ok {
|
||||
newArray := a.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `sorted` expected to be `array` got=%T", args[0].Type())
|
||||
arr := args[0].(*object.Array)
|
||||
newArray := arr.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -8,31 +11,25 @@ import (
|
||||
|
||||
// Split ...
|
||||
func Split(args ...object.Object) object.Object {
|
||||
if len(args) < 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"split", args,
|
||||
typing.RangeOfArgs(1, 2),
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if obj, ok := args[0].(*object.String); ok {
|
||||
var sep string
|
||||
var sep string
|
||||
s := args[0].(*object.String).Value
|
||||
|
||||
s := obj.Value
|
||||
|
||||
if len(args) == 2 {
|
||||
if obj, ok := args[1].(*object.String); ok {
|
||||
sep = obj.Value
|
||||
} else {
|
||||
return newError("expected arg #2 to be `str` got=%T", args[1])
|
||||
}
|
||||
}
|
||||
|
||||
tokens := strings.Split(s, sep)
|
||||
elements := make([]object.Object, len(tokens))
|
||||
for i, token := range tokens {
|
||||
elements[i] = &object.String{Value: token}
|
||||
}
|
||||
return &object.Array{Elements: elements}
|
||||
} else {
|
||||
return newError("expected arg #1 to be `str` got got=%T", args[0])
|
||||
if len(args) == 2 {
|
||||
sep = args[1].(*object.String).Value
|
||||
}
|
||||
|
||||
tokens := strings.Split(s, sep)
|
||||
elements := make([]object.Object, len(tokens))
|
||||
for i, token := range tokens {
|
||||
elements[i] = &object.String{Value: token}
|
||||
}
|
||||
return &object.Array{Elements: elements}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Str ...
|
||||
func Str(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
||||
if err := typing.Check(
|
||||
"str", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg, ok := args[0].(fmt.Stringer)
|
||||
if !ok {
|
||||
return newError("argument to `str` not supported, got %s", args[0].Type())
|
||||
}
|
||||
|
||||
return &object.String{Value: arg.String()}
|
||||
return &object.String{Value: args[0].String()}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,17 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// TypeOf ...
|
||||
// TypeOf ...
|
||||
func TypeOf(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
||||
if err := typing.Check(
|
||||
"type", args,
|
||||
typing.ExactArgs(1),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
return &object.String{Value: string(args[0].Type())}
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import "strings"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Upper ...
|
||||
func Upper(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"upper", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if str, ok := args[0].(*object.String); ok {
|
||||
return &object.String{Value: strings.ToUpper(str.Value)}
|
||||
}
|
||||
return newError("expected `str` argument to `upper` got=%T", args[0])
|
||||
return &object.String{Value: strings.ToUpper(args[0].(*object.String).Value)}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,27 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
|
||||
import (
|
||||
"os"
|
||||
"io/ioutil"
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// Write ...
|
||||
func Write(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"write", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
arg, ok := args[0].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #1 to `write` expected to be `str` got=%T", args[0].Type())
|
||||
}
|
||||
filename := arg.Value
|
||||
filename := args[0].(*object.String).Value
|
||||
data := []byte(args[1].(*object.String).Value)
|
||||
|
||||
arg, ok = args[1].(*object.String)
|
||||
if !ok {
|
||||
return newError("argument #2 to `write` expected to be `str` got=%T", args[1].Type())
|
||||
}
|
||||
data := []byte(arg.Value)
|
||||
|
||||
err := os.WriteFile(filename, data, 0755)
|
||||
err := ioutil.WriteFile(filename, data, 0755)
|
||||
if err != nil {
|
||||
return newError("error writing file: %s", err)
|
||||
return newError("IOError: error writing file %s: %s", filename, err)
|
||||
}
|
||||
|
||||
return &object.Null{}
|
||||
|
||||
Reference in New Issue
Block a user