aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-07-01 12:30:07 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-07-01 12:30:07 -0400
commit37a7beb28f4b69ffaefe7f701a9902e20b721e97 (patch)
treeda2aad1b83d0b636020308b1c2dde95bcce1125b /test
parent0c15c74352b64ab5e790802bad84ded18c875c65 (diff)
Add test for metamethods
Diffstat (limited to 'test')
-rw-r--r--test/metamethods.tm38
1 files changed, 38 insertions, 0 deletions
diff --git a/test/metamethods.tm b/test/metamethods.tm
new file mode 100644
index 00000000..97d9e5eb
--- /dev/null
+++ b/test/metamethods.tm
@@ -0,0 +1,38 @@
+struct Vec2(x,y:Int):
+ func __add(a,b:Vec2; inline)->Vec2:
+ return Vec2(a.x+b.x, a.y+b.y)
+
+ func __sub(a,b:Vec2; inline)->Vec2:
+ return Vec2(a.x-b.x, a.y-b.y)
+
+ func __mul(a,b:Vec2; inline)->Int:
+ return a.x*b.x + a.y*b.y
+
+ func __mul2(a:Vec2,b:Int; inline)->Vec2:
+ return Vec2(a.x*b, a.y*b)
+
+ func __mul3(a:Int,b:Vec2; inline)->Vec2:
+ return Vec2(a*b.x, a*b.y)
+
+ func __mul4(a,b:Vec2; inline)->Vec2:
+ return Vec2(a.x*b.x, a.y*b.y)
+
+func main():
+ >> x := Vec2(10, 20)
+ >> y := Vec2(100, 200)
+ >> x + y
+ = Vec2(x=110, y=220)
+ >> x - y
+ = Vec2(x=-90, y=-180)
+ >> x * y
+ = 5000
+ >> x * -1
+ = Vec2(x=-10, y=-20)
+ >> -10 * x
+ = Vec2(x=-100, y=-200)
+
+ >> x = Vec2(1, 2)
+ >> x += Vec2(10, 20)
+ = Vec2(x=11, y=22)
+ >> x *= Vec2(10, -1)
+ = Vec2(x=110, y=-22)