rearrange builtins
This commit is contained in:
336
object/object.go
336
object/object.go
@@ -1,14 +1,6 @@
|
||||
package object
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"monkey/ast"
|
||||
"monkey/code"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Type represents the type of an object
|
||||
type ObjectType string
|
||||
|
||||
const (
|
||||
@@ -39,333 +31,19 @@ type Immutable interface {
|
||||
Clone() Object
|
||||
}
|
||||
|
||||
// Object represents a value and implementations are expected to implement
|
||||
// `Type()` and `Inspect()` functions
|
||||
type Object interface {
|
||||
Type() ObjectType
|
||||
String() string
|
||||
Inspect() string
|
||||
}
|
||||
|
||||
type Integer struct {
|
||||
Value int64
|
||||
}
|
||||
|
||||
type CompiledFunction struct {
|
||||
Instructions code.Instructions
|
||||
NumLocals int
|
||||
NumParameters int
|
||||
}
|
||||
|
||||
func (i *Integer) Type() ObjectType {
|
||||
return INTEGER_OBJ
|
||||
}
|
||||
func (i *Integer) Inspect() string {
|
||||
return fmt.Sprintf("%d", i.Value)
|
||||
}
|
||||
func (i *Integer) Clone() Object {
|
||||
return &Integer{Value: i.Value}
|
||||
}
|
||||
func (i *Integer) String() string {
|
||||
return i.Inspect()
|
||||
}
|
||||
func (i *Integer) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Integer); ok {
|
||||
return i.Value == obj.Value
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Boolean struct {
|
||||
Value bool
|
||||
}
|
||||
|
||||
func (b *Boolean) Type() ObjectType {
|
||||
return BOOLEAN_OBJ
|
||||
}
|
||||
func (b *Boolean) Inspect() string {
|
||||
return fmt.Sprintf("%t", b.Value)
|
||||
}
|
||||
func (b *Boolean) Clone() Object {
|
||||
return &Boolean{Value: b.Value}
|
||||
}
|
||||
func (b *Boolean) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
func (b *Boolean) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Boolean); ok {
|
||||
return b.Value == obj.Value
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Null struct{}
|
||||
|
||||
func (n *Null) Type() ObjectType {
|
||||
return NULL_OBJ
|
||||
}
|
||||
func (n *Null) Inspect() string {
|
||||
return "null"
|
||||
}
|
||||
func (n *Null) String() string {
|
||||
return n.Inspect()
|
||||
}
|
||||
func (n *Null) Equal(other Object) bool {
|
||||
_, ok := other.(*Null)
|
||||
return ok
|
||||
}
|
||||
|
||||
type ReturnValue struct {
|
||||
Value Object
|
||||
}
|
||||
|
||||
func (rv *ReturnValue) Type() ObjectType {
|
||||
return RETURN_VALUE_OBJ
|
||||
}
|
||||
func (rv *ReturnValue) Inspect() string {
|
||||
return rv.Value.Inspect()
|
||||
}
|
||||
func (rv *ReturnValue) String() string {
|
||||
return rv.Inspect()
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *Error) Type() ObjectType {
|
||||
return ERROR_OBJ
|
||||
}
|
||||
func (e *Error) Inspect() string {
|
||||
return "Error: " + e.Message
|
||||
}
|
||||
func (e *Error) Clone() Object {
|
||||
return &Error{Message: e.Message}
|
||||
}
|
||||
func (e *Error) String() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
type Function struct {
|
||||
Parameters []*ast.Identifier
|
||||
Body *ast.BlockStatement
|
||||
Env *Environment
|
||||
}
|
||||
|
||||
func (f *Function) Type() ObjectType {
|
||||
return FUNCTION_OBJ
|
||||
}
|
||||
func (f *Function) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
params := []string{}
|
||||
for _, p := range f.Parameters {
|
||||
params = append(params, p.String())
|
||||
}
|
||||
|
||||
out.WriteString("fn")
|
||||
out.WriteString("(")
|
||||
out.WriteString(strings.Join(params, ", "))
|
||||
out.WriteString(") {\n")
|
||||
out.WriteString(f.Body.String())
|
||||
out.WriteString("\n}")
|
||||
|
||||
return out.String()
|
||||
}
|
||||
func (f *Function) String() string {
|
||||
return f.Inspect()
|
||||
}
|
||||
|
||||
type String struct {
|
||||
Value string
|
||||
}
|
||||
|
||||
func (s *String) Type() ObjectType {
|
||||
return STRING_OBJ
|
||||
}
|
||||
func (s *String) Inspect() string {
|
||||
return fmt.Sprintf("%#v", s.Value)
|
||||
}
|
||||
func (s *String) Clone() Object {
|
||||
return &String{Value: s.Value}
|
||||
}
|
||||
func (s *String) String() string {
|
||||
return s.Value
|
||||
}
|
||||
func (s *String) Equal(other Object) bool {
|
||||
if obj, ok := other.(*String); ok {
|
||||
return s.Value == obj.Value
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type BuiltinFunction func(args ...Object) Object
|
||||
|
||||
type Builtin struct {
|
||||
Name string
|
||||
Fn BuiltinFunction
|
||||
}
|
||||
|
||||
func (b *Builtin) Type() ObjectType {
|
||||
return BUILTIN_OBJ
|
||||
}
|
||||
func (b *Builtin) Inspect() string {
|
||||
return fmt.Sprintf("<built-in function %s>", b.Name)
|
||||
}
|
||||
func (b *Builtin) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
|
||||
type Array struct {
|
||||
Elements []Object
|
||||
}
|
||||
|
||||
func (ao *Array) Type() ObjectType {
|
||||
return ARRAY_OBJ
|
||||
}
|
||||
func (ao *Array) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
elements := []string{}
|
||||
for _, e := range ao.Elements {
|
||||
elements = append(elements, e.Inspect())
|
||||
}
|
||||
|
||||
out.WriteString("[")
|
||||
out.WriteString(strings.Join(elements, ", "))
|
||||
out.WriteString("]")
|
||||
|
||||
return out.String()
|
||||
}
|
||||
func (ao *Array) String() string {
|
||||
return ao.Inspect()
|
||||
}
|
||||
func (ao *Array) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Array); ok {
|
||||
if len(ao.Elements) != len(obj.Elements) {
|
||||
return false
|
||||
}
|
||||
for i, el := range ao.Elements {
|
||||
cmp, ok := el.(Comparable)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !cmp.Equal(obj.Elements[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type HashKey struct {
|
||||
Type ObjectType
|
||||
Value uint64
|
||||
}
|
||||
|
||||
func (b *Boolean) HashKey() HashKey {
|
||||
var value uint64
|
||||
|
||||
if b.Value {
|
||||
value = 1
|
||||
} else {
|
||||
value = 0
|
||||
}
|
||||
|
||||
return HashKey{Type: b.Type(), Value: value}
|
||||
}
|
||||
|
||||
// Hashable is the interface for all hashable objects which must implement
|
||||
// the HashKey() method which returns a HashKey result.
|
||||
type Hashable interface {
|
||||
HashKey() HashKey
|
||||
}
|
||||
|
||||
func (i *Integer) HashKey() HashKey {
|
||||
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
|
||||
}
|
||||
|
||||
func (s *String) HashKey() HashKey {
|
||||
h := fnv.New64a()
|
||||
h.Write([]byte(s.Value))
|
||||
|
||||
return HashKey{Type: s.Type(), Value: h.Sum64()}
|
||||
}
|
||||
|
||||
type HashPair struct {
|
||||
Key Object
|
||||
Value Object
|
||||
}
|
||||
|
||||
type Hash struct {
|
||||
Pairs map[HashKey]HashPair
|
||||
}
|
||||
|
||||
func (h *Hash) Type() ObjectType {
|
||||
return HASH_OBJ
|
||||
}
|
||||
func (h *Hash) Inspect() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
pairs := []string{}
|
||||
for _, pair := range h.Pairs {
|
||||
pairs = append(pairs, fmt.Sprintf("%s: %s", pair.Key.Inspect(), pair.Value.Inspect()))
|
||||
}
|
||||
|
||||
out.WriteString("{")
|
||||
out.WriteString(strings.Join(pairs, ", "))
|
||||
out.WriteString("}")
|
||||
|
||||
return out.String()
|
||||
}
|
||||
func (h *Hash) String() string {
|
||||
return h.Inspect()
|
||||
}
|
||||
func (h *Hash) Equal(other Object) bool {
|
||||
if obj, ok := other.(*Hash); ok {
|
||||
if len(h.Pairs) != len(obj.Pairs) {
|
||||
return false
|
||||
}
|
||||
for _, pair := range h.Pairs {
|
||||
left := pair.Value
|
||||
hashed := left.(Hashable)
|
||||
right, ok := obj.Pairs[hashed.HashKey()]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
cmp, ok := left.(Comparable)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if !cmp.Equal(right.Value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) Type() ObjectType {
|
||||
return COMPILED_FUNCTION_OBJ
|
||||
}
|
||||
func (cf *CompiledFunction) Inspect() string {
|
||||
return fmt.Sprintf("CompiledFunction[%p]", cf)
|
||||
}
|
||||
func (cf *CompiledFunction) String() string {
|
||||
return cf.Inspect()
|
||||
}
|
||||
|
||||
type Closure struct {
|
||||
Fn *CompiledFunction
|
||||
Free []Object
|
||||
}
|
||||
|
||||
func (c *Closure) Type() ObjectType {
|
||||
return CLOSURE_OBJ
|
||||
}
|
||||
func (c *Closure) Inspect() string {
|
||||
return fmt.Sprintf("Closure[%p]", c)
|
||||
}
|
||||
func (c *Closure) String() string {
|
||||
return c.Inspect()
|
||||
}
|
||||
// BuiltinFunction represents the builtin function type
|
||||
type BuiltinFunction func(args ...Object) Object
|
||||
|
||||
Reference in New Issue
Block a user