Fix repl feedback
Some checks failed
Build / build (push) Successful in 9m50s
Test / build (push) Failing after 15m42s

This commit is contained in:
Chuck Smith
2024-03-19 20:46:18 -04:00
parent e320cd1e68
commit f735215c28
3 changed files with 47 additions and 10 deletions

View File

@@ -85,7 +85,7 @@ func Input(args ...Object) Object {
// Print ... // Print ...
func Print(args ...Object) Object { func Print(args ...Object) Object {
for _, arg := range args { for _, arg := range args {
fmt.Println(arg.Inspect()) fmt.Println(arg.String())
} }
return nil return nil

View File

@@ -34,6 +34,7 @@ type Immutable interface {
type Object interface { type Object interface {
Type() ObjectType Type() ObjectType
String() string
Inspect() string Inspect() string
} }
@@ -56,6 +57,9 @@ func (i *Integer) Inspect() string {
func (i *Integer) Clone() Object { func (i *Integer) Clone() Object {
return &Integer{Value: i.Value} return &Integer{Value: i.Value}
} }
func (i *Integer) String() string {
return i.Inspect()
}
type Boolean struct { type Boolean struct {
Value bool Value bool
@@ -70,6 +74,9 @@ func (b *Boolean) Inspect() string {
func (b *Boolean) Clone() Object { func (b *Boolean) Clone() Object {
return &Boolean{Value: b.Value} return &Boolean{Value: b.Value}
} }
func (b *Boolean) String() string {
return b.Inspect()
}
type Null struct{} type Null struct{}
@@ -79,6 +86,9 @@ func (n *Null) Type() ObjectType {
func (n *Null) Inspect() string { func (n *Null) Inspect() string {
return "null" return "null"
} }
func (n *Null) String() string {
return n.Inspect()
}
type ReturnValue struct { type ReturnValue struct {
Value Object Value Object
@@ -87,10 +97,12 @@ type ReturnValue struct {
func (rv *ReturnValue) Type() ObjectType { func (rv *ReturnValue) Type() ObjectType {
return RETURN_VALUE_OBJ return RETURN_VALUE_OBJ
} }
func (rv *ReturnValue) Inspect() string { func (rv *ReturnValue) Inspect() string {
return rv.Value.Inspect() return rv.Value.Inspect()
} }
func (rv *ReturnValue) String() string {
return rv.Inspect()
}
type Error struct { type Error struct {
Message string Message string
@@ -105,6 +117,9 @@ func (e *Error) Inspect() string {
func (e *Error) Clone() Object { func (e *Error) Clone() Object {
return &Error{Message: e.Message} return &Error{Message: e.Message}
} }
func (e *Error) String() string {
return e.Message
}
type Function struct { type Function struct {
Parameters []*ast.Identifier Parameters []*ast.Identifier
@@ -115,7 +130,6 @@ type Function struct {
func (f *Function) Type() ObjectType { func (f *Function) Type() ObjectType {
return FUNCTION_OBJ return FUNCTION_OBJ
} }
func (f *Function) Inspect() string { func (f *Function) Inspect() string {
var out bytes.Buffer var out bytes.Buffer
@@ -133,6 +147,9 @@ func (f *Function) Inspect() string {
return out.String() return out.String()
} }
func (f *Function) String() string {
return f.Inspect()
}
type String struct { type String struct {
Value string Value string
@@ -142,11 +159,14 @@ func (s *String) Type() ObjectType {
return STRING_OBJ return STRING_OBJ
} }
func (s *String) Inspect() string { func (s *String) Inspect() string {
return s.Value return fmt.Sprintf("%#v", s.Value)
} }
func (s *String) Clone() Object { func (s *String) Clone() Object {
return &String{Value: s.Value} return &String{Value: s.Value}
} }
func (s *String) String() string {
return s.Value
}
type BuiltinFunction func(args ...Object) Object type BuiltinFunction func(args ...Object) Object
@@ -155,12 +175,15 @@ type Builtin struct {
Fn BuiltinFunction Fn BuiltinFunction
} }
func (b Builtin) Type() ObjectType { func (b *Builtin) Type() ObjectType {
return BUILTIN_OBJ return BUILTIN_OBJ
} }
func (b Builtin) Inspect() string { func (b *Builtin) Inspect() string {
return fmt.Sprintf("<built-in function %s>", b.Name) return fmt.Sprintf("<built-in function %s>", b.Name)
} }
func (b *Builtin) String() string {
return b.Inspect()
}
type Array struct { type Array struct {
Elements []Object Elements []Object
@@ -183,6 +206,9 @@ func (ao *Array) Inspect() string {
return out.String() return out.String()
} }
func (ao *Array) String() string {
return ao.Inspect()
}
type HashKey struct { type HashKey struct {
Type ObjectType Type ObjectType
@@ -242,6 +268,9 @@ func (h *Hash) Inspect() string {
return out.String() return out.String()
} }
func (h *Hash) String() string {
return h.Inspect()
}
func (cf *CompiledFunction) Type() ObjectType { func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ return COMPILED_FUNCTION_OBJ
@@ -249,6 +278,9 @@ func (cf *CompiledFunction) Type() ObjectType {
func (cf *CompiledFunction) Inspect() string { func (cf *CompiledFunction) Inspect() string {
return fmt.Sprintf("CompiledFunction[%p]", cf) return fmt.Sprintf("CompiledFunction[%p]", cf)
} }
func (cf *CompiledFunction) String() string {
return cf.Inspect()
}
type Closure struct { type Closure struct {
Fn *CompiledFunction Fn *CompiledFunction
@@ -261,3 +293,6 @@ func (c *Closure) Type() ObjectType {
func (c *Closure) Inspect() string { func (c *Closure) Inspect() string {
return fmt.Sprintf("Closure[%p]", c) return fmt.Sprintf("Closure[%p]", c)
} }
func (c *Closure) String() string {
return c.Inspect()
}

View File

@@ -164,7 +164,7 @@ func (r *REPL) StartEvalLoop(in io.Reader, out io.Writer, env *object.Environmen
} }
obj := evaluator.Eval(program, env) obj := evaluator.Eval(program, env)
if obj != nil { if _, ok := obj.(*object.Null); !ok {
io.WriteString(out, obj.Inspect()) io.WriteString(out, obj.Inspect())
io.WriteString(out, "\n") io.WriteString(out, "\n")
} }
@@ -216,10 +216,12 @@ func (r *REPL) StartExecLoop(in io.Reader, out io.Writer, state *VMState) {
return return
} }
stackTop := machine.LastPoppedStackElem() obj := machine.LastPoppedStackElem()
io.WriteString(out, stackTop.Inspect()) if _, ok := obj.(*object.Null); !ok {
io.WriteString(out, obj.Inspect())
io.WriteString(out, "\n") io.WriteString(out, "\n")
} }
}
} }
func (r *REPL) Run() { func (r *REPL) Run() {