File: lisp.c

package info (click to toggle)
juman 7.0-3.10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,528 kB
  • sloc: sh: 10,168; ansic: 9,976; perl: 5,195; makefile: 738
file content (696 lines) | stat: -rw-r--r-- 14,633 bytes parent folder | download | duplicates (2)
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
/*
==============================================================================
	lisp.c
		utility functions like LISP
		1990/11/12/Mon	Yutaka MYOKI(Nagao Lab., KUEE)
		1990/12/16/Mon	Last Modified
		special thanks to Itsuki NODA
==============================================================================
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include	<juman.h>

int dividing_code_p(int code);

/*
------------------------------------------------------------------------------
	definition of global variables
------------------------------------------------------------------------------
*/

extern int		LineNo;
extern int		LineNoForError;

/*
------------------------------------------------------------------------------
	LOCAL:
	definition of global variables
------------------------------------------------------------------------------
*/

U_CHAR		Buffer[BUFSIZE];
CELL		_TmpCell;
CELL		*TmpCell = &_TmpCell;
CELLTABLE	*CellTbl = NULL;

CELLTABLE	CellTbl_save;

/*
  以下の定義はchasenのlisp.cから流用
  chasenのサーバーモードでは\n.\nをEOFと見なす。それに対応するようにchasenの
  コードを流用
  それにともないfgetc関数をすべてcha_fgetc関数に変更
  また、jumanでは文法ファイルなどをファイルから読み込むためEOF.\nもEOFと
  見なすようにis_bolの設定部分を変更
  NACSIS 吉岡
*/
extern int fgetc(FILE *fp);
extern int ungetc(int c, FILE *fp);

static int (*my_getc)(FILE *fp) = fgetc;
static int (*my_ungetc)(int c, FILE *fp) = ungetc;

static int is_bol = 1;
static int c_stacked = EOF;

static int cha_getc(fp)
    FILE *fp;
{
    int c;

    if (c_stacked != EOF) {
	c = c_stacked;
	c_stacked = EOF;
    } else
      c = getc(fp);

    /* skip '\r' */
    if (c == '\r')
      c = getc(fp);

    if (c == EOf && is_bol) {
	/* skip '\r' */
	if ((c = getc(fp)) == '\r')
	  c = getc(fp);
	if (c == '\n')
	  c = EOF;
	/* \nの続かない04があった場合,04はスキップされてしまう */
    }

    if (c == '\n' || c == EOF)
      is_bol = 1;
    else
      is_bol = 0;

#if 0
    putc(c,stdout);fflush(stdout);
#endif

    return c;
}

static int cha_ungetc(c, fp)
    int c;
    FILE *fp;
{
    c_stacked = c;
}

void set_cha_getc()
{
    my_getc = cha_getc;
    my_ungetc = cha_ungetc;
}
void unset_cha_getc()
{
    my_getc = fgetc;
    my_ungetc = ungetc;
}

/*
------------------------------------------------------------------------------
	local error processing
------------------------------------------------------------------------------
*/

void error_in_lisp(void)
{
     fprintf(stderr, "\nparse error between line %d and %d.\n", 
	     LineNoForError, LineNo);
     my_exit(DicError);
}

