* Rewrote string->number stuff.
[bpt/guile.git] / libguile / read.c
1 /* Copyright (C) 1995,1996,1997,1999,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42
43 \f
44
45 #include <stdio.h>
46 #include "libguile/_scm.h"
47 #include "libguile/chars.h"
48 #include "libguile/eval.h"
49 #include "libguile/unif.h"
50 #include "libguile/keywords.h"
51 #include "libguile/alist.h"
52 #include "libguile/srcprop.h"
53 #include "libguile/hashtab.h"
54 #include "libguile/hash.h"
55 #include "libguile/ports.h"
56 #include "libguile/root.h"
57 #include "libguile/strings.h"
58 #include "libguile/vectors.h"
59
60 #include "libguile/validate.h"
61 #include "libguile/read.h"
62
63 \f
64
65 SCM_SYMBOL (scm_keyword_prefix, "prefix");
66
67 scm_t_option scm_read_opts[] = {
68 { SCM_OPTION_BOOLEAN, "copy", 0,
69 "Copy source code expressions." },
70 { SCM_OPTION_BOOLEAN, "positions", 0,
71 "Record positions of source code expressions." },
72 { SCM_OPTION_BOOLEAN, "case-insensitive", 0,
73 "Convert symbols to lower case."},
74 { SCM_OPTION_SCM, "keywords", SCM_UNPACK (SCM_BOOL_F),
75 "Style of keyword recognition: #f or 'prefix"}
76 };
77
78 SCM_DEFINE (scm_read_options, "read-options-interface", 0, 1, 0,
79 (SCM setting),
80 "Option interface for the read options. Instead of using\n"
81 "this procedure directly, use the procedures @code{read-enable},\n"
82 "@code{read-disable}, @code{read-set!} and @var{read-options}.")
83 #define FUNC_NAME s_scm_read_options
84 {
85 SCM ans = scm_options (setting,
86 scm_read_opts,
87 SCM_N_READ_OPTIONS,
88 FUNC_NAME);
89 if (SCM_COPY_SOURCE_P)
90 SCM_RECORD_POSITIONS_P = 1;
91 return ans;
92 }
93 #undef FUNC_NAME
94
95 /* An association list mapping extra hash characters to procedures. */
96 static SCM *scm_read_hash_procedures;
97
98 SCM_DEFINE (scm_read, "read", 0, 1, 0,
99 (SCM port),
100 "Read an s-expression from the input port @var{port}, or from\n"
101 "the current input port if @var{port} is not specified.\n"
102 "Any whitespace before the next token is discarded.")
103 #define FUNC_NAME s_scm_read
104 {
105 int c;
106 SCM tok_buf, copy;
107
108 if (SCM_UNBNDP (port))
109 port = scm_cur_inp;
110 SCM_VALIDATE_OPINPORT (1,port);
111
112 c = scm_flush_ws (port, (char *) NULL);
113 if (EOF == c)
114 return SCM_EOF_VAL;
115 scm_ungetc (c, port);
116
117 tok_buf = scm_allocate_string (30);
118 return scm_lreadr (&tok_buf, port, &copy);
119 }
120 #undef FUNC_NAME
121
122
123
124 char *
125 scm_grow_tok_buf (SCM *tok_buf)
126 {
127 size_t oldlen = SCM_STRING_LENGTH (*tok_buf);
128 SCM newstr = scm_allocate_string (2 * oldlen);
129 size_t i;
130
131 for (i = 0; i != oldlen; ++i)
132 SCM_STRING_CHARS (newstr) [i] = SCM_STRING_CHARS (*tok_buf) [i];
133
134 *tok_buf = newstr;
135 return SCM_STRING_CHARS (newstr);
136 }
137
138
139
140 int
141 scm_flush_ws (SCM port, const char *eoferr)
142 {
143 register int c;
144 while (1)
145 switch (c = scm_getc (port))
146 {
147 case EOF:
148 goteof:
149 if (eoferr)
150 {
151 if (!SCM_FALSEP (SCM_FILENAME (port)))
152 scm_misc_error (eoferr,
153 "end of file in ~A",
154 scm_list_1 (SCM_FILENAME (port)));
155 else
156 scm_misc_error (eoferr, "end of file", SCM_EOL);
157 }
158 return c;
159 case ';':
160 lp:
161 switch (c = scm_getc (port))
162 {
163 case EOF:
164 goto goteof;
165 default:
166 goto lp;
167 case SCM_LINE_INCREMENTORS:
168 break;
169 }
170 break;
171 case SCM_LINE_INCREMENTORS:
172 case SCM_SINGLE_SPACES:
173 case '\t':
174 break;
175 default:
176 return c;
177 }
178 }
179
180
181
182 int
183 scm_casei_streq (char *s1, char *s2)
184 {
185 while (*s1 && *s2)
186 if (scm_downcase((int)*s1) != scm_downcase((int)*s2))
187 return 0;
188 else
189 {
190 ++s1;
191 ++s2;
192 }
193 return !(*s1 || *s2);
194 }
195
196
197 /* recsexpr is used when recording expressions
198 * constructed by read:sharp.
199 */
200 #ifndef DEBUG_EXTENSIONS
201 #define recsexpr(obj, line, column, filename) (obj)
202 #else
203 static SCM
204 recsexpr (SCM obj, long line, int column, SCM filename)
205 {
206 if (!SCM_CONSP(obj)) {
207 return obj;
208 } else {
209 SCM tmp = obj, copy;
210 /* If this sexpr is visible in the read:sharp source, we want to
211 keep that information, so only record non-constant cons cells
212 which haven't previously been read by the reader. */
213 if (SCM_FALSEP (scm_whash_lookup (scm_source_whash, obj)))
214 {
215 if (SCM_COPY_SOURCE_P)
216 {
217 copy = scm_cons (recsexpr (SCM_CAR (obj), line, column, filename),
218 SCM_UNDEFINED);
219 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
220 {
221 SCM_SETCDR (copy, scm_cons (recsexpr (SCM_CAR (tmp),
222 line,
223 column,
224 filename),
225 SCM_UNDEFINED));
226 copy = SCM_CDR (copy);
227 }
228 SCM_SETCDR (copy, tmp);
229 }
230 else
231 {
232 recsexpr (SCM_CAR (obj), line, column, filename);
233 while ((tmp = SCM_CDR (tmp)) && SCM_CONSP (tmp))
234 recsexpr (SCM_CAR (tmp), line, column, filename);
235 copy = SCM_UNDEFINED;
236 }
237 scm_whash_insert (scm_source_whash,
238 obj,
239 scm_make_srcprops (line,
240 column,
241 filename,
242 copy,
243 SCM_EOL));
244 }
245 return obj;
246 }
247 }
248 #endif
249
250 /* Consume an SCSH-style block comment. Assume that we've already
251 read the initial `#!', and eat characters until we get a
252 newline/exclamation-point/sharp-sign/newline sequence. */
253
254 static void
255 skip_scsh_block_comment (SCM port)
256 #define FUNC_NAME "skip_scsh_block_comment"
257 {
258 /* Is this portable? Dear God, spare me from the non-eight-bit
259 characters. But is it tasteful? */
260 long history = 0;
261
262 for (;;)
263 {
264 int c = scm_getc (port);
265
266 if (c == EOF)
267 SCM_MISC_ERROR ("unterminated `#! ... !#' comment", SCM_EOL);
268 history = ((history << 8) | (c & 0xff)) & 0xffffffff;
269
270 /* Were the last four characters read "\n!#\n"? */
271 if (history == (('\n' << 24) | ('!' << 16) | ('#' << 8) | '\n'))
272 return;
273 }
274 }
275 #undef FUNC_NAME
276
277
278 static SCM scm_get_hash_procedure(int c);
279
280 static char s_list[]="list";
281
282 SCM
283 scm_lreadr (SCM *tok_buf,SCM port,SCM *copy)
284 #define FUNC_NAME "scm_lreadr"
285 {
286 int c;
287 size_t j;
288 SCM p;
289
290 tryagain:
291 c = scm_flush_ws (port, s_scm_read);
292 tryagain_no_flush_ws:
293 switch (c)
294 {
295 case EOF:
296 return SCM_EOF_VAL;
297
298 case '(':
299 return SCM_RECORD_POSITIONS_P
300 ? scm_lreadrecparen (tok_buf, port, s_list, copy)
301 : scm_lreadparen (tok_buf, port, s_list, copy);
302 case ')':
303 SCM_MISC_ERROR ("unexpected \")\"", SCM_EOL);
304 goto tryagain;
305
306 case '\'':
307 p = scm_sym_quote;
308 goto recquote;
309 case '`':
310 p = scm_sym_quasiquote;
311 goto recquote;
312 case ',':
313 c = scm_getc (port);
314 if ('@' == c)
315 p = scm_sym_uq_splicing;
316 else
317 {
318 scm_ungetc (c, port);
319 p = scm_sym_unquote;
320 }
321 recquote:
322 p = scm_cons2 (p,
323 scm_lreadr (tok_buf, port, copy),
324 SCM_EOL);
325 if (SCM_RECORD_POSITIONS_P)
326 scm_whash_insert (scm_source_whash,
327 p,
328 scm_make_srcprops (SCM_LINUM (port),
329 SCM_COL (port) - 1,
330 SCM_FILENAME (port),
331 SCM_COPY_SOURCE_P
332 ? (*copy = scm_cons2 (SCM_CAR (p),
333 SCM_CAR (SCM_CDR (p)),
334 SCM_EOL))
335 : SCM_UNDEFINED,
336 SCM_EOL));
337 return p;
338 case '#':
339 c = scm_getc (port);
340
341 {
342 /* Check for user-defined hash procedure first, to allow
343 overriding of builtin hash read syntaxes. */
344 SCM sharp = scm_get_hash_procedure (c);
345 if (!SCM_FALSEP (sharp))
346 {
347 int line = SCM_LINUM (port);
348 int column = SCM_COL (port) - 2;
349 SCM got;
350
351 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
352 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
353 goto unkshrp;
354 if (SCM_RECORD_POSITIONS_P)
355 return *copy = recsexpr (got, line, column,
356 SCM_FILENAME (port));
357 else
358 return got;
359 }
360 }
361 switch (c)
362 {
363 case '(':
364 p = scm_lreadparen (tok_buf, port, "vector", copy);
365 return SCM_NULLP (p) ? scm_nullvect : scm_vector (p);
366
367 case 't':
368 case 'T':
369 return SCM_BOOL_T;
370 case 'f':
371 case 'F':
372 return SCM_BOOL_F;
373
374 case 'b':
375 case 'B':
376 case 'o':
377 case 'O':
378 case 'd':
379 case 'D':
380 case 'x':
381 case 'X':
382 case 'i':
383 case 'I':
384 case 'e':
385 case 'E':
386 scm_ungetc (c, port);
387 c = '#';
388 goto num;
389
390 case '!':
391 /* start of a shell script. Parse as a block comment,
392 terminated by !#, just like SCSH. */
393 skip_scsh_block_comment (port);
394 /* EOF is not an error here */
395 c = scm_flush_ws (port, (char *)NULL);
396 goto tryagain_no_flush_ws;
397
398 #ifdef HAVE_ARRAYS
399 case '*':
400 j = scm_read_token (c, tok_buf, port, 0);
401 p = scm_istr2bve (SCM_STRING_CHARS (*tok_buf) + 1, (long) (j - 1));
402 if (!SCM_FALSEP (p))
403 return p;
404 else
405 goto unkshrp;
406 #endif
407
408 case '{':
409 j = scm_read_token (c, tok_buf, port, 1);
410 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
411
412 case '\\':
413 c = scm_getc (port);
414 j = scm_read_token (c, tok_buf, port, 0);
415 if (j == 1)
416 return SCM_MAKE_CHAR (c);
417 if (c >= '0' && c < '8')
418 {
419 /* Dirk:FIXME:: This type of character syntax is not R5RS
420 * compliant. Further, it should be verified that the constant
421 * does only consist of octal digits. Finally, it should be
422 * checked whether the resulting fixnum is in the range of
423 * characters. */
424 p = scm_i_mem2number (SCM_STRING_CHARS (*tok_buf), j, 8);
425 if (SCM_INUMP (p))
426 return SCM_MAKE_CHAR (SCM_INUM (p));
427 }
428 for (c = 0; c < scm_n_charnames; c++)
429 if (scm_charnames[c]
430 && (scm_casei_streq (scm_charnames[c], SCM_STRING_CHARS (*tok_buf))))
431 return SCM_MAKE_CHAR (scm_charnums[c]);
432 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
433
434 /* #:SYMBOL is a syntax for keywords supported in all contexts. */
435 case ':':
436 j = scm_read_token ('-', tok_buf, port, 0);
437 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
438 return scm_make_keyword_from_dash_symbol (p);
439
440 default:
441 callshrp:
442 {
443 SCM sharp = scm_get_hash_procedure (c);
444
445 if (!SCM_FALSEP (sharp))
446 {
447 int line = SCM_LINUM (port);
448 int column = SCM_COL (port) - 2;
449 SCM got;
450
451 got = scm_call_2 (sharp, SCM_MAKE_CHAR (c), port);
452 if (SCM_EQ_P (got, SCM_UNSPECIFIED))
453 goto unkshrp;
454 if (SCM_RECORD_POSITIONS_P)
455 return *copy = recsexpr (got, line, column,
456 SCM_FILENAME (port));
457 else
458 return got;
459 }
460 }
461 unkshrp:
462 scm_misc_error (s_scm_read, "Unknown # object: ~S",
463 scm_list_1 (SCM_MAKE_CHAR (c)));
464 }
465
466 case '"':
467 j = 0;
468 while ('"' != (c = scm_getc (port)))
469 {
470 if (c == EOF)
471 SCM_MISC_ERROR ("end of file in string constant", SCM_EOL);
472
473 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
474 scm_grow_tok_buf (tok_buf);
475
476 if (c == '\\')
477 switch (c = scm_getc (port))
478 {
479 case '\n':
480 continue;
481 case '0':
482 c = '\0';
483 break;
484 case 'f':
485 c = '\f';
486 break;
487 case 'n':
488 c = '\n';
489 break;
490 case 'r':
491 c = '\r';
492 break;
493 case 't':
494 c = '\t';
495 break;
496 case 'a':
497 c = '\007';
498 break;
499 case 'v':
500 c = '\v';
501 break;
502 }
503 SCM_STRING_CHARS (*tok_buf)[j] = c;
504 ++j;
505 }
506 if (j == 0)
507 return scm_nullstr;
508 SCM_STRING_CHARS (*tok_buf)[j] = 0;
509 return scm_mem2string (SCM_STRING_CHARS (*tok_buf), j);
510
511 case '0': case '1': case '2': case '3': case '4':
512 case '5': case '6': case '7': case '8': case '9':
513 case '.':
514 case '-':
515 case '+':
516 num:
517 j = scm_read_token (c, tok_buf, port, 0);
518 if (j == 1 && (c == '+' || c == '-'))
519 /* Shortcut: Detected symbol '+ or '- */
520 goto tok;
521
522 p = scm_i_mem2number (SCM_STRING_CHARS (*tok_buf), j, 10);
523 if (!SCM_FALSEP (p))
524 return p;
525 if (c == '#')
526 {
527 if ((j == 2) && (scm_getc (port) == '('))
528 {
529 scm_ungetc ('(', port);
530 c = SCM_STRING_CHARS (*tok_buf)[1];
531 goto callshrp;
532 }
533 SCM_MISC_ERROR ("unknown # object", SCM_EOL);
534 }
535 goto tok;
536
537 case ':':
538 if (SCM_EQ_P (SCM_PACK (SCM_KEYWORD_STYLE), scm_keyword_prefix))
539 {
540 j = scm_read_token ('-', tok_buf, port, 0);
541 p = scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
542 return scm_make_keyword_from_dash_symbol (p);
543 }
544 /* fallthrough */
545 default:
546 j = scm_read_token (c, tok_buf, port, 0);
547 /* fallthrough */
548
549 tok:
550 return scm_mem2symbol (SCM_STRING_CHARS (*tok_buf), j);
551 }
552 }
553 #undef FUNC_NAME
554
555
556 #ifdef _UNICOS
557 _Pragma ("noopt"); /* # pragma _CRI noopt */
558 #endif
559
560 size_t
561 scm_read_token (int ic, SCM *tok_buf, SCM port, int weird)
562 {
563 register size_t j;
564 register int c;
565 register char *p;
566
567 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(ic) : ic);
568 p = SCM_STRING_CHARS (*tok_buf);
569
570 if (weird)
571 j = 0;
572 else
573 {
574 j = 0;
575 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
576 p = scm_grow_tok_buf (tok_buf);
577 p[j] = c;
578 ++j;
579 }
580
581 while (1)
582 {
583 while (j + 2 >= SCM_STRING_LENGTH (*tok_buf))
584 p = scm_grow_tok_buf (tok_buf);
585 c = scm_getc (port);
586 switch (c)
587 {
588 case '(':
589 case ')':
590 case '"':
591 case ';':
592 case SCM_WHITE_SPACES:
593 case SCM_LINE_INCREMENTORS:
594 if (weird)
595 goto default_case;
596
597 scm_ungetc (c, port);
598 case EOF:
599 eof_case:
600 p[j] = 0;
601 return j;
602 case '\\':
603 if (!weird)
604 goto default_case;
605 else
606 {
607 c = scm_getc (port);
608 if (c == EOF)
609 goto eof_case;
610 else
611 goto default_case;
612 }
613 case '}':
614 if (!weird)
615 goto default_case;
616
617 c = scm_getc (port);
618 if (c == '#')
619 {
620 p[j] = 0;
621 return j;
622 }
623 else
624 {
625 scm_ungetc (c, port);
626 c = '}';
627 goto default_case;
628 }
629
630 default:
631 default_case:
632 {
633 c = (SCM_CASE_INSENSITIVE_P ? scm_downcase(c) : c);
634 p[j] = c;
635 ++j;
636 }
637
638 }
639 }
640 }
641
642 #ifdef _UNICOS
643 _Pragma ("opt"); /* # pragma _CRI opt */
644 #endif
645
646 SCM
647 scm_lreadparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
648 #define FUNC_NAME "scm_lreadparen"
649 {
650 SCM tmp;
651 SCM tl;
652 SCM ans;
653 int c;
654
655 c = scm_flush_ws (port, name);
656 if (')' == c)
657 return SCM_EOL;
658 scm_ungetc (c, port);
659 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
660 {
661 ans = scm_lreadr (tok_buf, port, copy);
662 closeit:
663 if (')' != (c = scm_flush_ws (port, name)))
664 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
665 return ans;
666 }
667 ans = tl = scm_cons (tmp, SCM_EOL);
668 while (')' != (c = scm_flush_ws (port, name)))
669 {
670 scm_ungetc (c, port);
671 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
672 {
673 SCM_SETCDR (tl, scm_lreadr (tok_buf, port, copy));
674 goto closeit;
675 }
676 SCM_SETCDR (tl, scm_cons (tmp, SCM_EOL));
677 tl = SCM_CDR (tl);
678 }
679 return ans;
680 }
681 #undef FUNC_NAME
682
683
684 SCM
685 scm_lreadrecparen (SCM *tok_buf, SCM port, char *name, SCM *copy)
686 #define FUNC_NAME "scm_lreadrecparen"
687 {
688 register int c;
689 register SCM tmp;
690 register SCM tl, tl2 = SCM_EOL;
691 SCM ans, ans2 = SCM_EOL;
692 /* Need to capture line and column numbers here. */
693 int line = SCM_LINUM (port);
694 int column = SCM_COL (port) - 1;
695
696 c = scm_flush_ws (port, name);
697 if (')' == c)
698 return SCM_EOL;
699 scm_ungetc (c, port);
700 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
701 {
702 ans = scm_lreadr (tok_buf, port, copy);
703 if (')' != (c = scm_flush_ws (port, name)))
704 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
705 return ans;
706 }
707 /* Build the head of the list structure. */
708 ans = tl = scm_cons (tmp, SCM_EOL);
709 if (SCM_COPY_SOURCE_P)
710 ans2 = tl2 = scm_cons (SCM_CONSP (tmp)
711 ? *copy
712 : tmp,
713 SCM_EOL);
714 while (')' != (c = scm_flush_ws (port, name)))
715 {
716 SCM new_tail;
717
718 scm_ungetc (c, port);
719 if (SCM_EQ_P (scm_sym_dot, (tmp = scm_lreadr (tok_buf, port, copy))))
720 {
721 SCM_SETCDR (tl, tmp = scm_lreadr (tok_buf, port, copy));
722 if (SCM_COPY_SOURCE_P)
723 SCM_SETCDR (tl2, scm_cons (SCM_CONSP (tmp)
724 ? *copy
725 : tmp,
726 SCM_EOL));
727 if (')' != (c = scm_flush_ws (port, name)))
728 SCM_MISC_ERROR ("missing close paren", SCM_EOL);
729 goto exit;
730 }
731
732 new_tail = scm_cons (tmp, SCM_EOL);
733 SCM_SETCDR (tl, new_tail);
734 tl = new_tail;
735
736 if (SCM_COPY_SOURCE_P)
737 {
738 SCM new_tail2 = scm_cons (SCM_CONSP (tmp) ? *copy : tmp, SCM_EOL);
739 SCM_SETCDR (tl2, new_tail2);
740 tl2 = new_tail2;
741 }
742 }
743 exit:
744 scm_whash_insert (scm_source_whash,
745 ans,
746 scm_make_srcprops (line,
747 column,
748 SCM_FILENAME (port),
749 SCM_COPY_SOURCE_P
750 ? *copy = ans2
751 : SCM_UNDEFINED,
752 SCM_EOL));
753 return ans;
754 }
755 #undef FUNC_NAME
756
757
758 \f
759
760 /* Manipulate the read-hash-procedures alist. This could be written in
761 Scheme, but maybe it will also be used by C code during initialisation. */
762 SCM_DEFINE (scm_read_hash_extend, "read-hash-extend", 2, 0, 0,
763 (SCM chr, SCM proc),
764 "Install the procedure @var{proc} for reading expressions\n"
765 "starting with the character sequence @code{#} and @var{chr}.\n"
766 "@var{proc} will be called with two arguments: the character\n"
767 "@var{chr} and the port to read further data from. The object\n"
768 "returned will be the return value of @code{read}.")
769 #define FUNC_NAME s_scm_read_hash_extend
770 {
771 SCM this;
772 SCM prev;
773
774 SCM_VALIDATE_CHAR (1, chr);
775 SCM_ASSERT (SCM_FALSEP (proc)
776 || SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T),
777 proc, SCM_ARG2, FUNC_NAME);
778
779 /* Check if chr is already in the alist. */
780 this = *scm_read_hash_procedures;
781 prev = SCM_BOOL_F;
782 while (1)
783 {
784 if (SCM_NULLP (this))
785 {
786 /* not found, so add it to the beginning. */
787 if (!SCM_FALSEP (proc))
788 {
789 *scm_read_hash_procedures =
790 scm_cons (scm_cons (chr, proc), *scm_read_hash_procedures);
791 }
792 break;
793 }
794 if (SCM_EQ_P (chr, SCM_CAAR (this)))
795 {
796 /* already in the alist. */
797 if (SCM_FALSEP (proc))
798 {
799 /* remove it. */
800 if (SCM_FALSEP (prev))
801 {
802 *scm_read_hash_procedures =
803 SCM_CDR (*scm_read_hash_procedures);
804 }
805 else
806 scm_set_cdr_x (prev, SCM_CDR (this));
807 }
808 else
809 {
810 /* replace it. */
811 scm_set_cdr_x (SCM_CAR (this), proc);
812 }
813 break;
814 }
815 prev = this;
816 this = SCM_CDR (this);
817 }
818
819 return SCM_UNSPECIFIED;
820 }
821 #undef FUNC_NAME
822
823 /* Recover the read-hash procedure corresponding to char c. */
824 static SCM
825 scm_get_hash_procedure (int c)
826 {
827 SCM rest = *scm_read_hash_procedures;
828
829 while (1)
830 {
831 if (SCM_NULLP (rest))
832 return SCM_BOOL_F;
833
834 if (SCM_CHAR (SCM_CAAR (rest)) == c)
835 return SCM_CDAR (rest);
836
837 rest = SCM_CDR (rest);
838 }
839 }
840
841 void
842 scm_init_read ()
843 {
844 scm_read_hash_procedures =
845 SCM_VARIABLE_LOC (scm_c_define ("read-hash-procedures", SCM_EOL));
846
847 scm_init_opts (scm_read_options, scm_read_opts, SCM_N_READ_OPTIONS);
848 #ifndef SCM_MAGIC_SNARFER
849 #include "libguile/read.x"
850 #endif
851 }
852
853 /*
854 Local Variables:
855 c-file-style: "gnu"
856 End:
857 */