Done with arrays and all its associated builtins
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
59acf6b1a1
commit
fa9f450278
4 changed files with 179 additions and 2 deletions
|
|
@ -1,6 +1,10 @@
|
|||
package evaluator
|
||||
|
||||
import "code.jmug.me/jmug/interpreter-in-go/pkg/object"
|
||||
import (
|
||||
"os"
|
||||
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/object"
|
||||
)
|
||||
|
||||
var builtins = map[string]*object.Builtin{
|
||||
"len": {
|
||||
|
|
@ -13,10 +17,96 @@ var builtins = map[string]*object.Builtin{
|
|||
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.
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue