seperated builtins
This commit is contained in:
29
object/builtin_assert.go
Normal file
29
object/builtin_assert.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Assert ...
|
||||
func Assert(args ...Object) Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != BOOLEAN_OBJ {
|
||||
return newError("argument #1 to `assert` must be BOOLEAN, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
if args[1].Type() != STRING_OBJ {
|
||||
return newError("argument #2 to `assert` must be STRING, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
if !args[0].(*Boolean).Value {
|
||||
fmt.Printf("Assertion Error: %s", args[1].(*String).Value)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
17
object/builtin_exit.go
Normal file
17
object/builtin_exit.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package object
|
||||
|
||||
import "os"
|
||||
|
||||
// Exit ...
|
||||
func Exit(args ...Object) Object {
|
||||
if len(args) == 1 {
|
||||
if args[0].Type() != INTEGER_OBJ {
|
||||
return newError("argument to `exit` must be INTEGER, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
os.Exit(int(args[0].(*Integer).Value))
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
20
object/builtin_first.go
Normal file
20
object/builtin_first.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package object
|
||||
|
||||
// First ...
|
||||
func First(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `first` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
if len(arr.Elements) > 0 {
|
||||
return arr.Elements[0]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
30
object/builtin_input.go
Normal file
30
object/builtin_input.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Input ...
|
||||
func Input(args ...Object) Object {
|
||||
if len(args) > 0 {
|
||||
obj, ok := args[0].(*String)
|
||||
if !ok {
|
||||
return newError(
|
||||
"argument to `input` not supported, got %s",
|
||||
args[0].Type(),
|
||||
)
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, obj.Value)
|
||||
}
|
||||
|
||||
buffer := bufio.NewReader(os.Stdin)
|
||||
|
||||
line, _, err := buffer.ReadLine()
|
||||
if err != nil && err != io.EOF {
|
||||
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
||||
}
|
||||
return &String{Value: string(line)}
|
||||
}
|
||||
21
object/builtin_last.go
Normal file
21
object/builtin_last.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package object
|
||||
|
||||
// Last ...
|
||||
func Last(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `last` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
return arr.Elements[length-1]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
21
object/builtin_len.go
Normal file
21
object/builtin_len.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package object
|
||||
|
||||
import "unicode/utf8"
|
||||
|
||||
// Len ...
|
||||
func Len(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
case *Array:
|
||||
return &Integer{Value: int64(len(arg.Elements))}
|
||||
case *String:
|
||||
return &Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
|
||||
default:
|
||||
return newError("argument to `len` not supported, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
}
|
||||
25
object/builtin_pop.go
Normal file
25
object/builtin_pop.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package object
|
||||
|
||||
// Pop ...
|
||||
func Pop(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `pop` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
if length == 0 {
|
||||
return newError("cannot pop from an empty array")
|
||||
}
|
||||
|
||||
element := arr.Elements[length-1]
|
||||
arr.Elements = arr.Elements[:length-1]
|
||||
|
||||
return element
|
||||
}
|
||||
12
object/builtin_print.go
Normal file
12
object/builtin_print.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package object
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Print ...
|
||||
func Print(args ...Object) Object {
|
||||
for _, arg := range args {
|
||||
fmt.Println(arg.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
26
object/builtin_push.go
Normal file
26
object/builtin_push.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package object
|
||||
|
||||
// Push ...
|
||||
func Push(args ...Object) Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `push` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
newElements := make([]Object, length+1)
|
||||
copy(newElements, arr.Elements)
|
||||
if immutable, ok := args[1].(Immutable); ok {
|
||||
newElements[length] = immutable.Clone()
|
||||
} else {
|
||||
newElements[length] = args[1]
|
||||
}
|
||||
|
||||
return &Array{Elements: newElements}
|
||||
}
|
||||
23
object/builtin_rest.go
Normal file
23
object/builtin_rest.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package object
|
||||
|
||||
// Rest ...
|
||||
func Rest(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `rest` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
newElements := make([]Object, length-1, length-1)
|
||||
copy(newElements, arr.Elements[1:length])
|
||||
return &Array{Elements: newElements}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Builtins ...
|
||||
@@ -41,199 +37,3 @@ func init() {
|
||||
func newError(format string, a ...interface{}) *Error {
|
||||
return &Error{Message: fmt.Sprintf(format, a...)}
|
||||
}
|
||||
|
||||
// Len ...
|
||||
func Len(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
|
||||
switch arg := args[0].(type) {
|
||||
case *Array:
|
||||
return &Integer{Value: int64(len(arg.Elements))}
|
||||
case *String:
|
||||
return &Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
|
||||
default:
|
||||
return newError("argument to `len` not supported, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
}
|
||||
|
||||
// Input ...
|
||||
func Input(args ...Object) Object {
|
||||
if len(args) > 0 {
|
||||
obj, ok := args[0].(*String)
|
||||
if !ok {
|
||||
return newError(
|
||||
"argument to `input` not supported, got %s",
|
||||
args[0].Type(),
|
||||
)
|
||||
}
|
||||
fmt.Fprintf(os.Stdout, obj.Value)
|
||||
}
|
||||
|
||||
buffer := bufio.NewReader(os.Stdin)
|
||||
|
||||
line, _, err := buffer.ReadLine()
|
||||
if err != nil && err != io.EOF {
|
||||
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
||||
}
|
||||
return &String{Value: string(line)}
|
||||
}
|
||||
|
||||
// Print ...
|
||||
func Print(args ...Object) Object {
|
||||
for _, arg := range args {
|
||||
fmt.Println(arg.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// First ...
|
||||
func First(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `first` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
if len(arr.Elements) > 0 {
|
||||
return arr.Elements[0]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Last ...
|
||||
func Last(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `last` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
return arr.Elements[length-1]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Rest ...
|
||||
func Rest(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `rest` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
if length > 0 {
|
||||
newElements := make([]Object, length-1, length-1)
|
||||
copy(newElements, arr.Elements[1:length])
|
||||
return &Array{Elements: newElements}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Push ...
|
||||
func Push(args ...Object) Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `push` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
newElements := make([]Object, length+1)
|
||||
copy(newElements, arr.Elements)
|
||||
if immutable, ok := args[1].(Immutable); ok {
|
||||
newElements[length] = immutable.Clone()
|
||||
} else {
|
||||
newElements[length] = args[1]
|
||||
}
|
||||
|
||||
return &Array{Elements: newElements}
|
||||
}
|
||||
|
||||
// Pop ...
|
||||
func Pop(args ...Object) Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != ARRAY_OBJ {
|
||||
return newError("argument to `pop` must be ARRAY, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
arr := args[0].(*Array)
|
||||
length := len(arr.Elements)
|
||||
|
||||
if length == 0 {
|
||||
return newError("cannot pop from an empty array")
|
||||
}
|
||||
|
||||
element := arr.Elements[length-1]
|
||||
arr.Elements = arr.Elements[:length-1]
|
||||
|
||||
return element
|
||||
}
|
||||
|
||||
// Exit ...
|
||||
func Exit(args ...Object) Object {
|
||||
if len(args) == 1 {
|
||||
if args[0].Type() != INTEGER_OBJ {
|
||||
return newError("argument to `exit` must be INTEGER, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
os.Exit(int(args[0].(*Integer).Value))
|
||||
} else {
|
||||
os.Exit(0)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Assert ...
|
||||
func Assert(args ...Object) Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=2",
|
||||
len(args))
|
||||
}
|
||||
if args[0].Type() != BOOLEAN_OBJ {
|
||||
return newError("argument #1 to `assert` must be BOOLEAN, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
if args[1].Type() != STRING_OBJ {
|
||||
return newError("argument #2 to `assert` must be STRING, got %s",
|
||||
args[0].Type())
|
||||
}
|
||||
|
||||
if !args[0].(*Boolean).Value {
|
||||
fmt.Printf("Assertion Error: %s", args[1].(*String).Value)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user