aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-03-25 10:15:58 +0200
committerDimitri Sokolyuk <demon@dim13.org>2018-03-25 10:15:58 +0200
commit99d08ce35ede2b5c75266240f20c4554060ddce0 (patch)
treea0ee1cc221cec03968046d4baade84666b9ee4ae /object
parenta6e158b26593fd77972bf8ceca7655f6b332596c (diff)
stringer
Diffstat (limited to 'object')
-rw-r--r--object/object.go52
-rw-r--r--object/objecttype_string.go16
2 files changed, 43 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
diff --git a/object/objecttype_string.go b/object/objecttype_string.go
new file mode 100644
index 0000000..80d3ee6
--- /dev/null
+++ b/object/objecttype_string.go
@@ -0,0 +1,16 @@
+// Code generated by "stringer -type=ObjectType"; DO NOT EDIT.
+
+package object
+
+import "strconv"
+
+const _ObjectType_name = "NULLERRORINTEGERBOOLEANSTRINGRETURN_VALUEFUNCTIONBUILTINARRAYHASHQUOTEMACRO"
+
+var _ObjectType_index = [...]uint8{0, 4, 9, 16, 23, 29, 41, 49, 56, 61, 65, 70, 75}
+
+func (i ObjectType) String() string {
+ if i < 0 || i >= ObjectType(len(_ObjectType_index)-1) {
+ return "ObjectType(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _ObjectType_name[_ObjectType_index[i]:_ObjectType_index[i+1]]
+}