type checking and error handling for builtins improved.
This commit is contained in:
@@ -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 {
|
||||
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())
|
||||
}
|
||||
x := args[0].(*object.Integer)
|
||||
y := args[1].(*object.Integer)
|
||||
value := pow(x.Value, y.Value)
|
||||
return &object.Integer{Value: value}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user