Debian Codegraph @

src/c/read.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * read.d - reader
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #define ECL_INCLUDE_MATH_H
16 #include <ecl/ecl.h>
17 #include <ecl/number.h>
18 #include <assert.h> /* for assert() */
19 #include <stdio.h>
20 #include <limits.h>
21 #include <float.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <ecl/internal.h>
25 #include <ecl/ecl-inl.h>
26 #include <ecl/bytecodes.h>
27
28 #undef _complex
29
30 static cl_object dispatch_macro_character(cl_object table, cl_object strm, int c, bool signal_error);
31
32 #define read_suppress (ecl_symbol_value(@'*read-suppress*') != ECL_NIL)
33
34 #ifdef ECL_UNICODE
35 # define TOKEN_STRING_DIM(s) ((s)->string.dim)
36 # define TOKEN_STRING_FILLP(s) ((s)->string.fillp)
37 # define TOKEN_STRING_CHAR(s,n) ((s)->string.self[n])
38 # define TOKEN_STRING_CHAR_SET(s,n,c) (s)->string.self[n]=(c)
39 # define TOKEN_STRING_CHAR_CMP(s,n,c) ((s)->string.self[n]==(c))
40 #else
41 # define TOKEN_STRING_DIM(s) ((s)->base_string.dim)
42 # define TOKEN_STRING_FILLP(s) ((s)->base_string.fillp)
43 # define TOKEN_STRING_CHAR(s,n) ((s)->base_string.self[n])
44 # define TOKEN_STRING_CHAR_SET(s,n,c) ((s)->base_string.self[n]=(c))
45 # define TOKEN_STRING_CHAR_CMP(s,n,c) ((s)->base_string.self[n]==(c))
46 #endif
47
48 #define ECL_READ_ONLY_TOKEN 1
49 #define ECL_READ_RETURN_IGNORABLE 3
50 #define ECL_READ_LIST_DOT 4
51
52 cl_object
53 si_get_buffer_string()
54 {
55 const cl_env_ptr env = ecl_process_env();
56 cl_object pool = env->string_pool;
57 cl_object output;
58 if (pool == ECL_NIL) {
59 #ifdef ECL_UNICODE
60 output = ecl_alloc_adjustable_extended_string(ECL_BUFFER_STRING_SIZE);
61 #else
62 output = ecl_alloc_adjustable_base_string(ECL_BUFFER_STRING_SIZE);
63 #endif
64 } else {
65 output = CAR(pool);
66 env->string_pool = CDR(pool);
67 }
68 TOKEN_STRING_FILLP(output) = 0;
69 @(return output);
70 }
71
72 cl_object
73 si_put_buffer_string(cl_object string)
74 {
75 if (string != ECL_NIL) {
76 const cl_env_ptr env = ecl_process_env();
77 cl_object pool = env->string_pool;
78 cl_index l = 0;
79 if (pool != ECL_NIL) {
80 /* We store the size of the pool in the string index */
81 l = TOKEN_STRING_FILLP(ECL_CONS_CAR(pool));
82 }
83 if (l < ECL_MAX_STRING_POOL_SIZE) {
84 /* Ok, by ignoring the following code, here we
85 * are doing like SBCL: we simply grow the
86 * input buffer and do not care about its
87 * size. */
88 #if 0
89 if (TOKEN_STRING_DIM(string) > 32*ECL_BUFFER_STRING_SIZE) {
90 /* String has been enlarged. Cut it. */
91 #ifdef ECL_UNICODE
92 string = ecl_alloc_adjustable_extended_string(ECL_BUFFER_STRING_SIZE);
93 #else
94 string = ecl_alloc_adjustable_base_string(ECL_BUFFER_STRING_SIZE);
95 #endif
96 }
97 #endif
98 TOKEN_STRING_FILLP(string) = l+1;
99 env->string_pool = CONS(string, pool);
100 }
101 }
102 @(return);
103 }
104
105 static void extra_argument (int c, cl_object stream, cl_object d);
106 static cl_object patch_sharp(const cl_env_ptr env, cl_object x);
107 static cl_object do_read_delimited_list(int d, cl_object strm, bool proper_list);
108
109 cl_object
110 ecl_read_object_non_recursive(cl_object in)
111 {
112 cl_object x;
113 const cl_env_ptr env = ecl_process_env();
114
115 ecl_bds_bind(env, @'si::*sharp-eq-context*', ECL_NIL);
116 ecl_bds_bind(env, @'si::*backq-level*', ecl_make_fixnum(0));
117 x = ecl_read_object(in);
118 x = patch_sharp(env, x);
119 ecl_bds_unwind_n(env, 2);
120 return x;
121 }
122
123 /*
124 * This routine inverts the case of the characters in the buffer which
125 * were not escaped. ESCAPE_LIST is a list of intevals of characters
126 * that were escaped, as in ({(low-limit . high-limit)}*). The list
127 * goes from the last interval to the first one, in reverse order,
128 * and thus we run the buffer from the end to the beginning.
129 */
130 static void
131 invert_buffer_case(cl_object x, cl_object escape_list, int sign)
132 {
133 cl_fixnum high_limit, low_limit;
134 cl_fixnum i = TOKEN_STRING_FILLP(x)-1;
135 do {
136 if (escape_list != ECL_NIL) {
137 cl_object escape_interval = CAR(escape_list);
138 high_limit = ecl_fixnum(CAR(escape_interval));
139 low_limit = ecl_fixnum(CDR(escape_interval));
140 escape_list = CDR(escape_list);
141 } else {
142 high_limit = low_limit = -1;
143 }
144 for (; i > high_limit; i--) {
145 /* The character is not escaped */
146 int c = TOKEN_STRING_CHAR(x,i);
147 if (ecl_upper_case_p(c) && (sign < 0)) {
148 c = ecl_char_downcase(c);
149 } else if (ecl_lower_case_p(c) && (sign > 0)) {
150 c = ecl_char_upcase(c);
151 }
152 TOKEN_STRING_CHAR_SET(x,i,c);
153 }
154 for (; i > low_limit; i--) {
155 /* The character is within an escaped interval */
156 ;
157 }
158 } while (i >= 0);
159 }
160
161 static cl_object
162 ecl_read_object_with_delimiter(cl_object in, int delimiter, int flags,
163 enum ecl_chattrib a)
164 {
165 cl_object x, token;
166 int c, base;
167 cl_object p;
168 cl_index length, i;
169 int colon, intern_flag;
170 bool external_symbol;
171 cl_env_ptr the_env = ecl_process_env();
172 cl_object rtbl = ecl_current_readtable();
173 enum ecl_readtable_case read_case = rtbl->readtable.read_case;
174 cl_object escape_list; /* intervals of escaped characters */
175 cl_fixnum upcase; /* # uppercase characters - # downcase characters */
176 cl_fixnum count; /* number of unescaped characters */
177 bool suppress = read_suppress;
178 if (a != cat_constituent) {
179 c = 0;
180 goto LOOP;
181 }
182 BEGIN:
183 do {
184 c = ecl_read_char(in);
185 if (c == delimiter) {
186 the_env->nvalues = 0;
187 return OBJNULL;
188 }
189 if (c == EOF)
190 FEend_of_file(in);
191 a = ecl_readtable_get(rtbl, c, &x);
192 } while (a == cat_whitespace);
193 if ((a == cat_terminating || a == cat_non_terminating) &&
194 (flags != ECL_READ_ONLY_TOKEN)) {
195 cl_object o;
196 if (ECL_HASH_TABLE_P(x)) {
197 if (suppress) {
198 o = dispatch_macro_character(x, in, c, FALSE);
199 if (o == OBJNULL)
200 goto BEGIN;
201 } else {
202 o = dispatch_macro_character(x, in, c, TRUE);
203 }
204 } else {
205 o = _ecl_funcall3(x, in, ECL_CODE_CHAR(c));
206 }
207 if (the_env->nvalues == 0) {
208 if (flags == ECL_READ_RETURN_IGNORABLE)
209 return ECL_NIL;
210 goto BEGIN;
211 }
212 unlikely_if (the_env->nvalues > 1) {
213 FEerror("The readmacro ~S returned ~D values.",
214 2, x, ecl_make_fixnum(the_env->nvalues));
215 }
216 return o;
217 }
218 LOOP:
219 p = escape_list = ECL_NIL;
220 upcase = count = length = 0;
221 external_symbol = colon = 0;
222 token = si_get_buffer_string();
223 for (;;) {
224 if (c == ':' && (flags != ECL_READ_ONLY_TOKEN) &&
225 a == cat_constituent) {
226 colon++;
227 goto NEXT;
228 }
229 if (colon > 2) {
230 while (colon--) {
231 ecl_string_push_extend(token, ':');
232 length++;
233 }
234 } else if (colon) {
235 external_symbol = (colon == 1);
236 TOKEN_STRING_CHAR_SET(token,length,'\0');
237 /* If the readtable case was :INVERT and all non-escaped characters
238 * had the same case, we revert their case. */
239 if (read_case == ecl_case_invert && count != 0) {
240 if (upcase == count) {
241 invert_buffer_case(token, escape_list, -1);
242 } else if (upcase == -count) {
243 invert_buffer_case(token, escape_list, +1);
244 }
245 }
246 if (length == 0) {
247 p = cl_core.keyword_package;
248 external_symbol = 0;
249 } else {
250 p = ecl_find_package_nolock(token);
251 }
252 if (Null(p) && !suppress) {
253 /* When loading binary files, we sometimes must create
254 symbols whose package has not yet been maked. We
255 allow it, but later on in ecl_init_module we make sure that
256 all referenced packages have been properly built.
257 */
258 cl_object name = cl_copy_seq(token);
259 unlikely_if (Null(the_env->packages_to_be_created_p)) {
260 FEerror("There is no package with the name ~A.", 1, name);
261 }
262 p = _ecl_package_to_be_created(the_env, name);
263 }
264 TOKEN_STRING_FILLP(token) = length = 0;
265 upcase = count = colon = 0;
266 escape_list = ECL_NIL;
267 }
268 if (a == cat_single_escape) {
269 c = ecl_read_char_noeof(in);
270 a = cat_constituent;
271 if (read_case == ecl_case_invert) {
272 escape_list = CONS(CONS(ecl_make_fixnum(length),
273 ecl_make_fixnum(length-1)),
274 escape_list);
275 } else {
276 escape_list = ECL_T;
277 }
278 ecl_string_push_extend(token, c);
279 length++;
280 goto NEXT;
281 }
282 if (a == cat_multiple_escape) {
283 cl_index begin = length;
284 for (;;) {
285 c = ecl_read_char_noeof(in);
286 a = ecl_readtable_get(rtbl, c, NULL);
287 if (a == cat_single_escape) {
288 c = ecl_read_char_noeof(in);
289 a = cat_constituent;
290 } else if (a == cat_multiple_escape)
291 break;
292 ecl_string_push_extend(token, c);
293 length++;
294 }
295 if (read_case == ecl_case_invert) {
296 escape_list = CONS(CONS(ecl_make_fixnum(begin),
297 ecl_make_fixnum(length-1)),
298 escape_list);
299 } else {
300 escape_list = ECL_T;
301 }
302 goto NEXT;
303 }
304 if (a == cat_whitespace || a == cat_terminating) {
305 ecl_unread_char(c, in);
306 break;
307 }
308 unlikely_if (ecl_invalid_character_p(c) && !suppress) {
309 FEreader_error("Found invalid character ~:C", in, 1, ECL_CODE_CHAR(c));
310 }
311 if (read_case != ecl_case_preserve) {
312 if (ecl_upper_case_p(c)) {
313 upcase++;
314 count++;
315 if (read_case == ecl_case_downcase)
316 c = ecl_char_downcase(c);
317 } else if (ecl_lower_case_p(c)) {
318 upcase--;
319 count++;
320 if (read_case == ecl_case_upcase)
321 c = ecl_char_upcase(c);
322 }
323 }
324 ecl_string_push_extend(token, c);
325 length++;
326 NEXT:
327 c = ecl_read_char(in);
328 if (c == EOF)
329 break;
330 a = ecl_readtable_get(rtbl, c, NULL);
331 }
332
333 if (suppress) {
334 x = ECL_NIL;
335 goto OUTPUT;
336 }
337
338 /* If there are some escaped characters, it must be a symbol */
339 if ((flags == ECL_READ_ONLY_TOKEN) || p != ECL_NIL ||
340 escape_list != ECL_NIL || length == 0)
341 goto SYMBOL;
342
343 /* The case in which the buffer is full of dots has to be especial cased */
344 if (length == 1 && TOKEN_STRING_CHAR_CMP(token,0,'.')) {
345 if (flags == ECL_READ_LIST_DOT) {
346 x = @'si::.';
347 goto OUTPUT;
348 } else {
349 FEreader_error("Dots appeared illegally.", in, 0);
350 }
351 } else {
352 int i;
353 for (i = 0; i < length; i++) {
354 if (!TOKEN_STRING_CHAR_CMP(token,i,'.'))
355 goto MAYBE_NUMBER;
356 }
357 FEreader_error("Dots appeared illegally.", in, 0);
358 }
359
360 MAYBE_NUMBER:
361 /* Here we try to parse a number from the content of the buffer */
362 base = ecl_current_read_base();
363 if ((base <= 10) && ecl_alpha_char_p(TOKEN_STRING_CHAR(token,0)))
364 goto SYMBOL;
365 x = ecl_parse_number(token, 0, TOKEN_STRING_FILLP(token), &i, base);
366 unlikely_if (x == ECL_NIL)
367 FEreader_error("Syntax error when reading number.~%Offending string: ~S.",
368 in, 1, token);
369 if (x != OBJNULL && length == i)
370 goto OUTPUT;
371 SYMBOL:
372 if (flags == ECL_READ_ONLY_TOKEN) {
373 the_env->nvalues = 1;
374 return token;
375 }
376
377 /*TOKEN_STRING_CHAR_SET(token,length,'\0');*/
378 /* If the readtable case was :INVERT and all non-escaped characters
379 * had the same case, we revert their case. */
380 if (read_case == ecl_case_invert && count != 0) {
381 if (upcase == count) {
382 invert_buffer_case(token, escape_list, -1);
383 } else if (upcase == -count) {
384 invert_buffer_case(token, escape_list, +1);
385 }
386 }
387 if (external_symbol) {
388 x = ecl_find_symbol(token, p, &intern_flag);
389 unlikely_if (intern_flag != ECL_EXTERNAL) {
390 FEreader_error("Cannot find the external symbol ~A in ~S.", in,
391 2, cl_copy_seq(token), p);
392 }
393 } else {
394 if (p == ECL_NIL) {
395 p = ecl_current_package();
396 }
397 /* INV: cl_make_symbol() copies the string */
398 x = ecl_intern(token, p, &intern_flag);
399 }
400 OUTPUT:
401 si_put_buffer_string(token);
402 the_env->nvalues = 1;
403 return x;
404 }
405
406 /*
407 ecl_read_object(in) reads an object from stream in.
408 This routine corresponds to COMMON Lisp function READ.
409 */
410 cl_object
411 ecl_read_object(cl_object in)
412 {
413 return ecl_read_object_with_delimiter(in, EOF, 0, cat_constituent);
414 }
415
416 cl_object
417 si_read_object_or_ignore(cl_object in, cl_object eof)
418 {
419 cl_object x;
420 const cl_env_ptr env = ecl_process_env();
421
422 ecl_bds_bind(env, @'si::*sharp-eq-context*', ECL_NIL);
423 ecl_bds_bind(env, @'si::*backq-level*', ecl_make_fixnum(0));
424 x = ecl_read_object_with_delimiter(in, EOF, ECL_READ_RETURN_IGNORABLE,
425 cat_constituent);
426 if (x == OBJNULL) {
427 env->nvalues = 1;
428 x = eof;
429 } else if (env->nvalues) {
430 x = patch_sharp(env, x);
431 }
432 ecl_bds_unwind_n(env, 2);
433 return x;
434 }
435
436 static cl_object
437 right_parenthesis_reader(cl_object in, cl_object character)
438 {
439 FEreader_error("Unmatched right parenthesis, #\\)", in, 0);
440 }
441
442 static cl_object
443 left_parenthesis_reader(cl_object in, cl_object character)
444 {
445 const char c = ')';
446 @(return do_read_delimited_list(c, in, 0));
447 }
448
449 /*
450 * BACKQUOTE READER
451 */
452
453 static
454 cl_object comma_reader(cl_object in, cl_object c)
455 {
456 cl_object x, y;
457 const cl_env_ptr env = ecl_process_env();
458 cl_fixnum backq_level = ecl_fixnum(ECL_SYM_VAL(env, @'si::*backq-level*'));
459
460 unlikely_if (backq_level <= 0 && !read_suppress)
461 FEreader_error("A comma has appeared out of a backquote.", in, 0);
462 /* Read character & complain at EOF */
463 c = cl_peek_char(2,ECL_NIL,in);
464 if (c == ECL_CODE_CHAR('@@')) {
465 x = @'si::unquote-splice';
466 ecl_read_char(in);
467 } else if (c == ECL_CODE_CHAR('.')) {
468 x = @'si::unquote-nsplice';
469 ecl_read_char(in);
470 } else {
471 x = @'si::unquote';
472 }
473 ECL_SETQ(env, @'si::*backq-level*', ecl_make_fixnum(backq_level-1));
474 y = ecl_read_object(in);
475 ECL_SETQ(env, @'si::*backq-level*', ecl_make_fixnum(backq_level));
476 return cl_list(2, x, y);
477 }
478
479 static
480 cl_object backquote_reader(cl_object in, cl_object c)
481 {
482 const cl_env_ptr the_env = ecl_process_env();
483 cl_fixnum backq_level = ecl_fixnum(ECL_SYM_VAL(the_env, @'si::*backq-level*'));
484 ECL_SETQ(the_env, @'si::*backq-level*', ecl_make_fixnum(backq_level+1));
485 c = ecl_read_object(in);
486 ECL_SETQ(the_env, @'si::*backq-level*', ecl_make_fixnum(backq_level));
487 unlikely_if (c == OBJNULL)
488 FEend_of_file(in);
489 unlikely_if (read_suppress)
490 @(return ECL_NIL);
491 #if 0
492 @(return cl_macroexpand_1(2, cl_list(2, @'si::quasiquote', in), ECL_NIL));;
493 #else
494 @(return cl_list(2,@'si::quasiquote',c));
495 #endif
496 }
497
498 /*
499 read_constituent(in, 0) reads a sequence of constituent characters from stream
500 in and places it in token. As a help, it returns TRUE or FALSE depending on
501 the value of *READ-SUPPRESS*.
502
503 The flag not_first is used by some reader macros to signify, that it is not a
504 standalone token. For example #x123 calls read_constituent(in, 1) for "123".
505 */
506 static cl_object
507 read_constituent(cl_object in, bool not_first)
508 {
509 int store = !read_suppress;
510 cl_object rtbl = ecl_current_readtable();
511 cl_object token = si_get_buffer_string();
512 do {
513 int c = ecl_read_char(in);
514 enum ecl_chattrib c_cat;
515 if (c == EOF) {
516 break;
517 }
518 c_cat = ecl_readtable_get(rtbl, c, NULL);
519 if (c_cat == cat_constituent ||
520 ((c_cat == cat_non_terminating) && not_first))
521 {
522 if (store) {
523 ecl_string_push_extend(token, c);
524 }
525 } else {
526 ecl_unread_char(c, in);
527 break;
528 }
529 not_first = 1;
530 } while(1);
531 return (read_suppress)? ECL_NIL : token;
532 }
533
534 static cl_object
535 double_quote_reader(cl_object in, cl_object c)
536 {
537 int delim = ECL_CHAR_CODE(c);
538 cl_object rtbl = ecl_current_readtable();
539 cl_object token = si_get_buffer_string();
540 cl_object output;
541 for (;;) {
542 int c = ecl_read_char_noeof(in);
543 if (c == delim)
544 break;
545 else if (ecl_readtable_get(rtbl, c, NULL) == cat_single_escape)
546 c = ecl_read_char_noeof(in);
547 ecl_string_push_extend(token, c);
548 }
549
550 /* Must be kept a (SIMPLE-ARRAY CHARACTERS (*)), see
551 * http://sourceforge.net/p/ecls/mailman/message/32272388/ */
552 output = cl_copy_seq(token);
553 si_put_buffer_string(token);
554 @(return output);
555 }
556
557 static cl_object
558 dispatch_reader_fun(cl_object in, cl_object dc)
559 {
560 cl_object readtable = ecl_current_readtable();
561 cl_object dispatch_table;
562 int c = ecl_char_code(dc);
563 ecl_readtable_get(readtable, c, &dispatch_table);
564 unlikely_if (!ECL_HASH_TABLE_P(dispatch_table))
565 FEreader_error("~C is not a dispatching macro character", in, 1, dc);
566 return dispatch_macro_character(dispatch_table, in, c, TRUE);
567 }
568
569 /*
570 Returns OBJNULL if no dispatch function is defined and signal_error
571 is false
572 */
573 static cl_object
574 dispatch_macro_character(cl_object table, cl_object in, int c, bool signal_error)
575 {
576 cl_object arg;
577 int d;
578 c = ecl_read_char_noeof(in);
579 d = ecl_digitp(c, 10);
580 if (d >= 0) {
581 cl_fixnum i = 0;
582 do {
583 i = 10*i + d;
584 c = ecl_read_char_noeof(in);
585 d = ecl_digitp(c, 10);
586 } while (d >= 0);
587 arg = ecl_make_fixnum(i);
588 } else {
589 arg = ECL_NIL;
590 }
591 {
592 cl_object dc = ECL_CODE_CHAR(c);
593 cl_object fun = ecl_gethash_safe(dc, table, ECL_NIL);
594 unlikely_if (Null(fun)) {
595 if (signal_error) {
596 FEreader_error("No dispatch function defined "
597 "for character ~S",
598 in, 1, dc);
599 } else {
600 return OBJNULL;
601 }
602 }
603 return _ecl_funcall4(fun, in, dc, arg);
604 }
605 }
606
607 static cl_object
608 single_quote_reader(cl_object in, cl_object c)
609 {
610 c = ecl_read_object(in);
611 unlikely_if (c == OBJNULL)
612 FEend_of_file(in);
613 @(return cl_list(2, @'quote', c));
614 }
615
616 static cl_object
617 void_reader3(cl_object in, cl_object c, cl_object f)
618 {
619 /* no result */
620 @(return);
621 }
622
623 static cl_object
624 semicolon_reader(cl_object in, cl_object c)
625 {
626 int auxc;
627
628 do
629 auxc = ecl_read_char(in);
630 while (auxc != '\n' && auxc != EOF);
631 /* no result */
632 @(return);
633 }
634
635 /*
636 sharpmacro routines
637 */
638
639 static cl_object
640 sharp_generic_error(cl_object in, cl_object c, cl_object n)
641 {
642 FEreader_error("The character ~:C is not a valid dispatch macro character",
643 in, 1, c);
644 }
645
646 static cl_object
647 sharp_C_reader(cl_object in, cl_object c, cl_object d)
648 {
649 const cl_env_ptr the_env = ecl_process_env();
650 cl_object x, real, imag;
651
652 if (d != ECL_NIL && !read_suppress)
653 extra_argument('C', in, d);
654 x = ecl_read_object(in);
655 unlikely_if (x == OBJNULL)
656 FEend_of_file(in);
657 if (read_suppress)
658 @(return ECL_NIL);
659 unlikely_if (!ECL_CONSP(x) || ecl_length(x) != 2)
660 FEreader_error("Reader macro #C should be followed by a list",
661 in, 0);
662 real = CAR(x);
663 imag = CADR(x);
664 /* INV: ecl_make_complex() checks its types. When reading circular
665 structures, we cannot check the types of the elements, and we
666 must build the complex number by hand. */
667 if ((CONSP(real) || CONSP(imag)) &&
668 !Null(ECL_SYM_VAL(the_env, @'si::*sharp-eq-context*')))
669 {
670 x = ecl_alloc_object(t_complex);
671 x->gencomplex.real = real;
672 x->gencomplex.imag = imag;
673 } else {
674 x = ecl_make_complex(real, imag);
675 }
676 @(return x);
677 }
678
679 static cl_object
680 sharp_backslash_reader(cl_object in, cl_object c, cl_object d)
681 {
682 const cl_env_ptr the_env = ecl_process_env();
683 cl_object token;
684 if (d != ECL_NIL && !read_suppress) {
685 unlikely_if (!ECL_FIXNUMP(d) || d != ecl_make_fixnum(0)) {
686 FEreader_error("~S is an illegal CHAR-FONT.", in, 1, d);
687 }
688 }
689 token = ecl_read_object_with_delimiter(in, EOF, ECL_READ_ONLY_TOKEN,
690 cat_single_escape);
691 if (token == ECL_NIL) {
692 c = ECL_NIL;
693 } else if (TOKEN_STRING_FILLP(token) == 1) {
694 c = ECL_CODE_CHAR(TOKEN_STRING_CHAR(token,0));
695 } else if (TOKEN_STRING_FILLP(token) == 2 && TOKEN_STRING_CHAR_CMP(token,0,'^')) {
696 /* #\^x */
697 c = ECL_CODE_CHAR(TOKEN_STRING_CHAR(token,1) & 037);
698 } else {
699 cl_object nc = cl_name_char(token);
700 unlikely_if (Null(nc)) {
701 FEreader_error("~S is an illegal character name.", in, 1, token);
702 }
703 c = nc;
704 }
705 si_put_buffer_string(token);
706 ecl_return1(the_env, c);
707 }
708
709 static cl_object
710 sharp_single_quote_reader(cl_object in, cl_object c, cl_object d)
711 {
712 bool suppress = read_suppress;
713 if(d != ECL_NIL && !suppress)
714 extra_argument('\'', in, d);
715 c = ecl_read_object(in);
716 unlikely_if (c == OBJNULL) {
717 FEend_of_file(in);
718 } else if (suppress) {
719 c = ECL_NIL;
720 } else {
721 c = cl_list(2, @'function', c);
722 }
723 @(return c);
724 }
725
726 static cl_object
727 sharp_Y_reader(cl_object in, cl_object c, cl_object d)
728 {
729 cl_index i;
730 cl_object x, rv, nth, lex;
731
732 if (d != ECL_NIL && !read_suppress)
733 extra_argument('Y', in, d);
734 x = ecl_read_object(in);
735 unlikely_if (x == OBJNULL) {
736 FEend_of_file(in);
737 }
738 if (read_suppress) {
739 @(return ECL_NIL);
740 }
741
742 unlikely_if (!ECL_CONSP(x) || ecl_length(x) < 5) {
743 FEreader_error("Reader macro #Y should be followed by a list", in, 0);
744 }
745
746 rv = ecl_alloc_object(t_bytecodes);
747
748 rv->bytecodes.name = ECL_CONS_CAR(x);
749 x = ECL_CONS_CDR(x);
750
751 lex = ECL_CONS_CAR(x);
752 x = ECL_CONS_CDR(x);
753
754 rv->bytecodes.definition = ECL_CONS_CAR(x);
755 x = ECL_CONS_CDR(x);
756
757 nth = ECL_CONS_CAR(x);
758 x = ECL_CONS_CDR(x);
759 rv->bytecodes.code_size = ecl_to_fix(cl_list_length(nth));
760 rv->bytecodes.code = ecl_alloc_atomic(rv->bytecodes.code_size * sizeof(uint16_t));
761 for ( i=0; !ecl_endp(nth) ; i++, nth=ECL_CONS_CDR(nth) )
762 ((cl_opcode*)(rv->bytecodes.code))[i] = ecl_to_fix(ECL_CONS_CAR(nth));
763
764 nth = ECL_CONS_CAR(x);
765 x = ECL_CONS_CDR(x);
766 rv->bytecodes.data = nth;
767
768 nth = ECL_CONS_CAR(x);
769 x = ECL_CONS_CDR(x);
770 rv->bytecodes.flex = nth;
771
772 nth = ECL_CONS_CAR(x);
773 x = ECL_CONS_CDR(x);
774 rv->bytecodes.nlcl = nth;
775
776 if (ECL_ATOM(x)) {
777 nth = ECL_NIL;
778 } else {
779 nth = ECL_CONS_CAR(x);
780 x = ECL_CONS_CDR(x);
781 }
782 rv->bytecodes.file = nth;
783 if (ECL_ATOM(x)) {
784 nth = ecl_make_fixnum(0);
785 } else {
786 nth = ECL_CONS_CAR(x);
787 x = ECL_CONS_CDR(x);
788 }
789 rv->bytecodes.file_position = nth;
790
791 rv->bytecodes.entry = _ecl_bytecodes_dispatch_vararg;
792
793 if (lex != ECL_NIL) {
794 cl_object x = ecl_alloc_object(t_bclosure);
795 x->bclosure.code = rv;
796 x->bclosure.lex = lex;
797 x->bclosure.entry = _ecl_bclosure_dispatch_vararg;
798 rv = x;
799 }
800 @(return rv);
801 }
802
803 #define QUOTE 1
804 #define EVAL 2
805 #define LIST 3
806 #define LISTX 4
807 #define APPEND 5
808 #define NCONC 6
809
810
811 /*
812 *----------------------------------------------------------------------
813 * Stack of unknown size
814 *----------------------------------------------------------------------
815 */
816
817 cl_object
818 si_make_backq_vector(cl_object d, cl_object data, cl_object in)
819 {
820 const cl_env_ptr the_env = ecl_process_env();
821 cl_object v, last;
822 cl_index dim, i;
823 if (Null(d)) {
824 dim = ecl_length(data);
825 } else {
826 dim = ecl_fixnum(d);
827 }
828 v = ecl_alloc_simple_vector(dim, ecl_aet_object);
829 for (i = 0, last = ECL_NIL; i < dim; i++) {
830 if (data == ECL_NIL) {
831 /* ... we fill the vector with the last element read (or NIL). */
832 for (; i < dim; i++) {
833 ecl_aset_unsafe(v, i, last);
834 }
835 break;
836 }
837 ecl_aset_unsafe(v, i, last = ecl_car(data));
838 data = ECL_CONS_CDR(data);
839 }
840 unlikely_if (data != ECL_NIL) {
841 if (in != ECL_NIL) {
842 FEreader_error("Vector larger than specified length,"
843 "~D.", in, 1, d);
844 } else {
845 FEerror("Vector larger than specified length, ~D", 1, d);
846 }
847 }
848 ecl_return1(the_env, v);
849 }
850
851 static cl_object
852 sharp_left_parenthesis_reader(cl_object in, cl_object c, cl_object d)
853 {
854 extern int _cl_backq_car(cl_object *);
855 const cl_env_ptr the_env = ecl_process_env();
856 cl_object v;
857 unlikely_if (!Null(d) &&
858 (!ECL_FIXNUMP(d) || ecl_fixnum_minusp(d) ||
859 ecl_fixnum_greater(d, ecl_make_fixnum(ECL_ARRAY_DIMENSION_LIMIT))))
860 {
861 FEreader_error("Invalid dimension size ~D in #()", in, 1, d);
862 }
863 if (ecl_fixnum_plusp(ECL_SYM_VAL(the_env, @'si::*backq-level*'))) {
864 /* First case: ther might be unquoted elements in the vector.
865 * Then we just create a form that generates the vector.
866 */
867 cl_object x = do_read_delimited_list(')', in, 1);
868 cl_index a = _cl_backq_car(&x);
869 if (a != QUOTE) {
870 v = cl_list(2, @'si::unquote',
871 cl_list(4, @'si::make-backq-vector', d, x, ECL_NIL));
872 } else {
873 return si_make_backq_vector(d, x, in);
874 }
875 } else if (read_suppress) {
876 /* Second case: *read-suppress* = t, we ignore the data */
877 do_read_delimited_list(')', in, 1);
878 v = ECL_NIL;
879 } else if (Null(d)) {
880 /* Third case: no dimension provided. Read a list and
881 coerce it to vector. */
882 return si_make_backq_vector(d, do_read_delimited_list(')', in, 1), in);
883 } else {
884 /* Finally: Both dimension and data are provided. The
885 amount of data cannot exceed the length, but it may
886 be smaller, and in that case...*/
887 cl_object last;
888 cl_index dim = ecl_fixnum(d), i;
889 v = ecl_alloc_simple_vector(dim, ecl_aet_object);
890 for (i = 0, last = ECL_NIL;; i++) {
891 cl_object aux = ecl_read_object_with_delimiter(in, ')', 0,
892 cat_constituent);
893 if (aux == OBJNULL)
894 break;
895 unlikely_if (i >= dim) {
896 FEreader_error("Vector larger than specified length,"
897 "~D.", in, 1, d);
898 }
899 ecl_aset_unsafe(v, i, last = aux);
900 }
901 /* ... we fill the vector with the last element read (or NIL). */
902 for (; i < dim; i++) {
903 ecl_aset_unsafe(v, i, last);
904 }
905 }
906 @(return v);
907 }
908
909 static cl_object
910 sharp_asterisk_reader(cl_object in, cl_object c, cl_object d)
911 {
912 cl_env_ptr env = ecl_process_env();
913 cl_index sp = ECL_STACK_INDEX(env);
914 cl_object last, elt, x;
915 cl_fixnum dim, dimcount, i;
916 cl_object rtbl = ecl_current_readtable();
917 enum ecl_chattrib a;
918
919 if (read_suppress) {
920 read_constituent(in, 1);
921 @(return ECL_NIL);
922 }
923 for (dimcount = 0 ;; dimcount++) {
924 int x = ecl_read_char(in);
925 if (x == EOF)
926 break;
927 a = ecl_readtable_get(rtbl, x, NULL);
928 if (a == cat_terminating || a == cat_whitespace) {
929 ecl_unread_char(x, in);
930 break;
931 }
932 unlikely_if (a == cat_single_escape || a == cat_multiple_escape ||
933 (x != '0' && x != '1'))
934 {
935 FEreader_error("Character ~:C is not allowed after #*",
936 in, 1, ECL_CODE_CHAR(x));
937 }
938 ECL_STACK_PUSH(env, ecl_make_fixnum(x == '1'));
939 }
940 if (Null(d)) {
941 dim = dimcount;
942 } else {
943 unlikely_if (!ECL_FIXNUMP(d) || ((dim = ecl_fixnum(d)) < 0) ||
944 (dim > ECL_ARRAY_DIMENSION_LIMIT))
945 {
946 FEreader_error("Wrong vector dimension size ~D in #*.",
947 in, 1, d);
948 }
949 unlikely_if (dimcount > dim)
950 FEreader_error("Too many elements in #*.", in, 0);
951 unlikely_if (dim && (dimcount == 0))
952 FEreader_error("Cannot fill the bit-vector #*.", in, 0);
953 }
954 last = ECL_STACK_REF(env,-1);
955 x = ecl_alloc_simple_vector(dim, ecl_aet_bit);
956 for (i = 0; i < dim; i++) {
957 elt = (i < dimcount) ? env->run_stack.org[sp+i] : last;
958 if (elt == ecl_make_fixnum(0))
959 x->vector.self.bit[i/CHAR_BIT] &= ~(0200 >> i%CHAR_BIT);
960 else
961 x->vector.self.bit[i/CHAR_BIT] |= 0200 >> i%CHAR_BIT;
962 }
963 ECL_STACK_POP_N_UNSAFE(env, dimcount);
964 @(return x);
965 }
966
967 static cl_object
968 sharp_colon_reader(cl_object in, cl_object ch, cl_object d)
969 {
970 cl_object rtbl = ecl_current_readtable();
971 enum ecl_chattrib a;
972 int c;
973 cl_object output, token;
974
975 if (d != ECL_NIL && !read_suppress)
976 extra_argument(':', in, d);
977 c = ecl_read_char_noeof(in);
978 a = ecl_readtable_get(rtbl, c, NULL);
979 token = si_get_buffer_string();
980 goto L;
981 for (;;) {
982 ecl_string_push_extend(token, c);
983 K:
984 c = ecl_read_char(in);
985 if (c == EOF)
986 goto M;
987 a = ecl_readtable_get(rtbl, c, NULL);
988 L:
989 if (a == cat_single_escape) {
990 c = ecl_read_char_noeof(in);
991 a = cat_constituent;
992 } else if (a == cat_multiple_escape) {
993 for (;;) {
994 c = ecl_read_char_noeof(in);
995 a = ecl_readtable_get(rtbl, c, NULL);
996 if (a == cat_single_escape) {
997 c = ecl_read_char_noeof(in);
998 a = cat_constituent;
999 } else if (a == cat_multiple_escape)
1000 break;
1001 ecl_string_push_extend(token, c);
1002 }
1003 goto K;
1004 } else if (ecl_lower_case_p(c)) {
1005 c = ecl_char_upcase(c);
1006 } else if (c == ':' && !read_suppress) {
1007 FEreader_error("An uninterned symbol must not contain a package prefix", in, 0);
1008 }
1009 if (a == cat_whitespace || a == cat_terminating)
1010 break;
1011 }
1012 ecl_unread_char(c, in);
1013
1014 M:
1015 if (read_suppress) {
1016 output = ECL_NIL;
1017 } else {
1018 output = cl_make_symbol(token);
1019 }
1020 si_put_buffer_string(token);
1021 @(return output);
1022 }
1023
1024 static cl_object
1025 sharp_dot_reader(cl_object in, cl_object c, cl_object d)
1026 {
1027 const cl_env_ptr env = ecl_process_env();
1028 if (d != ECL_NIL && !read_suppress)
1029 extra_argument('.', in, d);
1030 c = ecl_read_object(in);
1031 unlikely_if (c == OBJNULL)
1032 FEend_of_file(in);
1033 if (read_suppress) {
1034 @(return ECL_NIL);
1035 }
1036 unlikely_if (ecl_cmp_symbol_value(env, @'*read-eval*') == ECL_NIL)
1037 FEreader_error("Cannot evaluate the form #.~A", in, 1, c);
1038 /* FIXME! We should do something here to ensure that the #.
1039 * only uses the #n# that have been defined */
1040 c = patch_sharp(env, c);
1041 c = si_eval_with_env(1, c);
1042 @(return c);
1043 }
1044
1045 static cl_object
1046 read_number(cl_object in, int radix, cl_object macro_char)
1047 {
1048 cl_index i;
1049 cl_object x;
1050 /* read_constituent is called with not_first=true, because we are called by
1051 reader macros for composite tokens, like #x123. -- jd 2024-05-12 */
1052 cl_object token = read_constituent(in, 1);
1053 if (token == ECL_NIL) {
1054 x = ECL_NIL;
1055 } else {
1056 x = ecl_parse_number(token, 0, TOKEN_STRING_FILLP(token), &i, radix);
1057 unlikely_if (x == OBJNULL || x == ECL_NIL ||
1058 i != TOKEN_STRING_FILLP(token))
1059 {
1060 FEreader_error("Cannot parse the #~A readmacro.", in, 1,
1061 macro_char);
1062 }
1063 unlikely_if (cl_rationalp(x) == ECL_NIL) {
1064 FEreader_error("The float ~S appeared after the #~A readmacro.",
1065 in, 2, x, macro_char);
1066 }
1067 si_put_buffer_string(token);
1068 }
1069 return x;
1070 }
1071
1072 static cl_object
1073 sharp_B_reader(cl_object in, cl_object c, cl_object d)
1074 {
1075 if(d != ECL_NIL && !read_suppress)
1076 extra_argument('B', in, d);
1077 @(return (read_number(in, 2, ECL_CODE_CHAR('B'))));
1078 }
1079
1080 static cl_object
1081 sharp_O_reader(cl_object in, cl_object c, cl_object d)
1082 {
1083 if(d != ECL_NIL && !read_suppress)
1084 extra_argument('O', in, d);
1085 @(return (read_number(in, 8, ECL_CODE_CHAR('O'))));
1086 }
1087
1088 static cl_object
1089 sharp_X_reader(cl_object in, cl_object c, cl_object d)
1090 {
1091 if(d != ECL_NIL && !read_suppress)
1092 extra_argument('X', in, d);
1093 @(return (read_number(in, 16, ECL_CODE_CHAR('X'))));
1094 }
1095
1096 static cl_object
1097 sharp_R_reader(cl_object in, cl_object c, cl_object d)
1098 {
1099 int radix;
1100 if (read_suppress) {
1101 radix = 10;
1102 } else unlikely_if (!ECL_FIXNUMP(d)) {
1103 FEreader_error("No radix was supplied in the #R readmacro.", in, 0);
1104 } else {
1105 radix = ecl_fixnum(d);
1106 unlikely_if (radix > 36 || radix < 2) {
1107 FEreader_error("~S is an illegal radix.", in, 1, d);
1108 }
1109 }
1110 @(return (read_number(in, radix, ECL_CODE_CHAR('R'))));
1111 }
1112
1113 static cl_object
1114 sharp_eq_reader(cl_object in, cl_object c, cl_object d)
1115 {
1116 const cl_env_ptr the_env = ecl_process_env();
1117 cl_object pair, value;
1118 cl_object sharp_eq_context = ECL_SYM_VAL(the_env, @'si::*sharp-eq-context*');
1119
1120 if (read_suppress) {
1121 @(return);
1122 }
1123 unlikely_if (Null(d)) {
1124 FEreader_error("The #= readmacro requires an argument.", in, 0);
1125 }
1126 unlikely_if (ecl_assq(d, sharp_eq_context) != ECL_NIL) {
1127 FEreader_error("Duplicate definitions for #~D=.", in, 1, d);
1128 }
1129 pair = CONS(d, OBJNULL);
1130 ECL_SETQ(the_env, @'si::*sharp-eq-context*', CONS(pair, sharp_eq_context));
1131 value = ecl_read_object(in);
1132 unlikely_if (value == pair) {
1133 FEreader_error("#~D# is defined by itself.", in, 1, d);
1134 }
1135 ECL_RPLACD(pair, value);
1136 ecl_return1(the_env, value);
1137 }
1138
1139 static cl_object
1140 sharp_sharp_reader(cl_object in, cl_object c, cl_object d)
1141 {
1142 const cl_env_ptr the_env = ecl_process_env();
1143 cl_object pair;
1144
1145 if (read_suppress)
1146 ecl_return1(the_env, ECL_NIL);
1147 unlikely_if (Null(d)) {
1148 FEreader_error("The ## readmacro requires an argument.", in, 0);
1149 }
1150 pair = ecl_assq(d, ECL_SYM_VAL(the_env, @'si::*sharp-eq-context*'));
1151 unlikely_if (pair == ECL_NIL) {
1152 FEreader_error("#~D# is undefined.", in, 1, d);
1153 } else {
1154 cl_object value = ECL_CONS_CDR(pair);
1155 ecl_return1(the_env, (value == OBJNULL)? pair : value);
1156 }
1157 }
1158
1159 static cl_object
1160 do_patch_sharp(cl_object x, cl_object table)
1161 {
1162 /* The hash table maintains an association as follows:
1163 *
1164 * [1] object -> itself
1165 * The object has been processed by patch_sharp, use as it is.
1166 * [2] object -> nothing
1167 * The object has to be processed by do_patch_sharp.
1168 * [3] (# . object) -> object
1169 * This is the value of a #n# statement. The object might
1170 * or might not yet be processed by do_patch_sharp().
1171 */
1172 /* If x is a list, it is processed iteratively. For this, we store
1173 * the first and current cons cell */
1174 cl_object first_cons = OBJNULL;
1175 cl_object current_cons = OBJNULL;
1176 AGAIN:
1177 switch (ecl_t_of(x)) {
1178 case t_list: {
1179 cl_object y;
1180 if (Null(x))
1181 return (first_cons ? first_cons : x);
1182 y = ecl_gethash_safe(x, table, table);
1183 if (y == table) {
1184 /* case [2] */
1185 if (first_cons == OBJNULL)
1186 first_cons = x;
1187 break;
1188 } else if (y == x) {
1189 /* case [1] */
1190 return (first_cons ? first_cons : x);
1191 } else {
1192 /* case [3] */
1193 x = y;
1194 if (current_cons != OBJNULL)
1195 ECL_RPLACD(current_cons, x);
1196 goto AGAIN;
1197 }
1198 }
1199 case t_vector:
1200 case t_array:
1201 case t_complex:
1202 case t_bclosure:
1203 case t_bytecodes: {
1204 cl_object y = ecl_gethash_safe(x, table, table);
1205 if (y == table) {
1206 /* case [2] */
1207 break;
1208 }
1209 /* it can only be case [1] */
1210 }
1211 default:
1212 return (first_cons ? first_cons : x);
1213 }
1214 /* We eagerly mark the object as processed, to avoid infinite
1215 * recursion. */
1216 _ecl_sethash(x, table, x);
1217 switch (ecl_t_of(x)) {
1218 case t_list:
1219 current_cons = x;
1220 ECL_RPLACA(x, do_patch_sharp(ECL_CONS_CAR(x), table));
1221 cl_object rest = ECL_CONS_CDR(x);
1222 if (ecl_t_of(rest) == t_list) {
1223 x = rest;
1224 goto AGAIN;
1225 } else {
1226 ECL_RPLACD(x, do_patch_sharp(rest, table));
1227 }
1228 break;
1229 case t_vector:
1230 if (x->vector.elttype == ecl_aet_object) {
1231 cl_index i;
1232 for (i = 0; i < x->vector.fillp; i++)
1233 x->vector.self.t[i] =
1234 do_patch_sharp(x->vector.self.t[i], table);
1235 }
1236 break;
1237 case t_array:
1238 if (x->vector.elttype == ecl_aet_object) {
1239 cl_index i, j = x->array.dim;
1240 for (i = 0; i < j; i++)
1241 x->array.self.t[i] =
1242 do_patch_sharp(x->array.self.t[i], table);
1243 }
1244 break;
1245 case t_complex: {
1246 cl_object r = do_patch_sharp(x->gencomplex.real, table);
1247 cl_object i = do_patch_sharp(x->gencomplex.imag, table);
1248 if (r != x->gencomplex.real || i != x->gencomplex.imag) {
1249 cl_object c = ecl_make_complex(r, i);
1250 x->gencomplex = c->gencomplex;
1251 }
1252 break;
1253 }
1254 case t_bclosure: {
1255 x->bclosure.lex = do_patch_sharp(x->bclosure.lex, table);
1256 x->bclosure.code = do_patch_sharp(x->bclosure.code, table);
1257 break;
1258 }
1259 case t_bytecodes: {
1260 x->bytecodes.name = do_patch_sharp(x->bytecodes.name, table);
1261 x->bytecodes.definition = do_patch_sharp(x->bytecodes.definition, table);
1262 x->bytecodes.data = do_patch_sharp(x->bytecodes.data, table);
1263 x->bytecodes.flex = do_patch_sharp(x->bytecodes.flex, table);
1264 break;
1265 }
1266 default:;
1267 }
1268 return (first_cons ? first_cons : x);
1269 }
1270
1271 static cl_object
1272 patch_sharp(const cl_env_ptr the_env, cl_object x)
1273 {
1274 cl_object pairs = ECL_SYM_VAL(the_env, @'si::*sharp-eq-context*');
1275 if (pairs == ECL_NIL) {
1276 return x;
1277 } else {
1278 cl_object table =
1279 cl__make_hash_table(@'eq', ecl_make_fixnum(20), /* size */
1280 cl_core.rehash_size,
1281 cl_core.rehash_threshold);
1282 do {
1283 cl_object pair = ECL_CONS_CAR(pairs);
1284 _ecl_sethash(pair, table, ECL_CONS_CDR(pair));
1285 pairs = ECL_CONS_CDR(pairs);
1286 } while (pairs != ECL_NIL);
1287 return do_patch_sharp(x, table);
1288 }
1289 }
1290
1291 #define sharp_plus_reader void_reader3
1292 #define sharp_minus_reader void_reader3
1293
1294 static cl_object
1295 sharp_vertical_bar_reader(cl_object in, cl_object ch, cl_object d)
1296 {
1297 int c;
1298 int level = 0;
1299
1300 if (d != ECL_NIL && !read_suppress)
1301 extra_argument('|', in, d);
1302 for (;;) {
1303 c = ecl_read_char_noeof(in);
1304 L:
1305 if (c == '#') {
1306 c = ecl_read_char_noeof(in);
1307 if (c == '|')
1308 level++;
1309 } else if (c == '|') {
1310 c = ecl_read_char_noeof(in);
1311 if (c == '#') {
1312 if (level == 0)
1313 break;
1314 else
1315 --level;
1316 } else
1317 goto L;
1318 }
1319 }
1320 /* no result */
1321 @(return);
1322 }
1323
1324 /*
1325 #P" ... " returns the pathname with namestring ... .
1326 */
1327 static cl_object
1328 sharp_P_reader(cl_object in, cl_object c, cl_object d)
1329 {
1330 bool suppress = read_suppress;
1331 if (d != ECL_NIL && !suppress)
1332 extra_argument('P', in, d);
1333 d = ecl_read_object(in);
1334 if (suppress) {
1335 d = ECL_NIL;
1336 } else {
1337 d = cl_parse_namestring(3, d, ECL_NIL, ECL_NIL);
1338 }
1339 @(return d);
1340 }
1341
1342 /*
1343 #$ fixnum returns a random-state with the fixnum
1344 as its content.
1345 */
1346 static cl_object
1347 sharp_dollar_reader(cl_object in, cl_object c, cl_object d)
1348 {
1349 cl_object rs;
1350 if (d != ECL_NIL && !read_suppress)
1351 extra_argument('$', in, d);
1352 c = ecl_read_object(in);
1353 rs = ecl_make_random_state(c);
1354
1355 @(return rs);
1356 }
1357
1358 /*
1359 readtable routines
1360 */
1361
1362 static void ECL_INLINE
1363 assert_type_readtable(cl_object function, cl_narg narg, cl_object p)
1364 {
1365 unlikely_if (!ECL_READTABLEP(p)) {
1366 FEwrong_type_nth_arg(function, narg, p, @[readtable]);
1367 }
1368 }
1369
1370
1371 cl_object
1372 ecl_copy_readtable(cl_object from, cl_object to)
1373 {
1374 struct ecl_readtable_entry *from_rtab, *to_rtab;
1375 cl_index i;
1376 size_t entry_bytes = sizeof(struct ecl_readtable_entry);
1377 size_t total_bytes = entry_bytes * RTABSIZE;
1378 cl_object output;
1379
1380 assert_type_readtable(@[copy-readtable], 1, from);
1381 /* For the sake of garbage collector and thread safety we
1382 * create an incomplete object and only copy to the destination
1383 * at the end in a more or less "atomic" (meaning "fast") way.
1384 */
1385 output = ecl_alloc_object(t_readtable);
1386 output->readtable.locked = 0;
1387 output->readtable.table = to_rtab = (struct ecl_readtable_entry *)
1388 ecl_alloc_align(total_bytes, entry_bytes);
1389 from_rtab = from->readtable.table;
1390 memcpy(to_rtab, from_rtab, total_bytes);
1391 for (i = 0; i < RTABSIZE; i++) {
1392 cl_object d = from_rtab[i].dispatch;
1393 if (ECL_HASH_TABLE_P(d)) {
1394 d = si_copy_hash_table(d);
1395 }
1396 to_rtab[i].dispatch = d;
1397 }
1398 output->readtable.read_case = from->readtable.read_case;
1399 #ifdef ECL_UNICODE
1400 if (!Null(from->readtable.hash)) {
1401 output->readtable.hash = si_copy_hash_table(from->readtable.hash);
1402 } else {
1403 output->readtable.hash = ECL_NIL;
1404 }
1405 #endif
1406 if (!Null(to)) {
1407 assert_type_readtable(@[copy-readtable], 2, to);
1408 to->readtable = output->readtable;
1409 output = to;
1410 }
1411 return output;
1412 }
1413
1414 cl_object
1415 ecl_current_readtable(void)
1416 {
1417 const cl_env_ptr the_env = ecl_process_env();
1418 cl_object r;
1419
1420 /* INV: *readtable* always has a value */
1421 r = ECL_SYM_VAL(the_env, @'*readtable*');
1422 unlikely_if (!ECL_READTABLEP(r)) {
1423 ECL_SETQ(the_env, @'*readtable*', cl_core.standard_readtable);
1424 FEerror("The value of *READTABLE*, ~S, was not a readtable.", 1, r);
1425 }
1426 return r;
1427 }
1428
1429 int
1430 ecl_current_read_base(void)
1431 {
1432 const cl_env_ptr the_env = ecl_process_env();
1433 /* INV: *READ-BASE* always has a value */
1434 cl_object x = ECL_SYM_VAL(the_env, @'*read-base*');
1435 cl_fixnum b;
1436
1437 unlikely_if (!ECL_FIXNUMP(x) || ((b = ecl_fixnum(x)) < 2) || (b > 36))
1438 {
1439 ECL_SETQ(the_env, @'*read-base*', ecl_make_fixnum(10));
1440 FEerror("The value of *READ-BASE*~& ~S~%"
1441 "is not in the range (INTEGER 2 36)", 1, x);
1442 }
1443 return b;
1444 }
1445
1446 char
1447 ecl_current_read_default_float_format(void)
1448 {
1449 const cl_env_ptr the_env = ecl_process_env();
1450 cl_object x;
1451
1452 /* INV: *READ-DEFAULT-FLOAT-FORMAT* is always bound to something */
1453 x = ECL_SYM_VAL(the_env, @'*read-default-float-format*');
1454 if (x == @'single-float' || x == @'short-float')
1455 return 'F';
1456 if (x == @'double-float')
1457 return 'D';
1458 if (x == @'long-float') {
1459 return 'L';
1460 }
1461 ECL_SETQ(the_env, @'*read-default-float-format*', @'single-float');
1462 FEerror("The value of *READ-DEFAULT-FLOAT-FORMAT*~& ~S~%"
1463 "is not one of (SINGLE-FLOAT SHORT-FLOAT DOUBLE-FLOAT LONG-FLOAT)",
1464 1, x);
1465 }
1466
1467 static cl_object
1468 stream_or_default_input(cl_object stream)
1469 {
1470 const cl_env_ptr the_env = ecl_process_env();
1471 if (Null(stream))
1472 return ECL_SYM_VAL(the_env, @'*standard-input*');
1473 if (stream == ECL_T)
1474 return ECL_SYM_VAL(the_env, @'*terminal-io*');
1475 return stream;
1476 }
1477
1478 @(defun read (&optional (strm ECL_NIL) (eof_errorp ECL_T) eof_value recursivep)
1479 cl_object x;
1480 @
1481 strm = stream_or_default_input(strm);
1482 if (Null(recursivep)) {
1483 x = ecl_read_object_non_recursive(strm);
1484 } else {
1485 x = ecl_read_object(strm);
1486 }
1487 if (x == OBJNULL) {
1488 if (Null(eof_errorp)) {
1489 @(return eof_value);
1490 }
1491 FEend_of_file(strm);
1492 }
1493 /* Skip whitespace characters, but stop at beginning of new line or token */
1494 if (Null(recursivep)) {
1495 cl_object rtbl = ecl_current_readtable();
1496 int c = ecl_read_char(strm);
1497 if (c != EOF && (ecl_readtable_get(rtbl, c, NULL) != cat_whitespace)) {
1498 ecl_unread_char(c, strm);
1499 }
1500 }
1501 @(return x);
1502 @)
1503
1504 @(defun read_preserving_whitespace
1505 (&optional (strm ECL_NIL)
1506 (eof_errorp ECL_T)
1507 eof_value
1508 recursivep)
1509 cl_object x;
1510 @
1511 strm = stream_or_default_input(strm);
1512 if (Null(recursivep)) {
1513 x = ecl_read_object_non_recursive(strm);
1514 } else {
1515 x = ecl_read_object(strm);
1516 }
1517 if (x == OBJNULL) {
1518 if (Null(eof_errorp))
1519 @(return eof_value);
1520 FEend_of_file(strm);
1521 }
1522 @(return x);
1523 @)
1524
1525 static cl_object
1526 do_read_delimited_list(int d, cl_object in, bool proper_list)
1527 {
1528 int after_dot = 0;
1529 bool suppress = read_suppress;
1530 cl_object x, y = ECL_NIL;
1531 cl_object *p = &y;
1532 do {
1533 x = ecl_read_object_with_delimiter(in, d, ECL_READ_LIST_DOT,
1534 cat_constituent);
1535 if (x == OBJNULL) {
1536 /* End of the list. */
1537 unlikely_if (after_dot == 1) {
1538 /* Something like (1 . ) */
1539 FEreader_error("Object missing after a list dot", in, 0);
1540 }
1541 return y;
1542 } else if (x == @'si::.') {
1543 unlikely_if (proper_list) {
1544 FEreader_error("A dotted list was found where a proper list was expected.", in, 0);
1545 }
1546 unlikely_if (p == &y) {
1547 /* Something like (. 2) */
1548 FEreader_error("A dot appeared after a left parenthesis.", in, 0);
1549 }
1550 unlikely_if (after_dot) {
1551 /* Something like (1 . . 2) */
1552 FEreader_error("Two dots appeared consecutively.", in, 0);
1553 }
1554 after_dot = 1;
1555 } else if (after_dot) {
1556 unlikely_if (after_dot++ > 1) {
1557 /* Something like (1 . 2 3) */
1558 FEreader_error("Too many objects after a list dot", in, 0);
1559 }
1560 *p = x;
1561 } else if (!suppress) {
1562 *p = ecl_list1(x);
1563 p = &ECL_CONS_CDR(*p);
1564 }
1565 } while (1);
1566 }
1567
1568 @(defun read_delimited_list (d &optional (strm ECL_NIL) recursivep)
1569 cl_object l;
1570 int delimiter;
1571 @
1572 delimiter = ecl_char_code(d);
1573 strm = stream_or_default_input(strm);
1574 if (!Null(recursivep)) {
1575 l = do_read_delimited_list(delimiter, strm, 1);
1576 } else {
1577 ecl_bds_bind(the_env, @'si::*sharp-eq-context*', ECL_NIL);
1578 ecl_bds_bind(the_env, @'si::*backq-level*', ecl_make_fixnum(0));
1579 l = do_read_delimited_list(delimiter, strm, 1);
1580 l = patch_sharp(the_env, l);
1581 ecl_bds_unwind_n(the_env, 2);
1582 }
1583 @(return l);
1584 @)
1585
1586 @(defun read_line (&optional (strm ECL_NIL) (eof_errorp ECL_T) eof_value recursivep)
1587 int c;
1588 cl_object token, value0, value1;
1589 @
1590 strm = stream_or_default_input(strm);
1591 #ifdef ECL_CLOS_STREAMS
1592 if (!ECL_ANSI_STREAM_P(strm)) {
1593 value0 = _ecl_funcall2(@'gray::stream-read-line', strm);
1594 value1 = ecl_nth_value(the_env, 1);
1595 if ((Null(value0) || (ECL_STRINGP(value0) && (ecl_length(value0) == 0))) &&
1596 !Null(value1)) {
1597 if (!Null(eof_errorp))
1598 FEend_of_file(strm);
1599 value0 = eof_value;
1600 value1 = ECL_T;
1601 }
1602 goto OUTPUT;
1603 }
1604 #endif
1605 token = si_get_buffer_string();
1606 do {
1607 c = ecl_read_char(strm);
1608 if (c == EOF || c == '\n')
1609 break;
1610 ecl_string_push_extend(token, c);
1611 } while(1);
1612 if (c == EOF && TOKEN_STRING_FILLP(token) == 0) {
1613 if (!Null(eof_errorp))
1614 FEend_of_file(strm);
1615 value0 = eof_value;
1616 value1 = ECL_T;
1617 } else {
1618 #ifdef ECL_NEWLINE_IS_CRLF /* From \r\n, ignore \r */
1619 if (TOKEN_STRING_FILLP(token) > 0 &&
1620 TOKEN_STRING_CHAR_CMP(token,TOKEN_STRING_FILLP(token)-1,'\r'))
1621 TOKEN_STRING_FILLP(token)--;
1622 #endif
1623 #ifdef ECL_NEWLINE_IS_LFCR /* From \n\r, ignore \r */
1624 ecl_read_char(strm);
1625 #endif
1626 value0 = cl_copy_seq(token);
1627 value1 = (c == EOF? ECL_T : ECL_NIL);
1628 }
1629 si_put_buffer_string(token);
1630 OUTPUT:
1631 @(return value0 value1);
1632 @)
1633
1634 @(defun read-char (&optional (strm ECL_NIL) (eof_errorp ECL_T) eof_value recursivep)
1635 int c;
1636 cl_object output;
1637 @
1638 strm = stream_or_default_input(strm);
1639 c = ecl_read_char(strm);
1640 if (c != EOF)
1641 output = ECL_CODE_CHAR(c);
1642 else if (Null(eof_errorp))
1643 output = eof_value;
1644 else
1645 FEend_of_file(strm);
1646 @(return output);
1647 @)
1648
1649 @(defun unread_char (c &optional (strm ECL_NIL))
1650 @
1651 /* INV: unread_char() checks the type `c' */
1652 strm = stream_or_default_input(strm);
1653 ecl_unread_char(ecl_char_code(c), strm);
1654 @(return ECL_NIL);
1655 @)
1656
1657 @(defun peek-char (&optional peek_type (strm ECL_NIL) (eof_errorp ECL_T) eof_value recursivep)
1658 int c;
1659 cl_object rtbl = ecl_current_readtable();
1660 @
1661 strm = stream_or_default_input(strm);
1662 c = ecl_peek_char(strm);
1663 if (c != EOF && !Null(peek_type)) {
1664 if (peek_type == ECL_T) {
1665 do {
1666 /* If the character is not a whitespace, output */
1667 if (ecl_readtable_get(rtbl, c, NULL) != cat_whitespace)
1668 break;
1669 /* Otherwise, read the whitespace and peek the
1670 * next character */
1671 ecl_read_char(strm);
1672 c = ecl_peek_char(strm);
1673 } while (c != EOF);
1674 } else {
1675 do {
1676 /* If the character belongs to the given class,
1677 * we're done. */
1678 if (ecl_char_eq(ECL_CODE_CHAR(c), peek_type))
1679 break;
1680 /* Otherwise, consume the character and
1681 * peek the next one. */
1682 ecl_read_char(strm);
1683 c = ecl_peek_char(strm);
1684 } while (c != EOF);
1685 }
1686 }
1687 if (c != EOF) {
1688 eof_value = ECL_CODE_CHAR(c);
1689 } else if (!Null(eof_errorp)) {
1690 FEend_of_file(strm);
1691 }
1692 @(return eof_value);
1693 @)
1694
1695 @(defun listen (&optional (strm ECL_NIL))
1696 @
1697 strm = stream_or_default_input(strm);
1698 @(return ((ecl_listen_stream(strm) == ECL_LISTEN_AVAILABLE)? ECL_T : ECL_NIL));
1699 @)
1700
1701 @(defun read_char_no_hang (&optional (strm ECL_NIL) (eof_errorp ECL_T) eof_value recursivep)
1702 int f;
1703 @
1704 strm = stream_or_default_input(strm);
1705 #ifdef ECL_CLOS_STREAMS
1706 if (!ECL_ANSI_STREAM_P(strm)) {
1707 cl_object output =
1708 _ecl_funcall2(@'gray::stream-read-char-no-hang', strm);
1709 if (output == @':eof')
1710 goto END_OF_FILE;
1711 @(return output);
1712 }
1713 #endif
1714 f = ecl_listen_stream(strm);
1715 if (f == ECL_LISTEN_AVAILABLE) {
1716 int c = ecl_read_char(strm);
1717 if (c != EOF) {
1718 @(return ECL_CODE_CHAR(c));
1719 }
1720 } else if (f == ECL_LISTEN_NO_CHAR) {
1721 @(return @'nil');
1722 }
1723 /* We reach here if there was an EOF */
1724 END_OF_FILE:
1725 if (Null(eof_errorp)) {
1726 @(return eof_value);
1727 }
1728 else {
1729 FEend_of_file(strm);
1730 }
1731 @)
1732
1733 @(defun clear_input (&optional (strm ECL_NIL))
1734 @
1735 strm = stream_or_default_input(strm);
1736 ecl_clear_input(strm);
1737 @(return ECL_NIL);
1738 @)
1739
1740 @(defun read_byte (binary_input_stream &optional (eof_errorp ECL_T) eof_value)
1741 cl_object c;
1742 @
1743 c = ecl_read_byte(binary_input_stream);
1744 if (c == OBJNULL) {
1745 if (Null(eof_errorp)) {
1746 @(return eof_value);
1747 }
1748 else
1749 FEend_of_file(binary_input_stream);
1750 }
1751 @(return c);
1752 @)
1753
1754 @(defun read_sequence (sequence stream &key (start ecl_make_fixnum(0)) end)
1755 @
1756 #ifdef ECL_CLOS_STREAMS
1757 if (!ECL_ANSI_STREAM_P(stream))
1758 return funcall(5, @'gray::stream-read-sequence', stream, sequence, start, end);
1759 else
1760 #endif
1761 @(return si_do_read_sequence(sequence, stream, start, end));
1762 @)
1763
1764
1765 @(defun copy_readtable (&o (from ecl_current_readtable()) to)
1766 @
1767 if (Null(from)) {
1768 to = ecl_copy_readtable(cl_core.standard_readtable, to);
1769 } else {
1770 to = ecl_copy_readtable(from, to);
1771 }
1772 @(return to);
1773 @)
1774
1775 cl_object
1776 cl_readtable_case(cl_object r)
1777 {
1778 assert_type_readtable(@[readtable-case], 1, r);
1779 switch (r->readtable.read_case) {
1780 case ecl_case_upcase: r = @':upcase'; break;
1781 case ecl_case_downcase: r = @':downcase'; break;
1782 case ecl_case_invert: r = @':invert'; break;
1783 case ecl_case_preserve: r = @':preserve';
1784 }
1785 @(return r);
1786 }
1787
1788 static void
1789 error_locked_readtable(cl_object r)
1790 {
1791 cl_error(2, @"Cannot modify locked readtable ~A.", r);
1792 }
1793
1794 cl_object
1795 si_readtable_case_set(cl_object r, cl_object mode)
1796 {
1797 assert_type_readtable(@[readtable-case], 1, r);
1798 if (r->readtable.locked) {
1799 error_locked_readtable(r);
1800 }
1801 if (mode == @':upcase') {
1802 r->readtable.read_case = ecl_case_upcase;
1803 } else if (mode == @':downcase') {
1804 r->readtable.read_case = ecl_case_downcase;
1805 } else if (mode == @':preserve') {
1806 r->readtable.read_case = ecl_case_preserve;
1807 } else if (mode == @':invert') {
1808 r->readtable.read_case = ecl_case_invert;
1809 } else {
1810 const char *type = "(member :upcase :downcase :preserve :invert)";
1811 FEwrong_type_nth_arg(@[si::readtable-case-set], 2,
1812 mode, ecl_read_from_cstring(type));
1813 }
1814 @(return mode);
1815 }
1816
1817 cl_object
1818 cl_readtablep(cl_object readtable)
1819 {
1820 @(return (ECL_READTABLEP(readtable) ? ECL_T : ECL_NIL));
1821 }
1822
1823 int
1824 ecl_readtable_get(cl_object readtable, int c, cl_object *macro_or_table)
1825 {
1826 cl_object m;
1827 enum ecl_chattrib cat;
1828 #ifdef ECL_UNICODE
1829 if (c >= RTABSIZE) {
1830 cl_object hash = readtable->readtable.hash;
1831 cat = cat_constituent;
1832 m = ECL_NIL;
1833 if (!Null(hash)) {
1834 cl_object pair = ecl_gethash_safe(ECL_CODE_CHAR(c), hash, ECL_NIL);
1835 if (!Null(pair)) {
1836 cat = ecl_fixnum(ECL_CONS_CAR(pair));
1837 m = ECL_CONS_CDR(pair);
1838 }
1839 }
1840 } else
1841 #endif
1842 {
1843 m = readtable->readtable.table[c].dispatch;
1844 cat = readtable->readtable.table[c].syntax_type;
1845 }
1846 if (macro_or_table) *macro_or_table = m;
1847 return cat;
1848 }
1849
1850 void
1851 ecl_readtable_set(cl_object readtable, int c, enum ecl_chattrib cat,
1852 cl_object macro_or_table)
1853 {
1854 if (readtable->readtable.locked) {
1855 error_locked_readtable(readtable);
1856 }
1857 #ifdef ECL_UNICODE
1858 if (c >= RTABSIZE) {
1859 cl_object hash = readtable->readtable.hash;
1860 if (Null(hash)) {
1861 hash = cl__make_hash_table(@'eql', ecl_make_fixnum(128),
1862 cl_core.rehash_size,
1863 cl_core.rehash_threshold);
1864 readtable->readtable.hash = hash;
1865 }
1866 _ecl_sethash(ECL_CODE_CHAR(c), hash,
1867 CONS(ecl_make_fixnum(cat), macro_or_table));
1868 } else
1869 #endif
1870 {
1871 readtable->readtable.table[c].dispatch = macro_or_table;
1872 readtable->readtable.table[c].syntax_type = cat;
1873 }
1874 }
1875
1876 bool
1877 ecl_invalid_character_p(int c)
1878 {
1879 return (c <= 32) || (c == 127);
1880 }
1881
1882 @(defun set_syntax_from_char (tochr fromchr
1883 &o (tordtbl ecl_current_readtable())
1884 fromrdtbl)
1885 enum ecl_chattrib cat;
1886 cl_object dispatch;
1887 cl_fixnum fc, tc;
1888 @
1889 if (tordtbl->readtable.locked) {
1890 error_locked_readtable(tordtbl);
1891 }
1892 if (Null(fromrdtbl))
1893 fromrdtbl = cl_core.standard_readtable;
1894 assert_type_readtable(@[readtable-case], 1, tordtbl);
1895 assert_type_readtable(@[readtable-case], 2, fromrdtbl);
1896 fc = ecl_char_code(fromchr);
1897 tc = ecl_char_code(tochr);
1898
1899 cat = ecl_readtable_get(fromrdtbl, fc, &dispatch);
1900 if (ECL_READTABLEP(dispatch)) {
1901 dispatch = si_copy_hash_table(dispatch);
1902 }
1903 ecl_readtable_set(tordtbl, tc, cat, dispatch);
1904 @(return ECL_T);
1905 @)
1906
1907 @(defun set_macro_character (c function &optional non_terminating_p
1908 (readtable ecl_current_readtable()))
1909 @
1910 ecl_readtable_set(readtable, ecl_char_code(c),
1911 Null(non_terminating_p)?
1912 cat_terminating :
1913 cat_non_terminating,
1914 function);
1915 @(return ECL_T);
1916 @)
1917
1918 @(defun get_macro_character (c &optional (readtable ecl_current_readtable()))
1919 enum ecl_chattrib cat;
1920 cl_object dispatch;
1921 @
1922 if (Null(readtable))
1923 readtable = cl_core.standard_readtable;
1924 cat = ecl_readtable_get(readtable, ecl_char_code(c), &dispatch);
1925 if (ECL_HASH_TABLE_P(dispatch))
1926 dispatch = cl_core.dispatch_reader;
1927 @(return dispatch ((cat == cat_non_terminating)? ECL_T : ECL_NIL));
1928 @)
1929
1930 @(defun make_dispatch_macro_character (chr
1931 &optional non_terminating_p (readtable ecl_current_readtable()))
1932 enum ecl_chattrib cat;
1933 cl_object table;
1934 int c;
1935 @
1936 assert_type_readtable(@[make-dispatch-macro-character], 3, readtable);
1937 c = ecl_char_code(chr);
1938 cat = Null(non_terminating_p)? cat_terminating : cat_non_terminating;
1939 table = cl__make_hash_table(@'eql', ecl_make_fixnum(128),
1940 cl_core.rehash_size,
1941 cl_core.rehash_threshold);
1942 ecl_readtable_set(readtable, c, cat, table);
1943 @(return ECL_T);
1944 @)
1945
1946 @(defun set_dispatch_macro_character (dspchr subchr fnc
1947 &optional (readtable ecl_current_readtable()))
1948 cl_object table;
1949 cl_fixnum subcode;
1950 @
1951 assert_type_readtable(@[set-dispatch-macro-character], 4, readtable);
1952 ecl_readtable_get(readtable, ecl_char_code(dspchr), &table);
1953 unlikely_if (readtable->readtable.locked) {
1954 error_locked_readtable(readtable);
1955 }
1956 unlikely_if (!ECL_HASH_TABLE_P(table)) {
1957 FEerror("~S is not a dispatch character.", 1, dspchr);
1958 }
1959 subcode = ecl_char_code(subchr);
1960 if (Null(fnc)) {
1961 ecl_remhash(ECL_CODE_CHAR(subcode), table);
1962 } else {
1963 _ecl_sethash(ECL_CODE_CHAR(subcode), table, fnc);
1964 }
1965 if (ecl_lower_case_p(subcode)) {
1966 subcode = ecl_char_upcase(subcode);
1967 } else if (ecl_upper_case_p(subcode)) {
1968 subcode = ecl_char_downcase(subcode);
1969 }
1970 if (Null(fnc)) {
1971 ecl_remhash(ECL_CODE_CHAR(subcode), table);
1972 } else {
1973 _ecl_sethash(ECL_CODE_CHAR(subcode), table, fnc);
1974 }
1975 @(return ECL_T);
1976 @)
1977
1978 @(defun get_dispatch_macro_character (dspchr subchr
1979 &optional (readtable ecl_current_readtable()))
1980 cl_object table;
1981 cl_fixnum c;
1982 @
1983 if (Null(readtable)) {
1984 readtable = cl_core.standard_readtable;
1985 }
1986 assert_type_readtable(@[get-dispatch-macro-character], 3, readtable);
1987 c = ecl_char_code(dspchr);
1988 ecl_readtable_get(readtable, c, &table);
1989 unlikely_if (!ECL_HASH_TABLE_P(table)) {
1990 FEerror("~S is not a dispatch character.", 1, dspchr);
1991 }
1992 c = ecl_char_code(subchr);
1993
1994 /* Since macro characters may take a number as argument, it is
1995 not allowed to turn digits into dispatch macro characters */
1996 if (ecl_digitp(c, 10) >= 0)
1997 @(return ECL_NIL);
1998 @(return ecl_gethash_safe(subchr, table, ECL_NIL));
1999 @)
2000
2001 cl_object
2002 si_standard_readtable()
2003 {
2004 @(return cl_core.standard_readtable);
2005 }
2006
2007 @(defun ext::readtable-lock (r &optional yesno)
2008 cl_object output;
2009 @
2010 assert_type_readtable(@[ext::readtable-lock], 1, r);
2011 output = (r->readtable.locked)? ECL_T : ECL_NIL;
2012 if (narg > 1) {
2013 r->readtable.locked = !Null(yesno);
2014 }
2015 @(return output);
2016 @)
2017
2018 static void
2019 extra_argument(int c, cl_object stream, cl_object d)
2020 {
2021 FEreader_error("~S is an extra argument for the #~C readmacro.",
2022 stream, 2, d, ECL_CODE_CHAR(c));
2023 }
2024
2025
2026 #define make_cf2(f) ecl_make_cfun((cl_objectfn_fixed)(f), ECL_NIL, NULL, 2)
2027 #define make_cf3(f) ecl_make_cfun((cl_objectfn_fixed)(f), ECL_NIL, NULL, 3)
2028
2029 void
2030 init_read(void)
2031 {
2032 struct ecl_readtable_entry *rtab;
2033 cl_object r;
2034 int i;
2035
2036 cl_core.standard_readtable = r = ecl_alloc_object(t_readtable);
2037 r->readtable.locked = 0;
2038 r->readtable.read_case = ecl_case_upcase;
2039 r->readtable.table = rtab
2040 = (struct ecl_readtable_entry *)
2041 ecl_alloc(RTABSIZE * sizeof(struct ecl_readtable_entry));
2042 for (i = 0; i < RTABSIZE; i++) {
2043 rtab[i].syntax_type = cat_constituent;
2044 rtab[i].dispatch = ECL_NIL;
2045 }
2046 #ifdef ECL_UNICODE
2047 r->readtable.hash = ECL_NIL;
2048 #endif
2049
2050 cl_core.dispatch_reader = make_cf2(dispatch_reader_fun);
2051
2052 ecl_readtable_set(r, '\t', cat_whitespace, ECL_NIL);
2053 ecl_readtable_set(r, '\n', cat_whitespace, ECL_NIL);
2054 ecl_readtable_set(r, '\f', cat_whitespace, ECL_NIL);
2055 ecl_readtable_set(r, '\r', cat_whitespace, ECL_NIL);
2056 ecl_readtable_set(r, ' ', cat_whitespace, ECL_NIL);
2057
2058 ecl_readtable_set(r, '"', cat_terminating,
2059 make_cf2(double_quote_reader));
2060
2061 ecl_readtable_set(r, '\'', cat_terminating,
2062 make_cf2(single_quote_reader));
2063 ecl_readtable_set(r, '(', cat_terminating,
2064 make_cf2(left_parenthesis_reader));
2065 ecl_readtable_set(r, ')', cat_terminating,
2066 make_cf2(right_parenthesis_reader));
2067 ecl_readtable_set(r, ',', cat_terminating,
2068 make_cf2(comma_reader));
2069 ecl_readtable_set(r, ';', cat_terminating,
2070 make_cf2(semicolon_reader));
2071 ecl_readtable_set(r, '\\', cat_single_escape, ECL_NIL);
2072 ecl_readtable_set(r, '`', cat_terminating,
2073 make_cf2(backquote_reader));
2074 ecl_readtable_set(r, '|', cat_multiple_escape, ECL_NIL);
2075
2076 cl_make_dispatch_macro_character(3, ECL_CODE_CHAR('#'),
2077 ECL_T /* non terminating */, r);
2078
2079 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('C'),
2080 make_cf3(sharp_C_reader), r);
2081 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('\\'),
2082 make_cf3(sharp_backslash_reader), r);
2083 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('\''),
2084 make_cf3(sharp_single_quote_reader), r);
2085 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('('),
2086 make_cf3(sharp_left_parenthesis_reader), r);
2087 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('*'),
2088 make_cf3(sharp_asterisk_reader), r);
2089 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(':'),
2090 make_cf3(sharp_colon_reader), r);
2091 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('.'),
2092 make_cf3(sharp_dot_reader), r);
2093 /* Used for fasload only. */
2094 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('B'),
2095 make_cf3(sharp_B_reader), r);
2096 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('O'),
2097 make_cf3(sharp_O_reader), r);
2098 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('X'),
2099 make_cf3(sharp_X_reader), r);
2100 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('R'),
2101 make_cf3(sharp_R_reader), r);
2102 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('A'),
2103 @'si::sharp-a-reader', r);
2104 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('S'),
2105 @'si::sharp-s-reader', r);
2106 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('P'),
2107 make_cf3(sharp_P_reader), r);
2108
2109 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('='),
2110 make_cf3(sharp_eq_reader), r);
2111 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('#'),
2112 make_cf3(sharp_sharp_reader), r);
2113 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('+'),
2114 make_cf3(sharp_plus_reader), r);
2115 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('-'),
2116 make_cf3(sharp_minus_reader), r);
2117 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('|'),
2118 make_cf3(sharp_vertical_bar_reader), r);
2119 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('\b'),
2120 make_cf3(sharp_generic_error), r);
2121 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('\t'),
2122 make_cf3(sharp_generic_error), r);
2123 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(ECL_CHAR_CODE_NEWLINE),
2124 make_cf3(sharp_generic_error), r);
2125 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(ECL_CHAR_CODE_LINEFEED),
2126 make_cf3(sharp_generic_error), r);
2127 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('\f'),
2128 make_cf3(sharp_generic_error), r);
2129 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(ECL_CHAR_CODE_RETURN),
2130 make_cf3(sharp_generic_error), r);
2131 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(' '),
2132 make_cf3(sharp_generic_error), r);
2133 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR(')'),
2134 make_cf3(sharp_generic_error), r);
2135 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('<'),
2136 make_cf3(sharp_generic_error), r);
2137 /* This is specific to this implementation */
2138 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('$'),
2139 make_cf3(sharp_dollar_reader), r);
2140 /* This is specific to this implementation */
2141 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('Y'),
2142 make_cf3(sharp_Y_reader), r);
2143 /* This is specific to this implementation: ignore BOM */
2144 #ifdef ECL_UNICODE
2145 ecl_readtable_set(r, 0xfeff, cat_whitespace, ECL_NIL);
2146 #endif
2147
2148 /* Lock the standard read table so that we do not have to make copies
2149 * to keep it unchanged */
2150 r->readtable.locked = 1;
2151
2152 init_backq();
2153
2154 ECL_SET(@'*readtable*',
2155 r=ecl_copy_readtable(cl_core.standard_readtable, ECL_NIL));
2156 cl_set_dispatch_macro_character(4, ECL_CODE_CHAR('#'), ECL_CODE_CHAR('!'),
2157 ECL_NIL, r);
2158 ECL_SET(@'*read-default-float-format*', @'single-float');
2159
2160 {
2161 cl_object var, val;
2162 var = cl_list(25,
2163 @'*print-pprint-dispatch*', /* See end of pprint.lsp */
2164 @'*print-array*',
2165 @'*print-base*',
2166 @'*print-case*',
2167 @'*print-circle*',
2168 @'*print-escape*',
2169 @'*print-gensym*',
2170 @'*print-length*',
2171 @'*print-level*',
2172 @'*print-lines*',
2173 @'*print-miser-width*',
2174 @'*print-pretty*',
2175 @'*print-radix*',
2176 @'*print-readably*',
2177 @'*print-right-margin*',
2178 @'*read-base*',
2179 @'*read-default-float-format*',
2180 @'*read-eval*',
2181 @'*read-suppress*',
2182 @'*readtable*',
2183 @'*package*',
2184 @'si::*print-package*',
2185 @'si::*print-structure*',
2186 @'si::*sharp-eq-context*',
2187 @'si::*circle-counter*');
2188 val = cl_list(25,
2189 /**pprint-dispatch-table**/ ECL_NIL,
2190 /**print-array**/ ECL_T,
2191 /**print-base**/ ecl_make_fixnum(10),
2192 /**print-case**/ @':downcase',
2193 /**print-circle**/ ECL_T,
2194 /**print-escape**/ ECL_T,
2195 /**print-gensym**/ ECL_T,
2196 /**print-length**/ ECL_NIL,
2197 /**print-level**/ ECL_NIL,
2198 /**print-lines**/ ECL_NIL,
2199 /**print-miser-width**/ ECL_NIL,
2200 /**print-pretty**/ ECL_NIL,
2201 /**print-radix**/ ECL_NIL,
2202 /**print-readably**/ ECL_T,
2203 /**print-right-margin**/ ECL_NIL,
2204 /**read-base**/ ecl_make_fixnum(10),
2205 /**read-default-float-format**/ @'single-float',
2206 /**read-eval**/ ECL_T,
2207 /**read-suppress**/ ECL_NIL,
2208 /**readtable**/ cl_core.standard_readtable,
2209 /**package**/ cl_core.lisp_package,
2210 /*si::*print-package**/ cl_core.lisp_package,
2211 /*si::*print-structure**/ ECL_T,
2212 /*si::*sharp-eq-context**/ ECL_NIL,
2213 /*si::*cicle-counter**/ ECL_NIL);
2214 ECL_SET(@'si::+ecl-syntax-progv-list+', CONS(var,val));
2215 var = cl_list(23,
2216 @'*print-pprint-dispatch*', /* See end of pprint.lsp */
2217 @'*print-array*',
2218 @'*print-base*',
2219 @'*print-case*',
2220 @'*print-circle*',
2221 @'*print-escape*',
2222 @'*print-gensym*',
2223 @'*print-length*',
2224 @'*print-level*',
2225 @'*print-lines*',
2226 @'*print-miser-width*',
2227 @'*print-pretty*',
2228 @'*print-radix*',
2229 @'*print-readably*',
2230 @'*print-right-margin*',
2231 @'*read-base*',
2232 @'*read-default-float-format*',
2233 @'*read-eval*',
2234 @'*read-suppress*',
2235 @'*readtable*',
2236 @'*package*',
2237 @'si::*sharp-eq-context*',
2238 @'si::*circle-counter*');
2239 val = cl_list(23,
2240 /**pprint-dispatch-table**/ ECL_NIL,
2241 /**print-array**/ ECL_T,
2242 /**print-base**/ ecl_make_fixnum(10),
2243 /**print-case**/ @':upcase',
2244 /**print-circle**/ ECL_NIL,
2245 /**print-escape**/ ECL_T,
2246 /**print-gensym**/ ECL_T,
2247 /**print-length**/ ECL_NIL,
2248 /**print-level**/ ECL_NIL,
2249 /**print-lines**/ ECL_NIL,
2250 /**print-miser-width**/ ECL_NIL,
2251 /**print-pretty**/ ECL_NIL,
2252 /**print-radix**/ ECL_NIL,
2253 /**print-readably**/ ECL_T,
2254 /**print-right-margin**/ ECL_NIL,
2255 /**read-base**/ ecl_make_fixnum(10),
2256 /**read-default-float-format**/ @'single-float',
2257 /**read-eval**/ ECL_T,
2258 /**read-suppress**/ ECL_NIL,
2259 /**readtable**/ cl_core.standard_readtable,
2260 /**package**/ cl_core.user_package,
2261 /*si::*sharp-eq-context**/ ECL_NIL,
2262 /*si::*cicle-counter**/ ECL_NIL);
2263 ECL_SET(@'si::+io-syntax-progv-list+', CONS(var,val));
2264 }
2265 }
2266
2267 /*
2268 *----------------------------------------------------------------------
2269 *
2270 * ecl_init_module --
2271 * reads the data vector from stream into vector VV
2272 *
2273 * Results:
2274 * a vector.
2275 *
2276 *----------------------------------------------------------------------
2277 */
2278 static cl_object
2279 make_one_data_stream(const cl_object string)
2280 {
2281 #ifdef ECL_UNICODE
2282 return si_make_sequence_input_stream(3, string, @':external-format',
2283 @':utf-8');
2284 #else
2285 return ecl_make_string_input_stream(string, 0, ecl_length(string));
2286 #endif
2287 }
2288
2289 static cl_object
2290 make_data_stream(const cl_object *data)
2291 {
2292 if (data == 0 || data[0] == NULL) {
2293 return cl_core.null_stream;
2294 }
2295 if (data[1] == NULL) {
2296 return make_one_data_stream(data[0]);
2297 } else {
2298 cl_object stream_list = ECL_NIL;
2299 cl_index i;
2300 for (i = 0; data[i]; i++) {
2301 cl_object s = make_one_data_stream(data[i]);
2302 stream_list = ecl_cons(s, stream_list);
2303 }
2304 return cl_apply(2, @'make-concatenated-stream',
2305 cl_nreverse(stream_list));
2306 }
2307 }
2308
2309 cl_object
2310 ecl_init_module(cl_object block, void (*entry_point)(cl_object))
2311 {
2312 const cl_env_ptr env = ecl_process_env();
2313 volatile cl_object old_eptbc = env->packages_to_be_created;
2314 volatile cl_object x;
2315 cl_index i, len, perm_len, temp_len;
2316 cl_object in;
2317 cl_object *VV = NULL, *VVtemp = NULL;
2318
2319 if (block == NULL)
2320 block = ecl_make_codeblock();
2321 block->cblock.entry = entry_point;
2322
2323 in = OBJNULL;
2324 ECL_UNWIND_PROTECT_BEGIN(env) {
2325 cl_index bds_ndx;
2326 cl_object progv_list;
2327
2328 ecl_bds_bind(env, @'*package*', ecl_cmp_symbol_value(env, @'*package*'));
2329 ecl_bds_bind(env, @'*readtable*', ecl_cmp_symbol_value(env, @'*readtable*'));
2330 ecl_bds_bind(env, @'si::*cblock*', block);
2331 env->packages_to_be_created_p = ECL_T;
2332
2333 /* Communicate the library which Cblock we are using, and get
2334 * back the amount of data to be processed.
2335 */
2336 (*entry_point)(block);
2337 perm_len = block->cblock.data_size;
2338 temp_len = block->cblock.temp_data_size;
2339 len = perm_len + temp_len;
2340
2341 if (block->cblock.data_text == 0) {
2342 if (len) {
2343 /* Code from COMPILE uses data in *compiler-constants* */
2344 cl_object v = ECL_SYM_VAL(env,@'si::*compiler-constants*');
2345 unlikely_if (ecl_t_of(v) != t_vector ||
2346 v->vector.dim != len ||
2347 v->vector.elttype != ecl_aet_object)
2348 FEerror("Internal error: corrupted data in "
2349 "si::*compiler-constants*", 0);
2350 VV = block->cblock.data = v->vector.self.t;
2351 VVtemp = block->cblock.temp_data = NULL;
2352 }
2353 goto NO_DATA_LABEL;
2354 }
2355 if (len == 0) {
2356 VV = VVtemp = NULL;
2357 goto NO_DATA_LABEL;
2358 }
2359 #ifdef ECL_DYNAMIC_VV
2360 VV = block->cblock.data = perm_len? (cl_object *)ecl_alloc(perm_len * sizeof(cl_object)) : NULL;
2361 #else
2362 VV = block->cblock.data;
2363 #endif
2364 memset(VV, 0, perm_len * sizeof(*VV));
2365
2366 VVtemp = block->cblock.temp_data = temp_len? (cl_object *)ecl_alloc(temp_len * sizeof(cl_object)) : NULL;
2367 memset(VVtemp, 0, temp_len * sizeof(*VVtemp));
2368
2369 /* Read all data for the library */
2370 #ifdef ECL_EXTERNALIZABLE
2371 {
2372 unlikely_if (block->cblock.data_text == NULL) {
2373 unlikely_if (len > 0)
2374 FEreader_error("Not enough data while loading binary file", in, 0);
2375 } else {
2376 cl_object v = si_deserialize(*(block->cblock.data_text));
2377 unlikely_if (v->vector.dim < len)
2378 FEreader_error("Not enough data while loading binary file", in, 0);
2379 memcpy(VV, v->vector.self.t, perm_len * sizeof(cl_object));
2380 memcpy(VVtemp, v->vector.self.t + perm_len, temp_len * sizeof(cl_object));
2381 }
2382 }
2383 #else
2384 in = make_data_stream(block->cblock.data_text);
2385 progv_list = ECL_SYM_VAL(env, @'si::+ecl-syntax-progv-list+');
2386 bds_ndx = ecl_progv(env, ECL_CONS_CAR(progv_list),
2387 ECL_CONS_CDR(progv_list));
2388 for (i = 0 ; i < len; i++) {
2389 x = ecl_read_object(in);
2390 if (x == OBJNULL)
2391 break;
2392 if (i < perm_len)
2393 VV[i] = x;
2394 else
2395 VVtemp[i-perm_len] = x;
2396 }
2397 if (!Null(ECL_SYM_VAL(env, @'si::*sharp-eq-context*'))) {
2398 while (i--) {
2399 if (i < perm_len) {
2400 VV[i] = patch_sharp(env, VV[i]);
2401 } else {
2402 VVtemp[i-perm_len] = patch_sharp(env, VVtemp[i-perm_len]);
2403 }
2404 }
2405 }
2406 ecl_bds_unwind(env, bds_ndx);
2407 unlikely_if (i < len)
2408 FEreader_error("Not enough data while loading binary file", in, 0);
2409 cl_close(1,in);
2410 in = OBJNULL;
2411 #endif
2412 NO_DATA_LABEL:
2413 env->packages_to_be_created_p = ECL_NIL;
2414
2415 assert(block->cblock.cfuns_size == 0 || VV != NULL);
2416 for (i = 0; i < block->cblock.cfuns_size; i++) {
2417 const struct ecl_cfunfixed *prototype = block->cblock.cfuns+i;
2418 cl_index fname_location = ecl_fixnum(prototype->block);
2419 cl_object fname = VV[fname_location];
2420 cl_index location = ecl_fixnum(prototype->name);
2421 cl_object position = prototype->file_position;
2422 int narg = prototype->narg;
2423 if (prototype->t == t_cfunfixed) {
2424 VV[location] = ecl_make_cfun((cl_objectfn_fixed)prototype->entry,
2425 fname, block, narg);
2426 } else {
2427 VV[location] = ecl_make_cfun_va((cl_objectfn)prototype->entry,
2428 fname, block, narg);
2429 }
2430 /* Add source file info */
2431 if (position != ecl_make_fixnum(-1)) {
2432 ecl_set_function_source_file_info(VV[location],
2433 block->cblock.source,
2434 position);
2435 }
2436 }
2437 /* Execute top-level code */
2438 (*entry_point)(OBJNULL);
2439 x = cl_set_difference(2, env->packages_to_be_created, old_eptbc);
2440 old_eptbc = env->packages_to_be_created;
2441 unlikely_if (!Null(x)) {
2442 CEerror(ECL_T,
2443 Null(ECL_CONS_CDR(x))?
2444 "Package ~A referenced in "
2445 "compiled file~& ~A~&but has not been created":
2446 "The packages~& ~A~&were referenced in "
2447 "compiled file~& ~A~&but have not been created",
2448 2, x, block->cblock.name);
2449 }
2450 if (VVtemp) {
2451 block->cblock.temp_data = NULL;
2452 block->cblock.temp_data_size = 0;
2453 ecl_dealloc(VVtemp);
2454 }
2455 ecl_bds_unwind_n(env, 3);
2456 } ECL_UNWIND_PROTECT_THREAD_SAFE_EXIT {
2457 if (in != OBJNULL)
2458 cl_close(1,in);
2459 env->packages_to_be_created = old_eptbc;
2460 env->packages_to_be_created_p = ECL_NIL;
2461 } ECL_UNWIND_PROTECT_THREAD_SAFE_END;
2462
2463 return block;
2464 }