aboutsummaryrefslogtreecommitdiff
path: root/test/metamethods.tm
blob: 91f79f91a475fe4fc7d4bf9f22e7dbe460e7375b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
struct Vec2(x,y:Int)
    func plus(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x+b.x, a.y+b.y)

    func minus(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x-b.x, a.y-b.y)

    func dot(a,b:Vec2 -> Int; inline)
        return a.x*b.x + a.y*b.y

    func scaled_by(a:Vec2, k:Int -> Vec2; inline)
        return Vec2(a.x*k, a.y*k)

    func times(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x*b.x, a.y*b.y)

    func divided_by(a:Vec2, k:Int -> Vec2; inline)
        return Vec2(a.x/k, a.y/k)

    func negative(v:Vec2 -> Vec2; inline)
        return Vec2(-v.x, -v.y)

    func negated(v:Vec2 -> Vec2; inline)
        return Vec2(not v.x, not v.y)

    func bit_and(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x and b.x, a.y and b.y)

    func bit_or(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x or b.x, a.y or b.y)

    func bit_xor(a,b:Vec2 -> Vec2; inline)
        return Vec2(a.x xor b.x, a.y xor b.y)

    func left_shifted(v:Vec2, bits:Int -> Vec2; inline)
        return Vec2(v.x >> bits, v.y >> bits)

    func right_shifted(v:Vec2, bits:Int -> Vec2; inline)
        return Vec2(v.x << bits, v.y << bits)

    func modulo(v:Vec2, modulus:Int -> Vec2; inline)
        return Vec2(v.x mod modulus, v.y mod modulus)

    func modulo1(v:Vec2, modulus:Int -> Vec2; inline)
        return Vec2(v.x mod1 modulus, v.y mod1 modulus)

func main()
    >> x := Vec2(10, 20)
    >> y := Vec2(100, 200)
    assert x + y == Vec2(x=110, y=220)
    assert x - y == Vec2(x=-90, y=-180)
    assert x * y == Vec2(x=1000, y=4000)
    assert x.dot(y) == 5000
    assert x * -1 == Vec2(x=-10, y=-20)
    assert -10 * x == Vec2(x=-100, y=-200)

    >> x = Vec2(1, 2)
    >> x += Vec2(10, 20)
    assert x == Vec2(x=11, y=22)
    >> x *= Vec2(10, -1)
    assert x == Vec2(x=110, y=-22)

    >> x *= -1
    assert x == Vec2(x=-110, y=22)

    >> x = Vec2(1, 2)
    assert -x == Vec2(x=-1, y=-2)

    x = Vec2(1, 2)
    y = Vec2(4, 3)
    assert (x and y) == Vec2(x=0, y=2)
    assert (x or y) == Vec2(x=5, y=3)
    assert (x xor y) == Vec2(x=5, y=1)
    assert x / 2 == Vec2(x=0, y=1)
    assert x mod 3 == Vec2(x=1, y=2)
    assert x mod1 3 == Vec2(x=1, y=2)