Skip to content

Implement range object #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add __repr__, __str__ of range
__repr__ print start, stop of range
if step is not one, step is also printed

Fixes #86
  • Loading branch information
HyeockJinKim committed Sep 20, 2019
commit 0ff757f7620643b1bf7ff452272abc631c0eb2cb
40 changes: 40 additions & 0 deletions py/range.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

package py

import (
"strings"
)

// A python Range object
// FIXME one day support BigInts too!
type Range struct {
Expand Down Expand Up @@ -104,6 +108,42 @@ func (r *Range) M__iter__() (Object, error) {
}, nil
}

func (r *Range) M__str__() (Object, error) {
return r.M__repr__()
}

func (r *Range) repr() (Object, error) {
var b strings.Builder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b.WriteString("range(")
start, err := ReprAsString(r.Start)
if err != nil {
return nil, err
}
stop, err := ReprAsString(r.Stop)
if err != nil {
return nil, err
}
b.WriteString(start)
b.WriteString(", ")
b.WriteString(stop)

if r.Step != 1 {
step, err := ReprAsString(r.Step)
if err != nil {
return nil, err
}
b.WriteString(", ")
b.WriteString(step)
}
b.WriteString(")")

return String(b.String()), nil
}

func (r *Range) M__repr__() (Object, error) {
return r.repr()
}

func (r *Range) M__len__() (Object, error) {
return r.Length, nil
}
Expand Down