feat: implement setter syntax and assignment in named method calls
Add support for setter methods in the compiler by detecting '=' after a method name in namedCall, compiling the assigned value, and emitting the setter instruction. Includes new test cases for instance setters, static setters, assignment associativity, grouping, operator precedence errors, setter result values, and same-name getter/setter methods. Also updates the super call test with a TODO for super setter calls.
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
class Foo {
|
||||
new(value) { _value = value }
|
||||
toString { return _value }
|
||||
bar = value {
|
||||
_value = value
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
var a = new Foo("a")
|
||||
var b = new Foo("b")
|
||||
var c = new Foo("c")
|
||||
|
||||
// Assignment is right-associative.
|
||||
a.bar = b.bar = c.bar = "d"
|
||||
io.write(a.toString) // expect: d
|
||||
io.write(b.toString) // expect: d
|
||||
io.write(c.toString) // expect: d
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar = value { return value }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
(foo.bar) = "value" // expect error
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar = value { return value }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
"a" + foo.bar = "value" // expect error
|
||||
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
bar = value {
|
||||
io.write(value)
|
||||
}
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
foo.bar = "value" // expect: value
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar = value { return value }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
a is foo.bar = "value" // expect error
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar = value { return value }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
!foo.bar = "value" // expect error
|
||||
@@ -0,0 +1,6 @@
|
||||
class Foo {
|
||||
bar = value { return "result" }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
io.write(foo.bar = "value") // expect: result
|
||||
@@ -0,0 +1,8 @@
|
||||
class Foo {
|
||||
bar = value { io.write("set") }
|
||||
bar { io.write("get") }
|
||||
}
|
||||
|
||||
var foo = new Foo
|
||||
foo.bar = "value" // expect: set
|
||||
foo.bar // expect: get
|
||||
@@ -0,0 +1,7 @@
|
||||
class Foo {
|
||||
static bar = value {
|
||||
io.write(value)
|
||||
}
|
||||
}
|
||||
|
||||
Foo.bar = "value" // expect: value
|
||||
@@ -20,3 +20,4 @@ class Derived is Base {
|
||||
// TODO: Calling super outside of a class.
|
||||
// TODO: Super calls inside nested functions in methods.
|
||||
// TODO: Super where there is no inherited method.
|
||||
// TODO: Super setter calls.
|
||||
|
||||
Reference in New Issue
Block a user