feat: add user-defined operator methods with signature compilation
Add support for user-defined operator methods in classes by introducing a `SignatureFn` field to `GrammarRule` and implementing four signature compilation functions: `namedSignature`, `infixSignature`, `unarySignature`, and `mixedSignature`. These functions handle parameter parsing and name mangling for different operator types (infix, prefix, and mixed unary/infix). The change includes a new test file `test/method_operators.wren` that validates all supported operator overloads including arithmetic, comparison, and prefix operators.
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
class Foo {
|
||||
+ other { "infix + " + other }
|
||||
- other { "infix - " + other }
|
||||
* other { "infix * " + other }
|
||||
/ other { "infix / " + other }
|
||||
% other { "infix % " + other }
|
||||
< other { "infix < " + other }
|
||||
> other { "infix > " + other }
|
||||
<= other { "infix <= " + other }
|
||||
>= other { "infix >= " + other }
|
||||
== other { "infix == " + other }
|
||||
!= other { "infix != " + other }
|
||||
|
||||
! { "prefix !" }
|
||||
- { "prefix -" }
|
||||
}
|
||||
|
||||
var foo = Foo.new
|
||||
io.write(foo + "a") // expect: infix + a
|
||||
io.write(foo - "a") // expect: infix - a
|
||||
io.write(foo * "a") // expect: infix * a
|
||||
io.write(foo / "a") // expect: infix / a
|
||||
io.write(foo % "a") // expect: infix % a
|
||||
io.write(foo < "a") // expect: infix < a
|
||||
io.write(foo > "a") // expect: infix > a
|
||||
io.write(foo <= "a") // expect: infix <= a
|
||||
io.write(foo >= "a") // expect: infix >= a
|
||||
io.write(foo == "a") // expect: infix == a
|
||||
io.write(foo != "a") // expect: infix != a
|
||||
io.write(!foo) // expect: prefix !
|
||||
io.write(-foo) // expect: prefix -
|
||||
Reference in New Issue
Block a user