From 99d08ce35ede2b5c75266240f20c4554060ddce0 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 25 Mar 2018 10:15:58 +0200 Subject: stringer --- evaluator/builtins.go | 8 +++---- evaluator/evaluator.go | 14 ++++++------ object/object.go | 52 +++++++++++++++++++++++---------------------- object/objecttype_string.go | 16 ++++++++++++++ 4 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 object/objecttype_string.go diff --git a/evaluator/builtins.go b/evaluator/builtins.go index 711c3c9..38acaea 100644 --- a/evaluator/builtins.go +++ b/evaluator/builtins.go @@ -40,7 +40,7 @@ var builtins = map[string]*object.Builtin{ return newError("wrong number of arguments. got=%d, want=1", len(args)) } - if args[0].Type() != object.ARRAY_OBJ { + if args[0].Type() != object.ARRAY { return newError("argument to `first` must be ARRAY, got %s", args[0].Type()) } @@ -59,7 +59,7 @@ var builtins = map[string]*object.Builtin{ return newError("wrong number of arguments. got=%d, want=1", len(args)) } - if args[0].Type() != object.ARRAY_OBJ { + if args[0].Type() != object.ARRAY { return newError("argument to `last` must be ARRAY, got %s", args[0].Type()) } @@ -79,7 +79,7 @@ var builtins = map[string]*object.Builtin{ return newError("wrong number of arguments. got=%d, want=1", len(args)) } - if args[0].Type() != object.ARRAY_OBJ { + if args[0].Type() != object.ARRAY { return newError("argument to `rest` must be ARRAY, got %s", args[0].Type()) } @@ -101,7 +101,7 @@ var builtins = map[string]*object.Builtin{ return newError("wrong number of arguments. got=%d, want=2", len(args)) } - if args[0].Type() != object.ARRAY_OBJ { + if args[0].Type() != object.ARRAY { return newError("argument to `push` must be ARRAY, got %s", args[0].Type()) } diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 6ea4b01..89e6426 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -148,7 +148,7 @@ func evalBlockStatement(block *ast.BlockStatement, env *object.Environment) obje if result != nil { rt := result.Type() - if rt == object.RETURN_VALUE_OBJ || rt == object.ERROR_OBJ { + if rt == object.RETURN_VALUE || rt == object.ERROR { return result } } @@ -177,9 +177,9 @@ func evalPrefixExpression(operator string, right object.Object) object.Object { func evalInfixExpression(operator string, left, right object.Object) object.Object { switch { - case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ: + case left.Type() == object.INTEGER && right.Type() == object.INTEGER: return evalIntegerInfixExpression(operator, left, right) - case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ: + case left.Type() == object.STRING && right.Type() == object.STRING: return evalStringInfixExpression(operator, left, right) case operator == "==": return nativeBoolToBooleanObject(left == right) @@ -208,7 +208,7 @@ func evalBangOperatorExpression(right object.Object) object.Object { } func evalMinusPrefixOperatorExpression(right object.Object) object.Object { - if right.Type() != object.INTEGER_OBJ { + if right.Type() != object.INTEGER { return newError("unknown operator: -%s", right.Type()) } @@ -300,7 +300,7 @@ func newError(format string, a ...interface{}) *object.Error { func isError(obj object.Object) bool { if obj != nil { - return obj.Type() == object.ERROR_OBJ + return obj.Type() == object.ERROR } return false } @@ -355,9 +355,9 @@ func unwrapReturnValue(obj object.Object) object.Object { func evalIndexExpression(left, index object.Object) object.Object { switch { - case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.ARRAY && index.Type() == object.INTEGER: return evalArrayIndexExpression(left, index) - case left.Type() == object.HASH_OBJ: + case left.Type() == object.HASH: return evalHashIndexExpression(left, index) default: return newError("index operator not supported: %s", left.Type()) 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]] +} -- cgit v1.2.3