-
Notifications
You must be signed in to change notification settings - Fork 115
/
constants.tact
337 lines (294 loc) · 10.8 KB
/
constants.tact
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
const someGlobalConst: Int = 100;
const globalConst1: Int = 1;
const globalConst2: Int = globalConst1 + 1; // 2
const globalConst3: Int = globalFun1(globalConst1) + 1; // 4
const globalConst4: Int = globalFun2(globalConst1) + globalConst3; // 15
const globalConst5: Int = globalFun3(globalConst4); // 15
const globalConst6: Int = globalFun4(globalConst5 + 10); // 26
const globalConst7: Int = globalFun5(globalConst6 + 1, globalConst5 + 10); // 27
const globalConst8: Int = GCD_recursive(globalConst6, globalConst7 + 1); // GCD(26, 28) = 2
const globalConst9: Int = GCD_iterative(globalConst6, globalConst7 + 1); // 2
const globalConst10: Int = factorial_recursive(globalConst3); // 4! = 24
const globalConst11: Int = factorial_iterative(globalConst3); // 4! = 24
const globalConst12: Int = fibonacci_recursive(globalConst3 + 2); // fibonacci(6) = 8
const globalConst13: Int = fibonacci_iterative(globalConst3 + 2); // fibonacci(6) = 8
struct S {
a: Bool;
b: Int;
}
struct T {
a: Int;
s: S;
}
// Global functions
// Test assignments
fun globalFun1(v: Int): Int {
let i = v;
let j = 1;
let k = i + j;
k = k * 10;
k += 2;
k -= 1;
k /= 2;
k *= 5;
k %= 2;
k |= 9;
k &= 22;
k ^= 3;
return k;
}
// Test repeat
fun globalFun2(v: Int): Int {
let j = v;
repeat(10) {
let i = 1;
j += i;
}
return j;
}
// Test do..until
fun globalFun3(v: Int): Int {
let i = 20;
do {
i -= 1;
} until (i <= v);
return i;
}
// Test while
fun globalFun4(v: Int): Int {
let i = 20;
while (i <= v) {
i += 1;
}
return i;
}
// MAX, test conditionals
fun globalFun5(a: Int, b: Int): Int {
if (a <= b) {
return b;
} else {
return a;
}
}
// Recursive GCD for non-negative integers
fun GCD_recursive(a: Int, b: Int): Int {
if (a < 0 || b < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
if (b == 0) {
return a;
} else {
return GCD_recursive(b, a % b);
}
}
// Iterative GCD for non-negative integers
fun GCD_iterative(a: Int, b: Int): Int {
if (a < 0 || b < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
let na = a;
let nb = b;
while (nb != 0) {
let temp = nb;
nb = na % nb;
na = temp;
}
return na;
}
fun factorial_recursive(a: Int): Int {
if (a < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
if (a <= 1) {
return 1;
} else {
return a * factorial_recursive(a - 1);
}
}
fun factorial_iterative(a: Int): Int {
if (a < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
let result = 1;
let i = 2;
while (i <= a) {
result *= i;
i += 1;
}
return result;
}
fun fibonacci_recursive(a: Int): Int {
if (a < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
if (a <= 1) {
return a;
} else {
return fibonacci_recursive(a - 2) + fibonacci_recursive(a - 1);
}
}
fun fibonacci_iterative(a: Int): Int {
if (a < 0) {
// there will be a throws error here once
// try-catch is implemented in
// the interpreter
return 0;
}
if (a <= 1) {
return a;
}
let prev = 0;
let current = 1;
repeat (a - 1) {
let temp = current;
current += prev;
prev = temp;
}
return current;
}
contract ConstantTester {
const something1: Int = 10 + 1;
const something2: Int? = null;
const something3: Int = ton("123");
const something4: Int = ton("123") + ton("33.3") * 10;
const something5: String = "Hello world!";
const something6: Int = 10 * 1;
const something7: Int = 10 >> 1;
const something8: Int = (2 + 4) & 4;
const something9: Address = address("UQBKgXCNLPexWhs2L79kiARR1phGH1LwXxRbNsCFF9doczSI");
const something10: Address = newAddress(0, 0x4a81708d2cf7b15a1b362fbf64880451d698461f52f05f145b36c08517d76873);
const something11: Int = 123 ^ 35;
const something12: Int = -123 ^ 35;
const something13: Int = -123 ^ -35;
const something14: Int = pow(3, 5);
const something15: Int = pow2(5);
const something16: Int = -115792089237316195423570985008687907853269984665640564039457584007913129639936;
const something17: Int = 115792089237316195423570985008687907853269984665640564039457584007913129639935;
const something18: Int = -(pow2(255) - 1 + pow2(255));
const something19: Int = -(pow2(255) - 1 + pow2(255)) - 1;
const something20: Int = ~5;
// division rounds towards negative infinity, like Python's `//` operator
const something21: Int = 1 / 5; // = 0, as one would expect
const something22: Int = -1 / 5; // = -1, not zero as in JS/TS
const something23: Int = 1 / -5; // = -1, not zero as in JS/TS
const something24: Int = -1 / -5; // = 0, as one would expect
// modulo rounds towards negative infinity too
// the following holds: a / b * b + a % b == a, for all b != 0
const something25: Int = 1 % 5; // = 1
const something26: Int = -1 % 5; // = 4
const something27: Int = 1 % -5; // = -4
const something28: Int = -1 % -5; // = -1
const something29: Int? = true ? 42 : null;
const something30: Int? = false ? 42 : null;
const something31: Int = false ? (1 / 0) : 42; // no exception, then branch is ignored
const something32: Int = (true ? 42 : null)!!;
const something33: map<Int, Int> = emptyMap();
const something34: map<Int, Int> = null;
const something35: S = S {a: false || false, b: 21 + 21};
const something36: S = S {b: 21 + 21, a: false || false};
const something37: T = T {
a: pow(3, 3) + 3 * 5,
s: S {a: true ? 1 == 1 : false, b: ton("0.000000042")}
};
const something38: Int = 5 & 6 | 1 << (5 + 11) * 3 % 12 >> 11; // = 4
const something39: Bool = 42 == 43 || "foo" == "bar" || true != true ||
3 < 1 || 3 <= 1 || 1 < 3 || 1 <= 3 ||
newAddress(0, 0x606813c5f6a76175eae668630c6d8ffe229543610e3d204db245dd51f9ba0503)
!=
newAddress(0, 0x606813c5f6a76175eae668630c6d8ffe229543610e3d204db245dd51f9ba0503) ||
!(true && false);
const something40: Bool = T {
a: pow(3, 3) + 3 * 5,
s: S {a: true ? 1 == 1 : false, b: ton("0.000000042")}
}.s.a;
const something41: Address = newAddress(0, 0);
const something42: Address = newAddress(0, 0x12345);
const something43: Address = newAddress(0, 0x123456789abcdef);
const something44: Address = newAddress(0, 0x4a81708d2cf7b15a1b362fbf64880451d698461f52f05f145b36c08517d76873);
init() { }
receive() { }
get fun something1(): Int { return self.something1; }
get fun something2(): Int? { return self.something2; }
get fun something3(): Int { return self.something3; }
get fun something4(): Int { return self.something4; }
get fun something5(): String { return self.something5; }
get fun something6(): Int { return self.something6; }
get fun something7(): Int { return self.something7; }
get fun something8(): Int { return self.something8; }
get fun something9(): Address { return self.something9; }
get fun something10(): Address { return self.something10; }
get fun something11(): Int { return self.something11; }
get fun something12(): Int { return self.something12; }
get fun something13(): Int { return self.something13; }
get fun something14(): Int { return self.something14; }
get fun something15(): Int { return self.something15; }
get fun something16(): Int { return self.something16; }
get fun something17(): Int { return self.something17; }
get fun something18(): Int { return self.something18; }
get fun something19(): Int { return self.something19; }
get fun something20(): Int { return self.something20; }
get fun something21(): Int { return self.something21; }
get fun something22(): Int { return self.something22; }
get fun something23(): Int { return self.something23; }
get fun something24(): Int { return self.something24; }
get fun something25(): Int { return self.something25; }
get fun something26(): Int { return self.something26; }
get fun something27(): Int { return self.something27; }
get fun something28(): Int { return self.something28; }
get fun something29(): Int? { return self.something29; }
get fun something30(): Int? { return self.something30; }
get fun something31(): Int { return self.something31; }
get fun something32(): Int { return self.something32; }
get fun something33(): map<Int, Int> { return self.something33; }
get fun something34(): map<Int, Int> { return self.something34; }
get fun something35(): S { return self.something35; }
get fun something36(): S { return self.something36; }
get fun something37(): T { return self.something37; }
get fun something38(): Int { return self.something38; }
get fun something39(): Bool { return self.something39; }
get fun something40(): Bool { return self.something40; }
get fun something41(): Address { return self.something41; }
get fun something42(): Address { return self.something42; }
get fun something43(): Address { return self.something43; }
get fun something44(): Address { return self.something44; }
get fun globalConst1(): Int { return globalConst1; }
get fun globalConst2(): Int { return globalConst2; }
get fun globalConst3(): Int { return globalConst3; }
get fun globalConst4(): Int { return globalConst4; }
get fun globalConst5(): Int { return globalConst5; }
get fun globalConst6(): Int { return globalConst6; }
get fun globalConst7(): Int { return globalConst7; }
get fun globalConst8(): Int { return globalConst8; }
get fun globalConst9(): Int { return globalConst9; }
get fun globalConst10(): Int { return globalConst10; }
get fun globalConst11(): Int { return globalConst11; }
get fun globalConst12(): Int { return globalConst12; }
get fun globalConst13(): Int { return globalConst13; }
get fun minInt1(): Int {
return -115792089237316195423570985008687907853269984665640564039457584007913129639936;
}
get fun minInt2(): Int {
return -(pow2(255) - 1 + pow2(255));
}
get fun minInt3(): Int {
return -(pow2(255) - 1 + pow2(255)) - 1;
}
get fun globalConst(): Int {
return someGlobalConst;
}
}