forked from kuroko-lang/kuroko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_numeric.c
853 lines (728 loc) · 24.7 KB
/
obj_numeric.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#include <string.h>
#include <kuroko/vm.h>
#include <kuroko/value.h>
#include <kuroko/memory.h>
#include <kuroko/util.h>
#include "private.h"
#undef bool
#define IS_NoneType(o) (IS_NONE(o))
#define AS_NoneType(o) ((char)0)
#define CURRENT_CTYPE krk_integer_type
#define CURRENT_NAME self
FUNC_SIG(int,__init__) {
static __attribute__ ((unused)) const char* _method_name = "__init__";
METHOD_TAKES_AT_MOST(2);
if (argc < 2) return INTEGER_VAL(0);
if (IS_BOOLEAN(argv[1])) return INTEGER_VAL(AS_INTEGER(argv[1]));
if (IS_INTEGER(argv[1])) return argv[1];
if (IS_STRING(argv[1])) {
krk_integer_type _base = 10;
if (argc > 2) {
CHECK_ARG(2,int,krk_integer_type,base);
_base = base;
}
KrkValue result = krk_parse_int(AS_CSTRING(argv[1]), AS_STRING(argv[1])->length, _base);
if (IS_NONE(result)) {
return krk_runtimeError(vm.exceptions->valueError,
"invalid literal for int() with base %zd: %R", (ssize_t)_base, argv[1]);
}
return result;
}
if (IS_FLOATING(argv[1])) return INTEGER_VAL(AS_FLOATING(argv[1]));
if (IS_BOOLEAN(argv[1])) return INTEGER_VAL(AS_BOOLEAN(argv[1]));
return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%T'", "int", argv[1]);
}
KRK_Method(int,__str__) {
char tmp[100];
size_t l = snprintf(tmp, 100, PRIkrk_int, self);
return OBJECT_VAL(krk_copyString(tmp, l));
}
KRK_Method(int,__int__) { return argv[0]; }
KRK_Method(int,__float__) { return FLOATING_VAL(self); }
KRK_Method(int,__chr__) {
unsigned char bytes[5] = {0};
size_t len = krk_codepointToBytes(self, bytes);
return OBJECT_VAL(krk_copyString((char*)bytes, len));
}
KRK_Method(int,__eq__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) return self == AS_INTEGER(argv[1]);
else if (IS_FLOATING(argv[1])) return self == AS_FLOATING(argv[1]);
return NOTIMPL_VAL();
}
KRK_Method(int,__hash__) {
return INTEGER_VAL((uint32_t)AS_INTEGER(argv[0]));
}
static inline int matches(char c, const char * options) {
for (const char * o = options; *o; ++o) {
if (*o == c) return 1;
}
return 0;
}
const char * krk_parseCommonFormatSpec(struct ParsedFormatSpec *result, const char * spec, size_t length) {
result->fill = " ";
result->fillSize = 1;
if (length > 1) {
/* How wide is the first character? */
int i = 1;
if ((spec[0] & 0xC0) == 0xC0) { /* wider than one byte */
while ((spec[i] & 0xc0) == 0x80) i++; /* count continuation bytes */
}
/* Is the character after it an alignment? */
if (matches(spec[i],"<>=^")) {
result->fill = spec;
result->fillSize = i;
spec += i;
}
}
if (matches(*spec,"<>=^")) {
result->align = *spec;
spec++;
}
if (matches(*spec,"+- ")) {
result->sign = *spec;
spec++;
}
if (*spec == '#') {
result->alt = 1;
spec++;
}
if (!result->align && *spec == '0') {
result->align = '=';
result->fill = "0";
result->fillSize = 1;
spec++;
}
if (matches(*spec,"0123456789")) {
result->hasWidth = 1;
do {
result->width *= 10;
result->width += (*spec - '0');
spec++;
} while (matches(*spec,"0123456789"));
}
if (matches(*spec, "_,")) {
result->sep = *spec;
spec++;
}
if (*spec == '.') {
spec++;
if (!matches(*spec,"0123456789")) {
krk_runtimeError(vm.exceptions->valueError, "Format specifier missing precision");
return NULL;
}
result->hasPrecision = 1;
while (matches(*spec,"0123456789")) {
result->prec *= 10;
result->prec += (*spec - '0');
spec++;
}
}
if (*spec && spec[1] != 0) {
krk_runtimeError(vm.exceptions->valueError, "Invalid format specifier");
return NULL;
}
return spec;
}
KrkValue krk_doFormatString(const char * typeName, KrkString * format_spec, int positive, void * abs, int (*callback)(void *,int,int*)) {
struct ParsedFormatSpec opts = {0};
const char * spec = krk_parseCommonFormatSpec(&opts, format_spec->chars, format_spec->length);
if (!spec) return NONE_VAL();
const char * altPrefix = NULL;
const char * conversions = "0123456789abcdef";
int base = 0;
switch (*spec) {
case 0: /* unspecified */
case 'd': /* decimal integer */
base = 10;
break;
case 'b': /* binary */
base = 2;
altPrefix = "0b";
break;
case 'o': /* octal */
base = 8;
altPrefix = "0o";
break;
case 'x': /* hex */
base = 16;
altPrefix = "0x";
break;
case 'X': /* HEX */
base = 16;
conversions = "0123456789ABCDEF";
altPrefix = "0X";
break;
case 'c': /* unicode codepoint */
return krk_runtimeError(vm.exceptions->notImplementedError,
"TODO: 'c' format specifier");
case 'n': /* use local-specific separators (TODO) */
return krk_runtimeError(vm.exceptions->notImplementedError,
"TODO: 'n' format specifier");
default:
return krk_runtimeError(vm.exceptions->valueError,
"Unknown format code '%c' for object of type '%s'",
*spec,
typeName);
}
if (!opts.sign) opts.sign = '-';
if (!opts.align) opts.align = '>';
struct StringBuilder sb = {0};
int width = opts.width;
int l = 0;
if (opts.alt && altPrefix && width > 2) width -= 2;
if ((!positive || opts.sign == '+') && width > 1) width--;
int digits = 0;
int sepcount = opts.sep == ',' ? 3 : 4;
int more = 0;
do {
int digit = callback(abs, base, &more);
if (unlikely(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) {
discardStringBuilder(&sb);
return NONE_VAL();
}
if (digits && !more && digit == 0) {
/* Add backwards */
for (int i = 0; i < opts.fillSize; ++i) {
pushStringBuilder(&sb, opts.fill[opts.fillSize-1-i]);
}
} else {
pushStringBuilder(&sb, conversions[digit]);
}
l++;
digits++;
if (opts.sep && !(digits % sepcount) && (more || (opts.align == '=' && l < width))) {
pushStringBuilder(&sb, opts.sep);
l++;
if (opts.align == '=' && l == width) {
/* Add backwards */
for (int i = 0; i < opts.fillSize; ++i) {
pushStringBuilder(&sb, opts.fill[opts.fillSize-1-i]);
}
}
}
} while (more || (opts.align == '=' && l < width));
if (opts.alt && altPrefix) {
pushStringBuilder(&sb, altPrefix[1]);
pushStringBuilder(&sb, altPrefix[0]);
}
if (!positive || opts.sign == '+') {
pushStringBuilder(&sb, positive ? '+' : '-');
}
if (opts.align == '>') {
while (l < width) {
for (int i = 0; i < opts.fillSize; ++i) {
pushStringBuilder(&sb, opts.fill[opts.fillSize-1-i]);
}
l++;
}
} else if (opts.align == '^') {
int remaining = (width - l) / 2;
for (int i = 0; i < remaining; ++i) {
for (int i = 0; i < opts.fillSize; ++i) {
pushStringBuilder(&sb, opts.fill[opts.fillSize-1-i]);
}
l++;
}
}
for (size_t i = 0; i < sb.length / 2; ++i) {
char t = sb.bytes[i];
sb.bytes[i] = sb.bytes[sb.length - i - 1];
sb.bytes[sb.length - i - 1] = t;
}
if (opts.align == '<' || opts.align == '^') {
while (l < width) {
pushStringBuilderStr(&sb, opts.fill, opts.fillSize);
l++;
}
}
return finishStringBuilder(&sb);
}
static int formatIntCallback(void * a, int base, int *more) {
krk_integer_type v = *(krk_integer_type*)a;
int digit = v % base;
v /= base;
*(krk_integer_type*)a = v;
*more = v > 0;
return digit;
}
KRK_Method(int,__format__) {
METHOD_TAKES_EXACTLY(1);
CHECK_ARG(1,str,KrkString*,format_spec);
krk_integer_type abs = self < 0 ? -self : self;
return krk_doFormatString(krk_typeName(argv[0]), format_spec,
self >= 0,
&abs,
formatIntCallback);
}
/**
* We _could_ use the __builtin_XXX_overflow(_p) functions gcc+clang provide,
* but let's just do this ourselves, I guess?
*
* We cheat: We only bother calculating + checking if both values would fit
* in int32_t's. This ensures multiplication works fine.
*
* For any case where an int32_t would overflow, we do the 'long' operation
* and then reduce if that still yields something that would fit in our 'int48'.
*/
#define OVERFLOW_CHECKED_INT_OPERATION(name,operator) \
extern KrkValue krk_long_coerced_ ## name (krk_integer_type a, krk_integer_type b); \
_noexport \
KrkValue krk_int_op_ ## name (krk_integer_type a, krk_integer_type b) { \
if (likely((int32_t)a == a && (int32_t)b == b)) { \
int32_t result_one = a operator b; \
int64_t result_two = a operator b; \
if (likely(result_one == result_two)) return INTEGER_VAL(result_two); \
} \
return krk_long_coerced_ ## name (a, b); \
}
OVERFLOW_CHECKED_INT_OPERATION(add,+)
OVERFLOW_CHECKED_INT_OPERATION(sub,-)
OVERFLOW_CHECKED_INT_OPERATION(mul,*)
#define BASIC_BIN_OP(name,operator) \
KRK_Method(int,__ ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return krk_int_op_ ## name(self, AS_INTEGER(argv[1])); \
else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL((double)self operator AS_FLOATING(argv[1])); \
return NOTIMPL_VAL(); \
} \
KRK_Method(int,__r ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return krk_int_op_ ## name(AS_INTEGER(argv[1]), self); \
else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) operator (double)self); \
return NOTIMPL_VAL(); \
}
#define INT_ONLY_BIN_OP(name,operator) \
KRK_Method(int,__ ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return INTEGER_VAL(self operator AS_INTEGER(argv[1])); \
return NOTIMPL_VAL(); \
} \
KRK_Method(int,__r ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return INTEGER_VAL(AS_INTEGER(argv[1]) operator self); \
return NOTIMPL_VAL(); \
}
#define COMPARE_OP(name,operator) \
KRK_Method(int,__ ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return BOOLEAN_VAL(self operator AS_INTEGER(argv[1])); \
else if (likely(IS_FLOATING(argv[1]))) return BOOLEAN_VAL((double)self operator AS_FLOATING(argv[1])); \
return NOTIMPL_VAL(); \
}
BASIC_BIN_OP(add,+)
BASIC_BIN_OP(sub,-)
BASIC_BIN_OP(mul,*)
INT_ONLY_BIN_OP(or,|)
INT_ONLY_BIN_OP(xor,^)
INT_ONLY_BIN_OP(and,&)
#define DEFER_TO_LONG(name) \
extern KrkValue krk_long_coerced_ ## name (krk_integer_type a, krk_integer_type b); \
KRK_Method(int,__ ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return krk_long_coerced_ ## name (self, AS_INTEGER(argv[1])); \
return NOTIMPL_VAL(); \
} \
KRK_Method(int,__r ## name ## __) { \
if (likely(IS_INTEGER(argv[1]))) return krk_long_coerced_ ## name (AS_INTEGER(argv[1]), self); \
return NOTIMPL_VAL(); \
}
DEFER_TO_LONG(lshift)
DEFER_TO_LONG(rshift)
DEFER_TO_LONG(pow)
COMPARE_OP(lt, <)
COMPARE_OP(gt, >)
COMPARE_OP(le, <=)
COMPARE_OP(ge, >=)
#undef BASIC_BIN_OP
#undef INT_ONLY_BIN_OP
#undef COMPARE_OP
KRK_Method(int,__truediv__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) {
krk_integer_type b = AS_INTEGER(argv[1]);
if (unlikely(b == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero");
return FLOATING_VAL((double)self / (double)b);
} else if (likely(IS_FLOATING(argv[1]))) {
double b = AS_FLOATING(argv[1]);
if (unlikely(b == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
return FLOATING_VAL((double)self / b);
}
return NOTIMPL_VAL();
}
KRK_Method(int,__rtruediv__) {
METHOD_TAKES_EXACTLY(1);
if (unlikely(self == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero");
else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / (double)self);
else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) / (double)self);
return NOTIMPL_VAL();
}
#ifdef __TINYC__
#include <math.h>
#define __builtin_floor floor
#endif
/**
* These have been corrected to match the behavior with negatives
* that Python produces, for compatibility, and also because that's
* what our 'long' type does...
*/
static KrkValue _krk_int_div(krk_integer_type a, krk_integer_type b) {
if (unlikely(b == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division or modulo by zero");
if (a == 0) return INTEGER_VAL(0);
int64_t abs_a = a < 0 ? -a : a;
int64_t abs_b = b < 0 ? -b : b;
if ((a < 0) != (b < 0)) {
/* If signs don't match, the result is negative, and rounding down means away from 0... */
int64_t res = -1 - (abs_a - 1) / abs_b;
return INTEGER_VAL(res);
}
return INTEGER_VAL((abs_a / abs_b));
}
static KrkValue _krk_int_mod(krk_integer_type a, krk_integer_type b) {
if (unlikely(b == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division or modulo by zero");
if (a == 0) return INTEGER_VAL(0);
int64_t abs_a = a < 0 ? -a : a;
int64_t abs_b = b < 0 ? -b : b;
int64_t res;
if ((a < 0) != (b < 0)) {
/* If quotient would be negative, then remainder is inverted against the divisor. */
res = (abs_b - 1 - (abs_a - 1) % abs_b);
} else {
res = abs_a % abs_b;
}
/* Negative divisor always yields negative remainder, except when it's 0... */
return INTEGER_VAL((b < 0) ? -res : res);
}
KRK_Method(int,__mod__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) return _krk_int_mod(self, AS_INTEGER(argv[1]));
return NOTIMPL_VAL();
}
KRK_Method(int,__rmod__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) return _krk_int_mod(AS_INTEGER(argv[1]), self);
return NOTIMPL_VAL();
}
KRK_Method(int,__floordiv__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) {
return _krk_int_div(self,AS_INTEGER(argv[1]));
} else if (likely(IS_FLOATING(argv[1]))) {
double b = AS_FLOATING(argv[1]);
if (unlikely(b == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
return FLOATING_VAL(__builtin_floor((double)self / b));
}
return NOTIMPL_VAL();
}
KRK_Method(int,__rfloordiv__) {
METHOD_TAKES_EXACTLY(1);
if (unlikely(self == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero");
else if (likely(IS_INTEGER(argv[1]))) return _krk_int_div(AS_INTEGER(argv[1]), self);
else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(__builtin_floor(AS_FLOATING(argv[1]) / (double)self));
return NOTIMPL_VAL();
}
KRK_Method(int,__hex__) {
METHOD_TAKES_NONE();
char tmp[20];
unsigned long long val = self < 0 ? -self : self;
size_t len = snprintf(tmp, 20, "%s0x%llx", self < 0 ? "-" : "", val);
return OBJECT_VAL(krk_copyString(tmp,len));
}
KRK_Method(int,__oct__) {
METHOD_TAKES_NONE();
char tmp[20];
unsigned long long val = self < 0 ? -self : self;
size_t len = snprintf(tmp, 20, "%s0o%llo", self < 0 ? "-" : "", val);
return OBJECT_VAL(krk_copyString(tmp,len));
}
KRK_Method(int,__bin__) {
METHOD_TAKES_NONE();
unsigned long long val = self;
if (self < 0) val = -val;
struct StringBuilder sb = {0};
if (!val) pushStringBuilder(&sb, '0');
while (val) {
pushStringBuilder(&sb, (val & 1) ? '1' : '0');
val = val >> 1;
}
pushStringBuilder(&sb, 'b');
pushStringBuilder(&sb, '0');
if (self< 0) pushStringBuilder(&sb,'-');
/* Flip it */
for (size_t i = 0; i < sb.length / 2; ++i) {
char t = sb.bytes[i];
sb.bytes[i] = sb.bytes[sb.length - i - 1];
sb.bytes[sb.length - i - 1] = t;
}
return finishStringBuilder(&sb);
}
KRK_Method(int,__invert__) {
return INTEGER_VAL(~self);
}
KRK_Method(int,__neg__) {
return INTEGER_VAL(-self);
}
KRK_Method(int,__abs__) {
return self < 0 ? INTEGER_VAL(-self) : INTEGER_VAL(self);
}
KRK_Method(int,__pos__) {
return argv[0];
}
#undef CURRENT_CTYPE
#define CURRENT_CTYPE double
#define trySlowMethod(name) do { \
KrkClass * type = krk_getType(argv[1]); \
KrkValue method; \
while (type) { \
if (krk_tableGet(&type->methods, name, &method)) { \
krk_push(method); \
krk_push(argv[1]); \
return krk_callStack(1); \
} \
type = type->base; \
} \
} while (0)
FUNC_SIG(float,__init__) {
static __attribute__ ((unused)) const char* _method_name = "__init__";
METHOD_TAKES_AT_MOST(1);
if (argc < 2) return FLOATING_VAL(0.0);
if (IS_FLOATING(argv[1])) return argv[1];
if (IS_INTEGER(argv[1])) return FLOATING_VAL(AS_INTEGER(argv[1]));
if (IS_BOOLEAN(argv[1])) return FLOATING_VAL(AS_BOOLEAN(argv[1]));
trySlowMethod(vm.specialMethodNames[METHOD_FLOAT]);
return krk_runtimeError(vm.exceptions->typeError, "%s() argument must be a string or a number, not '%T'", "float", argv[1]);
}
KRK_Method(float,__int__) { return INTEGER_VAL(self); }
KRK_Method(float,__float__) { return argv[0]; }
static int isDigits(const char * c) {
while (*c) {
if (*c < '0' || *c > '9') return 0;
c++;
}
return 1;
}
KRK_Method(float,__str__) {
char tmp[100];
size_t l = snprintf(tmp, 97, "%.16g", self);
if (!strstr(tmp,".") && isDigits(tmp)) {
l = snprintf(tmp,100,"%.16g.0",self);
}
return OBJECT_VAL(krk_copyString(tmp, l));
}
KRK_Method(float,__eq__) {
METHOD_TAKES_EXACTLY(1);
if (IS_INTEGER(argv[1])) return self == (double)AS_INTEGER(argv[1]);
else if (IS_FLOATING(argv[1])) return self == AS_FLOATING(argv[1]);
return NOTIMPL_VAL();
}
KRK_Method(float,__hash__) {
return INTEGER_VAL((uint32_t)self);
}
KRK_Method(float,__neg__) {
return FLOATING_VAL(-self);
}
KRK_Method(float,__abs__) {
return self < 0.0 ? FLOATING_VAL(-self) : INTEGER_VAL(self);
}
#define BASIC_BIN_OP(name,operator) \
KRK_Method(float,__ ## name ## __) { \
METHOD_TAKES_EXACTLY(1); \
if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(self operator AS_FLOATING(argv[1])); \
else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL(self operator (double)AS_INTEGER(argv[1])); \
return NOTIMPL_VAL(); \
} \
KRK_Method(float,__r ## name ## __) { \
METHOD_TAKES_EXACTLY(1); \
if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) operator self); \
else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) operator self); \
return NOTIMPL_VAL(); \
}
#define COMPARE_OP(name,operator) \
KRK_Method(float,__ ## name ## __) { \
METHOD_TAKES_EXACTLY(1); \
if (likely(IS_FLOATING(argv[1]))) return BOOLEAN_VAL(self operator AS_FLOATING(argv[1])); \
else if (likely(IS_INTEGER(argv[1]))) return BOOLEAN_VAL(self operator (double)AS_INTEGER(argv[1])); \
return NOTIMPL_VAL(); \
}
BASIC_BIN_OP(add,+)
BASIC_BIN_OP(sub,-)
BASIC_BIN_OP(mul,*)
COMPARE_OP(lt, <)
COMPARE_OP(gt, >)
COMPARE_OP(le, <=)
COMPARE_OP(ge, >=)
#undef BASIC_BIN_OP
#undef COMPARE_OP
KRK_Method(float,__truediv__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_FLOATING(argv[1]))) {
double b = AS_FLOATING(argv[1]);
if (unlikely(b == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
return FLOATING_VAL(self / b);
} else if (likely(IS_INTEGER(argv[1]))) {
krk_integer_type b = AS_INTEGER(argv[1]);
if (unlikely(b == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero");
return FLOATING_VAL(self / (double)b);
}
return NOTIMPL_VAL();
}
KRK_Method(float,__rtruediv__) {
METHOD_TAKES_EXACTLY(1);
if (unlikely(self == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
else if (likely(IS_FLOATING(argv[1]))) return FLOATING_VAL(AS_FLOATING(argv[1]) / self);
else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / self);
return NOTIMPL_VAL();
}
KRK_Method(float,__floordiv__) {
METHOD_TAKES_EXACTLY(1);
if (likely(IS_INTEGER(argv[1]))) {
krk_integer_type b = AS_INTEGER(argv[1]);
if (unlikely(b == 0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "integer division by zero");
return FLOATING_VAL(__builtin_floor(self / (double)b));
} else if (IS_FLOATING(argv[1])) {
double b = AS_FLOATING(argv[1]);
if (unlikely(b == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
return FLOATING_VAL(__builtin_floor(self / b));
}
return NOTIMPL_VAL();
}
KRK_Method(float,__rfloordiv__) {
METHOD_TAKES_EXACTLY(1);
if (unlikely(self == 0.0)) return krk_runtimeError(vm.exceptions->zeroDivisionError, "float division by zero");
else if (likely(IS_INTEGER(argv[1]))) return FLOATING_VAL((double)AS_INTEGER(argv[1]) / self);
else if (IS_FLOATING(argv[1])) return FLOATING_VAL(__builtin_floor(AS_FLOATING(argv[1]) / self));
return NOTIMPL_VAL();
}
KRK_Method(float,__pos__) {
return argv[0];
}
#undef CURRENT_CTYPE
#define CURRENT_CTYPE krk_integer_type
FUNC_SIG(bool,__init__) {
static __attribute__ ((unused)) const char* _method_name = "__init__";
METHOD_TAKES_AT_MOST(1);
if (argc < 2) return BOOLEAN_VAL(0);
return BOOLEAN_VAL(!krk_isFalsey(argv[1]));
}
KRK_Method(bool,__str__) {
return OBJECT_VAL((self ? S("True") : S("False")));
}
FUNC_SIG(NoneType,__init__) {
if (argc > 1) return krk_runtimeError(vm.exceptions->argumentError, "%s takes no arguments", "NoneType");
return NONE_VAL();
}
KRK_Method(NoneType,__str__) {
return OBJECT_VAL(S("None"));
}
KRK_Method(NoneType,__hash__) {
return INTEGER_VAL((uint32_t)AS_INTEGER(argv[0]));
}
KRK_Method(NoneType,__eq__) {
METHOD_TAKES_EXACTLY(1);
if (IS_NONE(argv[1])) return BOOLEAN_VAL(1);
return NOTIMPL_VAL();
}
#define IS_NotImplementedType(o) IS_NOTIMPL(o)
#define AS_NotImplementedType(o) (1)
FUNC_SIG(NotImplementedType,__init__) {
if (argc > 1) return krk_runtimeError(vm.exceptions->argumentError, "%s takes no arguments", "NotImplementedType");
return NOTIMPL_VAL();
}
KRK_Method(NotImplementedType,__str__) {
return OBJECT_VAL(S("NotImplemented"));
}
KRK_Method(NotImplementedType,__hash__) {
return INTEGER_VAL(0);
}
KRK_Method(NotImplementedType,__eq__) {
METHOD_TAKES_EXACTLY(1);
if (IS_NOTIMPL(argv[1])) return BOOLEAN_VAL(1);
return NOTIMPL_VAL();
}
#undef BIND_METHOD
/* These class names conflict with C types, so we need to cheat a bit */
#define BIND_METHOD(klass,method) do { krk_defineNative(& _ ## klass->methods, #method, _ ## klass ## _ ## method); } while (0)
#define BIND_TRIPLET(klass,name) \
BIND_METHOD(klass,__ ## name ## __); \
BIND_METHOD(klass,__r ## name ## __); \
krk_defineNative(&_ ## klass->methods,"__i" #name "__",_ ## klass ## ___ ## name ## __);
_noexport
void _createAndBind_numericClasses(void) {
KrkClass * _int = ADD_BASE_CLASS(vm.baseClasses->intClass, "int", vm.baseClasses->objectClass);
_int->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_METHOD(int,__init__);
BIND_METHOD(int,__str__);
BIND_METHOD(int,__int__);
BIND_METHOD(int,__chr__);
BIND_METHOD(int,__float__);
BIND_METHOD(int,__eq__);
BIND_METHOD(int,__hash__);
BIND_METHOD(int,__format__);
BIND_TRIPLET(int,add);
BIND_TRIPLET(int,sub);
BIND_TRIPLET(int,mul);
BIND_TRIPLET(int,or);
BIND_TRIPLET(int,xor);
BIND_TRIPLET(int,and);
BIND_TRIPLET(int,lshift);
BIND_TRIPLET(int,rshift);
BIND_TRIPLET(int,mod);
BIND_TRIPLET(int,truediv);
BIND_TRIPLET(int,floordiv);
BIND_TRIPLET(int,pow);
BIND_METHOD(int,__lt__);
BIND_METHOD(int,__gt__);
BIND_METHOD(int,__le__);
BIND_METHOD(int,__ge__);
BIND_METHOD(int,__hex__);
BIND_METHOD(int,__oct__);
BIND_METHOD(int,__bin__);
BIND_METHOD(int,__invert__);
BIND_METHOD(int,__neg__);
BIND_METHOD(int,__abs__);
BIND_METHOD(int,__pos__);
krk_defineNative(&_int->methods, "__repr__", FUNC_NAME(int,__str__));
krk_finalizeClass(_int);
KRK_DOC(_int, "Convert a number or string type to an integer representation.");
KrkClass * _float = ADD_BASE_CLASS(vm.baseClasses->floatClass, "float", vm.baseClasses->objectClass);
_float->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_METHOD(float,__init__);
BIND_METHOD(float,__int__);
BIND_METHOD(float,__float__);
BIND_METHOD(float,__str__);
BIND_METHOD(float,__eq__);
BIND_METHOD(float,__hash__);
BIND_TRIPLET(float,add);
BIND_TRIPLET(float,sub);
BIND_TRIPLET(float,mul);
BIND_TRIPLET(float,truediv);
BIND_TRIPLET(float,floordiv);
BIND_METHOD(float,__lt__);
BIND_METHOD(float,__gt__);
BIND_METHOD(float,__le__);
BIND_METHOD(float,__ge__);
BIND_METHOD(float,__neg__);
BIND_METHOD(float,__abs__);
BIND_METHOD(float,__pos__);
krk_defineNative(&_float->methods, "__repr__", FUNC_NAME(float,__str__));
krk_finalizeClass(_float);
KRK_DOC(_float, "Convert a number or string type to a float representation.");
KrkClass * _bool = ADD_BASE_CLASS(vm.baseClasses->boolClass, "bool", vm.baseClasses->intClass);
_bool->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_METHOD(bool,__init__);
BIND_METHOD(bool,__str__);
krk_defineNative(&_bool->methods, "__repr__", FUNC_NAME(bool,__str__));
krk_finalizeClass(_bool);
KRK_DOC(_bool, "Returns False if the argument is 'falsey', otherwise True.");
KrkClass * _NoneType = ADD_BASE_CLASS(vm.baseClasses->noneTypeClass, "NoneType", vm.baseClasses->objectClass);
_NoneType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_METHOD(NoneType, __init__);
BIND_METHOD(NoneType, __str__);
BIND_METHOD(NoneType, __hash__);
BIND_METHOD(NoneType, __eq__);
krk_defineNative(&_NoneType->methods, "__repr__", FUNC_NAME(NoneType,__str__));
krk_finalizeClass(_NoneType);
KrkClass * _NotImplementedType = ADD_BASE_CLASS(vm.baseClasses->notImplClass, "NotImplementedType", vm.baseClasses->objectClass);
_NotImplementedType->obj.flags |= KRK_OBJ_FLAGS_NO_INHERIT;
BIND_METHOD(NotImplementedType, __init__);
BIND_METHOD(NotImplementedType, __str__);
BIND_METHOD(NotImplementedType, __hash__);
BIND_METHOD(NotImplementedType, __eq__);
krk_defineNative(&_NotImplementedType->methods, "__repr__", FUNC_NAME(NotImplementedType,__str__));
krk_finalizeClass(_NotImplementedType);
krk_attachNamedValue(&vm.builtins->fields, "NotImplemented", NOTIMPL_VAL());
}