void error_in_program(void)
{
     fprintf(stderr, "\n\"ifnextchar\" returns an unexpected code.\n");
     my_exit(ProgramError);
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<s_feof>:
	after skipping "whitespace" , return TRUE if fp points to EOF
------------------------------------------------------------------------------
*/

int s_feof(FILE *fp) /* bug fixed by kurohashi on 92/4/25 */
{
  int	code, c;
   
  if (s_feof_comment(fp) == EOF) {
    code = TRUE;
  } else {
    if ((c = my_getc(fp)) == EOF) {
      code = TRUE;
    } else {
      if ( (U_CHAR)c == '\n') {
	LineNo++;
	code = s_feof(fp);
      } else if ( (U_CHAR)c == ' ' || (U_CHAR)c == '\t') {
	code = s_feof(fp);
      } else {
	my_ungetc(c, fp);
	code = FALSE;
      }
    }
  }
  return code;
}

int s_feof_comment(FILE *fp)
{
     int	n;
     U_CHAR	Buffer[BUFSIZE];

     if ((n = ifnextchar(fp, (int)COMMENTCHAR)) == TRUE) {
	  while (my_getc(fp) != '\n' && !feof(fp)) {}
	  LineNo++;
	  return s_feof_comment(fp);
     }

     return n;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<make_cell>: make a new cell
------------------------------------------------------------------------------
*/

CELL *make_cell(void)
{
    return((CELL *)lisp_alloc(sizeof(CELL)));
}

/*
CELL *make_cell(void)
{
     if (CellTbl == NULL)
	  CellTbl = make_cell_table(NULL, CELLALLOCSTEP);
     if (CellTbl->max <= CellTbl->n)
	  CellTbl = make_cell_table(CellTbl, CELLALLOCSTEP);

     return &(CellTbl->cell[CellTbl->n++]);
}

CELLTABLE *make_cell_table(CELLTABLE *pre, int size)
{
     CELLTABLE	*tbl;

     tbl = (CELLTABLE *)my_alloc(sizeof(CELLTABLE));

     tbl->cell = (CELL *)my_alloc(sizeof(CELL) * size);
     tbl->pre = pre;
     tbl->next = NULL;
     tbl->max = size;
     tbl->n = 0;

     if (pre != NULL) pre->next = tbl;

     return tbl;
}

void free_cell(CELL *cell)
{
     if (Atomp(cell)) {
	  free(cell->value.atom);
     } else if (Consp(cell)) {
	  free_cell(car(cell));
	  free_cell(cdr(cell));
     } else {
	  return;
     }

     free(cell);
}
*/

/*
------------------------------------------------------------------------------
	FUNCTION
	<tmp_atom>: use <TmpCell>
------------------------------------------------------------------------------
*/

CELL *tmp_atom(U_CHAR *atom)

{
     _Tag(TmpCell) = ATOM;
     _Atom(TmpCell) = atom;

     return TmpCell;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<cons>: make <cons> from <car> & <cdr>
------------------------------------------------------------------------------
*/

CELL *cons(void *car, void *cdr)
{
     CELL *cell;

     cell = make_cell();
     _Tag(cell) = CONS;
     _Car(cell) = car;
     _Cdr(cell) = cdr;

     return cell;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<car>: take <car> from <cons>
------------------------------------------------------------------------------
*/

CELL *car(CELL *cell)
{
     if (Consp(cell))
	  return _Car(cell);
     else if (Null(cell))
	  return NIL;
     else {
	  s_print(stderr, cell);
	  fprintf(stderr, "is not list. in <car>\n");
	  error_in_lisp();
     }
     return NIL;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<cdr>: take <cdr> from <cons>
------------------------------------------------------------------------------
*/

CELL *cdr(CELL *cell)
{
    if (Consp(cell))
      return _Cdr(cell);
    else if (Null(cell))
      return NIL;
    else {
	s_print(stderr, cell);
	fprintf(stderr, "is not list. in <cdr>\n");
	error_in_lisp();
    }
    return NIL;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<equal>:
------------------------------------------------------------------------------
*/

int equal(void *x, void *y)
{
     if (Eq(x, y)) return TRUE;
     else if (Null(x) || Null(y)) return FALSE;
     else if (_Tag(x) != _Tag(y)) return FALSE;
     else if (_Tag(x) == ATOM)	  return !strcmp(_Atom(x), _Atom(y));
     else if (_Tag(x) == CONS)
	  return (equal(_Car(x), _Car(y)) && equal(_Cdr(x), _Cdr(y)));
     else
	  return FALSE;
}

int length(CELL *list)
{
     int	i;

     for (i = 0; Consp(list); i++) {
	  list = _Cdr(list);
     }

     return i;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<ifnextchar>: if next char is <c> return 1, otherwise return 0
------------------------------------------------------------------------------
*/

int ifnextchar(FILE *fp, int i)
{
     int 	c;

     do {
	  c = my_getc(fp);
	  if (c == '\n') LineNo++;
     } while (c == ' ' || c == '\t' || c == '\n' || c == '\r');

     if (c == EOF) return EOF;

     if (i == c) 
	  return TRUE;
     else {
	  my_ungetc(c, fp);
	  return FALSE;
     }
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<comment>: skip comment-line(s)
------------------------------------------------------------------------------
*/

int comment(FILE *fp)
{
     int	n;

     if ((n = ifnextchar(fp, (int)COMMENTCHAR)) == TRUE) {
	  while (my_getc(fp) != '\n' && !feof(fp)) {}
/*	  if (fgets(Buffer, BUFSIZE, fp) == NULL)
	       error(SystemError, "\"fgets\" error in <comment>.", EOA);*/
	  LineNo++;
	  comment(fp);
     } else if (n == EOF) {
	  /*
	  error_in_lisp();
	  */
     }

     return n;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<s_read>: read S-expression
------------------------------------------------------------------------------
*/

CELL *s_read(FILE *fp)
{
     int n;

     if ((n = ifnextchar(fp, (int)BPARENTHESIS)) == TRUE) {
	  return s_read_car(fp);
     } else if (n == FALSE) {
	  return s_read_atom(fp);
     }

     if (n == EOF) error_in_lisp();
     else	   error_in_program();

     return NIL;
}

int myscanf(FILE *fp, U_CHAR *cp)
{
    int code;

    code = my_getc(fp);
    if ( dividing_code_p(code) )
      return 0;
    else if ( code == EOF )
      return EOF;
    else if ( code == '"' ) {
	*cp++ = code;
	while ( 1 ) {
	    code = my_getc(fp);
	    if ( code == EOF )
	      error_in_lisp();
	    else if ( code == '"' ) {
		*cp++ = code;
		*cp++ = '\0';
		return 1;
	    }
#ifndef IO_ENCODING_SJIS
	    else if ( code == '\\' ) {
		*cp++ = code;
		if ( (code = my_getc(fp)) == EOF ) 
		    error_in_lisp();
		*cp++ = code;
	    }
#endif
	    else {
		*cp++ = code;
	    }
	}
    }
    else {
	*cp++ = code;
#ifndef IO_ENCODING_SJIS
	if (code == '\\') 
	    *(cp-1) = my_getc(fp); /* kuro on 12/01/94 */
#endif
	while ( 1 ) {
	    code = my_getc(fp);
	    if ( dividing_code_p(code) || code == EOF ) {
		*cp++ = '\0';
		my_ungetc(code, fp);
		return 1;
	    }
	    else {
		*cp++ = code;
#ifndef IO_ENCODING_SJIS
		if (code == '\\') 
		    *(cp-1) = my_getc(fp); /* kuro on 12/01/94 */
#endif
	    }
	}
    }
}

int dividing_code_p(int code)
{
    switch (code) {
      case '\n': case '\r': case '\t': case ';': case ' ':
      case BPARENTHESIS:
      case EPARENTHESIS:
	return 1;
      default:
	return 0;
    }
}

CELL *s_read_atom(FILE *fp)
{
     CELL *cell;
     U_CHAR *c;
     int n;
/* #ifdef _WIN32
    char *eucstr;
    #endif */

     comment(fp);
     
     /* changed by kurohashi.

     if (((n = fscanf(fp, SCANATOM, Buffer)) == 0) || (n == EOF)) {
	  error_in_lisp();
     }
     */

     if (((n = myscanf(fp, Buffer)) == 0) || (n == EOF)) {
	  error_in_lisp();
     }

/* #ifdef _WIN32
	eucstr = toStringEUC(Buffer);
	strcpy(Buffer, eucstr);
	free(eucstr);
	#endif */

     if (!strcmp(Buffer, NILSYMBOL)) {
	  cell = NIL;
     } else {
	  cell = new_cell();
	  _Tag(cell) = ATOM;
	  c = (U_CHAR *)lisp_alloc(sizeof(U_CHAR) * (strlen(Buffer)+1));
	  strcpy(c, Buffer);
	  _Atom(cell) = c;
     }

     return cell;
}

CELL *s_read_car(FILE *fp)
{
     CELL	*cell;
     int	n;

     comment(fp);

     if ((n = ifnextchar(fp, (int)EPARENTHESIS)) == TRUE) {
	  cell = (CELL *)NIL;
	  return cell;
     } else if (n == FALSE) {
	  cell = new_cell();
	  _Car(cell) = s_read(fp);
	  _Cdr(cell) = s_read_cdr(fp);
	  return cell;
     }

     if (n == EOF) error_in_lisp();
     else          error_in_program();

     return NIL;
}

CELL *s_read_cdr(FILE *fp)
{
     CELL	*cell;
     int	n;

     comment(fp);
     if ((n = ifnextchar(fp, (int)EPARENTHESIS)) == TRUE) {
	  cell = (CELL *)NIL;
	  return cell;
     } else if (n == FALSE) {
	  cell = s_read_car(fp);
	  return cell;
     }

     if (n == EOF) error_in_lisp();
     else          error_in_program();

     return NIL;
}

/*
------------------------------------------------------------------------------
	FUNCTION
	<assoc>:
------------------------------------------------------------------------------
*/

CELL *assoc(CELL *item, CELL *alist)
{
     for ( ; (!equal(item, (car(car(alist)))) && (!Null(alist)));
	      alist = cdr(alist))
	  ;
     return car(alist);
}
	      
/*
------------------------------------------------------------------------------
	PROCEDURE
	<s_print>: pretty print S-expression
------------------------------------------------------------------------------
*/

CELL *s_print(FILE *fp, CELL *cell)
{
     _s_print_(fp, cell);
     fputc('\n', fp);
}

CELL *_s_print_(FILE *fp, CELL *cell)
{
     if (Null(cell))
	  fprintf(fp, "%s", NILSYMBOL);
     else {
	  switch (_Tag(cell)) {
	  case CONS:
	       fprintf(fp, "%c", BPARENTHESIS);
	       _s_print_(fp, _Car(cell));
	       _s_print_cdr(fp, _Cdr(cell));
	       fprintf(fp, "%c", EPARENTHESIS);
	       break;
	  case ATOM:
	       fprintf(fp, "%s", _Atom(cell));
	       break;
	  default:
	       error(OtherError, "Illegal cell(in s_print)", EOA);
	  }
     }

     return cell;
}

CELL *_s_print_cdr(FILE *fp, CELL *cell)
{
     if (!Null(cell)) {
	  if (Consp(cell)) {
	       fprintf(fp, " ");
	       _s_print_(fp, _Car(cell));
	       _s_print_cdr(fp, _Cdr(cell));
	  } else {
	       fputc(' ', fp);
	       _s_print_(fp, cell);
	  }
     }

     return cell;
}

/*
------------------------------------------------------------------------------
	PROCEDURE			by yamaji
	<lisp_alloc>: あらかじめ一定領域を確保しておいて malloc を行う
------------------------------------------------------------------------------
*/

void *lisp_alloc(int n)
{
     CELLTABLE	*tbl;
     CELL *p;

     if (n % sizeof(CELL)) n = n/sizeof(CELL)+1; else n /= sizeof(CELL);
     if (CellTbl == NULL || CellTbl != NULL && CellTbl->n+n > CellTbl->max) {
	 /* 新たに一定領域を確保 */
	 if (CellTbl != NULL && CellTbl->next != NULL) {
	     CellTbl = CellTbl->next;
	     CellTbl->n = 0;
	 } else {
	     tbl = (CELLTABLE *)my_alloc(sizeof(CELLTABLE));
	     tbl->cell = (CELL *)my_alloc(sizeof(CELL)*BLOCKSIZE);
	     tbl->pre  = CellTbl;
	     tbl->next = NULL;
	     tbl->max  = BLOCKSIZE;
	     tbl->n    = 0;
	     if (CellTbl != NULL) CellTbl->next = tbl;
	     CellTbl = tbl;
	 }
     }
     p = CellTbl->cell + CellTbl->n;
     CellTbl->n += n;
     if (CellTbl->n > CellTbl->max) error_in_lisp();

     return((void *)p);
}

/*
------------------------------------------------------------------------------
	PROCEDURE			by yamaji
	<lisp_alloc_push>: 現在のメモリアロケート状態を記憶する
------------------------------------------------------------------------------
*/

void lisp_alloc_push(void)
{
    CellTbl_save = *CellTbl;
}

/*
------------------------------------------------------------------------------
	PROCEDURE			by yamaji
	<lisp_alloc_pop>: 記憶したメモリアロケート状態に戻す
------------------------------------------------------------------------------
*/

void lisp_alloc_pop(void)
{
    CellTbl->cell = CellTbl_save.cell;
    CellTbl->n    = CellTbl_save.n;
}