feat: add user-defined subscript operators with bracket parameter lists

Refactor parameter list parsing to support bracket-delimited parameters for
subscript operators. Extract finishParameterList from parameterList to handle
the common closing logic, and add TOKEN_RIGHT_BRACKET case for error messages.
Add comprehensive test suite for subscript getters and setters with 1-3
parameters in test/method/subscript_operators.wren.
This commit is contained in:
Bob Nystrom
2015-01-21 02:25:54 +00:00
parent 895f52c77b
commit a4baa42d6f
3 changed files with 78 additions and 30 deletions
-2
View File
@@ -21,5 +21,3 @@
list[-3] = 7
IO.print(list) // expect: [7, 6, 5]
}
// TODO: Not in this dir, but need tests for subscript setter grammar.
+16
View File
@@ -0,0 +1,16 @@
class Foo {
[a] { "1-subscript " + a }
[a, b] { "2-subscript " + a + " " + b }
[a, b, c] { "3-subscript " + a + " " + b + " " + c }
[a]=(value) { "1-subscript setter " + a + " = " + value }
[a, b]=(value) { "2-subscript setter " + a + " " + b + " = " + value }
[a, b, c]=(value) { "3-subscript setter " + a + " " + b + " " + c + " = " + value }
}
var foo = new Foo
IO.print(foo["a"]) // expect: 1-subscript a
IO.print(foo["a", "b"]) // expect: 2-subscript a b
IO.print(foo["a", "b", "c"]) // expect: 3-subscript a b c
IO.print(foo["a"] = "value") // expect: 1-subscript setter a = value
IO.print(foo["a", "b"] = "value") // expect: 2-subscript setter a b = value
IO.print(foo["a", "b", "c"] = "value") // expect: 3-subscript setter a b c = value