-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.hulk
196 lines (181 loc) · 4.8 KB
/
test.hulk
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
function tan(x: Number): Number => sin(x) / cos(x);
function cot(x) => 1 / tan(x);
function operate(x, y) {
print(x + y);
print(x - y);
print(x * y);
print(x / y);
}
function fib(n) => if (n == 0 or n == 1) 1 else fib(n-1) + fib(n-2);
function fact(x) => let f = 1 in for (i in range(1, x+1)) f := f * i;
function gcd(a, b) => while (a > 0)
let m = a % b in {
b := a;
a := m;
};
protocol Hashable {
hash(): Number;
}
protocol Equatable extends Hashable {
equals(other: Object): Boolean;
}
protocol Iterable {
next() : Boolean;
current() : Object;
}
type Range(min:Number, max:Number) {
min = min;
max = max;
current = min - 1;
next(): Boolean => (self.current := self.current + 1) < self.max;
current(): Number => self.current;
}
type Point(x,y) {
x = x;
y = y;
getX() => self.x;
getY() => self.y;
setX(x) => self.x := x;
setY(y) => self.y := y;
}
type PolarPoint(phi, rho) inherits Point(rho * sin(phi), rho * cos(phi)) {
rho() => sqrt(self.getX() ^ 2 + self.getY() ^ 2);
}
type Knight inherits Person {
name() => "Sir" @@ base();
}
type Person(firstname, lastname) {
firstname = firstname;
lastname = lastname;
name() => self.firstname @@ self.lastname;
hash() : Number {
5;
}
}
type Superman {
}
type Bird {
}
type Plane {
}
type A {
hello() => print("A");
}
type B inherits A {
hello() => print("B");
}
type C inherits A {
hello() => print("C");
}
{
42;
print(42);
print((((1 + 2) ^ 3) * 4) / 5);
print("Hello World");
print("The message is \"Hello World\"");
print("The meaning of life is " @ 42);
print(sin(2 * PI) ^ 2 + cos(3 * PI / log(4, 64)));
{
print(42);
print(sin(PI/2));
print("Hello World");
}
print(tan(PI) ** 2 + cot(PI) ** 2);
let msg = "Hello World" in print(msg);
let number = 42, text = "The meaning of life is" in
print(text @ number);
let number = 42 in
let text = "The meaning of life is" in
print(text @ number);
let number = 42 in (
let text = "The meaning of life is" in (
print(text @ number)
)
);
let a = 6, b = a * 7 in print(b);
let a = 6 in
let b = a * 7 in
print(b);
let a = 5, b = 10, c = 20 in {
print(a+b);
print(b*c);
print(c/a);
};
let a = (let b = 6 in b * 7) in print(a);
print(let b = 6 in b * 7);
let a = 20 in {
let a = 42 in print(a);
print(a);
};
let a = 7, a = 7 * 6 in print(a);
let a = 7 in
let a = 7 * 6 in
print(a);
let a = 0 in {
print(a);
a := 1;
print(a);
};
let a = 0 in
let b = a := 1 in {
print(a);
print(b);
};
let a = 42 in if (a % 2 == 0) print("Even") else print("odd");
let a = 42 in print(if (a % 2 == 0) "even" else "odd");
let a = 42 in
if (a % 2 == 0) {
print(a);
print("Even");
}
else print("Odd");
let a = 42, let mod = a % 3 in // error
print(
if (mod == 0) "Magic"
elif (mod % 3 == 1) "Woke"
else "Dumb"
);
let a = 10 in while (a >= 0) {
print(a);
a := a - 1;
}
for (x in range(0, 10)) print(x);
let iterable = range(0, 10) in
while (iterable.next())
let x = iterable.current() in
print(x);
let pt = new Point() in // error
print("x: " @ pt.getX() @ "; y: " @ pt.getY());
let pt = new Point(3,4) in
print("x: " @ pt.getX() @ "; y: " @ pt.getY());
let pt = new PolarPoint(3,4) in
print("rho: " @ pt.rho());
let p = new Knight("Phil", "Collins") in
print(p.name());
let p: Person = new Knight("Phil", "Collins") in print(p.name());
let x: Number = 42 in print(x);
let x = new Superman() in
print(
if (x is Bird) "It's bird!"
elif (x is Plane) "It's a plane!"
else "No, it's Superman!"
);
let x = 42 in print(x);
let total = { print("Total"); 5; } + 6 in print(total);
let x : A = if (rand() < 0.5) new B() else new C() in
if (x is B)
let y : B = x as B in {
y.hello();
}
else {
print("x cannot be downcasted to B");
};
let numbers = [1,2,3,4,5,6,7,8,9] in
for (x in numbers)
print(x);
let numbers = [1,2,3,4,5,6,7,8,9] in print(numbers[7]);
let squares = [x^2 || x in range(1,10)] in print(x); // error
let squares = [x^2 || x in range(1,10)] in for (x in squares) print(x);
let x : Hashable = new Person() in print(x.hash());
let x : Hashable = new Point(0,0) in print(x.hash()); // error
}