121 lines
2.8 KiB
Go
121 lines
2.8 KiB
Go
package evaluator
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"code.jmug.me/jmug/compiler-in-go/pkg/object"
|
|
)
|
|
|
|
var builtins = map[string]*object.Builtin{
|
|
"puts": {
|
|
Fn: func(args ...object.Object) object.Object {
|
|
for _, arg := range args {
|
|
fmt.Println(arg.Inspect())
|
|
}
|
|
return _NULL
|
|
},
|
|
},
|
|
"len": {
|
|
Fn: func(args ...object.Object) object.Object {
|
|
if len(args) != 1 {
|
|
return newError("wrong number of arguments. got=%d, want=1",
|
|
len(args))
|
|
}
|
|
|
|
switch arg := args[0].(type) {
|
|
case *object.String:
|
|
return &object.Integer{Value: int64(len(arg.Value))}
|
|
case *object.Array:
|
|
return &object.Integer{Value: int64(len(arg.Elements))}
|
|
default:
|
|
return newError("argument to `len` not supported, got %s",
|
|
args[0].Type())
|
|
}
|
|
},
|
|
},
|
|
"first": {
|
|
Fn: func(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())
|
|
}
|
|
|
|
arr := args[0].(*object.Array)
|
|
if len(arr.Elements) > 0 {
|
|
return arr.Elements[0]
|
|
}
|
|
|
|
return _NULL
|
|
},
|
|
},
|
|
"last": {
|
|
Fn: func(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())
|
|
}
|
|
|
|
arr := args[0].(*object.Array)
|
|
if len(arr.Elements) > 0 {
|
|
return arr.Elements[len(arr.Elements)-1]
|
|
}
|
|
return _NULL
|
|
},
|
|
},
|
|
"rest": {
|
|
Fn: func(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())
|
|
}
|
|
arr := args[0].(*object.Array).Elements
|
|
arrLen := len(arr)
|
|
if arrLen > 0 {
|
|
newArr := make([]object.Object, arrLen-1)
|
|
copy(newArr, arr[1:])
|
|
return &object.Array{Elements: newArr}
|
|
}
|
|
return _NULL
|
|
},
|
|
},
|
|
"push": {
|
|
Fn: func(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())
|
|
}
|
|
arr := args[0].(*object.Array).Elements
|
|
arrLen := len(arr)
|
|
newArr := make([]object.Object, arrLen+1)
|
|
copy(newArr, arr)
|
|
newArr[arrLen] = args[1]
|
|
return &object.Array{Elements: newArr}
|
|
},
|
|
},
|
|
"exit": {
|
|
Fn: func(args ...object.Object) object.Object {
|
|
if len(args) != 0 {
|
|
return newError("exit takes no arguments...")
|
|
}
|
|
os.Exit(0)
|
|
return nil // Make the compiler happy.
|
|
},
|
|
},
|
|
}
|