diff --git a/object/builtins.go b/object/builtins.go index b88e36f..52f9841 100644 --- a/object/builtins.go +++ b/object/builtins.go @@ -85,7 +85,7 @@ func Input(args ...Object) Object { // Print ... func Print(args ...Object) Object { for _, arg := range args { - fmt.Println(arg.Inspect()) + fmt.Println(arg.String()) } return nil diff --git a/object/object.go b/object/object.go index d6999ea..d82cda7 100644 --- a/object/object.go +++ b/object/object.go @@ -34,6 +34,7 @@ type Immutable interface { type Object interface { Type() ObjectType + String() string Inspect() string } @@ -56,6 +57,9 @@ func (i *Integer) Inspect() string { func (i *Integer) Clone() Object { return &Integer{Value: i.Value} } +func (i *Integer) String() string { + return i.Inspect() +} type Boolean struct { Value bool @@ -70,6 +74,9 @@ func (b *Boolean) Inspect() string { func (b *Boolean) Clone() Object { return &Boolean{Value: b.Value} } +func (b *Boolean) String() string { + return b.Inspect() +} type Null struct{} @@ -79,6 +86,9 @@ func (n *Null) Type() ObjectType { func (n *Null) Inspect() string { return "null" } +func (n *Null) String() string { + return n.Inspect() +} type ReturnValue struct { Value Object @@ -87,10 +97,12 @@ type ReturnValue struct { 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 @@ -105,6 +117,9 @@ func (e *Error) Inspect() string { func (e *Error) Clone() Object { return &Error{Message: e.Message} } +func (e *Error) String() string { + return e.Message +} type Function struct { Parameters []*ast.Identifier @@ -115,7 +130,6 @@ type Function struct { func (f *Function) Type() ObjectType { return FUNCTION_OBJ } - func (f *Function) Inspect() string { var out bytes.Buffer @@ -133,6 +147,9 @@ func (f *Function) Inspect() string { return out.String() } +func (f *Function) String() string { + return f.Inspect() +} type String struct { Value string @@ -142,11 +159,14 @@ func (s *String) Type() ObjectType { return STRING_OBJ } func (s *String) Inspect() string { - return s.Value + return fmt.Sprintf("%#v", s.Value) } func (s *String) Clone() Object { return &String{Value: s.Value} } +func (s *String) String() string { + return s.Value +} type BuiltinFunction func(args ...Object) Object @@ -155,12 +175,15 @@ type Builtin struct { Fn BuiltinFunction } -func (b Builtin) Type() ObjectType { +func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ } -func (b Builtin) Inspect() string { +func (b *Builtin) Inspect() string { return fmt.Sprintf("", b.Name) } +func (b *Builtin) String() string { + return b.Inspect() +} type Array struct { Elements []Object @@ -183,6 +206,9 @@ func (ao *Array) Inspect() string { return out.String() } +func (ao *Array) String() string { + return ao.Inspect() +} type HashKey struct { Type ObjectType @@ -242,6 +268,9 @@ func (h *Hash) Inspect() string { return out.String() } +func (h *Hash) String() string { + return h.Inspect() +} func (cf *CompiledFunction) Type() ObjectType { return COMPILED_FUNCTION_OBJ @@ -249,6 +278,9 @@ func (cf *CompiledFunction) Type() ObjectType { func (cf *CompiledFunction) Inspect() string { return fmt.Sprintf("CompiledFunction[%p]", cf) } +func (cf *CompiledFunction) String() string { + return cf.Inspect() +} type Closure struct { Fn *CompiledFunction @@ -261,3 +293,6 @@ func (c *Closure) Type() ObjectType { func (c *Closure) Inspect() string { return fmt.Sprintf("Closure[%p]", c) } +func (c *Closure) String() string { + return c.Inspect() +} diff --git a/repl/repl.go b/repl/repl.go index 6fd5a26..d1d5ec1 100644 --- a/repl/repl.go +++ b/repl/repl.go @@ -164,7 +164,7 @@ func (r *REPL) StartEvalLoop(in io.Reader, out io.Writer, env *object.Environmen } obj := evaluator.Eval(program, env) - if obj != nil { + if _, ok := obj.(*object.Null); !ok { io.WriteString(out, obj.Inspect()) io.WriteString(out, "\n") } @@ -216,9 +216,11 @@ func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) { return } - stackTop := machine.LastPoppedStackElem() - io.WriteString(out, stackTop.Inspect()) - io.WriteString(out, "\n") + obj := machine.LastPoppedStackElem() + if _, ok := obj.(*object.Null); !ok { + io.WriteString(out, obj.Inspect()) + io.WriteString(out, "\n") + } } }