tijani
44612f0756
git-svn-id: https://svn.tlawal.org/svn/monkey@50 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
72 lines
1.2 KiB
Go
72 lines
1.2 KiB
Go
package object
|
|
|
|
import "fmt"
|
|
|
|
type ObjectType string
|
|
|
|
const (
|
|
INTEGER_OBJECT = "INTEGER"
|
|
BOOLEAN_OBJECT = "BOOLEAN"
|
|
NULL_OBJECT = "NULL"
|
|
RETURN_VALUE_OBJECT = "RETURN_VALUE"
|
|
ERROR_OBJECT = "ERROR"
|
|
)
|
|
|
|
type Object interface {
|
|
Type() ObjectType
|
|
Inspect() string
|
|
}
|
|
|
|
// Integer
|
|
type Integer struct {
|
|
Value int64
|
|
}
|
|
|
|
func (i *Integer) Type() ObjectType {
|
|
return INTEGER_OBJECT
|
|
}
|
|
|
|
func (i *Integer) Inspect() string {
|
|
return fmt.Sprintf("%d", i.Value)
|
|
}
|
|
|
|
// Booleans
|
|
type Boolean struct {
|
|
Value bool
|
|
}
|
|
|
|
func (b *Boolean) Type() ObjectType {
|
|
return BOOLEAN_OBJECT
|
|
}
|
|
|
|
func (b *Boolean) Inspect() string {
|
|
return fmt.Sprintf("%t", b.Value)
|
|
}
|
|
|
|
// Null
|
|
type Null struct{}
|
|
|
|
func (n *Null) Type() ObjectType {
|
|
return NULL_OBJECT
|
|
}
|
|
|
|
func (n *Null) Inspect() string {
|
|
return "null"
|
|
}
|
|
|
|
// Return
|
|
type ReturnValue struct {
|
|
Value Object
|
|
}
|
|
|
|
func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJECT }
|
|
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
|
|
|
|
// Error
|
|
type Error struct {
|
|
Message string
|
|
}
|
|
|
|
func (err *Error) Type() ObjectType { return ERROR_OBJECT }
|
|
func (err *Error) Inspect() string { return "ERROR: " + err.Message }
|