mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 04:37:47 +08:00
34 lines
636 B
Plaintext
34 lines
636 B
Plaintext
|
|
class Vec2
|
|
def _init(x, y)
|
|
self.x = x
|
|
self.y = y
|
|
end
|
|
|
|
def add(other)
|
|
return Vec2(self.x + other.x,
|
|
self.y + other.y)
|
|
end
|
|
|
|
## 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
|
|
|
|
end
|
|
|
|
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')
|
|
|
|
|