188 lines
3.9 KiB
Go
188 lines
3.9 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 "???"
|
|
}
|
|
}
|
|
|
|
// Add is the interface for binary addition
|
|
type Add interface {
|
|
Add(other Object) (Object, error)
|
|
}
|
|
|
|
// Sub is the interface for binary subtraction
|
|
type Sub interface {
|
|
Sub(other Object) (Object, error)
|
|
}
|
|
|
|
// Mul is the interface for binary multiplication
|
|
type Mul interface {
|
|
Mul(other Object) (Object, error)
|
|
}
|
|
|
|
// Div is the interface for binary division
|
|
type Div interface {
|
|
Div(other Object) (Object, error)
|
|
}
|
|
|
|
// Mod is the interface for binary modulo
|
|
type Mod interface {
|
|
Mod(other Object) (Object, error)
|
|
}
|
|
|
|
// LogicalOr is the interface for logical or
|
|
type LogicalOr interface {
|
|
LogicalOr(other Object) (Object, error)
|
|
}
|
|
|
|
// LogicalAnd is the interface for logical and
|
|
type LogicalAnd interface {
|
|
LogicalAnd(other Object) (Object, error)
|
|
}
|
|
|
|
// BitwiseOr is the interface for bitwise or
|
|
type BitwiseOr interface {
|
|
BitwiseOr(other Object) (Object, error)
|
|
}
|
|
|
|
// BitwiseAnd is the interface for bitwise and
|
|
type BitwiseAnd interface {
|
|
BitwiseAnd(other Object) (Object, error)
|
|
}
|
|
|
|
// BitwiseXor is the interface for bitwise xor
|
|
type BitwiseXor interface {
|
|
BitwiseXor(other Object) (Object, error)
|
|
}
|
|
|
|
// BitwiseNot is the interface for bitwise not
|
|
type BitwiseNot interface {
|
|
BitwiseNot() Object
|
|
}
|
|
|
|
// LeftShift is the interface for bitwise left shift
|
|
type LeftShift interface {
|
|
LeftShift(other Object) (Object, error)
|
|
}
|
|
|
|
// RightShift is the interface for bitwise right shift
|
|
type RightShift interface {
|
|
RightShift(other Object) (Object, error)
|
|
}
|
|
|
|
// LogicalNot is the interface for logical not
|
|
type LogicalNot interface {
|
|
LogicalNot() Object
|
|
}
|
|
|
|
// Negate is the interface for unary negation
|
|
type Negate interface {
|
|
Negate() Object
|
|
}
|
|
|
|
// Setter is the interface for assigning a value to an index
|
|
type Setter interface {
|
|
Set(index, value Object) error
|
|
}
|
|
|
|
// Getter is the interface for getting a value from an index
|
|
type Getter interface {
|
|
Get(index Object) (Object, error)
|
|
}
|
|
|
|
// Comparable is the interface for comparing two Object and their underlying
|
|
// values. It is the responsibility of the caller (left) to check for types.
|
|
// Returns `true` iif the types and values are identical, `false` otherwise.
|
|
type Comparable interface {
|
|
Compare(other Object) int
|
|
}
|
|
|
|
// Sizeable is the interface for returning the size of an Object.
|
|
// Object(s) that have a valid size must implement this interface and the
|
|
// Len() method.
|
|
type Sizeable interface {
|
|
Len() int
|
|
}
|
|
|
|
// Immutable is the interface for all immutable objects which must implement
|
|
// the Clone() method used by binding names to values.
|
|
type Immutable interface {
|
|
Clone() 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
|
|
}
|
|
|
|
// Hashable is the interface for all hashable objects which must implement
|
|
// the HashKey() method which returns a HashKey result.
|
|
type Hashable interface {
|
|
HashKey() 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
|
|
}
|