forked from verhas/ScriptBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp.c
1354 lines (1231 loc) · 28.4 KB
/
lsp.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
/*
FILE: lsp.c
HEADER: lsp.h
For documentation of this module read the embedded documentation.
TO_HEADER:
typedef struct NODE
{
unsigned char ntype;
union
{
struct
{
struct NODE *_car,*_cdr;
} n_cons;
double fvalue;
long ivalue;
char *svalue;
}
n_value;
} *LVAL;
#define NTYPE_CON 1
#define NTYPE_FLO 2
#define NTYPE_INT 3
#define NTYPE_STR 4
#define NTYPE_SYM 5
#define NTYPE_CHR 6
#define NTYPE_FRE 7
#define null(x) ((x) == NIL)
#define freep(x) ((x)->ntype == NTYPE_FRE)
#define numberp(x) (floatp(x)||integerp(x))
#define endp(x) null(x)
#define eq(x,y) ((x)==(y))
#define evenp(x) (!oddp(x))
#define oddp(x) (getint(x)&1)
#define minusp(x) (floatp(x) ? getfloat(x) < 0.0 : getint(x) < 0 )
#define plusp(x) (floatp(x) ? getfloat(x) > 0.0 : getint(x) > 0 )
#define zerop(x) (floatp(x) ? getfloat(x) == 0.0 : getint(x) == 0 )
#define first(x) car(x)
#define second(x) cadr(x)
#define third(x) caddr(x)
#define fourth(x) cadddr(x)
#define listp(x) (consp(x)||null(x))
#define gettype(x) ((x)->ntype)
#define getstring(x) ((x)->n_value.svalue)
#define getint(x) ((x)->n_value.ivalue)
#define getfloat(x) ((x)->n_value.fvalue)
#define getchr(x) getint(x)
#define getsymbol(x) getstring(x)
#define caar(x) car(car(x))
#define cadr(x) car(cdr(x))
#define cdar(x) cdr(car(x))
#define cddr(x) cdr(cdr(x))
#define caaar(x) car(car(car(x)))
#define caadr(x) car(car(cdr(x)))
#define cadar(x) car(cdr(car(x)))
#define caddr(x) car(cdr(cdr(x)))
#define cdaar(x) cdr(car(car(x)))
#define cdadr(x) cdr(car(cdr(x)))
#define cddar(x) cdr(cdr(car(x)))
#define cdddr(x) cdr(cdr(cdr(x)))
#define caaaar(x) car(car(car(car(x))))
#define caaadr(x) car(car(car(cdr(x))))
#define caadar(x) car(car(cdr(car(x))))
#define caaddr(x) car(car(cdr(cdr(x))))
#define cadaar(x) car(cdr(car(car(x))))
#define cadadr(x) car(cdr(car(cdr(x))))
#define caddar(x) car(cdr(cdr(car(x))))
#define cadddr(x) car(cdr(cdr(cdr(x))))
#define cdaaar(x) cdr(car(car(car(x))))
#define cdaadr(x) cdr(car(car(cdr(x))))
#define cdadar(x) cdr(car(cdr(car(x))))
#define cdaddr(x) cdr(car(cdr(cdr(x))))
#define cddaar(x) cdr(cdr(car(car(x))))
#define cddadr(x) cdr(cdr(car(cdr(x))))
#define cdddar(x) cdr(cdr(cdr(car(x))))
#define cddddr(x) cdr(cdr(cdr(cdr(x))))
#define settype(x,v) ((x)->ntype=(v))
#define setcar(x,v) ((x)->n_value.n_cons._car=(v))
#define setcdr(x,v) ((x)->n_value.n_cons._cdr=(v))
#define setint(x,v) ((x)->n_value.ivalue=(v))
#define setfloat(x,v) ((x)->n_value.fvalue=(v))
#define setstring(x,v) ((x)->n_value.svalue=(v))
#define setchar(x,v) setint(x,v)
#define setsymbol(x,v) setstring(x,v)
#define sassoc(x,y) nthsassoc((x),(y),1)
#define dolist(X,Y,z) for( X= (z=Y) ? car(z) : NIL ; z ; \
X= (z=cdr(z)) ? car(z) : NIL )
#define dotimes(i,x) for(i = 0 ; i < x ; i++ )
#define loop for(;;)
#define newstring() newnode(NTYPE_STR)
#define newsymbol() newnode(NTYPE_SYM)
#define newint() newnode(NTYPE_INT)
#define newfloat() newnode(NTYPE_FLO)
#define newchar() newnode(NTYPE_CHR)
#define NIL (LVAL)0
#define SCR_WIDTH 70
#define BUFFERLENGTH 1024
#define BUFFERINC 1024
#define ERRSTRLEN 5
#define UNGET_BUFFER_LENGTH 10
typedef struct _tLspObject {
void *(*memory_allocating_function)(size_t, void *);
void (*memory_releasing_function)(void *, void *);
void *pMemorySegment;
FILE *f;
char cOpen,cClose; // the ( and the ) characters
int tabpos,scrsize;
char *buffer;
long cbBuffer;
int SymbolLength;
int CaseFlag;
int UngetBuffer[UNGET_BUFFER_LENGTH];
int UngetCounter;
} tLspObject,*tpLspObject;
*/
/*POD
=H Functions to handle LISP syntax files.
This module implements list handling. The actual structures and
names resemble the LISP naming as well as the handled file format is
LISP.
Using this module the programmer can easily read and write
LISP syntax file and can also handle the structures built up
in the memory.
Known bug:
You can only read a single file at a time.
CUT*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lsp.h"
static char escapers[] = "t\tn\nr\r";
#define ALLOC(X) (pLSP->memory_allocating_function((X),pLSP->pMemorySegment))
#define FREE(X) (pLSP->memory_releasing_function((X),pLSP->pMemorySegment))
#define BUFFER (pLSP->buffer)
#define TABPOS (pLSP->tabpos)
#define SCRSIZE (pLSP->scrsize)
/*
* local function to decide wheter a character is within some set
*/
static isinset(int ch,char *string)
{
while( ch != *string && *++string );
return *string;
}
/*
* Local defines used within this file.
*/
#define getnode() (LVAL)malloc(sizeof(struct NODE))
#define SRC_WIDTH 80 /* The width of the screen for pretty printing. */
#define WSPACE "\t \f\r\n"
#define CONST1 "!$%&*-+./0123456789:<=>?@[]^_{}~"
#define CONST2 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
#define NUMSET "0123456789"
#define NUMSET1 "0123456789-+"
#define numeral1(x) (isinset((x),NUMSET1))
#define space_p(x) isinset((x),WSPACE)
#define const_p(x) (isinset((x),CONST1)||isinset((x),CONST2))
#define const_p1(x) ((const_p(x))&&(!numeral1(x)))
#define numeral(x) (isinset((x),NUMSET))
#define spaceat(x,f) while(space_p(((x)=getC(pLSP,(f)))))
/*
* StrDup well known, however rarely implemented function
*/
#define StrDup(x) c_StrDup(pLSP,(x))
static char * c_StrDup(tpLspObject pLSP,char *s)
{
char *p;
p = (char *)ALLOC(sizeof(char)*(strlen(s)+1));
if( p == NULL )return NULL;
strcpy(p,s);
return p;
}
/*-----------------------------------------------------*/
/* Calculate 10^[a] */
static double pow10(double a)
{
int j,i;
double pro,k;
for( (i= a<0.0) && (a = -a) , j=(int)a , pro=1.0 , k=10; j ;
j%2 && (pro *=k) , j /= 2 , k *= k )
continue;
i && (pro=1.0/pro);
return pro;
}
/*
* Convert a string to double or long.
* string should contain the string
* whatis 0 string is invalid
* 1 string is float
* 2 string is integer
* dres contains the result if whatis 1
* lres contains the result if whatis 2
*
* First version if MINIMOS in FORTRAN
* Second version in MINIMOS2LISP converter
* This version in lsp.c
* (Could you tell the origin?)
*/
static void cnumeric(char *string, int *whatis, double *dres, long *lres)
{
double intpart,fracpart,exppart,man;
int i,sig,esig;
i=1;
sig= 1;
esig=1;
(( *string == '-' && (sig=(-1)) ) || *string == '+') && string++;
for( intpart = 0 ; numeral(*string) ; string++ )
{
intpart *= 10;
intpart += (*string)-'0';
}
if( *string == '.' )
for( man = 1.0 , fracpart = 0.0 ,i = 0 , string ++ ; numeral(*string)
; string ++ )
fracpart += (man *= 0.1) * ((*string)-'0');
if( *string == 'E' )
{ string++;
(*string == '-' && (esig=(-1))) || *string == '+' && string++;
for( exppart=0.0 , i = 0 ; numeral(*string) ; string++)
exppart = 10*exppart + (*string)-'0';
}
while( space_p(*string) )string++;
if( *string )
{
*whatis = 0;
return;
}
if( i )
{
*lres = sig*(long)intpart;
*whatis = 2;
return;
}
*dres = sig*(intpart + fracpart)*pow10(esig*exppart);
*whatis = 1;
return;
}
static int __GETC(int (*pfGetCharacter)(void *),
void *pvInput,
int *UngetBuffer,
int *UngetCounter
){
if( *UngetCounter ){
(*UngetCounter) --;
return UngetBuffer[*UngetCounter];
}
return pfGetCharacter(pvInput);
}
static void __UNGETC(int *UngetBuffer,
int *UngetCounter,
int ch
){
UngetBuffer[(*UngetCounter)++] = ch;
}
#define GETC(x) __GETC((int (*)(void *))getc,(x),pLSP->UngetBuffer,&(pLSP->UngetCounter))
#define UNGETC(x) __UNGETC(pLSP->UngetBuffer,&(pLSP->UngetCounter),x)
/*
* getC to read skipping the comments from the file.
*
* Each comment returns a newline or EOF
*
* Reading strings normal getc is used!
*/
static int getC(tpLspObject pLSP,
FILE *f){
int ch;
if( (ch=GETC(f)) == ';' )
while( (ch=GETC(f)) != '\n' && ch != EOF )
;
return ch;
}
#define SYMBOLLENGTH (pLSP->SymbolLength)
#define CASEFLAG (pLSP->CaseFlag)
static void * _mya(size_t x,void *y){
return malloc(x);
}
static void _myf(void *x, void *y){
free(x);
}
/*POD
=section lsp_init
=H Inititlaize the LSP system
/*FUNCTION*/
LVAL lsp_init(tpLspObject pLSP,
int SymLen,
int CaseFlg,
void *(*memory_allocating_function)(size_t, void *),
void (*memory_releasing_function)(void *, void *),
void *pMemorySegment
){
/*noverbatim
CUT*/
SYMBOLLENGTH = SymLen;
CASEFLAG = CaseFlg;
pLSP->memory_allocating_function = memory_allocating_function ?
memory_allocating_function
:
_mya;
pLSP->memory_releasing_function = memory_releasing_function ?
memory_releasing_function
:
_myf;
pLSP->pMemorySegment = pMemorySegment;
pLSP->UngetCounter = 0;
pLSP->cbBuffer = 0;
pLSP->buffer = NULL;
pLSP->cOpen = '(';
pLSP->cClose = ')';
return NIL;
}
/*POD
=section cons
=H Create a new cons node
/*FUNCTION*/
LVAL c_cons(tpLspObject pLSP
){
/*noverbatim
CUT*/
/*
TO_HEADER:
#define cons() c_cons(pLSP)
*/
LVAL p;
if( null((p = getnode())) )
return NIL;
settype(p,NTYPE_CON);
setcar(p,NIL);
setcdr(p,NIL);
return p;
}
/*
TO_HEADER:
#define newnode(x) c_newnode(pLSP,(x))
*/
/*POD
=section newnode
=H Create a new node
Create a new node with the given type.
/*FUNCTION*/
LVAL c_newnode(tpLspObject pLSP,
unsigned char type
){
/*noverbatim
CUT*/
LVAL p;
if( null((p = getnode())) )
return NIL;
settype(p,type);
switch( type )
{
case NTYPE_CON:
return NULL;
case NTYPE_FLO:
setfloat(p,0.0);
break;
case NTYPE_INT:
setint(p,0);
break;
case NTYPE_STR:
setstring(p,NULL);
break;
case NTYPE_SYM:
setsymbol(p,NULL);
break;
case NTYPE_CHR:
setchar(p,(char)0);
break;
default:
return NULL;
}
return p;
}
/*
TO_HEADER:
#define symcmp(x,y) c_symcmp(pLSP,(x),(y))
*/
/*POD
=section symcmp
=H Compare a symbol to a string.
If the symbol is the same as the string then it returns the pointer to the
symbol! (Garanteed.) Otherwise it returns NIL.
The symbol and the string matches if the first SymbolLength characters
are matching, and CaseFlag says the case sensitivity.
/*FUNCTION*/
LVAL c_symcmp(tpLspObject pLSP,
LVAL p,
char *s
){
/*noverbatim
CUT*/
int i;
char *w,cw,cs;
if( null(p) || !symbolp(p) )return NIL;
/* NOTE: A string should not be so long that decrementing -1 gets to 0! */
for( i = SYMBOLLENGTH , w = getstring(p) ;
i && *s && *w ; i-- , s++ , w++ ){
cw = !CASEFLAG && islower(*w) ? toupper(*w) : *w;
cs = !CASEFLAG && islower(*s) ? toupper(*s) : *s;
if( cw != cs )
return NIL;
}
return i && ( *w || *s ) ? NIL : p;
}
/*
TO_HEADER:
#define nthsassoc(x,y,z) c_nthsassoc(pLSP,(x),(y),(z))
*/
/*POD
=section nthsassoc
=H Get the nth assoc from a list.
This works only for B<symbols> !!!
The second argument should be a
string for symbol comparision.
/*FUNCTION*/
LVAL c_nthsassoc(tpLspObject pLSP,
LVAL p,
char *s,
int n
){
/*noverbatim
CUT*/
LVAL fp;
if( null(p) || !consp(p) )return NIL;
for( fp = p ; fp ; fp = cdr(fp) )
if( !car(fp) || !consp(car(fp)) || !symbolp(caar(fp)) )
continue;
else
if( symcmp(caar(fp),s) && !--n )return car(fp);
return NIL;
}
/*
TO_HEADER:
#define freelist(x) c_freelist(pLSP,(x))
*/
/*POD
=section freelist
=H Free a list
/*FUNCTION*/
LVAL c_freelist(tpLspObject pLSP,
LVAL p
){
/*noverbatim
CUT*/
if( null(p) || freep(p) )return NIL;
if(consp(p) )
{
settype(p,NTYPE_FRE);
freelist(car(p));
freelist(cdr(p));
}
if( stringp(p) )
FREE(getstring(p));
else if( symbolp(p) )
FREE(getsymbol(p));
FREE(p);
return NIL;
}
/*
TO_HEADER:
#define flatc(x) c_flatc(pLSP,(x))
*/
/*POD
=section flatc
=H flatc returns the length of printstring
/*FUNCTION*/
int c_flatc(tpLspObject pLSP,
LVAL p
){
/*noverbatim
CUT*/
int j;
LVAL fp;
if( null(p) )return 3;
switch( gettype(p) ){
case NTYPE_CON:
for( fp = p , j = 1/*(*/ ; fp ; fp = cdr(fp) )
j+= flatc(car(fp))+1/*space*/;
return p ? j : 1+j; /*) was calculated as a space. (Not always.) */
case NTYPE_FLO:
sprintf(BUFFER,"%lf",getfloat(p));
break;
case NTYPE_INT:
sprintf(BUFFER,"%ld",getint(p));
break;
case NTYPE_STR:
sprintf(BUFFER,"\"%s\"",getstring(p));
break;
case NTYPE_SYM:
sprintf(BUFFER,"%s",getsymbol(p));
break;
case NTYPE_CHR:
sprintf(BUFFER,"#\\%c",getchr(p));
break;
default:
return 0;
}
return strlen(BUFFER);
}
/*
* local pprinting function
*
* Do not try to understand how it works. When I wrote it I and God knew how
* it worked. I have forgotten...
* Ask God!
*
* p is the expression is to print.
* k is magic argument to handle non-algorithmic behaviour of pprint.
* (k holds internal beautiness (!) factor of printout. (AI!) :-)
* Serious:
* k=1 we dunno anything about expression
* k=1 the tabulatig spaces were alrady printed!
* k=2 we are in flatc mode
* -dont print tabulating space
* -there is no need to check flatc size.
*/
static LVAL __pprint(tpLspObject pLSP,LVAL p,int k)
#define _pprint(x) __pprint(pLSP,(x),1)
{
LVAL fp;
int j,multiline;
char *s;
if( null(p) )
{
fprintf(pLSP->f,"NIL");
return NIL;
}
switch(gettype(p))
{
case NTYPE_CON:
if( k == 2 || flatc(p) < SCRSIZE-TABPOS )
{
/* Print in flat mode. */
if( k == 1 )
fprintf(pLSP->f,"%*s(",TABPOS,"");
else
fprintf(pLSP->f,"(");
for( fp = p ; fp ; )
{
__pprint(pLSP,car(fp),2);
fp = cdr(fp);
if( fp )
fprintf(pLSP->f," ");
}
fprintf(pLSP->f,")");
return NIL;
}
if( atom(fp=car(p)) || flatc(fp) < (SCRSIZE-TABPOS)/2 )
{
fprintf(pLSP->f,"(");
SCRSIZE--; /* Schrink screen size thinking of the closing paren. */
j = flatc(fp)+2;/* ([flatc]SPACE */
TABPOS += j;
__pprint(pLSP,fp,0);
if( cdr(p) )
{
fprintf(pLSP->f," ");
__pprint(pLSP,cadr(p),0);
fprintf(pLSP->f,"\n");
for( fp = cdr(cdr(p)) ; fp ; )
{
fprintf(pLSP->f,"%*s",TABPOS,"");
__pprint(pLSP,car(fp),0);
fp = cdr(fp);
if( fp )
fprintf(pLSP->f,"\n");
}
}
TABPOS -= j;
fprintf(pLSP->f,")");
SCRSIZE++;
return NIL;
}
fprintf(pLSP->f,"(");
/* Schrink screen size thinking of the closing paren. */
SCRSIZE--;
TABPOS++;
__pprint(pLSP,car(p),0);
if( fp = cdr(p) )
fprintf(pLSP->f,"\n");
while( fp )
{
fprintf(pLSP->f,"%*s",TABPOS,"");
_pprint(car(fp));
fp = cdr(fp);
if( fp )
fprintf(pLSP->f,"\n");
}
TABPOS--;
fprintf(pLSP->f,")");
SCRSIZE++;
return NIL;
case NTYPE_FLO:
fprintf(pLSP->f,"%lf",getfloat(p));
return NIL;
case NTYPE_INT:
fprintf(pLSP->f,"%ld",getint(p));
return NIL;
case NTYPE_STR:
multiline = 0;
for( s=getstring(p) ; *s ; s++ )
if( *s == '\n' ){
multiline = 1;
break;
}
fprintf(pLSP->f,multiline ? "\"\"\"" : "\"");
for( s=getstring(p) ; *s ; s++ )
switch( *s )
{ /* Handle spacial characters. */
case '\"':
fprintf(pLSP->f,"\\\"");
break;
default:
fprintf(pLSP->f,"%c",*s);
break;
}
fprintf(pLSP->f,multiline ? "\"\"\"" : "\"");
return NIL;
case NTYPE_SYM:
fprintf(pLSP->f,"%s",getsymbol(p));
return NIL;
case NTYPE_CHR:
fprintf(pLSP->f,"#\\%c",getchr(p));
return NIL;
default:
return NIL;
}
fprintf(pLSP->f,BUFFER);
return NIL;
}
/*
TO_HEADER:
#define pprint(x,y) c_pprint(pLSP,(x),(y))
*/
/*POD
=section pprint
=H Pretty print a list.
=verbatim
pp-list
Pretty-print a list expression.
IF <the flatsize length of *expr is less than pp-maxlen*>
THEN print the expression on one line,
ELSE
IF <the car of the expression is an atom> or
<the flatsize length of the car of the expression is less than
the half of the rest of the space>
THEN print the expression in the following form:
"(<item0> <item1>
<item2>
...
<itemn> )"
ELSE
IF <the car of the expression is a list>
THEN print the expression in the following form:
"(<list1>
<item2>
...
<itemn> )"
If an expression can not fit into the area
| -------------------------------------------------------- | then it falls out and gets the end into the new line without printing \n :-(
=noverbatim
/*FUNCTION*/
LVAL c_pprint(tpLspObject pLSP,
LVAL p,
FILE *file
){
/*noverbatim
CUT*/
/* We start in the first column. */
TABPOS = 0;
/* Screen is not schrinked. */
SCRSIZE = SCR_WIDTH;
pLSP->f = file;
_pprint(p);
fprintf(pLSP->f,"\n");
return NIL;
}
/*
* local function to read a cons node
*/
static LVAL readcons(tpLspObject pLSP,FILE *f)
{
int ch;
spaceat(ch,f);
if( ch == pLSP->cClose )return NIL;
UNGETC(ch);
return readlist(f);
}
/* Store a character in the buffer at position 'index'
reallocate the buffer if neccessary.
*/
static int storech(tpLspObject pLSP,int i,int ch){
char *pszNewBuffer;
if( i >= pLSP->cbBuffer - 1 ){
pszNewBuffer = ALLOC(pLSP->cbBuffer+BUFFERINC);
if( pszNewBuffer == NULL )return 1;
if( pLSP->cbBuffer )
memcpy(pszNewBuffer,pLSP->buffer,pLSP->cbBuffer);
if( pLSP->buffer )
FREE(pLSP->buffer);
pLSP->buffer = pszNewBuffer;
pLSP->cbBuffer += BUFFERINC;
}
pLSP->buffer[i++] = ch;
pLSP->buffer[i] = (char)0;
return 0;
}
/*
* local function to read an expression
*/
static LVAL _readexpr(tpLspObject pLSP,FILE *f)
{
int ch,ch1,ch2,i;
LVAL p;
char *s;
double dval;
long lval;
spaceat(ch,f);
if( ch == EOF )
{
return NIL;
}
if( ch == pLSP->cClose )
{
return NIL;
}
if( ch == pLSP->cOpen )/* Read a cons node. */
return readcons(pLSP,f);
/**** Note: XLISP allows 1E++10 as a symbol. This is dangerous.
We do not change XLISP (so far), but here I exclude all symbol
names starting with numeral. */
if( const_p1(ch) )/* Read a symbol. */
{
for( i = 0 ; const_p(ch) ; i++ ){
if( storech(pLSP,i,ch) )return NIL;
ch = getC(pLSP,f);
}
UNGETC(ch);
/* Recognize NIL and nil symbols. */
if( !strcmp(BUFFER,"NIL") || !strcmp(BUFFER,"nil") )
return NIL;
p = newsymbol();
s = StrDup( BUFFER );
if( null(p) || s == NULL )return NIL;
setsymbol(p,s);
return p;
}
if( ch == '\"' ){
ch = GETC(f);
storech(pLSP,0,0); /* inititalize the buffer */
if( ch != '\"' )goto SimpleString;
ch = GETC(f);
if( ch != '\"' ){
UNGETC(ch);
ch = '\"';/* ch should hold the first character of the string that is " now */
goto SimpleString;
}
ch = GETC(f);
/* multi line string */
for( i = 0 ; ch != EOF ; i++ ){
if( ch == '\"' ){
ch1 = GETC(f);
ch2 = GETC(f);
if( ch1 == '\"' && ch2 == '\"' )break;
UNGETC(ch2);
UNGETC(ch1);
}
if( ch == '\\' ){
ch = GETC(f);
s = escapers;
while( *s ){
if( *s++ == ch ){
ch = *s;
break;
}
if( *s )s++;
}
}
if( storech(pLSP,i,ch) )return NIL;
ch = GETC(f);
}
p = newstring();
s = StrDup( BUFFER );
if( null(p) || s == NULL )return NIL;
setstring(p,s);
return p;
}
if( ch == '\"' ){/* Read a string. */
ch = GETC(f);/* Eat the " character. */
SimpleString:
for( i = 0 ; ch != '\"' && ch != EOF ; i++ ){
if( ch == '\\' ){
ch = GETC(f);
s = escapers;
while( *s ){
if( *s++ == ch ){
ch = *s;
break;
}
if( *s )s++;
}
}
if( ch == '\n' )return NIL;
if( storech(pLSP,i,ch) )return NIL;
ch = GETC(f);
}
p = newstring();
s = StrDup( BUFFER );
if( null(p) || s == NULL )
{
return NIL;
}
setstring(p,s);
return p;
}
if( numeral1(ch) )
{
for( i = 0 ; isinset(ch,"0123456789+-eE.") ; i++ )
{
if( storech(pLSP,i,ch) )return NIL;
ch = getC(pLSP,f);
}
UNGETC(ch);
cnumeric(BUFFER,&i,&dval,&lval);
switch( i )
{
case 0:
return NIL;
case 1:
/* A float number is coming. */
p = newfloat();
if( null(p) )
{
return NIL;
}
setfloat(p,dval);
return p;
case 2:
/* An integer is coming. */
p = newint();
if( null(p) )
{
return NIL;
}
setint(p,lval);
return p;
default:
return NIL;
}
}
return NIL;
}
/*
TO_HEADER:
#define readlist(x) c_readlist(pLSP,(x))
*/
/*POD
=section readlist
=H Read a list from a file.
The opening '(' character should be away already!
/*FUNCTION*/
LVAL c_readlist(tpLspObject pLSP,
FILE *f
){
/*noverbatim
CUT*/
int ch;
LVAL p,q;
spaceat(ch,f);
if( ch == pLSP->cClose || ch == EOF )return NIL;
UNGETC(ch);
q = cons();
if( null(q) )
{
return NIL;
}
p = _readexpr(pLSP,f);
setcar(q,p);
setcdr(q,readlist(f));
return q;
}
/*
TO_HEADER:
#define readexpr(x) c_readexpr(pLSP,(x))
*/
/*POD
=section readexpr
=H Read an expression from a file.
/*FUNCTION*/
LVAL c_readexpr(tpLspObject pLSP,
FILE *f
){
/*noverbatim
CUT*/
int ch;
spaceat(ch,f);
if( ch == EOF )return NIL;
UNGETC(ch);
return _readexpr(pLSP,f);
}
/*
TO_HEADER:
#define skipexpr(x) c_skipexpr(pLSP,(x))
*/
/*POD
=section skipexpr
=H Reads an expression and forgets
/*FUNCTION*/
LVAL c_skipexpr(tpLspObject pLSP,
FILE *f
){
/*noverbatim
CUT*/
LVAL p;