aboutsummaryrefslogtreecommitdiff
path: root/object/object.go
diff options
context:
space:
mode:
Diffstat (limited to 'object/object.go')
-rw-r--r--object/object.go52
1 files changed, 27 insertions, 25 deletions
diff --git a/object/object.go b/object/object.go
index 7faf808..c34b8e7 100644
--- a/object/object.go
+++ b/object/object.go
@@ -9,27 +9,29 @@ import (
"monkey/ast"
)
+//go:generate stringer -type=ObjectType
+
type BuiltinFunction func(args ...Object) Object
-type ObjectType string
+type ObjectType int
const (
- NULL_OBJ = "NULL"
- ERROR_OBJ = "ERROR"
+ NULL ObjectType = iota
+ ERROR
- INTEGER_OBJ = "INTEGER"
- BOOLEAN_OBJ = "BOOLEAN"
- STRING_OBJ = "STRING"
+ INTEGER
+ BOOLEAN
+ STRING
- RETURN_VALUE_OBJ = "RETURN_VALUE"
+ RETURN_VALUE
- FUNCTION_OBJ = "FUNCTION"
- BUILTIN_OBJ = "BUILTIN"
+ FUNCTION
+ BUILTIN
- ARRAY_OBJ = "ARRAY"
- HASH_OBJ = "HASH"
- QUOTE_OBJ = "QUOTE"
- MACRO_OBJ = "MACRO"
+ ARRAY
+ HASH
+ QUOTE
+ MACRO
)
type HashKey struct {
@@ -50,7 +52,7 @@ type Integer struct {
Value int64
}
-func (i *Integer) Type() ObjectType { return INTEGER_OBJ }
+func (i *Integer) Type() ObjectType { return INTEGER }
func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
func (i *Integer) HashKey() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
@@ -60,7 +62,7 @@ type Boolean struct {
Value bool
}
-func (b *Boolean) Type() ObjectType { return BOOLEAN_OBJ }
+func (b *Boolean) Type() ObjectType { return BOOLEAN }
func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
func (b *Boolean) HashKey() HashKey {
var value uint64
@@ -76,21 +78,21 @@ func (b *Boolean) HashKey() HashKey {
type Null struct{}
-func (n *Null) Type() ObjectType { return NULL_OBJ }
+func (n *Null) Type() ObjectType { return NULL }
func (n *Null) Inspect() string { return "null" }
type ReturnValue struct {
Value Object
}
-func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJ }
+func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE }
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
type Error struct {
Message string
}
-func (e *Error) Type() ObjectType { return ERROR_OBJ }
+func (e *Error) Type() ObjectType { return ERROR }
func (e *Error) Inspect() string { return "ERROR: " + e.Message }
type Function struct {
@@ -99,7 +101,7 @@ type Function struct {
Env *Environment
}
-func (f *Function) Type() ObjectType { return FUNCTION_OBJ }
+func (f *Function) Type() ObjectType { return FUNCTION }
func (f *Function) Inspect() string {
var out bytes.Buffer
@@ -122,7 +124,7 @@ type String struct {
Value string
}
-func (s *String) Type() ObjectType { return STRING_OBJ }
+func (s *String) Type() ObjectType { return STRING }
func (s *String) Inspect() string { return s.Value }
func (s *String) HashKey() HashKey {
h := fnv.New64a()
@@ -135,14 +137,14 @@ type Builtin struct {
Fn BuiltinFunction
}
-func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ }
+func (b *Builtin) Type() ObjectType { return BUILTIN }
func (b *Builtin) Inspect() string { return "builtin function" }
type Array struct {
Elements []Object
}
-func (ao *Array) Type() ObjectType { return ARRAY_OBJ }
+func (ao *Array) Type() ObjectType { return ARRAY }
func (ao *Array) Inspect() string {
var out bytes.Buffer
@@ -167,7 +169,7 @@ type Hash struct {
Pairs map[HashKey]HashPair
}
-func (h *Hash) Type() ObjectType { return HASH_OBJ }
+func (h *Hash) Type() ObjectType { return HASH }
func (h *Hash) Inspect() string {
var out bytes.Buffer
@@ -188,7 +190,7 @@ type Quote struct {
Node ast.Node
}
-func (q *Quote) Type() ObjectType { return QUOTE_OBJ }
+func (q *Quote) Type() ObjectType { return QUOTE }
func (q *Quote) Inspect() string {
return "QUOTE(" + q.Node.String() + ")"
}
@@ -199,7 +201,7 @@ type Macro struct {
Env *Environment
}
-func (m *Macro) Type() ObjectType { return MACRO_OBJ }
+func (m *Macro) Type() ObjectType { return MACRO }
func (m *Macro) Inspect() string {
var out bytes.Buffer