forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumstr.c
More file actions
2842 lines (2478 loc) · 75.9 KB
/
numstr.c
File metadata and controls
2842 lines (2478 loc) · 75.9 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
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Racket
Copyright (c) 2004-2013 PLT Design Inc.
Copyright (c) 2000-2001 Matthew Flatt
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA.
libscheme
Copyright (c) 1994 Brent Benson
All rights reserved.
*/
/* The bulk of this file is the number parser, an insane bit of code
that would probably be better off implemented via lex+yacc, except
the error messages are better this way.
Also, for no particularly good reason, random-number support is
here, though the real work is in newrandom.inc. */
#include "schpriv.h"
#include <math.h>
#include <string.h>
#include <ctype.h>
static Scheme_Object *number_to_string (int argc, Scheme_Object *argv[]);
static Scheme_Object *string_to_number (int argc, Scheme_Object *argv[]);
static Scheme_Object *bytes_to_integer (int argc, Scheme_Object *argv[]);
static Scheme_Object *integer_to_bytes (int argc, Scheme_Object *argv[]);
static Scheme_Object *bytes_to_real (int argc, Scheme_Object *argv[]);
static Scheme_Object *real_to_bytes (int argc, Scheme_Object *argv[]);
static Scheme_Object *bytes_to_long_double (int argc, Scheme_Object *argv[]);
static Scheme_Object *long_double_to_bytes (int argc, Scheme_Object *argv[]);
static Scheme_Object *system_big_endian_p (int argc, Scheme_Object *argv[]);
static Scheme_Object *random_seed(int argc, Scheme_Object *argv[]);
static Scheme_Object *sch_random(int argc, Scheme_Object *argv[]);
static Scheme_Object *make_pseudo_random_generator(int argc, Scheme_Object **argv);
static Scheme_Object *current_pseudo_random_generator(int argc, Scheme_Object **argv);
static Scheme_Object *current_sched_pseudo_random_generator(int argc, Scheme_Object **argv);
static Scheme_Object *pseudo_random_generator_p(int argc, Scheme_Object **argv);
static Scheme_Object *sch_unpack(int argc, Scheme_Object *argv[]);
static Scheme_Object *sch_pack(int argc, Scheme_Object *argv[]);
static Scheme_Object *sch_pack_bang(int argc, Scheme_Object *argv[]);
static Scheme_Object *sch_check_pack(int argc, Scheme_Object *argv[]);
static char *number_to_allocated_string(int radix, Scheme_Object *obj, int alloc);
READ_ONLY static char *infinity_str = "+inf.0";
READ_ONLY static char *minus_infinity_str = "-inf.0";
READ_ONLY static char *not_a_number_str = "+nan.0";
READ_ONLY static char *other_not_a_number_str = "-nan.0";
READ_ONLY static char *long_infinity_str = "+inf.t";
READ_ONLY static char *long_minus_infinity_str = "-inf.t";
READ_ONLY static char *long_not_a_number_str = "+nan.t";
READ_ONLY static char *long_other_not_a_number_str = "-nan.t";
/* Single-precision float literals.
Due to the structure of the reader, they have to be exactly 6
characters long. */
READ_ONLY static char *single_infinity_str = "+inf.f";
READ_ONLY static char *single_minus_infinity_str = "-inf.f";
READ_ONLY static char *single_not_a_number_str = "+nan.f";
READ_ONLY static char *single_other_not_a_number_str = "-nan.f";
#if !defined(SIXTY_FOUR_BIT_INTEGERS) && defined(NO_LONG_LONG_TYPE)
SHARED_OK static Scheme_Object *num_limits[3];
#endif
#ifdef SCHEME_BIG_ENDIAN
# define MZ_IS_BIG_ENDIAN 1
#else
# define MZ_IS_BIG_ENDIAN 0
#endif
#define TO_DOUBLE scheme_TO_DOUBLE
#define zeroi scheme_exact_zero
void scheme_init_numstr(Scheme_Env *env)
{
scheme_add_global_constant("number->string",
scheme_make_prim_w_arity(number_to_string,
"number->string",
1, 2),
env);
scheme_add_global_constant("string->number",
scheme_make_folding_prim(string_to_number,
"string->number",
1, 2, 1),
env);
scheme_add_global_constant("integer-bytes->integer",
scheme_make_prim_w_arity(bytes_to_integer,
"integer-bytes->integer",
2, 5),
env);
scheme_add_global_constant("integer->integer-bytes",
scheme_make_prim_w_arity(integer_to_bytes,
"integer->integer-bytes",
3, 6),
env);
scheme_add_global_constant("floating-point-bytes->real",
scheme_make_prim_w_arity(bytes_to_real,
"floating-point-bytes->real",
1, 4),
env);
scheme_add_global_constant("real->floating-point-bytes",
scheme_make_prim_w_arity(real_to_bytes,
"real->floating-point-bytes",
2, 5),
env);
scheme_add_global_constant("system-big-endian?",
scheme_make_prim_w_arity(system_big_endian_p,
"system-big-endian?",
0, 0),
env);
scheme_add_global_constant("random",
scheme_make_prim_w_arity(sch_random,
"random",
0, 2),
env);
scheme_add_global_constant("random-seed",
scheme_make_prim_w_arity(random_seed,
"random-seed",
1, 1),
env);
scheme_add_global_constant("make-pseudo-random-generator",
scheme_make_prim_w_arity(make_pseudo_random_generator,
"make-pseudo-random-generator",
0, 0),
env);
scheme_add_global_constant("vector->pseudo-random-generator",
scheme_make_prim_w_arity(sch_pack,
"vector->pseudo-random-generator",
1, 1),
env);
scheme_add_global_constant("vector->pseudo-random-generator!",
scheme_make_prim_w_arity(sch_pack_bang,
"vector->pseudo-random-generator!",
2, 2),
env);
scheme_add_global_constant("pseudo-random-generator->vector",
scheme_make_prim_w_arity(sch_unpack,
"pseudo-random-generator->vector",
1, 1),
env);
scheme_add_global_constant("pseudo-random-generator-vector?",
scheme_make_prim_w_arity(sch_check_pack,
"pseudo-random-generator-vector?",
1, 1),
env);
scheme_add_global_constant("pseudo-random-generator?",
scheme_make_prim_w_arity(pseudo_random_generator_p,
"pseudo-random-generator?",
1, 1),
env);
scheme_add_global_constant("current-pseudo-random-generator",
scheme_register_parameter(current_pseudo_random_generator,
"current-pseudo-random-generator",
MZCONFIG_RANDOM_STATE),
env);
scheme_add_global_constant("current-evt-pseudo-random-generator",
scheme_register_parameter(current_sched_pseudo_random_generator,
"current-evt-pseudo-random-generator",
MZCONFIG_SCHEDULER_RANDOM_STATE),
env);
#if !defined(SIXTY_FOUR_BIT_INTEGERS) && defined(NO_LONG_LONG_TYPE)
REGISTER_SO(num_limits);
{
Scheme_Object *a[2], *v;
a[0] = scheme_make_integer(1);
a[1] = scheme_make_integer(64);
a[0] = scheme_bitwise_shift(2, a);
v = scheme_sub1(1, a);
num_limits[MZ_U8HI] = v;
a[0] = v;
a[1] = scheme_make_integer(-1);
v = scheme_bitwise_shift(2, a);
num_limits[MZ_S8HI] = v;
a[0] = v;
v = scheme_bin_minus(scheme_make_integer(0), scheme_add1(1, a));
num_limits[MZ_S8LO] = v;
}
#endif
}
void scheme_init_extfl_numstr(Scheme_Env *env)
{
scheme_add_global_constant("floating-point-bytes->extfl",
scheme_make_prim_w_arity(bytes_to_long_double,
"floating-point-bytes->extfl",
1, 4),
env);
scheme_add_global_constant("extfl->floating-point-bytes",
scheme_make_prim_w_arity(long_double_to_bytes,
"extfl->floating-point-bytes",
1, 4),
env);
}
# ifdef SIN_COS_NEED_DEOPTIMIZE
# pragma optimize("g", off)
# define MK_SCH_TRIG(SCH_TRIG, c_trig) static double SCH_TRIG(double d) { return c_trig(d); }
MK_SCH_TRIG(SCH_SIN, sin)
MK_SCH_TRIG(SCH_COS, cos)
# pragma optimize("g", on)
# else
# define SCH_SIN sin
# define SCH_COS cos
# endif
/*========================================================================*/
/* number parsing */
/*========================================================================*/
#ifndef MZ_LONG_DOUBLE
static Scheme_Object *wrap_as_long_double(const char *s, int radix)
{
Scheme_Long_Double *d;
d = MALLOC_ONE_TAGGED(Scheme_Long_Double);
d->so.type = scheme_long_double_type;
if (radix == 10)
d->printed_form = s;
else {
char *s2;
intptr_t len;
len = strlen(s);
s2 = (char *)scheme_malloc_atomic(len + 3);
memcpy(s2 + 2, s, len+1);
s2[0] = '#';
s2[1] = ((radix == 8)
? 'o'
: ((radix == 2)
? 'b'
: 'x'));
d->printed_form = s2;
}
return (Scheme_Object *)d;
}
#endif
Scheme_Object *make_any_long_double()
{
#ifdef MZ_LONG_DOUBLE
return scheme_make_long_double(get_long_double_zero());
#else
return wrap_as_long_double("1t0", 10);
#endif
}
static int u_strcmp(mzchar *s, const char *t)
{
int i;
for (i = 0; s[i] && (s[i] == ((unsigned char *)t)[i]); i++) {
}
if (s[i] || t[i])
return 1;
return 0;
}
static Scheme_Object *read_special_number(const mzchar *str, int pos)
{
if ((str[pos] == '-' || str[pos] == '+') && scheme_isalpha(str[pos + 1])) {
mzchar s[7];
int i;
for (i = 0; i < 6; i++) {
s[i] = scheme_tolower(str[i + pos]);
}
s[i] = 0;
if (!u_strcmp(s, infinity_str)) {
#ifdef USE_SINGLE_FLOATS_AS_DEFAULT
return scheme_single_inf_object;
#else
return scheme_inf_object;
#endif
}
else if (!u_strcmp(s, minus_infinity_str)) {
#ifdef USE_SINGLE_FLOATS_AS_DEFAULT
return scheme_single_minus_inf_object;
#else
return scheme_minus_inf_object;
#endif
}
else if (!u_strcmp(s, not_a_number_str)
|| !u_strcmp(s, other_not_a_number_str)) {
#ifdef USE_SINGLE_FLOATS_AS_DEFAULT
return scheme_single_nan_object;
#else
return scheme_nan_object;
#endif
}
else if (!u_strcmp(s, long_infinity_str)) {
#ifdef MZ_LONG_DOUBLE
return scheme_long_inf_object;
#else
return wrap_as_long_double(long_infinity_str, 10);
#endif
}
else if (!u_strcmp(s, long_minus_infinity_str)) {
#ifdef MZ_LONG_DOUBLE
return scheme_long_minus_inf_object;
#else
return wrap_as_long_double(long_minus_infinity_str, 10);
#endif
}
else if (!u_strcmp(s, long_not_a_number_str)
|| !u_strcmp(s, long_other_not_a_number_str)) {
#ifdef MZ_LONG_DOUBLE
return scheme_long_nan_object;
#else
return wrap_as_long_double(long_not_a_number_str, 10);
#endif
}
/* Single-precision specials
If single-precision float support is disabled, promote. */
else if (!u_strcmp(s, single_infinity_str)) {
#ifdef MZ_USE_SINGLE_FLOATS
return scheme_single_inf_object;
#else
return scheme_inf_object;
#endif
}
else if (!u_strcmp(s, single_minus_infinity_str)) {
#ifdef MZ_USE_SINGLE_FLOATS
return scheme_single_minus_inf_object;
#else
return scheme_minus_inf_object;
#endif
}
else if (!u_strcmp(s, single_not_a_number_str)
|| !u_strcmp(s, single_other_not_a_number_str)) {
#ifdef MZ_USE_SINGLE_FLOATS
return scheme_single_nan_object;
#else
return scheme_nan_object;
#endif
}
}
return NULL;
}
/* Exponent threshold for obvious infinity. Must be at least
max(MAX_FAST_FLOATREAD_LEN, MAX_FLOATREAD_PRECISION_DIGITS) more
than the larget possible FP exponent. */
#define CHECK_INF_EXP_THRESHOLD(extfl) (extfl ? 6000 : 400)
/* Don't bother reading more than the following number of digits in a
floating-point mantissa: */
#define MAX_FLOATREAD_PRECISION_DIGITS(extfl) CHECK_INF_EXP_THRESHOLD(extfl)
#ifdef USE_EXPLICT_FP_FORM_CHECK
/* Fixes Linux problem of 0e... => non-number (0 with ptr at e...) */
/* Fixes SunOS problem with numbers like .3e2666666666666 => 0.0 */
/* Fixes HP/UX problem with numbers like .3e2666666666666 => non-number */
# ifdef MZ_XFORM
END_XFORM_ARITH;
# endif
static double STRTOD(const char *orig_c, char **f, int extfl)
{
int neg = 0;
int found_dot = 0, is_infinity = 0, is_zero = 0;
const char *c = orig_c;
*f = (char *)c;
if (*c == '-') {
c++;
neg = 1;
} else if (*c == '+') {
c++;
}
if (!isdigit((unsigned char)*c)) {
if (*c == '.') {
if (!isdigit((unsigned char)c[1]))
return 0; /* no digits - bad! */
} else
return 0; /* no digits - bad! */
}
for (; *c; c++) {
int ch = *c;
if (isdigit(ch)) {
/* ok */
} else if ((ch == 'e') || (ch == 'E')) {
int e = 0, neg_exp = 0;
c++;
if (*c == '-') {
c++;
neg_exp = 1;
} else if (*c == '+') {
c++;
}
if (!isdigit((unsigned char)*c))
return 0; /* no digits - bad! */
for (; *c; c++) {
int ch = *c;
if (!isdigit(ch))
return 0; /* not a digit - bad! */
else {
e = (e * 10) + (ch - '0');
if (e > CHECK_INF_EXP_THRESHOLD(extfl)) {
if (neg_exp)
is_zero = 1;
else
is_infinity = 1;
}
}
}
break;
} else if (ch == '.') {
if (found_dot)
return 0; /* two dots - shouldn't happen */
found_dot = 1;
} else
return 0; /* unknown non-digit - shouldn't happen */
}
*f = (char *)c;
#ifdef MZ_LONG_DOUBLE
if (is_infinity) {
if (neg)
return scheme_long_minus_infinity_val;
else
return scheme_long_infinity_val;
}
if (is_zero) {
if (neg)
return scheme_long_floating_point_nzero;
else
return scheme_long_floating_point_zero;
}
/* It's OK if c is ok: */
return strtold(orig_c, NULL);
#else
if (is_infinity) {
if (neg)
return scheme_minus_infinity_val;
else
return scheme_infinity_val;
}
if (is_zero) {
if (neg)
return scheme_floating_point_nzero;
else
return scheme_floating_point_zero;
}
/* It's OK if c is ok: */
return strtod(orig_c, NULL);
#endif
}
# ifdef MZ_XFORM_GC
START_XFORM_ARITH;
# endif
#else
# define STRTOD(x, y, extfl) strtod(x, y)
#endif
static Scheme_Object *CHECK_SINGLE(Scheme_Object *v, int s, int long_dbl)
{
if (SCHEME_DBLP(v)) {
#ifdef MZ_USE_SINGLE_FLOATS
if (s)
return scheme_make_float((float)SCHEME_DBL_VAL(v));
#endif
}
return v;
}
#define DISALLOW_EXTFLONUM(special, other) \
if ((special && SCHEME_LONG_DBLP(special)) || (other && SCHEME_LONG_DBLP(other))) { \
if (report) \
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation, \
"read: cannot combine extflonum into complex number: %u", \
str, len); \
return scheme_false; \
}
Scheme_Object *scheme_read_number(const mzchar *str, intptr_t len,
int is_float,
int is_not_float,
int decimal_means_float,
int radix, int radix_set,
Scheme_Object *complain,
int *div_by_zero,
int test_only,
Scheme_Object *stxsrc, intptr_t line, intptr_t col, intptr_t pos, intptr_t span,
Scheme_Object *indentation)
{
int i, has_decimal, must_parse, has_slash;
int report, delta;
Scheme_Object *next_complain;
int has_hash, has_expt, has_i, has_sign, has_at, has_hash_since_slash;
int saw_digit_since_slash, saw_nonzero_digit;
Scheme_Object *o;
#ifdef MZ_USE_SINGLE_FLOATS
int sgl;
#endif
int is_long_double = 0;
if (len < 0)
len = scheme_char_strlen(str);
delta = 0;
while (str[delta] == '#') {
if (str[delta+1] != 'E' && str[delta+1] != 'e' && str[delta+1] != 'I' && str[delta+1] != 'i') {
if (radix_set) {
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: bad radix specification: %u",
str, len);
else
return scheme_false;
}
radix_set = 1;
} else {
if (is_float || is_not_float) {
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: bad exactness specification: %u",
str, len);
else
return scheme_false;
}
}
switch (str[delta+1]) {
case 'B':
case 'b':
radix = 2;
break;
case 'O':
case 'o':
radix = 8;
break;
case 'D':
case 'd':
radix = 10;
break;
case 'X':
case 'x':
radix = 16;
break;
case 'I':
case 'i':
is_float = 1;
break;
case 'E':
case 'e':
is_not_float = 1;
break;
default:
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: bad `#' indicator `%c': %u",
str[delta+1], str, len);
return scheme_false;
}
delta += 2;
}
must_parse = (radix_set || is_float || is_not_float);
report = complain && must_parse;
next_complain = must_parse ? complain : NULL;
if (!(len - delta)) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: no digits");
return scheme_false;
}
/* look for +inf.0, etc: */
if (len - delta == 6) {
Scheme_Object *special;
special = read_special_number(str, delta);
if (special) {
if (!is_not_float)
return special;
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: no exact representation for %V",
special);
return scheme_false;
}
}
/* Look for <special>+...i and ...<special>i */
if ((len-delta > 7) && str[len-1] == 'i') {
Scheme_Object *special;
mzchar *s2;
/* Try <special>+...i */
special = read_special_number(str, delta);
if (special) {
s2 = (mzchar *)scheme_malloc_atomic((len - delta - 6 + 4 + 1) * sizeof(mzchar));
s2[0] = '+';
s2[1] = '0';
s2[2] = (SCHEME_DBLP(special) ? '.' : 's');
s2[3] = '0';
memcpy(s2 + 4, str + delta + 6, (len - delta - 5) * sizeof(mzchar));
} else {
/* Try ...<special>i: */
special = read_special_number(str, len - 7);
if (special) {
s2 = (mzchar *)scheme_malloc_atomic((len - delta - 6 + 4 + 1) * sizeof(mzchar));
memcpy(s2, str + delta, (len - delta - 7) * sizeof(mzchar));
s2[len - delta - 7] = '+';
s2[len - delta - 7 + 1] = '0';
s2[len - delta - 7 + 2] = (SCHEME_DBLP(special) ? '.' : 's');
s2[len - delta - 7 + 3] = '0';
s2[len - delta - 7 + 4] = 'i';
s2[len - delta - 7 + 5] = 0;
if (!SCHEME_LONG_DBLP(special))
special = scheme_bin_mult(special, scheme_plus_i);
} else
s2 = NULL;
}
if (special) {
Scheme_Object *other;
int dbz = 0;
if (is_not_float) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: no exact representation for %V",
special);
return scheme_false;
}
other = scheme_read_number(s2, len - delta - 6 + 4,
is_float, is_not_float, 1,
radix, 1, 0,
&dbz, test_only,
stxsrc, line, col, pos, span,
indentation);
DISALLOW_EXTFLONUM(special, other);
if (dbz) {
if (div_by_zero)
*div_by_zero = 1;
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: division by zero: %u",
str, len);
return scheme_false;
}
if (!SCHEME_FALSEP(other))
return scheme_bin_plus(special, other);
if (!complain)
return scheme_false;
}
} else if ((len-delta == 7) && str[len-1] == 'i') {
/* Try <special>i */
Scheme_Object *special;
special = read_special_number(str, delta);
if (special) {
special = scheme_make_complex(scheme_make_integer(0), special);
if (is_not_float) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: no exact representation for %V",
special);
return scheme_false;
}
DISALLOW_EXTFLONUM(special, special);
return special;
}
}
/* Look for <special>@... and ...@<special> */
if ((len - delta > 7) && ((str[delta+6] == '@') || (str[len - 7] == '@'))) {
Scheme_Object *special;
mzchar *s2;
int spec_mag = 0;
/* Try <special>@... */
if (str[delta+6] == '@')
special = read_special_number(str, delta);
else
special = NULL;
if (special) {
s2 = (mzchar *)scheme_malloc_atomic((len - delta - 6) * sizeof(mzchar));
memcpy(s2, str + delta + 7, (len - delta - 6) * sizeof(mzchar));
spec_mag = 1;
} else {
if (str[len - 7] == '@')
special = read_special_number(str, len - 6);
else
special = NULL;
if (special) {
s2 = (mzchar *)scheme_malloc_atomic((len - delta - 6) * sizeof(mzchar));
memcpy(s2, str + delta, (len - delta - 7) * sizeof(mzchar));
s2[len - delta - 7] = 0;
} else
s2 = NULL;
}
if (special) {
Scheme_Object *other;
int dbz = 0;
/* s2 can't contain @: */
for (i = 0; s2[i]; i++) {
if (s2[i] == '@')
break;
}
if (s2[i])
other = scheme_false;
else
other = scheme_read_number(s2, len - delta - 7,
is_float, is_not_float, 1,
radix, 1, 0,
&dbz, test_only,
stxsrc, line, col, pos, span,
indentation);
DISALLOW_EXTFLONUM(special, other);
if (dbz) {
if (div_by_zero)
*div_by_zero = 1;
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: division by zero: %u",
str, len);
return scheme_false;
}
if (!SCHEME_FALSEP(other)) {
/* If string is complex, not well-formed: */
if (!SCHEME_COMPLEXP(other)) {
Scheme_Object *a[2];
if (spec_mag) {
a[0] = special;
a[1] = other;
} else {
a[0] = other;
a[1] = special;
}
return scheme_make_polar(2, a);
}
}
if (!complain)
return scheme_false;
}
}
#define isinexactmark(ch) ((ch == 'e') || (ch == 'E') \
|| (ch == 's') || (ch == 'S') \
|| (ch == 'f') || (ch == 'F') \
|| (ch == 'd') || (ch == 'D') \
|| (ch == 'l') || (ch == 'L') \
|| (ch == 't') || (ch == 'T'))
#define isAdigit(ch) ((ch >= '0') && (ch <= '9'))
#define isbaseNdigit(N, ch) (((ch >= 'a') && (ch <= (mzchar)('a' + N - 11))) \
|| ((ch >= 'A') && (ch <= (mzchar)('A' + N - 11))))
has_i = 0;
has_at = 0;
has_sign = delta-1;
for (i = delta; i < len; i++) {
mzchar ch = str[i];
if (!ch) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: embedded null character: %u",
str, len);
return scheme_false;
} else if (isinexactmark(ch) && ((radix <= 10) || !isbaseNdigit(radix, ch))) {
/* If a sign follows, don't count it */
if (str[i+1] == '+' || str[i+1] == '-')
i++;
} else if ((ch == '+') || (ch == '-')) {
if ((has_sign > delta) || ((has_sign == delta) && (i == delta+1))) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: too many signs: %u",
str, len);
return scheme_false;
}
has_sign = i;
} else if (((ch == 'I') || (ch == 'i')) && (has_sign >= delta)) {
if (has_at) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: cannot mix `@' and `i': %u",
str, len);
return scheme_false;
}
if (i + 1 < len) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: `i' must be at the end: %u",
str, len);
return scheme_false;
}
has_i = i;
} else if (ch == '@') {
if (has_at) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: too many `@'s: %u",
str, len);
return scheme_false;
}
if (i == delta) {
if (report)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: `@' cannot be at start: %u",
str, len);
return scheme_false;
}
has_at = i;
if (has_sign >= delta)
has_sign = delta-1;
}
}
if (has_i) {
Scheme_Object *n1, *n2;
mzchar *first, *second;
int fdbz = 0, sdbz = 0;
if (has_sign != delta) {
first = (mzchar *)scheme_malloc_atomic((has_sign - delta + 1) * sizeof(mzchar));
memcpy(first, str + delta, (has_sign - delta) * sizeof(mzchar));
first[has_sign - delta] = 0;
} else
first = NULL;
if (has_i - has_sign > 1) {
second = (mzchar *)scheme_malloc_atomic((has_i - has_sign + 1) * sizeof(mzchar));
memcpy(second, str + has_sign, (has_i - has_sign) * sizeof(mzchar));
second[has_i - has_sign] = 0;
} else
second = NULL;
if (first)
n1 = scheme_read_number(first, has_sign - delta,
is_float, is_not_float, decimal_means_float,
radix, 1, next_complain,
&fdbz, test_only,
stxsrc, line, col, pos, span,
indentation);
else
n1 = zeroi;
if (SAME_OBJ(n1, scheme_false) && !fdbz)
return scheme_false;
/* This +nan.0 test looks unnecessary -- Matthew, 08/14/01 */
else if (SCHEME_FLOATP(n1)) {
double d = SCHEME_FLOAT_VAL(n1);
if (MZ_IS_NAN(d))
return scheme_false;
}
if (second)
n2 = scheme_read_number(second, has_i - has_sign,
is_float, is_not_float, decimal_means_float,
radix, 1, next_complain,
&sdbz, test_only,
stxsrc, line, col, pos, span,
indentation);
else if (str[has_sign] == '-')
n2 = scheme_make_integer(-1);
else
n2 = scheme_make_integer(1);
if (SAME_OBJ(n2, scheme_false) && !sdbz)
return scheme_false;
/* This +nan.0 test looks unnecessary -- Matthew, 08/14/01 */
else if (SCHEME_FLOATP(n2)) {
double d = SCHEME_FLOAT_VAL(n2);
if (MZ_IS_NAN(d))
return scheme_false;
}
DISALLOW_EXTFLONUM(n1, n2);
if (fdbz || sdbz) {
if (div_by_zero)
*div_by_zero = 1;
if (complain)
scheme_read_err(complain, stxsrc, line, col, pos, span, 0, indentation,
"read: division by zero: %u",
str, len);
return scheme_false;
}
if (!is_not_float && ((SCHEME_FLOATP(n1) && (n2 != zeroi)) || is_float))
n2 = scheme_exact_to_inexact(1, &n2); /* uses default conversion: float or double */
else if (is_not_float)
n2 = scheme_inexact_to_exact(1, &n2);
if (!is_not_float && ((SCHEME_FLOATP(n2) && (n1 != zeroi)) || is_float))
n1 = scheme_exact_to_inexact(1, &n1); /* uses default conversion: float or double */
else if (is_not_float)
n1 = scheme_inexact_to_exact(1, &n1);
return scheme_make_complex(n1, n2);
}
if (has_at) {
Scheme_Object *n1, *n2;
double d1, d2, r1, r2;
mzchar *first;
const mzchar *second;
int fdbz = 0, sdbz = 0;
first = (mzchar *)scheme_malloc_atomic((has_at - delta + 1) * sizeof(mzchar));
memcpy(first, str + delta, (has_at - delta) * sizeof(mzchar));
first[has_at - delta] = 0;
#ifdef MZ_PRECISE_GC
{
/* Can't pass mis-aligned pointer to scheme_read_number. */
int slen = len - (has_at + 1) + 1;
second = (mzchar *)scheme_malloc_atomic(slen * sizeof(mzchar));
memcpy((mzchar *)second, str + has_at + 1, slen * sizeof(mzchar));
}
#else
second = str + has_at + 1;
#endif
n2 = scheme_read_number(second, len - has_at - 1,
is_float, is_not_float, decimal_means_float,
radix, 1, next_complain,
&fdbz, test_only,
stxsrc, line, col, pos, span,
indentation);
if (!fdbz) {
if (SCHEME_FALSEP(n2))
return scheme_false;
/* Special case: angle is zero => real number */
if (n2 == zeroi)
return scheme_read_number(first, has_at - delta,
is_float, is_not_float, decimal_means_float,
radix, 1, complain,
div_by_zero,
test_only,
stxsrc, line, col, pos, span,
indentation);