-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
1,048 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package evaluator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/0daryo/ody/object" | ||
) | ||
|
||
var builtins = map[string]*object.Builtin{ | ||
"len": &object.Builtin{ | ||
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": &object.Builtin{ | ||
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": &object.Builtin{ | ||
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) | ||
length := len(arr.Elements) | ||
if length > 0 { | ||
return arr.Elements[length-1] | ||
} | ||
return NULL | ||
}, | ||
}, | ||
"rest": &object.Builtin{ | ||
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) | ||
length := len(arr.Elements) | ||
if length > 0 { | ||
newElements := make([]object.Object, length-1) | ||
copy(newElements, arr.Elements[1:length]) | ||
return &object.Array{Elements: newElements} | ||
} | ||
return NULL | ||
}, | ||
}, | ||
"push": &object.Builtin{ | ||
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) | ||
length := len(arr.Elements) | ||
if length > 0 { | ||
newElements := make([]object.Object, length+1) | ||
copy(newElements, arr.Elements) | ||
newElements[length] = args[1] | ||
return &object.Array{Elements: newElements} | ||
} | ||
return NULL | ||
}, | ||
}, | ||
"puts": &object.Builtin{ | ||
Fn: func(args ...object.Object) object.Object { | ||
for _, arg := range args { | ||
fmt.Println(arg.Inspect()) | ||
} | ||
return NULL | ||
}, | ||
}, | ||
} |
Oops, something went wrong.