Skip to content

Commit

Permalink
Catch errors like m[xy++] if xy is not incrementable and lay ground w…
Browse files Browse the repository at this point in the history
…ork for #189 (failing test)
  • Loading branch information
ldemailly committed Sep 3, 2024
1 parent 0e417d3 commit ffcc2fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,17 @@ func (s *State) evalInternal(node any) object.Object { //nolint:funlen,gocyclo,g
var index object.Object
if node.Token.Type() == token.DOT {
// index is the string value and not an identifier.
index = object.String{Value: node.Index.Value().Literal()}
key := node.Index.Value().Literal() // could be anything including "x++" from https://github.com/grol-io/grol/issues/189
index = object.String{Value: key}
} else {
if node.Index.Value().Type() == token.COLON {
rangeExp := node.Index.(*ast.InfixExpression)
return s.evalIndexRangeExpression(left, rangeExp.Left, rangeExp.Right)
}
index = s.evalInternal(node.Index)
if index.Type() == object.ERROR {
return index
}
}
return s.evalIndexExpression(left, index)
case *ast.Comment:
Expand Down
15 changes: 15 additions & 0 deletions eval/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,3 +1022,18 @@ func TestAliasTwice(t *testing.T) {
t.Errorf("wrong result, got %q", res.Inspect())
}
}

/* (failing) test for https://github.com/grol-io/grol/issues/189
func TestIncrMatrix(t *testing.T) {
inp := `m={"v":3};()=>{m.v++}();m.v`
s := eval.NewState()
res, err := eval.EvalString(s, inp, false)
if err != nil {
t.Errorf("should not have errored: %v", err)
}
expected := "4"
if res.Inspect() != expected {
t.Errorf("wrong result, got %q", res.Inspect())
}
}
*/

0 comments on commit ffcc2fa

Please sign in to comment.