type checking and error handling for builtins improved.
This commit is contained in:
@@ -5,7 +5,7 @@ steps:
|
||||
- name: build
|
||||
image: golang:latest
|
||||
commands:
|
||||
- make test
|
||||
- go test -v -short -cover -coverprofile=coverage.txt ./...
|
||||
|
||||
- name: coverage
|
||||
image: plugins/codecov
|
||||
|
||||
@@ -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 {
|
||||
i := args[0].(*object.Integer)
|
||||
value := i.Value
|
||||
if value < 0 {
|
||||
value = value * -1
|
||||
}
|
||||
return &object.Integer{Value: value}
|
||||
}
|
||||
return newError("argument to `abs` not supported, got %s", args[0].Type())
|
||||
}
|
||||
|
||||
@@ -1,9 +1,19 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
// 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 ...
|
||||
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 {
|
||||
i := args[0].(*object.Integer)
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
i := args[0].(*object.Integer)
|
||||
return &object.String{Value: fmt.Sprintf("%c", rune(i.Value))}
|
||||
}
|
||||
return newError("argument to `chr` not supported, got %s", args[0].Type())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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}
|
||||
} 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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
if err := typing.Check(
|
||||
"find", args,
|
||||
typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
needle := args[1].(*object.String)
|
||||
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])
|
||||
}
|
||||
} else if haystack, ok := args[0].(*object.Array); ok {
|
||||
|
||||
// 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 {
|
||||
i := args[0].(*object.Integer)
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
)
|
||||
if err := typing.Check(
|
||||
"input", args,
|
||||
typing.RangeOfArgs(0, 1),
|
||||
typing.WithTypes(object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, obj.Value)
|
||||
|
||||
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 {
|
||||
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)}
|
||||
} 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])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
str := args[0].(*object.String)
|
||||
return &object.String{Value: strings.ToLower(str.Value)}
|
||||
}
|
||||
return newError("expected `str` argument to `lower` got=%T", args[0])
|
||||
}
|
||||
|
||||
@@ -2,17 +2,21 @@ 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 {
|
||||
a := args[0].(*object.Array)
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
@@ -24,6 +28,4 @@ func Max(args ...object.Object) object.Object {
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -2,17 +2,21 @@ 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 {
|
||||
a := args[0].(*object.Array)
|
||||
// TODO: Make this more generic
|
||||
xs := make([]int, len(a.Elements))
|
||||
for n, e := range a.Elements {
|
||||
@@ -24,6 +28,4 @@ func Min(args ...object.Object) object.Object {
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
i := args[0].(*object.Integer)
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
s := args[0].(*object.String)
|
||||
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))
|
||||
}
|
||||
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 {
|
||||
x := args[0].(*object.Integer)
|
||||
y := args[1].(*object.Integer)
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
arr := args[0].(*object.Array)
|
||||
newArray := arr.Copy()
|
||||
newArray.Reverse()
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `reversed` expected to be `array` got=%T", args[0].Type())
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
arr := args[0].(*object.Array)
|
||||
newArray := arr.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `sorted` expected to be `array` got=%T", args[0].Type())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -8,22 +11,19 @@ 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
|
||||
|
||||
s := obj.Value
|
||||
s := args[0].(*object.String).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])
|
||||
}
|
||||
sep = args[1].(*object.String).Value
|
||||
}
|
||||
|
||||
tokens := strings.Split(s, sep)
|
||||
@@ -32,7 +32,4 @@ func Split(args ...object.Object) object.Object {
|
||||
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])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 ...
|
||||
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{}
|
||||
|
||||
@@ -398,24 +398,24 @@ func TestBuiltinFunctions(t *testing.T) {
|
||||
{`len("")`, 0},
|
||||
{`len("four")`, 4},
|
||||
{`len("hello world")`, 11},
|
||||
{`len(1)`, errors.New("argument to `len` not supported, got int")},
|
||||
{`len("one", "two")`, errors.New("wrong number of arguments. got=2, want=1")},
|
||||
{`len(1)`, errors.New("TypeError: object of type 'int' has no len()")},
|
||||
{`len("one", "two")`, errors.New("TypeError: len() takes exactly 1 argument (2 given)")},
|
||||
{`len("∑")`, 1},
|
||||
{`len([1, 2, 3])`, 3},
|
||||
{`len([])`, 0},
|
||||
{`first([1, 2, 3])`, 1},
|
||||
{`first([])`, nil},
|
||||
{`first(1)`, errors.New("argument to `first` must be array, got int")},
|
||||
{`first(1)`, errors.New("TypeError: first() expected argument #1 to be `array` got `int`")},
|
||||
{`last([1, 2, 3])`, 3},
|
||||
{`last([])`, nil},
|
||||
{`last(1)`, errors.New("argument to `last` must be array, got int")},
|
||||
{`last(1)`, errors.New("TypeError: last() expected argument #1 to be `array` got `int`")},
|
||||
{`rest([1, 2, 3])`, []int{2, 3}},
|
||||
{`rest([])`, nil},
|
||||
{`push([], 1)`, []int{1}},
|
||||
{`push(1, 1)`, errors.New("argument to `push` must be array, got int")},
|
||||
{`push(1, 1)`, errors.New("TypeError: push() expected argument #1 to be `array` got `int`")},
|
||||
{`print("Hello World")`, nil},
|
||||
{`input()`, ""},
|
||||
{`pop([])`, errors.New("cannot pop from an empty array")},
|
||||
{`pop([])`, errors.New("IndexError: pop from an empty array")},
|
||||
{`pop([1])`, 1},
|
||||
{`bool(1)`, true},
|
||||
{`bool(0)`, false},
|
||||
|
||||
@@ -14,6 +14,36 @@ func (ao *Array) Type() ObjectType {
|
||||
return ARRAY_OBJ
|
||||
}
|
||||
|
||||
func (ao *Array) Bool() bool {
|
||||
return len(ao.Elements) > 0
|
||||
}
|
||||
|
||||
func (a *Array) PopLeft() Object {
|
||||
if len(a.Elements) > 0 {
|
||||
e := a.Elements[0]
|
||||
a.Elements = a.Elements[1:]
|
||||
return e
|
||||
}
|
||||
return &Null{}
|
||||
}
|
||||
|
||||
func (a *Array) PopRight() Object {
|
||||
if len(a.Elements) > 0 {
|
||||
e := a.Elements[(len(a.Elements) - 1)]
|
||||
a.Elements = a.Elements[:(len(a.Elements) - 1)]
|
||||
return e
|
||||
}
|
||||
return &Null{}
|
||||
}
|
||||
|
||||
func (a *Array) Prepend(obj Object) {
|
||||
a.Elements = append([]Object{obj}, a.Elements...)
|
||||
}
|
||||
|
||||
func (a *Array) Append(obj Object) {
|
||||
a.Elements = append(a.Elements, obj)
|
||||
}
|
||||
|
||||
func (ao *Array) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -32,9 +62,9 @@ func (ao *Array) String() string {
|
||||
return ao.Inspect()
|
||||
}
|
||||
|
||||
func (a *Array) Less(i, j int) bool {
|
||||
if cmp, ok := a.Elements[i].(Comparable); ok {
|
||||
return cmp.Compare(a.Elements[j]) == -1
|
||||
func (ao *Array) Less(i, j int) bool {
|
||||
if cmp, ok := ao.Elements[i].(Comparable); ok {
|
||||
return cmp.Compare(ao.Elements[j]) == -1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -8,6 +8,10 @@ type Boolean struct {
|
||||
Value bool
|
||||
}
|
||||
|
||||
func (b *Boolean) Bool() bool {
|
||||
return b.Value
|
||||
}
|
||||
|
||||
func (b *Boolean) Type() ObjectType {
|
||||
return BOOLEAN_OBJ
|
||||
}
|
||||
|
||||
@@ -7,12 +7,18 @@ type Builtin struct {
|
||||
Fn BuiltinFunction
|
||||
}
|
||||
|
||||
func (b *Builtin) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Builtin) Type() ObjectType {
|
||||
return BUILTIN_OBJ
|
||||
}
|
||||
|
||||
func (b *Builtin) Inspect() string {
|
||||
return fmt.Sprintf("<built-in function %s>", b.Name)
|
||||
}
|
||||
|
||||
func (b *Builtin) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
|
||||
@@ -11,12 +11,18 @@ type CompiledFunction struct {
|
||||
NumParameters int
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) Type() ObjectType {
|
||||
return COMPILED_FUNCTION_OBJ
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) Inspect() string {
|
||||
return fmt.Sprintf("CompiledFunction[%p]", cf)
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) String() string {
|
||||
return cf.Inspect()
|
||||
}
|
||||
@@ -26,12 +32,18 @@ type Closure struct {
|
||||
Free []Object
|
||||
}
|
||||
|
||||
func (c *Closure) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *Closure) Type() ObjectType {
|
||||
return CLOSURE_OBJ
|
||||
}
|
||||
|
||||
func (c *Closure) Inspect() string {
|
||||
return fmt.Sprintf("Closure[%p]", c)
|
||||
}
|
||||
|
||||
func (c *Closure) String() string {
|
||||
return c.Inspect()
|
||||
}
|
||||
|
||||
@@ -4,15 +4,22 @@ type Error struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *Error) Bool() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *Error) Type() ObjectType {
|
||||
return ERROR_OBJ
|
||||
}
|
||||
|
||||
func (e *Error) Inspect() string {
|
||||
return "Error: " + e.Message
|
||||
}
|
||||
|
||||
func (e *Error) Clone() Object {
|
||||
return &Error{Message: e.Message}
|
||||
}
|
||||
|
||||
func (e *Error) String() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
@@ -12,9 +12,14 @@ type Function struct {
|
||||
Env *Environment
|
||||
}
|
||||
|
||||
func (f *Function) Bool() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *Function) Type() ObjectType {
|
||||
return FUNCTION_OBJ
|
||||
}
|
||||
|
||||
func (f *Function) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -32,6 +37,7 @@ func (f *Function) Inspect() string {
|
||||
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (f *Function) String() string {
|
||||
return f.Inspect()
|
||||
}
|
||||
@@ -40,12 +46,18 @@ type ReturnValue struct {
|
||||
Value Object
|
||||
}
|
||||
|
||||
func (rv *ReturnValue) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (rv *ReturnValue) Type() ObjectType {
|
||||
return RETURN_VALUE_OBJ
|
||||
}
|
||||
|
||||
func (rv *ReturnValue) Inspect() string {
|
||||
return rv.Value.Inspect()
|
||||
}
|
||||
|
||||
func (rv *ReturnValue) String() string {
|
||||
return rv.Inspect()
|
||||
}
|
||||
|
||||
@@ -44,6 +44,14 @@ type Hash struct {
|
||||
Pairs map[HashKey]HashPair
|
||||
}
|
||||
|
||||
func (h *Hash) Len() int {
|
||||
return len(h.Pairs)
|
||||
}
|
||||
|
||||
func (h *Hash) Bool() bool {
|
||||
return len(h.Pairs) > 0
|
||||
}
|
||||
|
||||
func (h *Hash) Type() ObjectType {
|
||||
return HASH_OBJ
|
||||
}
|
||||
@@ -62,6 +70,7 @@ func (h *Hash) Inspect() string {
|
||||
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (h *Hash) String() string {
|
||||
return h.Inspect()
|
||||
}
|
||||
|
||||
@@ -6,6 +6,10 @@ type Integer struct {
|
||||
Value int64
|
||||
}
|
||||
|
||||
func (i *Integer) Bool() bool {
|
||||
return i.Value != 0
|
||||
}
|
||||
|
||||
func (i *Integer) Type() ObjectType {
|
||||
return INTEGER_OBJ
|
||||
}
|
||||
|
||||
@@ -2,6 +2,10 @@ package object
|
||||
|
||||
type Null struct{}
|
||||
|
||||
func (n *Null) Bool() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *Null) Type() ObjectType {
|
||||
return NULL_OBJ
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package object
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Type represents the type of an object
|
||||
type ObjectType string
|
||||
|
||||
@@ -25,6 +27,13 @@ type Comparable interface {
|
||||
Compare(other Object) int
|
||||
}
|
||||
|
||||
// Sizeable is the interface for returning the size of an Object.
|
||||
// Object(s) that have a valid size must implement this interface and the
|
||||
// Len() method.
|
||||
type Sizeable interface {
|
||||
Len() int
|
||||
}
|
||||
|
||||
// Immutable is the interface for all immutable objects which must implement
|
||||
// the Clone() method used by binding names to values.
|
||||
type Immutable interface {
|
||||
@@ -34,8 +43,9 @@ type Immutable interface {
|
||||
// Object represents a value and implementations are expected to implement
|
||||
// `Type()` and `Inspect()` functions
|
||||
type Object interface {
|
||||
fmt.Stringer
|
||||
Type() ObjectType
|
||||
String() string
|
||||
Bool() bool
|
||||
Inspect() string
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
package object
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type String struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (s *String) Len() int {
|
||||
return utf8.RuneCountInString(s.Value)
|
||||
}
|
||||
|
||||
func (s *String) Bool() bool {
|
||||
return s.Value != ""
|
||||
}
|
||||
|
||||
func (s *String) Type() ObjectType {
|
||||
return STRING_OBJ
|
||||
}
|
||||
|
||||
56
typing/typing.go
Normal file
56
typing/typing.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package typing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"monkey/object"
|
||||
)
|
||||
|
||||
type CheckFunc func(name string, args []object.Object) error
|
||||
|
||||
func Check(name string, args []object.Object, checks ...CheckFunc) error {
|
||||
for _, check := range checks {
|
||||
if err := check(name, args); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExactArgs(n int) CheckFunc {
|
||||
return func(name string, args []object.Object) error {
|
||||
if len(args) != n {
|
||||
return fmt.Errorf("TypeError: %s() takes exactly %d argument (%d given)", name, n, len(args))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func MinimumArgs(n int) CheckFunc {
|
||||
return func(name string, args []object.Object) error {
|
||||
if len(args) < n {
|
||||
return fmt.Errorf("TypeError: %s() takes a minimum %d arguments (%d given)", name, n, len(args))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func RangeOfArgs(n, m int) CheckFunc {
|
||||
return func(name string, args []object.Object) error {
|
||||
if len(args) < n || len(args) > m {
|
||||
return fmt.Errorf("TypeError: %s() takes at least %d arguments at most %d (%d given)", name, n, m, len(args))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithTypes(types ...object.ObjectType) CheckFunc {
|
||||
return func(name string, args []object.Object) error {
|
||||
for i, t := range types {
|
||||
if i < len(args) && args[i].Type() != t {
|
||||
return fmt.Errorf("TypeError: %s() expected argument #%d to be `%s` got `%s`", name, i+1, t, args[i].Type())
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -709,12 +709,12 @@ func TestBuiltinFunctions(t *testing.T) {
|
||||
{
|
||||
`len(1)`,
|
||||
&object.Error{
|
||||
Message: "argument to `len` not supported, got int",
|
||||
Message: "TypeError: object of type 'int' has no len()",
|
||||
},
|
||||
},
|
||||
{`len("one", "two")`,
|
||||
&object.Error{
|
||||
Message: "wrong number of arguments. got=2, want=1",
|
||||
Message: "TypeError: len() takes exactly 1 argument (2 given)",
|
||||
},
|
||||
},
|
||||
{`len([1, 2, 3])`, 3},
|
||||
@@ -724,27 +724,27 @@ func TestBuiltinFunctions(t *testing.T) {
|
||||
{`first([])`, Null},
|
||||
{`first(1)`,
|
||||
&object.Error{
|
||||
Message: "argument to `first` must be array, got int",
|
||||
Message: "TypeError: first() expected argument #1 to be `array` got `int`",
|
||||
},
|
||||
},
|
||||
{`last([1, 2, 3])`, 3},
|
||||
{`last([])`, Null},
|
||||
{`last(1)`,
|
||||
&object.Error{
|
||||
Message: "argument to `last` must be array, got int",
|
||||
Message: "TypeError: last() expected argument #1 to be `array` got `int`",
|
||||
},
|
||||
},
|
||||
{`rest([1, 2, 3])`, []int{2, 3}},
|
||||
{`rest([])`, Null},
|
||||
{`rest([])`, []int{}},
|
||||
{`push([], 1)`, []int{1}},
|
||||
{`push(1, 1)`,
|
||||
&object.Error{
|
||||
Message: "argument to `push` must be array, got int",
|
||||
Message: "TypeError: push() expected argument #1 to be `array` got `int`",
|
||||
},
|
||||
},
|
||||
{`input()`, ""},
|
||||
{`pop([])`, &object.Error{
|
||||
Message: "cannot pop from an empty array",
|
||||
Message: "IndexError: pop from an empty array",
|
||||
},
|
||||
},
|
||||
{`pop([1])`, 1},
|
||||
|
||||
Reference in New Issue
Block a user