type checking and error handling for builtins improved.
Some checks failed
Build / build (push) Successful in 11m16s
Test / build (push) Failing after 17m0s

This commit is contained in:
Chuck Smith
2024-03-25 16:18:08 -04:00
parent 1c99d2198b
commit 6d234099d1
52 changed files with 650 additions and 433 deletions

View File

@@ -1,40 +1,18 @@
package builtins
import "monkey/object"
import (
"monkey/object"
"monkey/typing"
)
// Bool ...
func Bool(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1", len(args))
if err := typing.Check(
"bool", args,
typing.ExactArgs(1),
); err != nil {
return newError(err.Error())
}
switch arg := args[0].(type) {
case *object.Null:
return &object.Boolean{Value: false}
case *object.Boolean:
return arg
case *object.Integer:
if arg.Value == 0 {
return &object.Boolean{Value: false}
}
return &object.Boolean{Value: true}
case *object.String:
if len(arg.Value) > 0 {
return &object.Boolean{Value: true}
}
return &object.Boolean{Value: false}
case *object.Array:
if len(arg.Elements) > 0 {
return &object.Boolean{Value: true}
}
return &object.Boolean{Value: false}
case *object.Hash:
if len(arg.Pairs) > 0 {
return &object.Boolean{Value: true}
}
return &object.Boolean{Value: false}
default:
return newError("argument to `bool` not supported, got=%s", args[0].Type())
}
return &object.Boolean{Value: args[0].Bool()}
}