Skip to content
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

Fix indexable object types #80

Merged
merged 2 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion ast/assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Assign struct {
Token token.Token
Name *Identifier
Name Expression
Value Expression
}

Expand Down
21 changes: 21 additions & 0 deletions ast/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ast

import (
"bytes"
"fmt"

"github.com/flipez/rocket-lang/token"
)
Expand All @@ -24,3 +25,23 @@ func (ie *Index) String() string {

return out.String()
}

type RangeIndex struct {
Token token.Token
Left Expression
FirstIndex Expression
SecondIndex Expression
}

func (rie *RangeIndex) TokenLiteral() string { return rie.Token.Literal }
func (rie *RangeIndex) String() string {
str := fmt.Sprintf("(%s[", rie.Left)
if rie.FirstIndex != nil {
str += rie.FirstIndex.String()
}
str += ":"
if rie.SecondIndex != nil {
str += rie.SecondIndex.String()
}
return str + "])"
}
20 changes: 20 additions & 0 deletions docs/content/docs/literals/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ menu:



```js
a = [1, 2, 3, 4, 5]
puts(a[2])
puts(a[-2])
puts(a[:2])
puts(a[:-2])
puts(a[2:])
puts(a[-2:])
puts(a[1:-2])

// should output
[1, 2]
[1, 2, 3]
[3, 4, 5]
[4, 5]
[2, 3]
[1, 2, 8, 9, 5]

```

## Literal Specific Methods

### first()
Expand Down
21 changes: 21 additions & 0 deletions docs/content/docs/literals/hash.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ menu:

```js
people = [{"name": "Anna", "age": 24}, {"name": "Bob", "age": 99}];

// reassign of values
h = {"a": 1, 2: true}
puts(h["a"])
puts(h[2])
h["a"] = 3
h["b"] = "moo"
puts(h["a"])
puts(h["b"])
puts(h[2])h = {"a": 1, 2: true}
puts(h["a"])
puts(h[2])
h["a"] = 3
h["b"] = "moo"

// should output
1
true
3
"moo"
true
```

## Literal Specific Methods
Expand Down
23 changes: 23 additions & 0 deletions docs/content/docs/literals/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ b = "test" + "_string";

is_true = "test" == "test";
is_false = "test" == "string";

s = "abcdef"
puts(s[2])
puts(s[-2])
puts(s[:2])
puts(s[:-2])
puts(s[2:])
puts(s[-2:])
puts(s[1:-2])

s[2] = "C"
s[-2] = "E"
puts(s)

// should output
"c"
"e"
"ab"
"abcd"
"cdef"
"ef"
"bcd"
"abCdEf"
```

## Literal Specific Methods
Expand Down
69 changes: 65 additions & 4 deletions docs/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,81 @@ func main() {
b = "test" + "_string";

is_true = "test" == "test";
is_false = "test" == "string";`,
is_false = "test" == "string";

s = "abcdef"
puts(s[2])
puts(s[-2])
puts(s[:2])
puts(s[:-2])
puts(s[2:])
puts(s[-2:])
puts(s[1:-2])

s[2] = "C"
s[-2] = "E"
puts(s)

// should output
"c"
"e"
"ab"
"abcd"
"cdef"
"ef"
"bcd"
"abCdEf"`,
LiteralMethods: string_methods,
DefaultMethods: default_methods}
create_doc("docs/templates/literal.md", "docs/content/docs/literals/string.md", tempData)

tempData = templateData{
Title: "Array",
Title: "Array",
Example: `a = [1, 2, 3, 4, 5]
puts(a[2])
puts(a[-2])
puts(a[:2])
puts(a[:-2])
puts(a[2:])
puts(a[-2:])
puts(a[1:-2])

// should output
[1, 2]
[1, 2, 3]
[3, 4, 5]
[4, 5]
[2, 3]
[1, 2, 8, 9, 5]
`,
LiteralMethods: array_methods,
DefaultMethods: default_methods}
create_doc("docs/templates/literal.md", "docs/content/docs/literals/array.md", tempData)

tempData = templateData{
Title: "Hash",
Example: `people = [{"name": "Anna", "age": 24}, {"name": "Bob", "age": 99}];`,
Title: "Hash",
Example: `people = [{"name": "Anna", "age": 24}, {"name": "Bob", "age": 99}];

// reassign of values
h = {"a": 1, 2: true}
puts(h["a"])
puts(h[2])
h["a"] = 3
h["b"] = "moo"
puts(h["a"])
puts(h["b"])
puts(h[2])h = {"a": 1, 2: true}
puts(h["a"])
puts(h[2])
h["a"] = 3
h["b"] = "moo"

// should output
1
true
3
"moo"
true`,
LiteralMethods: hash_methods,
DefaultMethods: default_methods}
create_doc("docs/templates/literal.md", "docs/content/docs/literals/hash.md", tempData)
Expand Down
76 changes: 75 additions & 1 deletion evaluator/assign.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package evaluator

import (
"fmt"
"math"

"github.com/flipez/rocket-lang/ast"
"github.com/flipez/rocket-lang/object"
)
Expand All @@ -11,6 +14,77 @@ func evalAssign(a *ast.Assign, env *object.Environment) (val object.Object) {
return evaluated
}

env.Set(a.Name.String(), evaluated)
switch v := a.Name.(type) {
case *ast.Identifier:
env.Set(v.String(), evaluated)
case *ast.Index:
obj, _ := env.Get(v.Left.String())
switch o := obj.(type) {
case *object.Array:
idx, err := handleIntegerIndex(v, env)
if err != nil {
return object.NewError(err)
}

if l := int64(len(o.Elements)); int64(math.Abs(float64(idx))) >= l {
return object.NewErrorFormat(
"index out of range, got %d but array has only %d elements", idx, l,
)
}

if idx < 0 {
idx = int64(len(o.Elements)) + idx
}

o.Elements[idx] = evaluated
case *object.Hash:
obj := Eval(v.Index, env)
h, ok := obj.(object.Hashable)
if !ok {
return object.NewErrorFormat("expected index to be hashable")
}

key := h.HashKey()
o.Pairs[key] = object.HashPair{Key: obj, Value: evaluated}
case *object.String:
idx, err := handleIntegerIndex(v, env)
if err != nil {
return object.NewError(err)
}

if l := int64(len(o.Value)); int64(math.Abs(float64(idx))) >= l {
return object.NewErrorFormat(
"index out of range, got %d but string is only %d long", idx, l,
)
}

if idx < 0 {
idx = int64(len(o.Value)) + idx
}

strEval, ok := evaluated.(*object.String)
if !ok {
return object.NewErrorFormat("expected STRING object, got %s", evaluated.Type())
}
if l := len(strEval.Value); l != 1 {
return object.NewErrorFormat(
"expected STRING object to have a length of 1, got %d", l,
)
}

o.Value = o.Value[:idx] + strEval.Value + o.Value[idx+1:]
default:
return object.NewErrorFormat("expected object to be indexable")
}
}
return evaluated
}

func handleIntegerIndex(ai *ast.Index, env *object.Environment) (int64, error) {
obj := Eval(ai.Index, env)
num, ok := obj.(*object.Integer)
if !ok {
return 0, fmt.Errorf("expected index to be an INTEGER, got %s", obj.Type())
}
return num.Value, nil
}
Loading