Skip to content

Commit

Permalink
Represent proxy values in repl as their underlying values
Browse files Browse the repository at this point in the history
  • Loading branch information
thesephist committed Feb 24, 2020
1 parent a331650 commit d3660c5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/xin/lib.go
Expand Up @@ -25,6 +25,7 @@ func loadStandardLibrary(vm *Vm) InterpreterError {
"str",
"src",
"stat",
"os",
"test",
}

Expand Down
16 changes: 12 additions & 4 deletions pkg/xin/map.go
@@ -1,16 +1,20 @@
package xin

import (
"strings"
)

// StringValue is of type []byte, which is unhashable
// so we convert StringValues to hashable string type
// before using as map keys
type hashableStringProxy string

func (v hashableStringProxy) String() string {
return "(<string (proxy)> " + string(v) + ")"
return string(v)
}

func (v hashableStringProxy) Repr() string {
return v.String()
return "'" + strings.ReplaceAll(string(v), "'", "\\'") + "'"
}

func (v hashableStringProxy) Equal(ov Value) bool {
Expand All @@ -22,7 +26,7 @@ func (v hashableStringProxy) Equal(ov Value) bool {
type hashableNativeFormProxy string

func (v hashableNativeFormProxy) String() string {
return "(<native form (proxy)> " + string(v) + ")"
return "(<native form> " + string(v) + ")"
}

func (v hashableNativeFormProxy) Repr() string {
Expand Down Expand Up @@ -76,7 +80,11 @@ func (v MapValue) String() string {
}

func (v MapValue) Repr() string {
return v.String()
ss := ""
for k, val := range *v.items {
ss += " " + k.Repr() + "->" + val.Repr()
}
return "(<map>" + ss + ")"
}

func (v MapValue) Equal(o Value) bool {
Expand Down
8 changes: 8 additions & 0 deletions pkg/xin/reader.go
Expand Up @@ -3,6 +3,7 @@ package xin
import (
"io"
"io/ioutil"
"strings"
)

type reader struct {
Expand All @@ -25,6 +26,13 @@ func newReader(path string, r io.Reader) (*reader, error) {
max: len(asString),
}
rdr.position.path = path

// read shebang line if present
if strings.HasPrefix(asString, "#!") {
rdr.upto("\n")
rdr.skip()
}

return &rdr, nil
}
func (rdr *reader) done() bool {
Expand Down
5 changes: 2 additions & 3 deletions pkg/xin/runtime.go
Expand Up @@ -2,7 +2,6 @@ package xin

import (
"bufio"
"fmt"
"io"
"math"
"math/rand"
Expand All @@ -19,7 +18,7 @@ type NativeFormValue struct {
}

func (v NativeFormValue) String() string {
return fmt.Sprintf("(<native form> %s)", v.name)
return "(<native form> " + v.name + ")"
}

func (v NativeFormValue) Repr() string {
Expand All @@ -39,7 +38,7 @@ func loadAllDefaultValues(vm *Vm) {

stdoutStream := NewStream()
stdoutStream.callbacks.sink = func(v Value) InterpreterError {
fmt.Printf(v.String())
os.Stdout.Write([]byte(v.String()))
return nil
}
fr.Put("os::stdout", stdoutStream)
Expand Down
7 changes: 7 additions & 0 deletions pkg/xin/value.go
Expand Up @@ -14,8 +14,15 @@ import (
// exception to this case, where a proxy Value type is used instead
// which is hashable.
type Value interface {
// String is stable, string representation of a Xin value
// that can be used to back the to-string type conversion of values
String() string

// Repr is an unstable, human-readable representation of Xin values used for
// debugging and the repl, should not be considered a stable API
// to be used in the language internally.
Repr() string

Equal(Value) bool
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/xin/vec.go
Expand Up @@ -28,7 +28,11 @@ func (v VecValue) String() string {
}

func (v VecValue) Repr() string {
return v.String()
ss := ""
for _, item := range v.underlying.items {
ss += " " + item.Repr()
}
return "(<vec>" + ss + ")"
}

func (v VecValue) Equal(o Value) bool {
Expand Down

0 comments on commit d3660c5

Please sign in to comment.