/
pl-write.c
2316 lines (1881 loc) · 51.2 KB
/
pl-write.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
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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: J.Wielemaker@vu.nl
WWW: http://www.swi-prolog.org
Copyright (c) 1985-2020, University of Amsterdam
VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "pl-incl.h"
#include "pl-arith.h"
#include "pl-dict.h"
#include <math.h>
#include "os/pl-dtoa.h"
#include "os/pl-ctype.h"
#include <stdio.h> /* sprintf() */
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef HAVE_FLOAT_H
#include <float.h>
#endif
#ifdef HAVE_IEEEFP_H
#include <ieeefp.h>
#endif
#ifdef fpclassify
#define HAVE_FPCLASSIFY 1
#endif
typedef struct
{ int flags; /* PL_WRT_* flags */
int max_depth; /* depth limit */
int depth; /* current depth */
atom_t spacing; /* Where to insert spaces */
Module module; /* Module for operators */
IOSTREAM *out; /* stream to write to */
term_t portray_goal; /* call/2 activated portray hook */
term_t write_options; /* original write options */
term_t prec_opt; /* term in write options with prec */
} write_options;
static bool writeTerm2(term_t term, int prec,
write_options *options, bool arg) WUNUSED;
static bool writeTerm(term_t t, int prec,
write_options *options) WUNUSED;
static bool writeArgTerm(term_t t, int prec,
write_options *options, bool arg) WUNUSED;
static int PutToken(const char *s, IOSTREAM *stream);
static int writeAtom(atom_t a, write_options *options);
static int callPortray(term_t arg, int prec, write_options *options);
static int enterPortray(ARG1_LD);
static void leavePortray(ARG1_LD);
char *
var_name_ptr__LD(Word p, char *name ARG_LD)
{ size_t iref;
deRef(p);
if (p > (Word) lBase)
iref = ((Word)p - (Word)lBase)*2+1;
else
iref = ((Word)p - (Word)gBase)*2;
Ssprintf(name, "_%lld", (int64_t)iref);
return name;
}
char *
varName(term_t t, char *name)
{ GET_LD
Word p = valTermRef(t);
return var_name_ptr(p, name);
}
static int
atomIsVarName(atom_t a)
{ Atom atom = atomValue(a);
if ( false(atom->type, PL_BLOB_TEXT) || atom->length == 0 )
fail;
if ( isUCSAtom(atom) )
{ pl_wchar_t *w = (pl_wchar_t*)atom->name;
size_t len = atom->length / sizeof(pl_wchar_t);
return atom_varnameW(w, len);
} else
{ const char *s = atom->name;
size_t len = atom->length;
if ( isUpper(*s) || *s == '_' )
{ for(s++; --len > 0; s++)
{ if ( !isAlpha(*s) )
return FALSE;
}
return TRUE;
}
return FALSE;
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Return: TRUE: processes
FALSE: not processed
-1: error
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
writeNumberVar(term_t t, write_options *options ARG_LD)
{ Word p = valTermRef(t);
Functor f;
deRef(p);
if ( !isTerm(*p) )
return FALSE;
f = valueTerm(*p);
if ( f->definition != FUNCTOR_isovar1 )
return FALSE;
if ( LD->var_names.numbervars_frame )
{ FliFrame fr = (FliFrame)valTermRef(LD->var_names.numbervars_frame);
assert(fr->magic == FLI_MAGIC);
if ( fr->mark.globaltop > (Word)f )
return FALSE; /* older $VAR term */
}
p = &f->arguments[0];
deRef(p);
if ( isInteger(*p) )
{ int64_t n = valInteger(*p);
char buf[32]; /* Max is H354745078340568300 */
if ( n < 0 )
{ sprintf(buf, "S_%" PRId64, -n);
} else
{ int i = (int)(n % 26);
int64_t j = n / 26;
if ( j == 0 )
{ buf[0] = i+'A';
buf[1] = EOS;
} else
{ sprintf(buf, "%c%" PRId64, i+'A', j);
}
}
return PutToken(buf, options->out) ? TRUE : -1;
}
if ( isAtom(*p) && atomIsVarName(*p) )
{ write_options o2 = *options;
clear(&o2, PL_WRT_QUOTED);
return writeAtom(*p, &o2) ? TRUE : -1;
}
return FALSE;
}
#define AT_LOWER 0
#define AT_QUOTE 1
#define AT_FULLSTOP 2
#define AT_SYMBOL 3
#define AT_SOLO 4
#define AT_SPECIAL 5
/* Note: this only deals with ISO Latin-1 atoms; wide atoms are handled
by writeUCSAtom()
*/
static int
truePrologFlagNoLD(unsigned int flag)
{ GET_LD
return truePrologFlag(flag);
}
static inline int
wr_is_symbol(int c, write_options *options)
{ return ( isSymbol(c) ||
(c == '`' &&
options &&
(options->flags & PL_WRT_BACKQUOTE_IS_SYMBOL)) );
}
static int
atomType(atom_t a, write_options *options)
{ Atom atom = atomValue(a);
char *s = atom->name;
size_t len = atom->length;
IOSTREAM *fd = options ? options->out : NULL;
Module m = options ? options->module : MODULE_user;
if ( len == 0 )
return AT_QUOTE;
if ( isLower(*s) || (true(m, M_VARPREFIX) && isAlpha(*s)) )
{ do
{ for( ++s;
--len > 0 && isAlpha(*s) && (!fd || Scanrepresent(*s, fd)==0);
s++)
;
} while ( len >= 2 &&
*s == '.' && isAlpha(s[1]) &&
truePrologFlagNoLD(PLFLAG_DOT_IN_ATOM) &&
(!options || false(options, PL_WRT_NODOTINATOM))
);
return len == 0 ? AT_LOWER : AT_QUOTE;
}
if ( wr_is_symbol(*s, options) )
{ size_t left = len;
if ( len == 1 && s[0] == '.' )
return AT_FULLSTOP;
if ( len >= 2 && s[0] == '/' && s[1] == '*' )
return AT_QUOTE;
for( ;
left > 0 && wr_is_symbol(*s, options) &&
(!fd || Scanrepresent(*s, fd)==0);
s++, left--)
;
if ( left > 0 )
return AT_QUOTE;
return AT_SYMBOL;
}
/* % should be quoted! */
if ( len == 1 && *s != '%' )
{ if ( isSolo(*s) )
return AT_SOLO;
}
if ( a == ATOM_nil || a == ATOM_curl )
return AT_SPECIAL;
return AT_QUOTE;
}
static int
unquoted_atomW(atom_t atom, IOSTREAM *fd, int flags)
{ Atom ap = atomValue(atom);
pl_wchar_t *s = (pl_wchar_t*)ap->name;
size_t len = ap->length/sizeof(pl_wchar_t);
if ( len == 0 )
return FALSE;
if ( !f_is_prolog_atom_start(*s) )
{ for( ; len > 0; s++, len--)
{ if ( !f_is_prolog_symbol(*s) ||
(fd && Scanrepresent(*s, fd)<0) )
return FALSE;
}
return TRUE;
}
do
{ for( ++s;
( --len > 0 &&
f_is_prolog_identifier_continue(*s) &&
(!fd || Scanrepresent(*s, fd)==0)
);
s++)
;
} while ( len >= 2 &&
*s == '.' && f_is_prolog_identifier_continue(s[1]) &&
truePrologFlagNoLD(PLFLAG_DOT_IN_ATOM) &&
!(flags&PL_WRT_NODOTINATOM)
);
return len == 0;
}
int
unquoted_atom(atom_t a)
{ Atom ap = atomValue(a);
if ( true(ap->type, PL_BLOB_TEXT) )
{ if ( !ap->type->write ) /* ordinary atoms */
{ return atomType(a, NULL) != AT_QUOTE;
} else if ( isUCSAtom(ap) ) /* wide atoms */
{ return unquoted_atomW(a, NULL, 0);
}
}
return FALSE;
}
/*******************************
* PRIMITIVE WRITES *
*******************************/
#define TRUE_WITH_SPACE 2 /* OK, and emitted leading space before token */
static bool
Putc(int c, IOSTREAM *s)
{ return Sputcode(c, s) != EOF;
}
static bool
PutString(const char *str, IOSTREAM *s)
{ const unsigned char *q = (const unsigned char *)str;
for( ; *q != EOS; q++ )
{ if ( Sputcode(*q, s) == EOF )
return FALSE;
}
return TRUE;
}
static bool
PutComma(write_options *options)
{ if ( options->spacing == ATOM_next_argument )
return PutString(", ", options->out);
else
return PutString(",", options->out);
}
static bool
PutBar(write_options *options)
{ if ( options->spacing == ATOM_next_argument )
return PutString("| ", options->out);
else
return PutString("|", options->out);
}
static bool
PutStringN(const char *str, size_t length, IOSTREAM *s)
{ size_t i;
const unsigned char *q = (const unsigned char *)str;
for(i=0; i<length; i++, q++)
{ if ( Sputcode(*q, s) == EOF )
return FALSE;
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PutOpenToken() inserts a space in the output stream if the last-written
and given character require a space to ensure a token-break.
The C_* flags denote special cases handled using a flag. The first flag
is 0x200000, which is above the Unicode range.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#define C_PREFIX_SIGN 0x00200000 /* +/- as prefix op */
#define C_PREFIX_OP 0x00400000 /* any prefix op */
#define C_INFIX_OP 0x00800000 /* any infix op */
#define C_MASK 0xffe00000
#define isquote(c) ((c) == '\'' || (c) == '"')
static bool
needSpace(int c, IOSTREAM *s)
{ if ( c == EOF )
{ s->lastc = EOF;
return FALSE;
}
if ( s->lastc == EOF )
return FALSE;
if ( (s->lastc&C_PREFIX_SIGN) && (isDigit(c) || f_is_prolog_symbol(c)) )
return TRUE;
if ( (s->lastc&C_PREFIX_OP) && ( c == '(' || c == '{' ) )
return TRUE; /* avoid op(...) */
if ( (s->lastc&C_INFIX_OP) && c == '(' )
return FALSE;
s->lastc &= ~C_MASK;
if ( ((f_is_prolog_identifier_continue(s->lastc) &&
f_is_prolog_identifier_continue(c)) ||
(f_is_prolog_symbol(s->lastc) && f_is_prolog_symbol(c)) ||
(c == '(' && !isPunctW(s->lastc)) ||
(c == '\'' && (isDigit(s->lastc))) ||
(isquote(c) && s->lastc == c)
) )
return TRUE;
return FALSE;
}
static int
PutOpenToken(int c, IOSTREAM *s)
{ if ( needSpace(c, s) )
{ TRY(Putc(' ', s));
return TRUE_WITH_SPACE;
}
return TRUE;
}
static int
PutToken(const char *s, IOSTREAM *stream)
{ if ( s[0] )
{ int rc;
TRY(rc=PutOpenToken(s[0]&0xff, stream));
TRY(PutString(s, stream));
return rc;
}
return TRUE;
}
static int
PutTokenN(const char *s, size_t len, IOSTREAM *stream)
{ if ( len > 0 )
{ int rc;
TRY(rc=PutOpenToken(s[0]&0xff, stream));
TRY(PutStringN(s, len, stream));
return rc;
}
return TRUE;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PutOpenBrace()/PutCloseBrace() are used to put additional braces around
a term to avoid an operator precedence problem. If the last emitted
character is alphanumerical, there should be a space before the
openbrace to avoid interpretation as a term. E.g. not (a,b) instead of
not(a,b). Reported by Stefan.Mueller@dfki.de.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static int
PutOpenBrace(IOSTREAM *s)
{ int rc;
TRY(rc=PutOpenToken('(', s));
TRY(Putc('(', s));
return rc;
}
static bool
PutCloseBrace(IOSTREAM *s)
{ return Putc(')', s);
}
static bool
putQuoted(int c, int quote, int flags, IOSTREAM *stream)
{ if ( (flags & PL_WRT_CHARESCAPES) )
{ if ( c == ' ' ||
(!(c < 0xff && !isGraph(c)) && c != quote && c != '\\') )
{ TRY(Putc(c, stream));
} else
{ char esc[8];
esc[1] = EOS;
if ( c == quote )
{ esc[0] = c;
} else
{ switch(c)
{ case 7:
esc[0] = 'a';
break;
case '\b':
esc[0] = 'b';
break;
case '\t':
esc[0] = 't';
break;
case '\n':
esc[0] = 'n';
break;
case 11:
esc[0] = 'v';
break;
case '\r':
esc[0] = 'r';
break;
case '\f':
esc[0] = 'f';
break;
case '\\':
esc[0] = '\\';
break;
default:
if ( c <= 0xff )
Ssprintf(esc, "%03o\\", c);
else
assert(0); /* to be done */
}
}
if ( !Putc('\\', stream) ||
!PutString(esc, stream) )
fail;
}
} else
{ if ( !Putc(c, stream) )
fail;
if ( c == quote || c == '\\' ) /* write '' or \\ */
{ if ( !Putc(c, stream) )
fail;
}
}
return TRUE;
}
static bool
writeQuoted(IOSTREAM *stream, const char *text, size_t len, int quote,
write_options *options)
{ const unsigned char *s = (const unsigned char *)text;
TRY(Putc(quote, stream));
while(len-- > 0)
{ TRY(putQuoted(*s++, quote, options->flags, stream));
}
return Putc(quote, stream);
}
#if O_ATTVAR
static bool
writeAttVar(term_t av, write_options *options)
{ GET_LD
char buf[32];
TRY(PutToken(varName(av, buf), options->out));
if ( (options->flags & PL_WRT_ATTVAR_DOTS) )
{ return PutString("{...}", options->out);
} else if ( (options->flags & PL_WRT_ATTVAR_WRITE) )
{ fid_t fid;
term_t a;
if ( !(fid = PL_open_foreign_frame()) )
return FALSE;
Sputcode('{', options->out);
a = PL_new_term_ref();
PL_get_attr__LD(av, a PASS_LD);
if ( !writeTerm(a, 1200, options) )
return FALSE;
Sputcode('}', options->out);
PL_close_foreign_frame(fid);
return TRUE;
} else if ( (options->flags & PL_WRT_ATTVAR_PORTRAY) &&
GD->cleaning <= CLN_PROLOG )
{ static predicate_t pred;
IOSTREAM *old;
wakeup_state wstate;
int rc;
if ( !pred )
pred = _PL_predicate("portray_attvar", 1, "$attvar",
&GD->procedures.portray_attvar1);
if ( !enterPortray(PASS_LD1) )
return FALSE;
if ( !saveWakeup(&wstate, TRUE PASS_LD) )
return FALSE;
old = Scurout;
Scurout = options->out;
rc = PL_call_predicate(NULL, PL_Q_NODEBUG|PL_Q_PASS_EXCEPTION, pred, av);
if ( rc != TRUE && !PL_exception(0) )
rc = TRUE;
Scurout = old;
restoreWakeup(&wstate PASS_LD);
leavePortray(PASS_LD1);
return rc;
}
succeed;
}
#endif
static bool
writeBlob(atom_t a, write_options *options)
{ Atom atom = atomValue(a);
unsigned char const *s, *e;
TRY(PutString("<#", options->out));
s = (unsigned char const *)atom->name;
for (e = s + atom->length; s != e; s++)
{ static char *digits = "0123456789abcdef";
TRY(Putc(digits[(*s >> 4) & 0xf], options->out));
TRY(Putc(digits[(*s ) & 0xf], options->out));
}
return PutString(">", options->out);
}
static int /* FALSE, TRUE or TRUE_WITH_SPACE */
writeAtom(atom_t a, write_options *options)
{ Atom atom = atomValue(a);
if ( (options->flags & PL_WRT_BLOB_PORTRAY) &&
false(atom->type, PL_BLOB_TEXT) &&
GD->cleaning <= CLN_PROLOG &&
a != ATOM_nil )
{ GET_LD
int rc;
fid_t fid;
term_t av;
if ( !(fid = PL_open_foreign_frame()) )
return FALSE;
av = PL_new_term_ref();
PL_put_atom(av, a);
rc = callPortray(av, 1200, options);
PL_close_foreign_frame(fid);
switch(rc)
{ case TRUE:
return TRUE;
case FALSE:
break;
default:
return FALSE; /* error */
}
}
if ( atom->type->write )
return (*atom->type->write)(options->out, a, options->flags);
if ( false(atom->type, PL_BLOB_TEXT) )
return writeBlob(a, options);
if ( true(options, PL_WRT_QUOTED) )
{ switch( atomType(a, options) )
{ case AT_LOWER:
case AT_SYMBOL:
case AT_SOLO:
case AT_SPECIAL:
return PutToken(atom->name, options->out);
case AT_QUOTE:
case AT_FULLSTOP:
default:
{ int rc;
TRY(rc=PutOpenToken('\'', options->out));
TRY(writeQuoted(options->out,
atom->name,
atom->length,
'\'', options));
return rc;
}
}
} else
return PutTokenN(atom->name, atom->length, options->out);
}
int
writeAtomToStream(IOSTREAM *s, atom_t atom)
{ write_options options;
memset(&options, 0, sizeof(options));
options.out = s;
options.module = MODULE_user;
return writeAtom(atom, &options);
}
int
writeUCSAtom(IOSTREAM *fd, atom_t atom, int flags)
{ Atom a = atomValue(atom);
pl_wchar_t *s = (pl_wchar_t*)a->name;
size_t len = a->length/sizeof(pl_wchar_t);
pl_wchar_t *e = &s[len];
if ( (flags&PL_WRT_QUOTED) && !unquoted_atomW(atom, fd, flags) )
{ pl_wchar_t quote = L'\'';
TRY(PutOpenToken(quote, fd) &&
Putc(quote, fd));
while(s < e)
{ TRY(putQuoted(*s++, quote, flags, fd));
}
return Putc(quote, fd);
}
if ( s < e && !PutOpenToken(s[0], fd) )
return FALSE;
for( ; s<e; s++)
{ if ( !Putc(*s, fd) )
return FALSE;
}
return TRUE;
}
#ifdef O_RESERVED_SYMBOLS
int
writeReservedSymbol(IOSTREAM *fd, atom_t atom, int flags)
{ Atom a = atomValue(atom);
const char *s = a->name;
size_t len = a->length;
const char *e = &s[len];
if ( atom == ATOM_nil )
return PutToken("[]", fd);
if ( (flags&PL_WRT_QUOTED) )
{ char quote = '\'';
if ( PutOpenToken('C', fd) &&
Putc('C', fd) &&
Putc(quote, fd) )
{ while(s < e)
{ if ( !putQuoted(*s++, quote, flags, fd) )
return FALSE;
}
return Putc(quote, fd);
}
}
if ( s < e && !PutOpenToken(s[0], fd) )
return FALSE;
for( ; s<e; s++)
{ if ( !Putc(*s, fd) )
return FALSE;
}
return TRUE;
}
#endif
#if O_STRING
static inline int
get_chr_from_text(const PL_chars_t *t, int index)
{ switch(t->encoding)
{ case ENC_ISO_LATIN_1:
return t->text.t[index]&0xff;
case ENC_WCHAR:
return t->text.w[index];
default:
assert(0);
return 0;
}
}
static int
writeString(term_t t, write_options *options)
{ GET_LD
PL_chars_t txt;
int rc = TRUE;
PL_STRINGS_MARK();
PL_get_text(t, &txt, CVT_STRING);
if ( true(options, PL_WRT_QUOTED) )
{ int quote;
unsigned int i;
if ( true(options, PL_WRT_BACKQUOTED_STRING) )
quote = '`';
else
quote = '"';
if ( !(rc=Putc(quote, options->out)) )
goto out;
for(i=0; i<txt.length; i++)
{ int chr = get_chr_from_text(&txt, i);
if ( !(rc=putQuoted(chr, quote, options->flags, options->out)) )
goto out;
}
rc = Putc(quote, options->out);
} else
{ unsigned int i;
for(i=0; i<txt.length; i++)
{ int chr = get_chr_from_text(&txt, i);
if ( !(rc=Putc(chr, options->out)) )
break;
}
}
PL_STRINGS_RELEASE();
out:
PL_free_text(&txt);
return rc;
}
#endif /*O_STRING*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Formatting a float. This used to use sprintf(), but there are two
problems with this. First of all, this uses the current locale, which is
complicated to avoid. Second, it does not provide a mode that guarantees
reliable read-back. Using %g gets closest, but %.15g doesn't guarantee
read-back and %.17g does, but prints 0.1 as 0.100..001, etc.
This uses dtoa.c. See pl-dtoa.c for how this is packed into SWI-Prolog.
TBD: The number of cases are large. We should see whether it is possible
to clean this up a bit. The 5 cases as such are real: there is no way
around these.
NaN and Inf printing based on
http://eclipseclp.org/Specs/core_update_float.html, with comments from
Joachim Schimpf.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#ifdef HAVE_IEEE754_H
#include <ieee754.h>
#else
union ieee754_double
{ double d;
struct
{
#ifdef WORDS_BIGENDIAN
unsigned int negative:1;
unsigned int exponent:11;
unsigned int mantissa0:20;
unsigned int mantissa1:32;
#else
#ifdef FLOAT_WORDS_BIGENDIAN
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
unsigned int mantissa1:32;
#else
unsigned int mantissa1:32;
unsigned int mantissa0:20;
unsigned int exponent:11;
unsigned int negative:1;
#endif
#endif
} ieee;
};
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Joachim Schimpf: The exponent is stored with a "bias" of 1023, so
3ff=1023 means 0. And 0 means that the mantissa is to be multiplied by
2^0 = 1, maybe that's where my confusion came from...
To add to the confusion, the mantissa is a "hidden bit" representation,
i.e. its actual value is 1.<the 52 bits actually stored>, so with a 0
exponent the value of the resulting number is always >= 1 and < 2.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
double
NaN_value(double f)
{ union ieee754_double u;
u.d = f;
assert(u.ieee.exponent == 0x7ff); /* NaN exponent */
u.ieee.exponent = 0x3ff;
return u.d;
}
static char *
writeNaN(double f, char *buf)
{ format_float(NaN_value(f), buf);
strcat(buf, "NaN");
return buf;
}
strnumstat
make_nan(double *f)
{ union ieee754_double u;
u.d = *f;
u.ieee.exponent = 0x7ff; /* NaN exponent */
if ( isnan(u.d) )
{ *f = u.d;
return NUM_OK;
}
return NUM_CONSTRANGE; /* 1.0NaN is in fact 1.0Inf */
}
static char *
writeINF(double f, char *buf)
{ number n;
n.value.f = f;
n.type = V_FLOAT;
if ( ar_signbit(&n) < 0 )
return strcpy(buf, "-1.0Inf");
else
return strcpy(buf, "1.0Inf");
}
static char *
format_special_float(double f, char *buf)
{
#ifdef HAVE_FPCLASSIFY
switch(fpclassify(f))
{ case FP_NAN:
return writeNaN(f, buf);
case FP_INFINITE:
return writeINF(f, buf);
}
#else
#ifdef HAVE_FPCLASS
switch(fpclass(f))
{ case FP_SNAN:
case FP_QNAN:
return writeNaN(f, buf);
case FP_NINF:
case FP_PINF:
return writeINF(f, buf);
case FP_NDENORM: /* pos/neg denormalized non-zero */
case FP_PDENORM:
case FP_NNORM: /* pos/neg normalized non-zero */
case FP_PNORM:
case FP_NZERO: /* pos/neg zero */
case FP_PZERO:
break;
}
#else
#ifdef HAVE__FPCLASS
switch(_fpclass(f))
{ case _FPCLASS_SNAN: