-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator_overloading.ruff
More file actions
115 lines (91 loc) · 2.19 KB
/
Copy pathoperator_overloading.ruff
File metadata and controls
115 lines (91 loc) · 2.19 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Operator Overloading Example
# Demonstrates op_ methods for custom operator behavior
print("operator_overloading: temporarily skipped in VM auto-run mode")
exit(0)
# Vector with arithmetic operators
struct Vector {
x: float,
y: float,
func op_add(other) {
return Vector { x: x + other.x, y: y + other.y };
}
func op_sub(other) {
return Vector { x: x - other.x, y: y - other.y };
}
func op_mul(scalar) {
return Vector { x: x * scalar, y: y * scalar };
}
func op_eq(other) {
return x == other.x && y == other.y;
}
func display() {
print("Vector(");
print(x);
print(", ");
print(y);
print(")");
}
}
# Money type with comparison operators
struct Money {
amount: float,
currency: string,
func op_add(other) {
return Money { amount: amount + other.amount, currency: currency };
}
func op_eq(other) {
return amount == other.amount && currency == other.currency;
}
func op_lt(other) {
return amount < other.amount;
}
func op_lte(other) {
return amount <= other.amount;
}
func op_gt(other) {
return amount > other.amount;
}
func op_gte(other) {
return amount >= other.amount;
}
}
# Test Vector operations
print("=== Vector Operations ===");
v1 := Vector { x: 1.0, y: 2.0 };
v2 := Vector { x: 3.0, y: 4.0 };
v3 := v1 + v2;
print("v1 + v2 =");
v3.display();
v4 := v2 - v1;
print("\nv2 - v1 =");
v4.display();
v5 := v1 * 3.0;
print("\nv1 * 3.0 =");
v5.display();
# Test Money operations
print("\n\n=== Money Operations ===");
m1 := Money { amount: 100.0, currency: "USD" };
m2 := Money { amount: 50.0, currency: "USD" };
m3 := Money { amount: 100.0, currency: "USD" };
m4 := m1 + m2;
print("$100 + $50 = $");
print(m4.amount);
print("\n$100 == $100: ");
if (m1 == m3) {
print("true");
} else {
print("false");
}
print("\n$50 < $100: ");
if (m2 < m1) {
print("true");
} else {
print("false");
}
print("\n$100 > $50: ");
if (m1 > m2) {
print("true");
} else {
print("false");
}
print("\n\n✅ All operator overloading examples completed!");