Files
monkey/internal/object/object.go
csmith 99f7553d67
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled
refactor objects
2024-04-01 17:34:10 -04:00

92 lines
1.6 KiB
Go

package object
import "fmt"
// Type represents the type of an object
type Type int
const (
NullType = iota
IntegerType
StringType
BooleanType
ReturnType
ErrorType
FunctionType
CFunctionType
BuiltinType
ClosureType
ArrayType
HashType
ModuleType
)
func (t Type) String() string {
switch t {
case NullType:
return "null"
case IntegerType:
return "int"
case StringType:
return "str"
case BooleanType:
return "bool"
case ReturnType:
return "Return"
case ErrorType:
return "error"
case FunctionType:
return "fn"
case CFunctionType:
return "CFunction"
case BuiltinType:
return "Builtin"
case ClosureType:
return "Closure"
case ArrayType:
return "array"
case HashType:
return "hash"
case ModuleType:
return "module"
default:
return "???"
}
}
// Comparable is the interface for objects to implement suitable comparisons
type Comparable interface {
Compare(other Object) int
}
// Copyable is the interface for creating copies of objects
type Copyable interface {
Copy() Object
}
// Object represents a value and implementations are expected to implement
// `Type()` and `Inspect()` functions
type Object interface {
fmt.Stringer
Type() Type
Bool() bool
Inspect() string
}
// Hasher is the interface for objects to provide suitable hash keys
type Hasher interface {
Hash() HashKey
}
// BuiltinFunction represents the builtin function type
type BuiltinFunction func(args ...Object) Object
func AssertTypes(obj Object, types ...Type) bool {
for _, t := range types {
if t == obj.Type() {
return true
}
}
return false
}