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:
Bob Nystrom
2013-11-14 18:27:33 +00:00
parent d56828b65b
commit 338dc2288f
2 changed files with 164 additions and 101 deletions
+31
View File
@@ -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 -