Skip to content

Commit

Permalink
Merge branch '1.4.x' into implicit-return-value
Browse files Browse the repository at this point in the history
  • Loading branch information
odino committed May 12, 2019
2 parents acfbfdf + acfc803 commit 43e9e6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
14 changes: 7 additions & 7 deletions evaluator/evaluator_test.go
Expand Up @@ -756,7 +756,7 @@ b
c")`, []string{"a", "b", "c"}},
{`[1, 2].sort()`, []int{1, 2}},
{`["b", "a"].sort()`, []string{"a", "b"}},
{`["b", 1].sort()`, "argument to `sort` must be an homogeneous array (elements of the same type), got [b, 1]"},
{`["b", 1].sort()`, `argument to 'sort' must be an homogeneous array (elements of the same type), got ["b", 1]`},
{`[{}].sort()`, "cannot sort an array with given elements elements ([{}])"},
{`[[]].sort()`, "cannot sort an array with given elements elements ([[]])"},
{`[1, 2].some(f(x) {x == 2})`, true},
Expand Down Expand Up @@ -817,7 +817,7 @@ c")`, []string{"a", "b", "c"}},
{`"a".str()`, "a"},
{`1.str()`, "1"},
{`[1].str()`, "[1]"},
{`{"a": 10}.str()`, `{a: 10}`},
{`{"a": 10}.str()`, `{"a": 10}`},
{`"a great movie".title()`, "A Great Movie"},
{`"A great movie".lower()`, "a great movie"},
{`"A great movie".upper()`, "A GREAT MOVIE"},
Expand Down Expand Up @@ -1489,7 +1489,7 @@ z
{`
a = split("a\nb\nc", "\n")
str(a)
`, `[a, b, c]`,
`, `["a", "b", "c"]`,
},
{`
a = split("a\nb\nc", "\n")
Expand Down Expand Up @@ -1522,7 +1522,7 @@ func TestEvalAssignIndex(t *testing.T) {
a[5] = 55
str(a)
`,
"[99, 12, string, 4, 88, 55, 66]",
`[99, 12, "string", 4, 88, 55, 66]`,
},
{`
h = {"a": 1, "b": 2, "c": 3}
Expand All @@ -1534,9 +1534,9 @@ func TestEvalAssignIndex(t *testing.T) {
h.d = 99
h.d += 1
h.z.x = 66
h.f = 88
h.f = 1.23
str(h)
`, "{1.23: string, a: 100, b: 2, c: 33, d: 100, e: 55, f: 88, z: {x: 66, y: 20}}",
`, `{"1.23": "string", "a": 100, "b": 2, "c": 33, "d": 100, "e": 55, "f": 1.23, "z": {"x": 66, "y": 20}}`,
},
}

Expand All @@ -1563,7 +1563,7 @@ func TestHashFunctions(t *testing.T) {
hp = pop(h, "c")
hp = h.pop("d")
str(h)
`, "{b: 2}",
`, `{"b": 2}`,
},
}

Expand Down
2 changes: 1 addition & 1 deletion evaluator/functions.go
Expand Up @@ -869,7 +869,7 @@ func sortFn(tok token.Token, args ...object.Object) object.Object {
}

if !arr.Homogeneous() {
return newError(tok, "argument to `sort` must be an homogeneous array (elements of the same type), got %s", arr.Inspect())
return newError(tok, "argument to 'sort' must be an homogeneous array (elements of the same type), got %s", arr.Inspect())
}

switch elements[0].(type) {
Expand Down
18 changes: 16 additions & 2 deletions object/object.go
Expand Up @@ -54,6 +54,7 @@ type Hashable interface {
type Object interface {
Type() ObjectType
Inspect() string
Json() string
}

type Iterable interface {
Expand All @@ -78,6 +79,7 @@ func (n *Number) Inspect() string {
}
return strconv.FormatFloat(n.Value, 'f', -1, 64)
}
func (n *Number) Json() string { return n.Inspect() }
func (n *Number) ZeroValue() float64 { return float64(0) }
func (n *Number) Int() int { return int(n.Value) }

Expand All @@ -88,13 +90,15 @@ type Boolean struct {

func (b *Boolean) Type() ObjectType { return BOOLEAN_OBJ }
func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
func (b *Boolean) Json() string { return b.Inspect() }

type Null struct {
Token token.Token
}

func (n *Null) Type() ObjectType { return NULL_OBJ }
func (n *Null) Inspect() string { return "null" }
func (n *Null) Json() string { return n.Inspect() }

type ReturnValue struct {
Token token.Token
Expand All @@ -103,13 +107,15 @@ type ReturnValue struct {

func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJ }
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
func (rv *ReturnValue) Json() string { return rv.Inspect() }

type Error struct {
Message string
}

func (e *Error) Type() ObjectType { return ERROR_OBJ }
func (e *Error) Inspect() string { return "ERROR: " + e.Message }
func (e *Error) Json() string { return e.Inspect() }

type Function struct {
Token token.Token
Expand Down Expand Up @@ -137,6 +143,8 @@ func (f *Function) Inspect() string {
return out.String()
}

func (f *Function) Json() string { return f.Inspect() }

// The String is a special fella.
//
// Like ints, or bools, you might
Expand Down Expand Up @@ -176,6 +184,7 @@ type String struct {

func (s *String) Type() ObjectType { return STRING_OBJ }
func (s *String) Inspect() string { return s.Value }
func (s *String) Json() string { return `"` + s.Inspect() + `"` }
func (s *String) ZeroValue() string { return "" }
func (s *String) HashKey() HashKey {
return HashKey{Type: s.Type(), Value: s.Value}
Expand Down Expand Up @@ -266,6 +275,7 @@ type Builtin struct {

func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ }
func (b *Builtin) Inspect() string { return "builtin function" }
func (b *Builtin) Json() string { return b.Inspect() }

type Array struct {
Token token.Token
Expand Down Expand Up @@ -305,12 +315,13 @@ func (ao *Array) Homogeneous() bool {
func (ao *Array) Empty() bool {
return len(ao.Elements) == 0
}

func (ao *Array) Inspect() string {
var out bytes.Buffer

elements := []string{}
for _, e := range ao.Elements {
elements = append(elements, e.Inspect())
elements = append(elements, e.Json())
}

out.WriteString("[")
Expand All @@ -320,6 +331,8 @@ func (ao *Array) Inspect() string {
return out.String()
}

func (ao *Array) Json() string { return ao.Inspect() }

type HashPair struct {
Key Object
Value Object
Expand All @@ -343,7 +356,7 @@ func (h *Hash) Inspect() string {

pairs := []string{}
for _, pair := range h.Pairs {
pairs = append(pairs, fmt.Sprintf("%s: %s", pair.Key.Inspect(), pair.Value.Inspect()))
pairs = append(pairs, fmt.Sprintf(`%s: %s`, pair.Key.Json(), pair.Value.Json()))
}
// create stable key ordered output
sort.Strings(pairs)
Expand All @@ -354,6 +367,7 @@ func (h *Hash) Inspect() string {

return out.String()
}
func (h *Hash) Json() string { return h.Inspect() }

// Pretty convoluted logic here we could
// refactor.
Expand Down

0 comments on commit 43e9e6d

Please sign in to comment.