pocketlang/tests/lang/class.pk

34 lines
636 B
Plaintext
Raw Normal View History

2021-06-20 23:28:31 +08:00
2022-04-21 19:53:03 +08:00
class Vec2
def _init(x, y)
self.x = x
self.y = y
end
2021-06-20 23:28:31 +08:00
2022-04-21 19:53:03 +08:00
def add(other)
return Vec2(self.x + other.x,
self.y + other.y)
end
2021-06-20 23:28:31 +08:00
2022-04-21 19:53:03 +08:00
## Note that operator overloading / friend functions
## haven't implemented at this point (to_string won't actually
## override it).
def to_string
return "[${self.x}, ${self.y}]"
end
2021-06-20 23:28:31 +08:00
2022-04-21 19:53:03 +08:00
end
2021-06-20 23:28:31 +08:00
2022-04-21 19:53:03 +08:00
v1 = Vec2(1, 2); assert(v1.x == 1 and v1.y == 2)
print("v1 = ${v1.to_string()}")
v2 = Vec2(3, 4); assert(v2.x == 3 and v2.y == 4)
print("v2 = ${v2.to_string()}")
v3 = v1.add(v2); assert(v3.x == 4 and v3.y == 6)
print("v3 = ${v3.to_string()}")
print('ALL TESTS PASSED')
2021-06-20 23:28:31 +08:00