Mercurial > hg > xemacs-beta
annotate src/data.c @ 5117:3742ea8250b5 ben-lisp-object ben-lisp-object-final-ws-year-2005
Checking in final CVS version of workspace 'ben-lisp-object'
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 00:20:27 -0600 |
parents | 1e7cc382eb16 |
children | e0db3c197671 |
rev | line source |
---|---|
428 | 1 /* Primitive operations on Lisp data types for XEmacs Lisp interpreter. |
2 Copyright (C) 1985, 1986, 1988, 1992, 1993, 1994, 1995 | |
3 Free Software Foundation, Inc. | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
4 Copyright (C) 2000, 2001, 2002, 2003, 2005 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: Mule 2.0, FSF 19.30. Some of FSF's data.c is in | |
24 XEmacs' symbols.c. */ | |
25 | |
26 /* This file has been Mule-ized. */ | |
27 | |
28 #include <config.h> | |
29 #include "lisp.h" | |
30 | |
31 #include "buffer.h" | |
32 #include "bytecode.h" | |
33 #include "syssignal.h" | |
771 | 34 #include "sysfloat.h" |
428 | 35 |
36 Lisp_Object Qnil, Qt, Qquote, Qlambda, Qunbound; | |
37 Lisp_Object Qerror_conditions, Qerror_message; | |
442 | 38 Lisp_Object Qerror, Qquit, Qsyntax_error, Qinvalid_read_syntax; |
563 | 39 Lisp_Object Qlist_formation_error, Qstructure_formation_error; |
442 | 40 Lisp_Object Qmalformed_list, Qmalformed_property_list; |
41 Lisp_Object Qcircular_list, Qcircular_property_list; | |
563 | 42 Lisp_Object Qinvalid_argument, Qinvalid_constant, Qwrong_type_argument; |
43 Lisp_Object Qargs_out_of_range; | |
442 | 44 Lisp_Object Qwrong_number_of_arguments, Qinvalid_function, Qno_catch; |
563 | 45 Lisp_Object Qinternal_error, Qinvalid_state, Qstack_overflow, Qout_of_memory; |
428 | 46 Lisp_Object Qvoid_variable, Qcyclic_variable_indirection; |
47 Lisp_Object Qvoid_function, Qcyclic_function_indirection; | |
563 | 48 Lisp_Object Qinvalid_operation, Qinvalid_change, Qprinting_unreadable_object; |
442 | 49 Lisp_Object Qsetting_constant; |
50 Lisp_Object Qediting_error; | |
51 Lisp_Object Qbeginning_of_buffer, Qend_of_buffer, Qbuffer_read_only; | |
563 | 52 Lisp_Object Qio_error, Qfile_error, Qconversion_error, Qend_of_file; |
580 | 53 Lisp_Object Qtext_conversion_error; |
428 | 54 Lisp_Object Qarith_error, Qrange_error, Qdomain_error; |
55 Lisp_Object Qsingularity_error, Qoverflow_error, Qunderflow_error; | |
1983 | 56 Lisp_Object Qintegerp, Qnatnump, Qnonnegativep, Qsymbolp; |
428 | 57 Lisp_Object Qlistp, Qtrue_list_p, Qweak_listp; |
58 Lisp_Object Qconsp, Qsubrp; | |
59 Lisp_Object Qcharacterp, Qstringp, Qarrayp, Qsequencep, Qvectorp; | |
60 Lisp_Object Qchar_or_string_p, Qmarkerp, Qinteger_or_marker_p, Qbufferp; | |
61 Lisp_Object Qinteger_or_char_p, Qinteger_char_or_marker_p; | |
62 Lisp_Object Qnumberp, Qnumber_char_or_marker_p; | |
63 Lisp_Object Qbit_vectorp, Qbitp, Qcdr; | |
64 | |
563 | 65 Lisp_Object Qerror_lacks_explanatory_string; |
428 | 66 Lisp_Object Qfloatp; |
67 | |
68 #ifdef DEBUG_XEMACS | |
69 | |
70 int debug_issue_ebola_notices; | |
71 | |
458 | 72 Fixnum debug_ebola_backtrace_length; |
428 | 73 |
74 int | |
75 eq_with_ebola_notice (Lisp_Object obj1, Lisp_Object obj2) | |
76 { | |
77 if (debug_issue_ebola_notices | |
78 && ((CHARP (obj1) && INTP (obj2)) || (CHARP (obj2) && INTP (obj1)))) | |
79 { | |
80 /* #### It would be really nice if this were a proper warning | |
1551 | 81 instead of brain-dead print to Qexternal_debugging_output. */ |
826 | 82 write_c_string |
83 (Qexternal_debugging_output, | |
84 "Comparison between integer and character is constant nil ("); | |
428 | 85 Fprinc (obj1, Qexternal_debugging_output); |
826 | 86 write_c_string (Qexternal_debugging_output, " and "); |
428 | 87 Fprinc (obj2, Qexternal_debugging_output); |
826 | 88 write_c_string (Qexternal_debugging_output, ")\n"); |
428 | 89 debug_short_backtrace (debug_ebola_backtrace_length); |
90 } | |
91 return EQ (obj1, obj2); | |
92 } | |
93 | |
94 #endif /* DEBUG_XEMACS */ | |
95 | |
96 | |
97 | |
98 Lisp_Object | |
99 wrong_type_argument (Lisp_Object predicate, Lisp_Object value) | |
100 { | |
101 /* This function can GC */ | |
102 REGISTER Lisp_Object tem; | |
103 do | |
104 { | |
105 value = Fsignal (Qwrong_type_argument, list2 (predicate, value)); | |
106 tem = call1 (predicate, value); | |
107 } | |
108 while (NILP (tem)); | |
109 return value; | |
110 } | |
111 | |
112 DOESNT_RETURN | |
113 dead_wrong_type_argument (Lisp_Object predicate, Lisp_Object value) | |
114 { | |
563 | 115 signal_error_1 (Qwrong_type_argument, list2 (predicate, value)); |
428 | 116 } |
117 | |
118 DEFUN ("wrong-type-argument", Fwrong_type_argument, 2, 2, 0, /* | |
119 Signal an error until the correct type value is given by the user. | |
120 This function loops, signalling a continuable `wrong-type-argument' error | |
121 with PREDICATE and VALUE as the data associated with the error and then | |
122 calling PREDICATE on the returned value, until the value gotten satisfies | |
123 PREDICATE. At that point, the gotten value is returned. | |
124 */ | |
125 (predicate, value)) | |
126 { | |
127 return wrong_type_argument (predicate, value); | |
128 } | |
129 | |
130 DOESNT_RETURN | |
131 c_write_error (Lisp_Object obj) | |
132 { | |
563 | 133 signal_error (Qsetting_constant, |
134 "Attempt to modify read-only object (c)", obj); | |
428 | 135 } |
136 | |
137 DOESNT_RETURN | |
138 lisp_write_error (Lisp_Object obj) | |
139 { | |
563 | 140 signal_error (Qsetting_constant, |
141 "Attempt to modify read-only object (lisp)", obj); | |
428 | 142 } |
143 | |
144 DOESNT_RETURN | |
145 args_out_of_range (Lisp_Object a1, Lisp_Object a2) | |
146 { | |
563 | 147 signal_error_1 (Qargs_out_of_range, list2 (a1, a2)); |
428 | 148 } |
149 | |
150 DOESNT_RETURN | |
151 args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3) | |
152 { | |
563 | 153 signal_error_1 (Qargs_out_of_range, list3 (a1, a2, a3)); |
428 | 154 } |
155 | |
156 void | |
157 check_int_range (EMACS_INT val, EMACS_INT min, EMACS_INT max) | |
158 { | |
159 if (val < min || val > max) | |
160 args_out_of_range_3 (make_int (val), make_int (min), make_int (max)); | |
161 } | |
162 | |
163 /* On some machines, XINT needs a temporary location. | |
164 Here it is, in case it is needed. */ | |
165 | |
166 EMACS_INT sign_extend_temp; | |
167 | |
168 /* On a few machines, XINT can only be done by calling this. */ | |
169 /* XEmacs: only used by m/convex.h */ | |
170 EMACS_INT sign_extend_lisp_int (EMACS_INT num); | |
171 EMACS_INT | |
172 sign_extend_lisp_int (EMACS_INT num) | |
173 { | |
2039 | 174 if (num & (1L << (INT_VALBITS - 1))) |
175 return num | ((-1L) << INT_VALBITS); | |
428 | 176 else |
2039 | 177 return num & (EMACS_INT) ((1UL << INT_VALBITS) - 1); |
428 | 178 } |
179 | |
180 | |
181 /* Data type predicates */ | |
182 | |
183 DEFUN ("eq", Feq, 2, 2, 0, /* | |
184 Return t if the two args are the same Lisp object. | |
185 */ | |
444 | 186 (object1, object2)) |
428 | 187 { |
444 | 188 return EQ_WITH_EBOLA_NOTICE (object1, object2) ? Qt : Qnil; |
428 | 189 } |
190 | |
191 DEFUN ("old-eq", Fold_eq, 2, 2, 0, /* | |
192 Return t if the two args are (in most cases) the same Lisp object. | |
193 | |
194 Special kludge: A character is considered `old-eq' to its equivalent integer | |
195 even though they are not the same object and are in fact of different | |
196 types. This is ABSOLUTELY AND UTTERLY HORRENDOUS but is necessary to | |
197 preserve byte-code compatibility with v19. This kludge is known as the | |
198 \"char-int confoundance disease\" and appears in a number of other | |
199 functions with `old-foo' equivalents. | |
200 | |
201 Do not use this function! | |
202 */ | |
444 | 203 (object1, object2)) |
428 | 204 { |
205 /* #### blasphemy */ | |
444 | 206 return HACKEQ_UNSAFE (object1, object2) ? Qt : Qnil; |
428 | 207 } |
208 | |
209 DEFUN ("null", Fnull, 1, 1, 0, /* | |
210 Return t if OBJECT is nil. | |
211 */ | |
212 (object)) | |
213 { | |
214 return NILP (object) ? Qt : Qnil; | |
215 } | |
216 | |
217 DEFUN ("consp", Fconsp, 1, 1, 0, /* | |
218 Return t if OBJECT is a cons cell. `nil' is not a cons cell. | |
219 */ | |
220 (object)) | |
221 { | |
222 return CONSP (object) ? Qt : Qnil; | |
223 } | |
224 | |
225 DEFUN ("atom", Fatom, 1, 1, 0, /* | |
226 Return t if OBJECT is not a cons cell. `nil' is not a cons cell. | |
227 */ | |
228 (object)) | |
229 { | |
230 return CONSP (object) ? Qnil : Qt; | |
231 } | |
232 | |
233 DEFUN ("listp", Flistp, 1, 1, 0, /* | |
234 Return t if OBJECT is a list. `nil' is a list. | |
235 */ | |
236 (object)) | |
237 { | |
238 return LISTP (object) ? Qt : Qnil; | |
239 } | |
240 | |
241 DEFUN ("nlistp", Fnlistp, 1, 1, 0, /* | |
242 Return t if OBJECT is not a list. `nil' is a list. | |
243 */ | |
244 (object)) | |
245 { | |
246 return LISTP (object) ? Qnil : Qt; | |
247 } | |
248 | |
249 DEFUN ("true-list-p", Ftrue_list_p, 1, 1, 0, /* | |
1551 | 250 Return t if OBJECT is an acyclic, nil-terminated (ie, not dotted), list. |
428 | 251 */ |
252 (object)) | |
253 { | |
254 return TRUE_LIST_P (object) ? Qt : Qnil; | |
255 } | |
256 | |
257 DEFUN ("symbolp", Fsymbolp, 1, 1, 0, /* | |
258 Return t if OBJECT is a symbol. | |
259 */ | |
260 (object)) | |
261 { | |
262 return SYMBOLP (object) ? Qt : Qnil; | |
263 } | |
264 | |
265 DEFUN ("keywordp", Fkeywordp, 1, 1, 0, /* | |
266 Return t if OBJECT is a keyword. | |
267 */ | |
268 (object)) | |
269 { | |
270 return KEYWORDP (object) ? Qt : Qnil; | |
271 } | |
272 | |
273 DEFUN ("vectorp", Fvectorp, 1, 1, 0, /* | |
274 Return t if OBJECT is a vector. | |
275 */ | |
276 (object)) | |
277 { | |
278 return VECTORP (object) ? Qt : Qnil; | |
279 } | |
280 | |
281 DEFUN ("bit-vector-p", Fbit_vector_p, 1, 1, 0, /* | |
282 Return t if OBJECT is a bit vector. | |
283 */ | |
284 (object)) | |
285 { | |
286 return BIT_VECTORP (object) ? Qt : Qnil; | |
287 } | |
288 | |
289 DEFUN ("stringp", Fstringp, 1, 1, 0, /* | |
290 Return t if OBJECT is a string. | |
291 */ | |
292 (object)) | |
293 { | |
294 return STRINGP (object) ? Qt : Qnil; | |
295 } | |
296 | |
297 DEFUN ("arrayp", Farrayp, 1, 1, 0, /* | |
298 Return t if OBJECT is an array (string, vector, or bit vector). | |
299 */ | |
300 (object)) | |
301 { | |
302 return (VECTORP (object) || | |
303 STRINGP (object) || | |
304 BIT_VECTORP (object)) | |
305 ? Qt : Qnil; | |
306 } | |
307 | |
308 DEFUN ("sequencep", Fsequencep, 1, 1, 0, /* | |
309 Return t if OBJECT is a sequence (list or array). | |
310 */ | |
311 (object)) | |
312 { | |
313 return (LISTP (object) || | |
314 VECTORP (object) || | |
315 STRINGP (object) || | |
316 BIT_VECTORP (object)) | |
317 ? Qt : Qnil; | |
318 } | |
319 | |
320 DEFUN ("markerp", Fmarkerp, 1, 1, 0, /* | |
321 Return t if OBJECT is a marker (editor pointer). | |
322 */ | |
323 (object)) | |
324 { | |
325 return MARKERP (object) ? Qt : Qnil; | |
326 } | |
327 | |
328 DEFUN ("subrp", Fsubrp, 1, 1, 0, /* | |
329 Return t if OBJECT is a built-in function. | |
330 */ | |
331 (object)) | |
332 { | |
333 return SUBRP (object) ? Qt : Qnil; | |
334 } | |
335 | |
336 DEFUN ("subr-min-args", Fsubr_min_args, 1, 1, 0, /* | |
337 Return minimum number of args built-in function SUBR may be called with. | |
338 */ | |
339 (subr)) | |
340 { | |
341 CHECK_SUBR (subr); | |
342 return make_int (XSUBR (subr)->min_args); | |
343 } | |
344 | |
345 DEFUN ("subr-max-args", Fsubr_max_args, 1, 1, 0, /* | |
346 Return maximum number of args built-in function SUBR may be called with, | |
347 or nil if it takes an arbitrary number of arguments or is a special form. | |
348 */ | |
349 (subr)) | |
350 { | |
351 int nargs; | |
352 CHECK_SUBR (subr); | |
353 nargs = XSUBR (subr)->max_args; | |
354 if (nargs == MANY || nargs == UNEVALLED) | |
355 return Qnil; | |
356 else | |
357 return make_int (nargs); | |
358 } | |
359 | |
360 DEFUN ("subr-interactive", Fsubr_interactive, 1, 1, 0, /* | |
444 | 361 Return the interactive spec of the subr object SUBR, or nil. |
428 | 362 If non-nil, the return value will be a list whose first element is |
363 `interactive' and whose second element is the interactive spec. | |
364 */ | |
365 (subr)) | |
366 { | |
867 | 367 const CIbyte *prompt; |
428 | 368 CHECK_SUBR (subr); |
369 prompt = XSUBR (subr)->prompt; | |
771 | 370 return prompt ? list2 (Qinteractive, build_msg_string (prompt)) : Qnil; |
428 | 371 } |
372 | |
373 | |
374 DEFUN ("characterp", Fcharacterp, 1, 1, 0, /* | |
375 Return t if OBJECT is a character. | |
376 Unlike in XEmacs v19 and FSF Emacs, a character is its own primitive type. | |
377 Any character can be converted into an equivalent integer using | |
378 `char-int'. To convert the other way, use `int-char'; however, | |
379 only some integers can be converted into characters. Such an integer | |
380 is called a `char-int'; see `char-int-p'. | |
381 | |
382 Some functions that work on integers (e.g. the comparison functions | |
383 <, <=, =, /=, etc. and the arithmetic functions +, -, *, etc.) | |
384 accept characters and implicitly convert them into integers. In | |
385 general, functions that work on characters also accept char-ints and | |
386 implicitly convert them into characters. WARNING: Neither of these | |
387 behaviors is very desirable, and they are maintained for backward | |
388 compatibility with old E-Lisp programs that confounded characters and | |
389 integers willy-nilly. These behaviors may change in the future; therefore, | |
390 do not rely on them. Instead, use the character-specific functions such | |
391 as `char='. | |
392 */ | |
393 (object)) | |
394 { | |
395 return CHARP (object) ? Qt : Qnil; | |
396 } | |
397 | |
398 DEFUN ("char-to-int", Fchar_to_int, 1, 1, 0, /* | |
444 | 399 Convert CHARACTER into an equivalent integer. |
428 | 400 The resulting integer will always be non-negative. The integers in |
401 the range 0 - 255 map to characters as follows: | |
402 | |
403 0 - 31 Control set 0 | |
404 32 - 127 ASCII | |
405 128 - 159 Control set 1 | |
406 160 - 255 Right half of ISO-8859-1 | |
407 | |
408 If support for Mule does not exist, these are the only valid character | |
409 values. When Mule support exists, the values assigned to other characters | |
410 may vary depending on the particular version of XEmacs, the order in which | |
411 character sets were loaded, etc., and you should not depend on them. | |
412 */ | |
444 | 413 (character)) |
428 | 414 { |
444 | 415 CHECK_CHAR (character); |
416 return make_int (XCHAR (character)); | |
428 | 417 } |
418 | |
419 DEFUN ("int-to-char", Fint_to_char, 1, 1, 0, /* | |
444 | 420 Convert integer INTEGER into the equivalent character. |
428 | 421 Not all integers correspond to valid characters; use `char-int-p' to |
422 determine whether this is the case. If the integer cannot be converted, | |
423 nil is returned. | |
424 */ | |
425 (integer)) | |
426 { | |
427 CHECK_INT (integer); | |
428 if (CHAR_INTP (integer)) | |
429 return make_char (XINT (integer)); | |
430 else | |
431 return Qnil; | |
432 } | |
433 | |
434 DEFUN ("char-int-p", Fchar_int_p, 1, 1, 0, /* | |
435 Return t if OBJECT is an integer that can be converted into a character. | |
436 See `char-int'. | |
437 */ | |
438 (object)) | |
439 { | |
440 return CHAR_INTP (object) ? Qt : Qnil; | |
441 } | |
442 | |
443 DEFUN ("char-or-char-int-p", Fchar_or_char_int_p, 1, 1, 0, /* | |
444 Return t if OBJECT is a character or an integer that can be converted into one. | |
445 */ | |
446 (object)) | |
447 { | |
448 return CHAR_OR_CHAR_INTP (object) ? Qt : Qnil; | |
449 } | |
450 | |
451 DEFUN ("char-or-string-p", Fchar_or_string_p, 1, 1, 0, /* | |
452 Return t if OBJECT is a character (or a char-int) or a string. | |
453 It is semi-hateful that we allow a char-int here, as it goes against | |
454 the name of this function, but it makes the most sense considering the | |
455 other steps we take to maintain compatibility with the old character/integer | |
456 confoundedness in older versions of E-Lisp. | |
457 */ | |
458 (object)) | |
459 { | |
460 return CHAR_OR_CHAR_INTP (object) || STRINGP (object) ? Qt : Qnil; | |
461 } | |
462 | |
1983 | 463 #ifdef HAVE_BIGNUM |
464 /* In this case, integerp is defined in number.c. */ | |
465 DEFUN ("fixnump", Ffixnump, 1, 1, 0, /* | |
466 Return t if OBJECT is a fixnum. | |
467 */ | |
468 (object)) | |
469 { | |
470 return INTP (object) ? Qt : Qnil; | |
471 } | |
472 #else | |
428 | 473 DEFUN ("integerp", Fintegerp, 1, 1, 0, /* |
474 Return t if OBJECT is an integer. | |
475 */ | |
476 (object)) | |
477 { | |
478 return INTP (object) ? Qt : Qnil; | |
479 } | |
1983 | 480 #endif |
428 | 481 |
482 DEFUN ("integer-or-marker-p", Finteger_or_marker_p, 1, 1, 0, /* | |
483 Return t if OBJECT is an integer or a marker (editor pointer). | |
484 */ | |
485 (object)) | |
486 { | |
487 return INTP (object) || MARKERP (object) ? Qt : Qnil; | |
488 } | |
489 | |
490 DEFUN ("integer-or-char-p", Finteger_or_char_p, 1, 1, 0, /* | |
491 Return t if OBJECT is an integer or a character. | |
492 */ | |
493 (object)) | |
494 { | |
495 return INTP (object) || CHARP (object) ? Qt : Qnil; | |
496 } | |
497 | |
498 DEFUN ("integer-char-or-marker-p", Finteger_char_or_marker_p, 1, 1, 0, /* | |
499 Return t if OBJECT is an integer, character or a marker (editor pointer). | |
500 */ | |
501 (object)) | |
502 { | |
503 return INTP (object) || CHARP (object) || MARKERP (object) ? Qt : Qnil; | |
504 } | |
505 | |
506 DEFUN ("natnump", Fnatnump, 1, 1, 0, /* | |
507 Return t if OBJECT is a nonnegative integer. | |
508 */ | |
509 (object)) | |
510 { | |
1983 | 511 return NATNUMP (object) |
512 #ifdef HAVE_BIGNUM | |
513 || (BIGNUMP (object) && bignum_sign (XBIGNUM_DATA (object)) >= 0) | |
514 #endif | |
515 ? Qt : Qnil; | |
516 } | |
517 | |
518 DEFUN ("nonnegativep", Fnonnegativep, 1, 1, 0, /* | |
519 Return t if OBJECT is a nonnegative number. | |
520 */ | |
521 (object)) | |
522 { | |
523 return NATNUMP (object) | |
524 #ifdef HAVE_BIGNUM | |
525 || (BIGNUMP (object) && bignum_sign (XBIGNUM_DATA (object)) >= 0) | |
526 #endif | |
527 #ifdef HAVE_RATIO | |
528 || (RATIOP (object) && ratio_sign (XRATIO_DATA (object)) >= 0) | |
529 #endif | |
530 #ifdef HAVE_BIGFLOAT | |
531 || (BIGFLOATP (object) && bigfloat_sign (XBIGFLOAT_DATA (object)) >= 0) | |
532 #endif | |
533 ? Qt : Qnil; | |
428 | 534 } |
535 | |
536 DEFUN ("bitp", Fbitp, 1, 1, 0, /* | |
537 Return t if OBJECT is a bit (0 or 1). | |
538 */ | |
539 (object)) | |
540 { | |
541 return BITP (object) ? Qt : Qnil; | |
542 } | |
543 | |
544 DEFUN ("numberp", Fnumberp, 1, 1, 0, /* | |
545 Return t if OBJECT is a number (floating point or integer). | |
546 */ | |
547 (object)) | |
548 { | |
1983 | 549 #ifdef WITH_NUMBER_TYPES |
550 return NUMBERP (object) ? Qt : Qnil; | |
551 #else | |
428 | 552 return INT_OR_FLOATP (object) ? Qt : Qnil; |
1983 | 553 #endif |
428 | 554 } |
555 | |
556 DEFUN ("number-or-marker-p", Fnumber_or_marker_p, 1, 1, 0, /* | |
557 Return t if OBJECT is a number or a marker. | |
558 */ | |
559 (object)) | |
560 { | |
561 return INT_OR_FLOATP (object) || MARKERP (object) ? Qt : Qnil; | |
562 } | |
563 | |
564 DEFUN ("number-char-or-marker-p", Fnumber_char_or_marker_p, 1, 1, 0, /* | |
565 Return t if OBJECT is a number, character or a marker. | |
566 */ | |
567 (object)) | |
568 { | |
569 return (INT_OR_FLOATP (object) || | |
570 CHARP (object) || | |
571 MARKERP (object)) | |
572 ? Qt : Qnil; | |
573 } | |
574 | |
575 DEFUN ("floatp", Ffloatp, 1, 1, 0, /* | |
576 Return t if OBJECT is a floating point number. | |
577 */ | |
578 (object)) | |
579 { | |
580 return FLOATP (object) ? Qt : Qnil; | |
581 } | |
582 | |
583 DEFUN ("type-of", Ftype_of, 1, 1, 0, /* | |
584 Return a symbol representing the type of OBJECT. | |
585 */ | |
586 (object)) | |
587 { | |
588 switch (XTYPE (object)) | |
589 { | |
590 case Lisp_Type_Record: | |
591 return intern (XRECORD_LHEADER_IMPLEMENTATION (object)->name); | |
592 | |
593 case Lisp_Type_Char: return Qcharacter; | |
594 | |
595 default: return Qinteger; | |
596 } | |
597 } | |
598 | |
599 | |
600 /* Extract and set components of lists */ | |
601 | |
602 DEFUN ("car", Fcar, 1, 1, 0, /* | |
603 Return the car of LIST. If arg is nil, return nil. | |
604 Error if arg is not nil and not a cons cell. See also `car-safe'. | |
605 */ | |
606 (list)) | |
607 { | |
608 while (1) | |
609 { | |
610 if (CONSP (list)) | |
611 return XCAR (list); | |
612 else if (NILP (list)) | |
613 return Qnil; | |
614 else | |
615 list = wrong_type_argument (Qlistp, list); | |
616 } | |
617 } | |
618 | |
619 DEFUN ("car-safe", Fcar_safe, 1, 1, 0, /* | |
620 Return the car of OBJECT if it is a cons cell, or else nil. | |
621 */ | |
622 (object)) | |
623 { | |
624 return CONSP (object) ? XCAR (object) : Qnil; | |
625 } | |
626 | |
627 DEFUN ("cdr", Fcdr, 1, 1, 0, /* | |
628 Return the cdr of LIST. If arg is nil, return nil. | |
629 Error if arg is not nil and not a cons cell. See also `cdr-safe'. | |
630 */ | |
631 (list)) | |
632 { | |
633 while (1) | |
634 { | |
635 if (CONSP (list)) | |
636 return XCDR (list); | |
637 else if (NILP (list)) | |
638 return Qnil; | |
639 else | |
640 list = wrong_type_argument (Qlistp, list); | |
641 } | |
642 } | |
643 | |
644 DEFUN ("cdr-safe", Fcdr_safe, 1, 1, 0, /* | |
645 Return the cdr of OBJECT if it is a cons cell, else nil. | |
646 */ | |
647 (object)) | |
648 { | |
649 return CONSP (object) ? XCDR (object) : Qnil; | |
650 } | |
651 | |
652 DEFUN ("setcar", Fsetcar, 2, 2, 0, /* | |
444 | 653 Set the car of CONS-CELL to be NEWCAR. Return NEWCAR. |
428 | 654 */ |
444 | 655 (cons_cell, newcar)) |
428 | 656 { |
444 | 657 if (!CONSP (cons_cell)) |
658 cons_cell = wrong_type_argument (Qconsp, cons_cell); | |
428 | 659 |
444 | 660 XCAR (cons_cell) = newcar; |
428 | 661 return newcar; |
662 } | |
663 | |
664 DEFUN ("setcdr", Fsetcdr, 2, 2, 0, /* | |
444 | 665 Set the cdr of CONS-CELL to be NEWCDR. Return NEWCDR. |
428 | 666 */ |
444 | 667 (cons_cell, newcdr)) |
428 | 668 { |
444 | 669 if (!CONSP (cons_cell)) |
670 cons_cell = wrong_type_argument (Qconsp, cons_cell); | |
428 | 671 |
444 | 672 XCDR (cons_cell) = newcdr; |
428 | 673 return newcdr; |
674 } | |
675 | |
676 /* Find the function at the end of a chain of symbol function indirections. | |
677 | |
678 If OBJECT is a symbol, find the end of its function chain and | |
679 return the value found there. If OBJECT is not a symbol, just | |
680 return it. If there is a cycle in the function chain, signal a | |
681 cyclic-function-indirection error. | |
682 | |
442 | 683 This is like Findirect_function when VOID_FUNCTION_ERRORP is true. |
684 When VOID_FUNCTION_ERRORP is false, no error is signaled if the end | |
685 of the chain ends up being Qunbound. */ | |
428 | 686 Lisp_Object |
442 | 687 indirect_function (Lisp_Object object, int void_function_errorp) |
428 | 688 { |
689 #define FUNCTION_INDIRECTION_SUSPICION_LENGTH 16 | |
690 Lisp_Object tortoise, hare; | |
691 int count; | |
692 | |
693 for (hare = tortoise = object, count = 0; | |
694 SYMBOLP (hare); | |
695 hare = XSYMBOL (hare)->function, count++) | |
696 { | |
697 if (count < FUNCTION_INDIRECTION_SUSPICION_LENGTH) continue; | |
698 | |
699 if (count & 1) | |
700 tortoise = XSYMBOL (tortoise)->function; | |
701 if (EQ (hare, tortoise)) | |
702 return Fsignal (Qcyclic_function_indirection, list1 (object)); | |
703 } | |
704 | |
442 | 705 if (void_function_errorp && UNBOUNDP (hare)) |
436 | 706 return signal_void_function_error (object); |
428 | 707 |
708 return hare; | |
709 } | |
710 | |
711 DEFUN ("indirect-function", Findirect_function, 1, 1, 0, /* | |
712 Return the function at the end of OBJECT's function chain. | |
713 If OBJECT is a symbol, follow all function indirections and return | |
714 the final function binding. | |
715 If OBJECT is not a symbol, just return it. | |
716 Signal a void-function error if the final symbol is unbound. | |
717 Signal a cyclic-function-indirection error if there is a loop in the | |
718 function chain of symbols. | |
719 */ | |
720 (object)) | |
721 { | |
722 return indirect_function (object, 1); | |
723 } | |
724 | |
725 /* Extract and set vector and string elements */ | |
726 | |
727 DEFUN ("aref", Faref, 2, 2, 0, /* | |
728 Return the element of ARRAY at index INDEX. | |
729 ARRAY may be a vector, bit vector, or string. INDEX starts at 0. | |
730 */ | |
731 (array, index_)) | |
732 { | |
733 EMACS_INT idx; | |
734 | |
735 retry: | |
736 | |
737 if (INTP (index_)) idx = XINT (index_); | |
738 else if (CHARP (index_)) idx = XCHAR (index_); /* yuck! */ | |
739 else | |
740 { | |
741 index_ = wrong_type_argument (Qinteger_or_char_p, index_); | |
742 goto retry; | |
743 } | |
744 | |
745 if (idx < 0) goto range_error; | |
746 | |
747 if (VECTORP (array)) | |
748 { | |
749 if (idx >= XVECTOR_LENGTH (array)) goto range_error; | |
750 return XVECTOR_DATA (array)[idx]; | |
751 } | |
752 else if (BIT_VECTORP (array)) | |
753 { | |
647 | 754 if (idx >= (EMACS_INT) bit_vector_length (XBIT_VECTOR (array))) |
755 goto range_error; | |
428 | 756 return make_int (bit_vector_bit (XBIT_VECTOR (array), idx)); |
757 } | |
758 else if (STRINGP (array)) | |
759 { | |
826 | 760 if (idx >= string_char_length (array)) goto range_error; |
867 | 761 return make_char (string_ichar (array, idx)); |
428 | 762 } |
763 #ifdef LOSING_BYTECODE | |
764 else if (COMPILED_FUNCTIONP (array)) | |
765 { | |
766 /* Weird, gross compatibility kludge */ | |
767 return Felt (array, index_); | |
768 } | |
769 #endif | |
770 else | |
771 { | |
772 check_losing_bytecode ("aref", array); | |
773 array = wrong_type_argument (Qarrayp, array); | |
774 goto retry; | |
775 } | |
776 | |
777 range_error: | |
778 args_out_of_range (array, index_); | |
1204 | 779 RETURN_NOT_REACHED (Qnil); |
428 | 780 } |
781 | |
782 DEFUN ("aset", Faset, 3, 3, 0, /* | |
783 Store into the element of ARRAY at index INDEX the value NEWVAL. | |
784 ARRAY may be a vector, bit vector, or string. INDEX starts at 0. | |
785 */ | |
786 (array, index_, newval)) | |
787 { | |
788 EMACS_INT idx; | |
789 | |
790 retry: | |
791 | |
792 if (INTP (index_)) idx = XINT (index_); | |
793 else if (CHARP (index_)) idx = XCHAR (index_); /* yuck! */ | |
794 else | |
795 { | |
796 index_ = wrong_type_argument (Qinteger_or_char_p, index_); | |
797 goto retry; | |
798 } | |
799 | |
800 if (idx < 0) goto range_error; | |
801 | |
771 | 802 CHECK_LISP_WRITEABLE (array); |
428 | 803 if (VECTORP (array)) |
804 { | |
805 if (idx >= XVECTOR_LENGTH (array)) goto range_error; | |
806 XVECTOR_DATA (array)[idx] = newval; | |
807 } | |
808 else if (BIT_VECTORP (array)) | |
809 { | |
647 | 810 if (idx >= (EMACS_INT) bit_vector_length (XBIT_VECTOR (array))) |
811 goto range_error; | |
428 | 812 CHECK_BIT (newval); |
813 set_bit_vector_bit (XBIT_VECTOR (array), idx, !ZEROP (newval)); | |
814 } | |
815 else if (STRINGP (array)) | |
816 { | |
817 CHECK_CHAR_COERCE_INT (newval); | |
826 | 818 if (idx >= string_char_length (array)) goto range_error; |
793 | 819 set_string_char (array, idx, XCHAR (newval)); |
428 | 820 bump_string_modiff (array); |
821 } | |
822 else | |
823 { | |
824 array = wrong_type_argument (Qarrayp, array); | |
825 goto retry; | |
826 } | |
827 | |
828 return newval; | |
829 | |
830 range_error: | |
831 args_out_of_range (array, index_); | |
1204 | 832 RETURN_NOT_REACHED (Qnil); |
428 | 833 } |
834 | |
835 | |
836 /**********************************************************************/ | |
837 /* Arithmetic functions */ | |
838 /**********************************************************************/ | |
2001 | 839 #ifndef WITH_NUMBER_TYPES |
428 | 840 typedef struct |
841 { | |
842 int int_p; | |
843 union | |
844 { | |
845 EMACS_INT ival; | |
846 double dval; | |
847 } c; | |
848 } int_or_double; | |
849 | |
850 static void | |
851 number_char_or_marker_to_int_or_double (Lisp_Object obj, int_or_double *p) | |
852 { | |
853 retry: | |
854 p->int_p = 1; | |
855 if (INTP (obj)) p->c.ival = XINT (obj); | |
856 else if (CHARP (obj)) p->c.ival = XCHAR (obj); | |
857 else if (MARKERP (obj)) p->c.ival = marker_position (obj); | |
858 else if (FLOATP (obj)) p->c.dval = XFLOAT_DATA (obj), p->int_p = 0; | |
859 else | |
860 { | |
861 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj); | |
862 goto retry; | |
863 } | |
864 } | |
865 | |
866 static double | |
867 number_char_or_marker_to_double (Lisp_Object obj) | |
868 { | |
869 retry: | |
870 if (INTP (obj)) return (double) XINT (obj); | |
871 else if (CHARP (obj)) return (double) XCHAR (obj); | |
872 else if (MARKERP (obj)) return (double) marker_position (obj); | |
873 else if (FLOATP (obj)) return XFLOAT_DATA (obj); | |
874 else | |
875 { | |
876 obj = wrong_type_argument (Qnumber_char_or_marker_p, obj); | |
877 goto retry; | |
878 } | |
879 } | |
2001 | 880 #endif /* WITH_NUMBER_TYPES */ |
428 | 881 |
882 static EMACS_INT | |
883 integer_char_or_marker_to_int (Lisp_Object obj) | |
884 { | |
885 retry: | |
886 if (INTP (obj)) return XINT (obj); | |
887 else if (CHARP (obj)) return XCHAR (obj); | |
888 else if (MARKERP (obj)) return marker_position (obj); | |
889 else | |
890 { | |
891 obj = wrong_type_argument (Qinteger_char_or_marker_p, obj); | |
892 goto retry; | |
893 } | |
894 } | |
895 | |
1983 | 896 #ifdef WITH_NUMBER_TYPES |
897 | |
898 #ifdef HAVE_BIGNUM | |
899 #define BIGNUM_CASE(op) \ | |
900 case BIGNUM_T: \ | |
901 if (!bignum_##op (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2))) \ | |
902 return Qnil; \ | |
903 break; | |
904 #else | |
905 #define BIGNUM_CASE(op) | |
906 #endif /* HAVE_BIGNUM */ | |
907 | |
908 #ifdef HAVE_RATIO | |
909 #define RATIO_CASE(op) \ | |
910 case RATIO_T: \ | |
911 if (!ratio_##op (XRATIO_DATA (obj1), XRATIO_DATA (obj2))) \ | |
912 return Qnil; \ | |
913 break; | |
914 #else | |
915 #define RATIO_CASE(op) | |
916 #endif /* HAVE_RATIO */ | |
917 | |
918 #ifdef HAVE_BIGFLOAT | |
919 #define BIGFLOAT_CASE(op) \ | |
920 case BIGFLOAT_T: \ | |
921 if (!bigfloat_##op (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2))) \ | |
922 return Qnil; \ | |
923 break; | |
924 #else | |
925 #define BIGFLOAT_CASE(op) | |
926 #endif /* HAVE_BIGFLOAT */ | |
927 | |
928 #define ARITHCOMPARE_MANY(c_op,op) \ | |
929 { \ | |
930 REGISTER int i; \ | |
931 Lisp_Object obj1, obj2; \ | |
932 \ | |
933 for (i = 1; i < nargs; i++) \ | |
934 { \ | |
935 obj1 = args[i - 1]; \ | |
936 obj2 = args[i]; \ | |
937 switch (promote_args (&obj1, &obj2)) \ | |
938 { \ | |
939 case FIXNUM_T: \ | |
940 if (!(XREALINT (obj1) c_op XREALINT (obj2))) \ | |
941 return Qnil; \ | |
942 break; \ | |
943 BIGNUM_CASE (op) \ | |
944 RATIO_CASE (op) \ | |
945 case FLOAT_T: \ | |
946 if (!(XFLOAT_DATA (obj1) c_op XFLOAT_DATA (obj2))) \ | |
947 return Qnil; \ | |
948 break; \ | |
949 BIGFLOAT_CASE (op) \ | |
950 } \ | |
951 } \ | |
952 return Qt; \ | |
953 } | |
954 #else /* !WITH_NUMBER_TYPES */ | |
955 #define ARITHCOMPARE_MANY(c_op,op) \ | |
428 | 956 { \ |
957 int_or_double iod1, iod2, *p = &iod1, *q = &iod2; \ | |
958 Lisp_Object *args_end = args + nargs; \ | |
959 \ | |
960 number_char_or_marker_to_int_or_double (*args++, p); \ | |
961 \ | |
962 while (args < args_end) \ | |
963 { \ | |
964 number_char_or_marker_to_int_or_double (*args++, q); \ | |
965 \ | |
966 if (!((p->int_p && q->int_p) ? \ | |
1983 | 967 (p->c.ival c_op q->c.ival) : \ |
968 ((p->int_p ? (double) p->c.ival : p->c.dval) c_op \ | |
428 | 969 (q->int_p ? (double) q->c.ival : q->c.dval)))) \ |
970 return Qnil; \ | |
971 \ | |
972 { /* swap */ int_or_double *r = p; p = q; q = r; } \ | |
973 } \ | |
974 return Qt; \ | |
975 } | |
1983 | 976 #endif /* WITH_NUMBER_TYPES */ |
428 | 977 |
978 DEFUN ("=", Feqlsign, 1, MANY, 0, /* | |
979 Return t if all the arguments are numerically equal. | |
980 The arguments may be numbers, characters or markers. | |
981 */ | |
982 (int nargs, Lisp_Object *args)) | |
983 { | |
1983 | 984 ARITHCOMPARE_MANY (==, eql) |
428 | 985 } |
986 | |
987 DEFUN ("<", Flss, 1, MANY, 0, /* | |
988 Return t if the sequence of arguments is monotonically increasing. | |
989 The arguments may be numbers, characters or markers. | |
990 */ | |
991 (int nargs, Lisp_Object *args)) | |
992 { | |
1983 | 993 ARITHCOMPARE_MANY (<, lt) |
428 | 994 } |
995 | |
996 DEFUN (">", Fgtr, 1, MANY, 0, /* | |
997 Return t if the sequence of arguments is monotonically decreasing. | |
998 The arguments may be numbers, characters or markers. | |
999 */ | |
1000 (int nargs, Lisp_Object *args)) | |
1001 { | |
1983 | 1002 ARITHCOMPARE_MANY (>, gt) |
428 | 1003 } |
1004 | |
1005 DEFUN ("<=", Fleq, 1, MANY, 0, /* | |
1006 Return t if the sequence of arguments is monotonically nondecreasing. | |
1007 The arguments may be numbers, characters or markers. | |
1008 */ | |
1009 (int nargs, Lisp_Object *args)) | |
1010 { | |
1983 | 1011 ARITHCOMPARE_MANY (<=, le) |
428 | 1012 } |
1013 | |
1014 DEFUN (">=", Fgeq, 1, MANY, 0, /* | |
1015 Return t if the sequence of arguments is monotonically nonincreasing. | |
1016 The arguments may be numbers, characters or markers. | |
1017 */ | |
1018 (int nargs, Lisp_Object *args)) | |
1019 { | |
1983 | 1020 ARITHCOMPARE_MANY (>=, ge) |
428 | 1021 } |
1022 | |
1983 | 1023 /* Unlike all the other comparisons, this is an O(N*N) algorithm. But who |
1024 cares? Inspection of all elisp code distributed by xemacs.org shows that | |
1025 it is almost always called with 2 arguments, rarely with 3, and never with | |
1026 more than 3. The constant factors of algorithms with better asymptotic | |
1027 complexity are higher, which means that those algorithms will run SLOWER | |
1028 than this one in the common case. Optimize the common case! */ | |
428 | 1029 DEFUN ("/=", Fneq, 1, MANY, 0, /* |
1030 Return t if no two arguments are numerically equal. | |
1031 The arguments may be numbers, characters or markers. | |
1032 */ | |
1033 (int nargs, Lisp_Object *args)) | |
1034 { | |
1983 | 1035 #ifdef WITH_NUMBER_TYPES |
1036 REGISTER int i, j; | |
1037 Lisp_Object obj1, obj2; | |
1038 | |
1039 for (i = 0; i < nargs - 1; i++) | |
1040 { | |
1041 obj1 = args[i]; | |
1042 for (j = i + 1; j < nargs; j++) | |
1043 { | |
1044 obj2 = args[j]; | |
1045 switch (promote_args (&obj1, &obj2)) | |
1046 { | |
1047 case FIXNUM_T: | |
1048 if (XREALINT (obj1) == XREALINT (obj2)) | |
1049 return Qnil; | |
1050 break; | |
1051 #ifdef HAVE_BIGNUM | |
1052 case BIGNUM_T: | |
1053 if (bignum_eql (XBIGNUM_DATA (obj1), XBIGNUM_DATA (obj2))) | |
1054 return Qnil; | |
1055 break; | |
1056 #endif | |
1057 #ifdef HAVE_RATIO | |
1058 case RATIO_T: | |
1059 if (ratio_eql (XRATIO_DATA (obj1), XRATIO_DATA (obj2))) | |
1060 return Qnil; | |
1061 break; | |
1062 #endif | |
1063 case FLOAT_T: | |
1064 if (XFLOAT_DATA (obj1) == XFLOAT_DATA (obj2)) | |
1065 return Qnil; | |
1066 break; | |
1067 #ifdef HAVE_BIGFLOAT | |
1068 case BIGFLOAT_T: | |
1069 if (bigfloat_eql (XBIGFLOAT_DATA (obj1), XBIGFLOAT_DATA (obj2))) | |
1070 return Qnil; | |
1071 break; | |
1072 #endif | |
1073 } | |
1074 } | |
1075 } | |
1076 return Qt; | |
1077 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1078 Lisp_Object *args_end = args + nargs; |
1079 Lisp_Object *p, *q; | |
1080 | |
1081 /* Unlike all the other comparisons, this is an N*N algorithm. | |
1082 We could use a hash table for nargs > 50 to make this linear. */ | |
1083 for (p = args; p < args_end; p++) | |
1084 { | |
1085 int_or_double iod1, iod2; | |
1086 number_char_or_marker_to_int_or_double (*p, &iod1); | |
1087 | |
1088 for (q = p + 1; q < args_end; q++) | |
1089 { | |
1090 number_char_or_marker_to_int_or_double (*q, &iod2); | |
1091 | |
1092 if (!((iod1.int_p && iod2.int_p) ? | |
1093 (iod1.c.ival != iod2.c.ival) : | |
1094 ((iod1.int_p ? (double) iod1.c.ival : iod1.c.dval) != | |
1095 (iod2.int_p ? (double) iod2.c.ival : iod2.c.dval)))) | |
1096 return Qnil; | |
1097 } | |
1098 } | |
1099 return Qt; | |
1983 | 1100 #endif /* WITH_NUMBER_TYPES */ |
428 | 1101 } |
1102 | |
1103 DEFUN ("zerop", Fzerop, 1, 1, 0, /* | |
1104 Return t if NUMBER is zero. | |
1105 */ | |
1106 (number)) | |
1107 { | |
1108 retry: | |
1109 if (INTP (number)) | |
1110 return EQ (number, Qzero) ? Qt : Qnil; | |
1983 | 1111 #ifdef HAVE_BIGNUM |
1112 else if (BIGNUMP (number)) | |
1113 return bignum_sign (XBIGNUM_DATA (number)) == 0 ? Qt : Qnil; | |
1114 #endif | |
1115 #ifdef HAVE_RATIO | |
1116 else if (RATIOP (number)) | |
1117 return ratio_sign (XRATIO_DATA (number)) == 0 ? Qt : Qnil; | |
1118 #endif | |
428 | 1119 else if (FLOATP (number)) |
1120 return XFLOAT_DATA (number) == 0.0 ? Qt : Qnil; | |
1983 | 1121 #ifdef HAVE_BIGFLOAT |
1122 else if (BIGFLOATP (number)) | |
1123 return bigfloat_sign (XBIGFLOAT_DATA (number)) == 0 ? Qt : Qnil; | |
1124 #endif | |
428 | 1125 else |
1126 { | |
1127 number = wrong_type_argument (Qnumberp, number); | |
1128 goto retry; | |
1129 } | |
1130 } | |
1131 | |
1132 /* Convert between a 32-bit value and a cons of two 16-bit values. | |
1133 This is used to pass 32-bit integers to and from the user. | |
1134 Use time_to_lisp() and lisp_to_time() for time values. | |
1135 | |
1136 If you're thinking of using this to store a pointer into a Lisp Object | |
1137 for internal purposes (such as when calling record_unwind_protect()), | |
1138 try using make_opaque_ptr()/get_opaque_ptr() instead. */ | |
1139 Lisp_Object | |
1140 word_to_lisp (unsigned int item) | |
1141 { | |
1142 return Fcons (make_int (item >> 16), make_int (item & 0xffff)); | |
1143 } | |
1144 | |
1145 unsigned int | |
1146 lisp_to_word (Lisp_Object item) | |
1147 { | |
1148 if (INTP (item)) | |
1149 return XINT (item); | |
1150 else | |
1151 { | |
1152 Lisp_Object top = Fcar (item); | |
1153 Lisp_Object bot = Fcdr (item); | |
1154 CHECK_INT (top); | |
1155 CHECK_INT (bot); | |
1156 return (XINT (top) << 16) | (XINT (bot) & 0xffff); | |
1157 } | |
1158 } | |
1159 | |
1160 | |
1161 DEFUN ("number-to-string", Fnumber_to_string, 1, 1, 0, /* | |
444 | 1162 Convert NUMBER to a string by printing it in decimal. |
428 | 1163 Uses a minus sign if negative. |
444 | 1164 NUMBER may be an integer or a floating point number. |
1983 | 1165 If supported, it may also be a ratio. |
428 | 1166 */ |
444 | 1167 (number)) |
428 | 1168 { |
1983 | 1169 #ifdef WITH_NUMBER_TYPES |
1170 CHECK_NUMBER (number); | |
1171 #else | |
444 | 1172 CHECK_INT_OR_FLOAT (number); |
1983 | 1173 #endif |
428 | 1174 |
444 | 1175 if (FLOATP (number)) |
428 | 1176 { |
1177 char pigbuf[350]; /* see comments in float_to_string */ | |
1178 | |
444 | 1179 float_to_string (pigbuf, XFLOAT_DATA (number)); |
428 | 1180 return build_string (pigbuf); |
1181 } | |
1983 | 1182 #ifdef HAVE_BIGNUM |
1183 if (BIGNUMP (number)) | |
1184 { | |
1185 char *str = bignum_to_string (XBIGNUM_DATA (number), 10); | |
1186 Lisp_Object retval = build_string (str); | |
1187 xfree (str, char *); | |
1188 return retval; | |
1189 } | |
1190 #endif | |
1191 #ifdef HAVE_RATIO | |
1192 if (RATIOP (number)) | |
1193 { | |
1194 char *str = ratio_to_string (XRATIO_DATA (number), 10); | |
1195 Lisp_Object retval = build_string (str); | |
1196 xfree (str, char *); | |
1197 return retval; | |
1198 } | |
1199 #endif | |
1200 #ifdef HAVE_BIGFLOAT | |
1201 if (BIGFLOATP (number)) | |
1202 { | |
1203 char *str = bigfloat_to_string (XBIGFLOAT_DATA (number), 10); | |
1204 Lisp_Object retval = build_string (str); | |
1205 xfree (str, char *); | |
1206 return retval; | |
1207 } | |
1208 #endif | |
428 | 1209 |
603 | 1210 { |
1211 char buffer[DECIMAL_PRINT_SIZE (long)]; | |
1212 | |
1213 long_to_string (buffer, XINT (number)); | |
1214 return build_string (buffer); | |
1215 } | |
428 | 1216 } |
1217 | |
2001 | 1218 #ifndef HAVE_BIGNUM |
428 | 1219 static int |
1220 digit_to_number (int character, int base) | |
1221 { | |
1222 /* Assumes ASCII */ | |
1223 int digit = ((character >= '0' && character <= '9') ? character - '0' : | |
1224 (character >= 'a' && character <= 'z') ? character - 'a' + 10 : | |
1225 (character >= 'A' && character <= 'Z') ? character - 'A' + 10 : | |
1226 -1); | |
1227 | |
1228 return digit >= base ? -1 : digit; | |
1229 } | |
2001 | 1230 #endif |
428 | 1231 |
1232 DEFUN ("string-to-number", Fstring_to_number, 1, 2, 0, /* | |
444 | 1233 Convert STRING to a number by parsing it as a number in base BASE. |
428 | 1234 This parses both integers and floating point numbers. |
1983 | 1235 If they are supported, it also reads ratios. |
428 | 1236 It ignores leading spaces and tabs. |
1237 | |
444 | 1238 If BASE is nil or omitted, base 10 is used. |
1239 BASE must be an integer between 2 and 16 (inclusive). | |
428 | 1240 Floating point numbers always use base 10. |
1241 */ | |
1242 (string, base)) | |
1243 { | |
1995 | 1244 Ibyte *p; |
428 | 1245 int b; |
1246 | |
1247 CHECK_STRING (string); | |
1248 | |
1249 if (NILP (base)) | |
1250 b = 10; | |
1251 else | |
1252 { | |
1253 CHECK_INT (base); | |
1254 b = XINT (base); | |
1255 check_int_range (b, 2, 16); | |
1256 } | |
1257 | |
1995 | 1258 p = XSTRING_DATA (string); |
428 | 1259 |
1260 /* Skip any whitespace at the front of the number. Some versions of | |
1261 atoi do this anyway, so we might as well make Emacs lisp consistent. */ | |
1262 while (*p == ' ' || *p == '\t') | |
1263 p++; | |
1264 | |
1995 | 1265 if (isfloat_string ((const char *) p) && b == 10) |
1983 | 1266 { |
1267 #ifdef HAVE_BIGFLOAT | |
1268 if (ZEROP (Vdefault_float_precision)) | |
1269 #endif | |
1995 | 1270 return make_float (atof ((const char *) p)); |
1983 | 1271 #ifdef HAVE_BIGFLOAT |
1272 else | |
1273 { | |
2013 | 1274 /* The GMP version of bigfloat_set_string (mpf_set_str) has the |
1275 following limitation: if p starts with a '+' sign, it does | |
1276 nothing; i.e., it leaves its bigfloat argument untouched. | |
1277 Therefore, move p past any leading '+' signs. */ | |
2010 | 1278 if (*p == '+') |
1279 p++; | |
1983 | 1280 bigfloat_set_prec (scratch_bigfloat, bigfloat_get_default_prec ()); |
1995 | 1281 bigfloat_set_string (scratch_bigfloat, (const char *) p, b); |
1983 | 1282 return make_bigfloat_bf (scratch_bigfloat); |
1283 } | |
1284 #endif | |
1285 } | |
1286 | |
1287 #ifdef HAVE_RATIO | |
1288 if (qxestrchr (p, '/') != NULL) | |
1289 { | |
2013 | 1290 /* The GMP version of ratio_set_string (mpq_set_str) has the following |
1291 limitations: | |
1292 - If p starts with a '+' sign, it does nothing; i.e., it leaves its | |
1293 ratio argument untouched. | |
1294 - If p has a '+' sign after the '/' (e.g., 300/+400), it sets the | |
1295 numerator from the string, but *leaves the denominator unchanged*. | |
1296 - If p has trailing nonnumeric characters, it sets the numerator from | |
1297 the string, but leaves the denominator unchanged. | |
1298 - If p has more than one '/', (e.g., 1/2/3), then it sets the | |
1299 numerator from the string, but leaves the denominator unchanged. | |
1300 | |
1301 Therefore, move p past any leading '+' signs, temporarily drop a null | |
1302 after the numeric characters we are trying to convert, and then put | |
1303 the nulled character back afterward. I am not going to fix problem | |
1304 #2; just don't write ratios that look like that. */ | |
1305 Ibyte *end, save; | |
1306 | |
2010 | 1307 if (*p == '+') |
1308 p++; | |
2013 | 1309 |
2014 | 1310 end = p; |
1311 if (*end == '-') | |
1312 end++; | |
1313 while ((*end >= '0' && *end <= '9') || | |
2013 | 1314 (b > 10 && *end >= 'a' && *end <= 'a' + b - 11) || |
2014 | 1315 (b > 10 && *end >= 'A' && *end <= 'A' + b - 11)) |
1316 end++; | |
2013 | 1317 if (*end == '/') |
2014 | 1318 { |
1319 end++; | |
1320 if (*end == '-') | |
1321 end++; | |
1322 while ((*end >= '0' && *end <= '9') || | |
1323 (b > 10 && *end >= 'a' && *end <= 'a' + b - 11) || | |
1324 (b > 10 && *end >= 'A' && *end <= 'A' + b - 11)) | |
1325 end++; | |
1326 } | |
2013 | 1327 save = *end; |
1328 *end = '\0'; | |
1995 | 1329 ratio_set_string (scratch_ratio, (const char *) p, b); |
2013 | 1330 *end = save; |
1331 ratio_canonicalize (scratch_ratio); | |
1983 | 1332 return make_ratio_rt (scratch_ratio); |
1333 } | |
1334 #endif /* HAVE_RATIO */ | |
1335 | |
1336 #ifdef HAVE_BIGNUM | |
2013 | 1337 { |
1338 /* The GMP version of bignum_set_string (mpz_set_str) has the following | |
1339 limitations: | |
1340 - If p starts with a '+' sign, it does nothing; i.e., it leaves its | |
1341 bignum argument untouched. | |
1342 - If p is the empty string, it does nothing. | |
1343 - If p has trailing nonnumeric characters, it does nothing. | |
1344 | |
1345 Therefore, move p past any leading '+' signs, temporarily drop a null | |
1346 after the numeric characters we are trying to convert, special case the | |
1347 empty string, and then put the nulled character back afterward. */ | |
1348 Ibyte *end, save; | |
1349 Lisp_Object retval; | |
1350 | |
1351 if (*p == '+') | |
1352 p++; | |
2014 | 1353 end = p; |
1354 if (*end == '-') | |
1355 end++; | |
1356 while ((*end >= '0' && *end <= '9') || | |
2013 | 1357 (b > 10 && *end >= 'a' && *end <= 'a' + b - 11) || |
2014 | 1358 (b > 10 && *end >= 'A' && *end <= 'A' + b - 11)) |
1359 end++; | |
2013 | 1360 save = *end; |
1361 *end = '\0'; | |
1362 if (*p == '\0') | |
1363 retval = make_int (0); | |
1364 else | |
1365 { | |
1366 bignum_set_string (scratch_bignum, (const char *) p, b); | |
1367 retval = Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
1368 } | |
1369 *end = save; | |
1370 return retval; | |
1371 } | |
1983 | 1372 #else |
428 | 1373 if (b == 10) |
1374 { | |
1375 /* Use the system-provided functions for base 10. */ | |
1376 #if SIZEOF_EMACS_INT == SIZEOF_INT | |
2054 | 1377 return make_int (atoi ((char*) p)); |
428 | 1378 #elif SIZEOF_EMACS_INT == SIZEOF_LONG |
2054 | 1379 return make_int (atol ((char*) p)); |
428 | 1380 #elif SIZEOF_EMACS_INT == SIZEOF_LONG_LONG |
2054 | 1381 return make_int (atoll ((char*) p)); |
428 | 1382 #endif |
1383 } | |
1384 else | |
1385 { | |
444 | 1386 int negative = 1; |
428 | 1387 EMACS_INT v = 0; |
1388 | |
1389 if (*p == '-') | |
1390 { | |
1391 negative = -1; | |
1392 p++; | |
1393 } | |
1394 else if (*p == '+') | |
1395 p++; | |
1396 while (1) | |
1397 { | |
444 | 1398 int digit = digit_to_number (*p++, b); |
428 | 1399 if (digit < 0) |
1400 break; | |
1401 v = v * b + digit; | |
1402 } | |
1403 return make_int (negative * v); | |
1404 } | |
1983 | 1405 #endif /* HAVE_BIGNUM */ |
428 | 1406 } |
1407 | |
1408 | |
1409 DEFUN ("+", Fplus, 0, MANY, 0, /* | |
1410 Return sum of any number of arguments. | |
1411 The arguments should all be numbers, characters or markers. | |
1412 */ | |
1413 (int nargs, Lisp_Object *args)) | |
1414 { | |
1983 | 1415 #ifdef WITH_NUMBER_TYPES |
1416 REGISTER int i; | |
1417 Lisp_Object accum = make_int (0), addend; | |
1418 | |
1419 for (i = 0; i < nargs; i++) | |
1420 { | |
1421 addend = args[i]; | |
1422 switch (promote_args (&accum, &addend)) | |
1423 { | |
1424 case FIXNUM_T: | |
1425 accum = make_integer (XREALINT (accum) + XREALINT (addend)); | |
1426 break; | |
1427 #ifdef HAVE_BIGNUM | |
1428 case BIGNUM_T: | |
1429 bignum_add (scratch_bignum, XBIGNUM_DATA (accum), | |
1430 XBIGNUM_DATA (addend)); | |
1431 accum = make_bignum_bg (scratch_bignum); | |
1432 break; | |
1433 #endif | |
1434 #ifdef HAVE_RATIO | |
1435 case RATIO_T: | |
1436 ratio_add (scratch_ratio, XRATIO_DATA (accum), | |
1437 XRATIO_DATA (addend)); | |
1438 accum = make_ratio_rt (scratch_ratio); | |
1439 break; | |
1440 #endif | |
1441 case FLOAT_T: | |
1442 accum = make_float (XFLOAT_DATA (accum) + XFLOAT_DATA (addend)); | |
1443 break; | |
1444 #ifdef HAVE_BIGFLOAT | |
1445 case BIGFLOAT_T: | |
1446 bigfloat_set_prec (scratch_bigfloat, | |
1447 max (XBIGFLOAT_GET_PREC (addend), | |
1448 XBIGFLOAT_GET_PREC (accum))); | |
1449 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (accum), | |
1450 XBIGFLOAT_DATA (addend)); | |
1451 accum = make_bigfloat_bf (scratch_bigfloat); | |
1452 break; | |
1453 #endif | |
1454 } | |
1455 } | |
1456 return Fcanonicalize_number (accum); | |
1457 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1458 EMACS_INT iaccum = 0; |
1459 Lisp_Object *args_end = args + nargs; | |
1460 | |
1461 while (args < args_end) | |
1462 { | |
1463 int_or_double iod; | |
1464 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1465 if (iod.int_p) | |
1466 iaccum += iod.c.ival; | |
1467 else | |
1468 { | |
1469 double daccum = (double) iaccum + iod.c.dval; | |
1470 while (args < args_end) | |
1471 daccum += number_char_or_marker_to_double (*args++); | |
1472 return make_float (daccum); | |
1473 } | |
1474 } | |
1475 | |
1476 return make_int (iaccum); | |
1983 | 1477 #endif /* WITH_NUMBER_TYPES */ |
428 | 1478 } |
1479 | |
1480 DEFUN ("-", Fminus, 1, MANY, 0, /* | |
1481 Negate number or subtract numbers, characters or markers. | |
1482 With one arg, negates it. With more than one arg, | |
1483 subtracts all but the first from the first. | |
1484 */ | |
1485 (int nargs, Lisp_Object *args)) | |
1486 { | |
1983 | 1487 #ifdef WITH_NUMBER_TYPES |
1488 REGISTER int i; | |
1489 Lisp_Object accum = args[0], subtrahend; | |
1490 | |
1491 if (nargs == 1) | |
1492 { | |
1493 if (CHARP (accum)) | |
1494 accum = make_int (XCHAR (accum)); | |
1495 else if (MARKERP (accum)) | |
1496 accum = make_int (marker_position (accum)); | |
1497 | |
1498 /* Invert the sign of accum */ | |
1499 CHECK_NUMBER (accum); | |
1500 switch (get_number_type (accum)) | |
1501 { | |
1502 case FIXNUM_T: | |
1503 return make_integer (-XREALINT (accum)); | |
1504 #ifdef HAVE_BIGNUM | |
1505 case BIGNUM_T: | |
1506 bignum_neg (scratch_bignum, XBIGNUM_DATA (accum)); | |
1507 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
1508 #endif | |
1509 #ifdef HAVE_RATIO | |
1510 case RATIO_T: | |
1511 ratio_neg (scratch_ratio, XRATIO_DATA (accum)); | |
1512 return make_ratio_rt (scratch_ratio); | |
1513 #endif | |
1514 case FLOAT_T: | |
1515 return make_float (-XFLOAT_DATA (accum)); | |
1516 #ifdef HAVE_BIGFLOAT | |
1517 case BIGFLOAT_T: | |
1518 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (accum)); | |
1519 bigfloat_neg (scratch_bigfloat, XBIGFLOAT_DATA (accum)); | |
1520 return make_bigfloat_bf (scratch_bigfloat); | |
1521 #endif | |
1522 } | |
1523 } | |
1524 else | |
1525 { | |
1526 /* Subtrace the remaining arguments from accum */ | |
1527 for (i = 1; i < nargs; i++) | |
1528 { | |
1529 subtrahend = args[i]; | |
1530 switch (promote_args (&accum, &subtrahend)) | |
1531 { | |
1532 case FIXNUM_T: | |
1533 accum = make_integer (XREALINT (accum) - XREALINT (subtrahend)); | |
1534 break; | |
1535 #ifdef HAVE_BIGNUM | |
1536 case BIGNUM_T: | |
1537 bignum_sub (scratch_bignum, XBIGNUM_DATA (accum), | |
1538 XBIGNUM_DATA (subtrahend)); | |
1539 accum = make_bignum_bg (scratch_bignum); | |
1540 break; | |
1541 #endif | |
1542 #ifdef HAVE_RATIO | |
1543 case RATIO_T: | |
1544 ratio_sub (scratch_ratio, XRATIO_DATA (accum), | |
1545 XRATIO_DATA (subtrahend)); | |
1546 accum = make_ratio_rt (scratch_ratio); | |
1547 break; | |
1548 #endif | |
1549 case FLOAT_T: | |
1550 accum = | |
1551 make_float (XFLOAT_DATA (accum) - XFLOAT_DATA (subtrahend)); | |
1552 break; | |
1553 #ifdef HAVE_BIGFLOAT | |
1554 case BIGFLOAT_T: | |
1555 bigfloat_set_prec (scratch_bigfloat, | |
1556 max (XBIGFLOAT_GET_PREC (subtrahend), | |
1557 XBIGFLOAT_GET_PREC (accum))); | |
1558 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (accum), | |
1559 XBIGFLOAT_DATA (subtrahend)); | |
1560 accum = make_bigfloat_bf (scratch_bigfloat); | |
1561 break; | |
1562 #endif | |
1563 } | |
1564 } | |
1565 } | |
1566 return Fcanonicalize_number (accum); | |
1567 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1568 EMACS_INT iaccum; |
1569 double daccum; | |
1570 Lisp_Object *args_end = args + nargs; | |
1571 int_or_double iod; | |
1572 | |
1573 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1574 if (iod.int_p) | |
1575 iaccum = nargs > 1 ? iod.c.ival : - iod.c.ival; | |
1576 else | |
1577 { | |
1578 daccum = nargs > 1 ? iod.c.dval : - iod.c.dval; | |
1579 goto do_float; | |
1580 } | |
1581 | |
1582 while (args < args_end) | |
1583 { | |
1584 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1585 if (iod.int_p) | |
1586 iaccum -= iod.c.ival; | |
1587 else | |
1588 { | |
1589 daccum = (double) iaccum - iod.c.dval; | |
1590 goto do_float; | |
1591 } | |
1592 } | |
1593 | |
1594 return make_int (iaccum); | |
1595 | |
1596 do_float: | |
1597 for (; args < args_end; args++) | |
1598 daccum -= number_char_or_marker_to_double (*args); | |
1599 return make_float (daccum); | |
1983 | 1600 #endif /* WITH_NUMBER_TYPES */ |
428 | 1601 } |
1602 | |
1603 DEFUN ("*", Ftimes, 0, MANY, 0, /* | |
1604 Return product of any number of arguments. | |
1605 The arguments should all be numbers, characters or markers. | |
1606 */ | |
1607 (int nargs, Lisp_Object *args)) | |
1608 { | |
1983 | 1609 #ifdef WITH_NUMBER_TYPES |
1610 REGISTER int i; | |
1611 /* Start with a bignum to avoid overflow */ | |
1612 Lisp_Object accum = make_bignum (1L), multiplier; | |
1613 | |
1614 for (i = 0; i < nargs; i++) | |
1615 { | |
1616 multiplier = args[i]; | |
1617 switch (promote_args (&accum, &multiplier)) | |
1618 { | |
1619 #ifdef HAVE_BIGNUM | |
1620 case BIGNUM_T: | |
1621 bignum_mul (scratch_bignum, XBIGNUM_DATA (accum), | |
1622 XBIGNUM_DATA (multiplier)); | |
1623 accum = make_bignum_bg (scratch_bignum); | |
1624 break; | |
1625 #endif | |
1626 #ifdef HAVE_RATIO | |
1627 case RATIO_T: | |
1628 ratio_mul (scratch_ratio, XRATIO_DATA (accum), | |
1629 XRATIO_DATA (multiplier)); | |
1630 accum = make_ratio_rt (scratch_ratio); | |
1631 break; | |
1632 #endif | |
1633 case FLOAT_T: | |
1634 accum = make_float (XFLOAT_DATA (accum) * XFLOAT_DATA (multiplier)); | |
1635 break; | |
1636 #ifdef HAVE_BIGFLOAT | |
1637 case BIGFLOAT_T: | |
1638 bigfloat_set_prec (scratch_bigfloat, | |
1639 max (XBIGFLOAT_GET_PREC (multiplier), | |
1640 XBIGFLOAT_GET_PREC (accum))); | |
1641 bigfloat_mul (scratch_bigfloat, XBIGFLOAT_DATA (accum), | |
1642 XBIGFLOAT_DATA (multiplier)); | |
1643 accum = make_bigfloat_bf (scratch_bigfloat); | |
1644 break; | |
1645 #endif | |
1646 } | |
1647 } | |
1648 return Fcanonicalize_number (accum); | |
1649 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1650 EMACS_INT iaccum = 1; |
1651 Lisp_Object *args_end = args + nargs; | |
1652 | |
1653 while (args < args_end) | |
1654 { | |
1655 int_or_double iod; | |
1656 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1657 if (iod.int_p) | |
1658 iaccum *= iod.c.ival; | |
1659 else | |
1660 { | |
1661 double daccum = (double) iaccum * iod.c.dval; | |
1662 while (args < args_end) | |
1663 daccum *= number_char_or_marker_to_double (*args++); | |
1664 return make_float (daccum); | |
1665 } | |
1666 } | |
1667 | |
1668 return make_int (iaccum); | |
1983 | 1669 #endif /* WITH_NUMBER_TYPES */ |
428 | 1670 } |
1671 | |
1983 | 1672 #ifdef HAVE_RATIO |
1673 DEFUN ("div", Fdiv, 1, MANY, 0, /* | |
1674 Same as `/', but dividing integers creates a ratio instead of truncating. | |
1675 Note that this is a departure from Common Lisp, where / creates ratios when | |
1676 dividing integers. Having a separate function lets us avoid breaking existing | |
1677 Emacs Lisp code that expects / to do integer division. | |
1678 */ | |
1679 (int nargs, Lisp_Object *args)) | |
1680 { | |
1681 REGISTER int i; | |
1682 Lisp_Object accum, divisor; | |
1683 | |
1684 if (nargs == 1) | |
1685 { | |
1686 i = 0; | |
1687 accum = make_int (1); | |
1688 } | |
1689 else | |
1690 { | |
1691 i = 1; | |
1692 accum = args[0]; | |
1693 } | |
1694 for (; i < nargs; i++) | |
1695 { | |
1696 divisor = args[i]; | |
1697 switch (promote_args (&accum, &divisor)) | |
1698 { | |
1699 case FIXNUM_T: | |
1700 if (XREALINT (divisor) == 0) goto divide_by_zero; | |
1701 bignum_set_long (scratch_bignum, XREALINT (accum)); | |
1702 bignum_set_long (scratch_bignum2, XREALINT (divisor)); | |
1703 accum = make_ratio_bg (scratch_bignum, scratch_bignum2); | |
1704 break; | |
1705 case BIGNUM_T: | |
1706 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero; | |
1707 accum = make_ratio_bg (XBIGNUM_DATA (accum), XBIGNUM_DATA (divisor)); | |
1708 break; | |
1709 case RATIO_T: | |
1710 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero; | |
1711 ratio_div (scratch_ratio, XRATIO_DATA (accum), | |
1712 XRATIO_DATA (divisor)); | |
1713 accum = make_ratio_rt (scratch_ratio); | |
1714 break; | |
1715 case FLOAT_T: | |
1716 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero; | |
1717 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor)); | |
1718 break; | |
1719 #ifdef HAVE_BIGFLOAT | |
1720 case BIGFLOAT_T: | |
1721 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0) | |
1722 goto divide_by_zero; | |
1723 bigfloat_set_prec (scratch_bigfloat, | |
1724 max (XBIGFLOAT_GET_PREC (divisor), | |
1725 XBIGFLOAT_GET_PREC (accum))); | |
1726 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum), | |
1727 XBIGFLOAT_DATA (divisor)); | |
1728 accum = make_bigfloat_bf (scratch_bigfloat); | |
1729 break; | |
1730 #endif | |
1731 } | |
1732 } | |
1733 return Fcanonicalize_number (accum); | |
1734 | |
1735 divide_by_zero: | |
1736 Fsignal (Qarith_error, Qnil); | |
1737 return Qnil; /* not (usually) reached */ | |
1738 } | |
1739 #endif /* HAVE_RATIO */ | |
1740 | |
428 | 1741 DEFUN ("/", Fquo, 1, MANY, 0, /* |
1742 Return first argument divided by all the remaining arguments. | |
1743 The arguments must be numbers, characters or markers. | |
1744 With one argument, reciprocates the argument. | |
1745 */ | |
1746 (int nargs, Lisp_Object *args)) | |
1747 { | |
1983 | 1748 #ifdef WITH_NUMBER_TYPES |
1749 REGISTER int i; | |
1750 Lisp_Object accum, divisor; | |
1751 | |
1752 if (nargs == 1) | |
1753 { | |
1754 i = 0; | |
1755 accum = make_int (1); | |
1756 } | |
1757 else | |
1758 { | |
1759 i = 1; | |
1760 accum = args[0]; | |
1761 } | |
1762 for (; i < nargs; i++) | |
1763 { | |
1764 divisor = args[i]; | |
1765 switch (promote_args (&accum, &divisor)) | |
1766 { | |
1767 case FIXNUM_T: | |
1768 if (XREALINT (divisor) == 0) goto divide_by_zero; | |
1769 accum = make_integer (XREALINT (accum) / XREALINT (divisor)); | |
1770 break; | |
1771 #ifdef HAVE_BIGNUM | |
1772 case BIGNUM_T: | |
1773 if (bignum_sign (XBIGNUM_DATA (divisor)) == 0) goto divide_by_zero; | |
1774 bignum_div (scratch_bignum, XBIGNUM_DATA (accum), | |
1775 XBIGNUM_DATA (divisor)); | |
1776 accum = make_bignum_bg (scratch_bignum); | |
1777 break; | |
1778 #endif | |
1779 #ifdef HAVE_RATIO | |
1780 case RATIO_T: | |
1781 if (ratio_sign (XRATIO_DATA (divisor)) == 0) goto divide_by_zero; | |
1782 ratio_div (scratch_ratio, XRATIO_DATA (accum), | |
1783 XRATIO_DATA (divisor)); | |
1784 accum = make_ratio_rt (scratch_ratio); | |
1785 break; | |
1786 #endif | |
1787 case FLOAT_T: | |
1788 if (XFLOAT_DATA (divisor) == 0.0) goto divide_by_zero; | |
1789 accum = make_float (XFLOAT_DATA (accum) / XFLOAT_DATA (divisor)); | |
1790 break; | |
1791 #ifdef HAVE_BIGFLOAT | |
1792 case BIGFLOAT_T: | |
1793 if (bigfloat_sign (XBIGFLOAT_DATA (divisor)) == 0) | |
1794 goto divide_by_zero; | |
1795 bigfloat_set_prec (scratch_bigfloat, | |
1796 max (XBIGFLOAT_GET_PREC (divisor), | |
1797 XBIGFLOAT_GET_PREC (accum))); | |
1798 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (accum), | |
1799 XBIGFLOAT_DATA (divisor)); | |
1800 accum = make_bigfloat_bf (scratch_bigfloat); | |
1801 break; | |
1802 #endif | |
1803 } | |
1804 } | |
1805 return Fcanonicalize_number (accum); | |
1806 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1807 EMACS_INT iaccum; |
1808 double daccum; | |
1809 Lisp_Object *args_end = args + nargs; | |
1810 int_or_double iod; | |
1811 | |
1812 if (nargs == 1) | |
1813 iaccum = 1; | |
1814 else | |
1815 { | |
1816 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1817 if (iod.int_p) | |
1818 iaccum = iod.c.ival; | |
1819 else | |
1820 { | |
1821 daccum = iod.c.dval; | |
1822 goto divide_floats; | |
1823 } | |
1824 } | |
1825 | |
1826 while (args < args_end) | |
1827 { | |
1828 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1829 if (iod.int_p) | |
1830 { | |
1831 if (iod.c.ival == 0) goto divide_by_zero; | |
1832 iaccum /= iod.c.ival; | |
1833 } | |
1834 else | |
1835 { | |
1836 if (iod.c.dval == 0) goto divide_by_zero; | |
1837 daccum = (double) iaccum / iod.c.dval; | |
1838 goto divide_floats; | |
1839 } | |
1840 } | |
1841 | |
1842 return make_int (iaccum); | |
1843 | |
1844 divide_floats: | |
1845 for (; args < args_end; args++) | |
1846 { | |
1847 double dval = number_char_or_marker_to_double (*args); | |
1848 if (dval == 0) goto divide_by_zero; | |
1849 daccum /= dval; | |
1850 } | |
1851 return make_float (daccum); | |
1983 | 1852 #endif /* WITH_NUMBER_TYPES */ |
428 | 1853 |
1854 divide_by_zero: | |
1855 Fsignal (Qarith_error, Qnil); | |
801 | 1856 return Qnil; /* not (usually) reached */ |
428 | 1857 } |
1858 | |
1859 DEFUN ("max", Fmax, 1, MANY, 0, /* | |
1860 Return largest of all the arguments. | |
1983 | 1861 All arguments must be real numbers, characters or markers. |
428 | 1862 The value is always a number; markers and characters are converted |
1863 to numbers. | |
1864 */ | |
1865 (int nargs, Lisp_Object *args)) | |
1866 { | |
1983 | 1867 #ifdef WITH_NUMBER_TYPES |
1868 REGISTER int i, maxindex = 0; | |
1869 Lisp_Object comp1, comp2; | |
1870 | |
1871 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0]))) | |
1872 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]); | |
1873 if (CHARP (args[0])) | |
1874 args[0] = make_int (XCHAR (args[0])); | |
1875 else if (MARKERP (args[0])) | |
1876 args[0] = make_int (marker_position (args[0])); | |
1877 for (i = 1; i < nargs; i++) | |
1878 { | |
1879 comp1 = args[maxindex]; | |
1880 comp2 = args[i]; | |
1881 switch (promote_args (&comp1, &comp2)) | |
1882 { | |
1883 case FIXNUM_T: | |
1884 if (XREALINT (comp1) < XREALINT (comp2)) | |
1885 maxindex = i; | |
1886 break; | |
1887 #ifdef HAVE_BIGNUM | |
1888 case BIGNUM_T: | |
1889 if (bignum_lt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2))) | |
1890 maxindex = i; | |
1891 break; | |
1892 #endif | |
1893 #ifdef HAVE_RATIO | |
1894 case RATIO_T: | |
1895 if (ratio_lt (XRATIO_DATA (comp1), XRATIO_DATA (comp2))) | |
1896 maxindex = i; | |
1897 break; | |
1898 #endif | |
1899 case FLOAT_T: | |
1900 if (XFLOAT_DATA (comp1) < XFLOAT_DATA (comp2)) | |
1901 maxindex = i; | |
1902 break; | |
1903 #ifdef HAVE_BIGFLOAT | |
1904 case BIGFLOAT_T: | |
1905 if (bigfloat_lt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2))) | |
1906 maxindex = i; | |
1907 break; | |
1908 #endif | |
1909 } | |
1910 } | |
1911 return args[maxindex]; | |
1912 #else /* !WITH_NUMBER_TYPES */ | |
428 | 1913 EMACS_INT imax; |
1914 double dmax; | |
1915 Lisp_Object *args_end = args + nargs; | |
1916 int_or_double iod; | |
1917 | |
1918 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1919 if (iod.int_p) | |
1920 imax = iod.c.ival; | |
1921 else | |
1922 { | |
1923 dmax = iod.c.dval; | |
1924 goto max_floats; | |
1925 } | |
1926 | |
1927 while (args < args_end) | |
1928 { | |
1929 number_char_or_marker_to_int_or_double (*args++, &iod); | |
1930 if (iod.int_p) | |
1931 { | |
1932 if (imax < iod.c.ival) imax = iod.c.ival; | |
1933 } | |
1934 else | |
1935 { | |
1936 dmax = (double) imax; | |
1937 if (dmax < iod.c.dval) dmax = iod.c.dval; | |
1938 goto max_floats; | |
1939 } | |
1940 } | |
1941 | |
1942 return make_int (imax); | |
1943 | |
1944 max_floats: | |
1945 while (args < args_end) | |
1946 { | |
1947 double dval = number_char_or_marker_to_double (*args++); | |
1948 if (dmax < dval) dmax = dval; | |
1949 } | |
1950 return make_float (dmax); | |
1983 | 1951 #endif /* WITH_NUMBER_TYPES */ |
428 | 1952 } |
1953 | |
1954 DEFUN ("min", Fmin, 1, MANY, 0, /* | |
1955 Return smallest of all the arguments. | |
1956 All arguments must be numbers, characters or markers. | |
1957 The value is always a number; markers and characters are converted | |
1958 to numbers. | |
1959 */ | |
1960 (int nargs, Lisp_Object *args)) | |
1961 { | |
1983 | 1962 #ifdef WITH_NUMBER_TYPES |
1963 REGISTER int i, minindex = 0; | |
1964 Lisp_Object comp1, comp2; | |
1965 | |
1966 while (!(CHARP (args[0]) || MARKERP (args[0]) || REALP (args[0]))) | |
1967 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]); | |
1968 if (CHARP (args[0])) | |
1969 args[0] = make_int (XCHAR (args[0])); | |
1970 else if (MARKERP (args[0])) | |
1971 args[0] = make_int (marker_position (args[0])); | |
1972 for (i = 1; i < nargs; i++) | |
1973 { | |
1974 comp1 = args[minindex]; | |
1975 comp2 = args[i]; | |
1976 switch (promote_args (&comp1, &comp2)) | |
1977 { | |
1978 case FIXNUM_T: | |
1979 if (XREALINT (comp1) > XREALINT (comp2)) | |
1980 minindex = i; | |
1981 break; | |
1982 #ifdef HAVE_BIGNUM | |
1983 case BIGNUM_T: | |
1984 if (bignum_gt (XBIGNUM_DATA (comp1), XBIGNUM_DATA (comp2))) | |
1985 minindex = i; | |
1986 break; | |
1987 #endif | |
1988 #ifdef HAVE_RATIO | |
1989 case RATIO_T: | |
1990 if (ratio_gt (XRATIO_DATA (comp1), XRATIO_DATA (comp2))) | |
1991 minindex = i; | |
1992 break; | |
1993 #endif | |
1994 case FLOAT_T: | |
1995 if (XFLOAT_DATA (comp1) > XFLOAT_DATA (comp2)) | |
1996 minindex = i; | |
1997 break; | |
1998 #ifdef HAVE_BIGFLOAT | |
1999 case BIGFLOAT_T: | |
2000 if (bigfloat_gt (XBIGFLOAT_DATA (comp1), XBIGFLOAT_DATA (comp2))) | |
2001 minindex = i; | |
2002 break; | |
2003 #endif | |
2004 } | |
2005 } | |
2006 return args[minindex]; | |
2007 #else /* !WITH_NUMBER_TYPES */ | |
428 | 2008 EMACS_INT imin; |
2009 double dmin; | |
2010 Lisp_Object *args_end = args + nargs; | |
2011 int_or_double iod; | |
2012 | |
2013 number_char_or_marker_to_int_or_double (*args++, &iod); | |
2014 if (iod.int_p) | |
2015 imin = iod.c.ival; | |
2016 else | |
2017 { | |
2018 dmin = iod.c.dval; | |
2019 goto min_floats; | |
2020 } | |
2021 | |
2022 while (args < args_end) | |
2023 { | |
2024 number_char_or_marker_to_int_or_double (*args++, &iod); | |
2025 if (iod.int_p) | |
2026 { | |
2027 if (imin > iod.c.ival) imin = iod.c.ival; | |
2028 } | |
2029 else | |
2030 { | |
2031 dmin = (double) imin; | |
2032 if (dmin > iod.c.dval) dmin = iod.c.dval; | |
2033 goto min_floats; | |
2034 } | |
2035 } | |
2036 | |
2037 return make_int (imin); | |
2038 | |
2039 min_floats: | |
2040 while (args < args_end) | |
2041 { | |
2042 double dval = number_char_or_marker_to_double (*args++); | |
2043 if (dmin > dval) dmin = dval; | |
2044 } | |
2045 return make_float (dmin); | |
1983 | 2046 #endif /* WITH_NUMBER_TYPES */ |
428 | 2047 } |
2048 | |
2049 DEFUN ("logand", Flogand, 0, MANY, 0, /* | |
2050 Return bitwise-and of all the arguments. | |
2051 Arguments may be integers, or markers or characters converted to integers. | |
2052 */ | |
2053 (int nargs, Lisp_Object *args)) | |
2054 { | |
1983 | 2055 #ifdef HAVE_BIGNUM |
2056 REGISTER int i; | |
2057 Lisp_Object result, other; | |
2058 | |
2059 if (nargs == 0) | |
2060 return make_int (~0); | |
2061 | |
2062 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0]))) | |
2063 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]); | |
2064 | |
2065 result = args[0]; | |
2066 if (CHARP (result)) | |
2067 result = make_int (XCHAR (result)); | |
2068 else if (MARKERP (result)) | |
2069 result = make_int (marker_position (result)); | |
2070 for (i = 1; i < nargs; i++) | |
2071 { | |
2072 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i]))) | |
2073 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]); | |
2074 other = args[i]; | |
1995 | 2075 switch (promote_args (&result, &other)) |
1983 | 2076 { |
2077 case FIXNUM_T: | |
1995 | 2078 result = make_int (XREALINT (result) & XREALINT (other)); |
1983 | 2079 break; |
2080 case BIGNUM_T: | |
2081 bignum_and (scratch_bignum, XBIGNUM_DATA (result), | |
2082 XBIGNUM_DATA (other)); | |
2083 result = make_bignum_bg (scratch_bignum); | |
2084 break; | |
2085 } | |
2086 } | |
2087 return Fcanonicalize_number (result); | |
2088 #else /* !HAVE_BIGNUM */ | |
428 | 2089 EMACS_INT bits = ~0; |
2090 Lisp_Object *args_end = args + nargs; | |
2091 | |
2092 while (args < args_end) | |
2093 bits &= integer_char_or_marker_to_int (*args++); | |
2094 | |
2095 return make_int (bits); | |
1983 | 2096 #endif /* HAVE_BIGNUM */ |
428 | 2097 } |
2098 | |
2099 DEFUN ("logior", Flogior, 0, MANY, 0, /* | |
2100 Return bitwise-or of all the arguments. | |
2101 Arguments may be integers, or markers or characters converted to integers. | |
2102 */ | |
2103 (int nargs, Lisp_Object *args)) | |
2104 { | |
1983 | 2105 #ifdef HAVE_BIGNUM |
2106 REGISTER int i; | |
2107 Lisp_Object result, other; | |
2108 | |
2109 if (nargs == 0) | |
2110 return make_int (0); | |
2111 | |
2112 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0]))) | |
2113 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]); | |
2114 | |
2115 result = args[0]; | |
2116 if (CHARP (result)) | |
2117 result = make_int (XCHAR (result)); | |
2118 else if (MARKERP (result)) | |
2119 result = make_int (marker_position (result)); | |
2120 for (i = 1; i < nargs; i++) | |
2121 { | |
2122 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i]))) | |
2123 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]); | |
2124 other = args[i]; | |
2125 switch (promote_args (&result, &other)) | |
2126 { | |
2127 case FIXNUM_T: | |
1992 | 2128 result = make_int (XREALINT (result) | XREALINT (other)); |
1983 | 2129 break; |
2130 case BIGNUM_T: | |
2131 bignum_ior (scratch_bignum, XBIGNUM_DATA (result), | |
2132 XBIGNUM_DATA (other)); | |
2133 result = make_bignum_bg (scratch_bignum); | |
2134 break; | |
2135 } | |
2136 } | |
2137 return Fcanonicalize_number (result); | |
2138 #else /* !HAVE_BIGNUM */ | |
428 | 2139 EMACS_INT bits = 0; |
2140 Lisp_Object *args_end = args + nargs; | |
2141 | |
2142 while (args < args_end) | |
2143 bits |= integer_char_or_marker_to_int (*args++); | |
2144 | |
2145 return make_int (bits); | |
1983 | 2146 #endif /* HAVE_BIGNUM */ |
428 | 2147 } |
2148 | |
2149 DEFUN ("logxor", Flogxor, 0, MANY, 0, /* | |
2150 Return bitwise-exclusive-or of all the arguments. | |
2151 Arguments may be integers, or markers or characters converted to integers. | |
2152 */ | |
2153 (int nargs, Lisp_Object *args)) | |
2154 { | |
1983 | 2155 #ifdef HAVE_BIGNUM |
2156 REGISTER int i; | |
2157 Lisp_Object result, other; | |
2158 | |
2159 if (nargs == 0) | |
2160 return make_int (0); | |
2161 | |
2162 while (!(CHARP (args[0]) || MARKERP (args[0]) || INTEGERP (args[0]))) | |
2163 args[0] = wrong_type_argument (Qnumber_char_or_marker_p, args[0]); | |
2164 | |
2165 result = args[0]; | |
2166 if (CHARP (result)) | |
2167 result = make_int (XCHAR (result)); | |
2168 else if (MARKERP (result)) | |
2169 result = make_int (marker_position (result)); | |
2170 for (i = 1; i < nargs; i++) | |
2171 { | |
2172 while (!(CHARP (args[i]) || MARKERP (args[i]) || INTEGERP (args[i]))) | |
2173 args[i] = wrong_type_argument (Qnumber_char_or_marker_p, args[i]); | |
2174 other = args[i]; | |
2175 if (promote_args (&result, &other) == FIXNUM_T) | |
2176 { | |
2177 result = make_int (XREALINT (result) ^ XREALINT (other)); | |
2178 } | |
2179 else | |
2180 { | |
2181 bignum_xor (scratch_bignum, XBIGNUM_DATA (result), | |
2182 XBIGNUM_DATA (other)); | |
2183 result = make_bignum_bg (scratch_bignum); | |
2184 } | |
2185 } | |
2186 return Fcanonicalize_number (result); | |
2187 #else /* !HAVE_BIGNUM */ | |
428 | 2188 EMACS_INT bits = 0; |
2189 Lisp_Object *args_end = args + nargs; | |
2190 | |
2191 while (args < args_end) | |
2192 bits ^= integer_char_or_marker_to_int (*args++); | |
2193 | |
2194 return make_int (bits); | |
1983 | 2195 #endif /* !HAVE_BIGNUM */ |
428 | 2196 } |
2197 | |
2198 DEFUN ("lognot", Flognot, 1, 1, 0, /* | |
2199 Return the bitwise complement of NUMBER. | |
2200 NUMBER may be an integer, marker or character converted to integer. | |
2201 */ | |
2202 (number)) | |
2203 { | |
1983 | 2204 #ifdef HAVE_BIGNUM |
2205 if (BIGNUMP (number)) | |
2206 { | |
2207 bignum_not (scratch_bignum, XBIGNUM_DATA (number)); | |
2208 return make_bignum_bg (scratch_bignum); | |
2209 } | |
2210 #endif /* HAVE_BIGNUM */ | |
428 | 2211 return make_int (~ integer_char_or_marker_to_int (number)); |
2212 } | |
2213 | |
2214 DEFUN ("%", Frem, 2, 2, 0, /* | |
2215 Return remainder of first arg divided by second. | |
2216 Both must be integers, characters or markers. | |
2217 */ | |
444 | 2218 (number1, number2)) |
428 | 2219 { |
1983 | 2220 #ifdef HAVE_BIGNUM |
2221 while (!(CHARP (number1) || MARKERP (number1) || INTEGERP (number1))) | |
2222 number1 = wrong_type_argument (Qnumber_char_or_marker_p, number1); | |
2223 while (!(CHARP (number2) || MARKERP (number2) || INTEGERP (number2))) | |
2224 number2 = wrong_type_argument (Qnumber_char_or_marker_p, number2); | |
2225 | |
2226 if (promote_args (&number1, &number2) == FIXNUM_T) | |
2227 { | |
2228 if (XREALINT (number2) == 0) | |
2229 Fsignal (Qarith_error, Qnil); | |
2230 return make_int (XREALINT (number1) % XREALINT (number2)); | |
2231 } | |
2232 else | |
2233 { | |
2234 if (bignum_sign (XBIGNUM_DATA (number2)) == 0) | |
2235 Fsignal (Qarith_error, Qnil); | |
2236 bignum_mod (scratch_bignum, XBIGNUM_DATA (number1), | |
2237 XBIGNUM_DATA (number2)); | |
2238 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
2239 } | |
2240 #else /* !HAVE_BIGNUM */ | |
444 | 2241 EMACS_INT ival1 = integer_char_or_marker_to_int (number1); |
2242 EMACS_INT ival2 = integer_char_or_marker_to_int (number2); | |
428 | 2243 |
2244 if (ival2 == 0) | |
2245 Fsignal (Qarith_error, Qnil); | |
2246 | |
2247 return make_int (ival1 % ival2); | |
1983 | 2248 #endif /* HAVE_BIGNUM */ |
428 | 2249 } |
2250 | |
2251 /* Note, ANSI *requires* the presence of the fmod() library routine. | |
2252 If your system doesn't have it, complain to your vendor, because | |
2253 that is a bug. */ | |
2254 | |
2255 #ifndef HAVE_FMOD | |
2256 double | |
2257 fmod (double f1, double f2) | |
2258 { | |
2259 if (f2 < 0.0) | |
2260 f2 = -f2; | |
2261 return f1 - f2 * floor (f1/f2); | |
2262 } | |
2263 #endif /* ! HAVE_FMOD */ | |
2264 | |
2265 | |
2266 DEFUN ("mod", Fmod, 2, 2, 0, /* | |
2267 Return X modulo Y. | |
2268 The result falls between zero (inclusive) and Y (exclusive). | |
2269 Both X and Y must be numbers, characters or markers. | |
2270 If either argument is a float, a float will be returned. | |
2271 */ | |
2272 (x, y)) | |
2273 { | |
1983 | 2274 #ifdef WITH_NUMBER_TYPES |
2275 while (!(CHARP (x) || MARKERP (x) || REALP (x))) | |
2276 x = wrong_type_argument (Qnumber_char_or_marker_p, x); | |
2277 while (!(CHARP (y) || MARKERP (y) || REALP (y))) | |
2278 y = wrong_type_argument (Qnumber_char_or_marker_p, y); | |
2279 switch (promote_args (&x, &y)) | |
2280 { | |
2281 case FIXNUM_T: | |
2282 { | |
2283 EMACS_INT ival; | |
2284 if (XREALINT (y) == 0) goto divide_by_zero; | |
2285 ival = XREALINT (x) % XREALINT (y); | |
2286 /* If the "remainder" comes out with the wrong sign, fix it. */ | |
2287 if (XREALINT (y) < 0 ? ival > 0 : ival < 0) | |
2288 ival += XREALINT (y); | |
2289 return make_int (ival); | |
2290 } | |
2291 #ifdef HAVE_BIGNUM | |
2292 case BIGNUM_T: | |
2293 if (bignum_sign (XBIGNUM_DATA (y)) == 0) goto divide_by_zero; | |
2294 bignum_mod (scratch_bignum, XBIGNUM_DATA (x), XBIGNUM_DATA (y)); | |
2295 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
2296 #endif | |
2297 #ifdef HAVE_RATIO | |
2298 case RATIO_T: | |
2299 if (ratio_sign (XRATIO_DATA (y)) == 0) goto divide_by_zero; | |
2300 ratio_div (scratch_ratio, XRATIO_DATA (x), XRATIO_DATA (y)); | |
2301 bignum_div (scratch_bignum, ratio_numerator (scratch_ratio), | |
2302 ratio_denominator (scratch_ratio)); | |
2303 ratio_set_bignum (scratch_ratio, scratch_bignum); | |
2304 ratio_mul (scratch_ratio, scratch_ratio, XRATIO_DATA (y)); | |
2305 ratio_sub (scratch_ratio, XRATIO_DATA (x), scratch_ratio); | |
2306 return Fcanonicalize_number (make_ratio_rt (scratch_ratio)); | |
2307 #endif | |
2308 case FLOAT_T: | |
2309 { | |
2310 double dval; | |
2311 if (XFLOAT_DATA (y) == 0.0) goto divide_by_zero; | |
2312 dval = fmod (XFLOAT_DATA (x), XFLOAT_DATA (y)); | |
2313 /* If the "remainder" comes out with the wrong sign, fix it. */ | |
2314 if (XFLOAT_DATA (y) < 0 ? dval > 0 : dval < 0) | |
2315 dval += XFLOAT_DATA (y); | |
2316 return make_float (dval); | |
2317 } | |
2318 #ifdef HAVE_BIGFLOAT | |
2319 case BIGFLOAT_T: | |
2320 bigfloat_set_prec (scratch_bigfloat, | |
2321 max (XBIGFLOAT_GET_PREC (x), XBIGFLOAT_GET_PREC (y))); | |
2322 bigfloat_div (scratch_bigfloat, XBIGFLOAT_DATA (x), XBIGFLOAT_DATA (y)); | |
2323 bigfloat_trunc (scratch_bigfloat, scratch_bigfloat); | |
2324 bigfloat_mul (scratch_bigfloat, scratch_bigfloat, XBIGFLOAT_DATA (y)); | |
2325 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (x), scratch_bigfloat); | |
2326 return make_bigfloat_bf (scratch_bigfloat); | |
2327 #endif | |
2328 } | |
2329 #else /* !WITH_NUMBER_TYPES */ | |
428 | 2330 int_or_double iod1, iod2; |
2331 number_char_or_marker_to_int_or_double (x, &iod1); | |
2332 number_char_or_marker_to_int_or_double (y, &iod2); | |
2333 | |
2334 if (!iod1.int_p || !iod2.int_p) | |
2335 { | |
2336 double dval1 = iod1.int_p ? (double) iod1.c.ival : iod1.c.dval; | |
2337 double dval2 = iod2.int_p ? (double) iod2.c.ival : iod2.c.dval; | |
2338 if (dval2 == 0) goto divide_by_zero; | |
2339 dval1 = fmod (dval1, dval2); | |
2340 | |
2341 /* If the "remainder" comes out with the wrong sign, fix it. */ | |
2342 if (dval2 < 0 ? dval1 > 0 : dval1 < 0) | |
2343 dval1 += dval2; | |
2344 | |
2345 return make_float (dval1); | |
2346 } | |
1104 | 2347 |
428 | 2348 { |
2349 EMACS_INT ival; | |
2350 if (iod2.c.ival == 0) goto divide_by_zero; | |
2351 | |
2352 ival = iod1.c.ival % iod2.c.ival; | |
2353 | |
2354 /* If the "remainder" comes out with the wrong sign, fix it. */ | |
2355 if (iod2.c.ival < 0 ? ival > 0 : ival < 0) | |
2356 ival += iod2.c.ival; | |
2357 | |
2358 return make_int (ival); | |
2359 } | |
1983 | 2360 #endif /* WITH_NUMBER_TYPES */ |
428 | 2361 |
2362 divide_by_zero: | |
2363 Fsignal (Qarith_error, Qnil); | |
801 | 2364 return Qnil; /* not (usually) reached */ |
428 | 2365 } |
2366 | |
2367 DEFUN ("ash", Fash, 2, 2, 0, /* | |
2368 Return VALUE with its bits shifted left by COUNT. | |
2369 If COUNT is negative, shifting is actually to the right. | |
2370 In this case, the sign bit is duplicated. | |
1983 | 2371 This function cannot be applied to bignums, as there is no leftmost sign bit |
2372 to be duplicated. Use `lsh' instead. | |
428 | 2373 */ |
2374 (value, count)) | |
2375 { | |
2376 CHECK_INT_COERCE_CHAR (value); | |
2377 CONCHECK_INT (count); | |
2378 | |
2379 return make_int (XINT (count) > 0 ? | |
2380 XINT (value) << XINT (count) : | |
2381 XINT (value) >> -XINT (count)); | |
2382 } | |
2383 | |
2384 DEFUN ("lsh", Flsh, 2, 2, 0, /* | |
2385 Return VALUE with its bits shifted left by COUNT. | |
2386 If COUNT is negative, shifting is actually to the right. | |
2387 In this case, zeros are shifted in on the left. | |
2388 */ | |
2389 (value, count)) | |
2390 { | |
1983 | 2391 #ifdef HAVE_BIGNUM |
2392 while (!(CHARP (value) || MARKERP (value) || INTEGERP (value))) | |
2393 wrong_type_argument (Qnumber_char_or_marker_p, value); | |
2394 CONCHECK_INTEGER (count); | |
2395 | |
2396 if (promote_args (&value, &count) == FIXNUM_T) | |
2397 { | |
2398 if (XREALINT (count) <= 0) | |
2399 return make_int (XREALINT (value) >> -XREALINT (count)); | |
2400 /* Use bignums to avoid overflow */ | |
2401 bignum_set_long (scratch_bignum2, XREALINT (value)); | |
2402 bignum_lshift (scratch_bignum, scratch_bignum2, XREALINT (count)); | |
2403 return Fcanonicalize_number (make_bignum_bg (scratch_bignum)); | |
2404 } | |
2405 else | |
2406 { | |
2407 if (bignum_sign (XBIGNUM_DATA (count)) <= 0) | |
2408 { | |
2409 bignum_neg (scratch_bignum, XBIGNUM_DATA (count)); | |
2410 if (!bignum_fits_ulong_p (scratch_bignum)) | |
2411 args_out_of_range (Qnumber_char_or_marker_p, count); | |
2412 bignum_rshift (scratch_bignum2, XBIGNUM_DATA (value), | |
2413 bignum_to_ulong (scratch_bignum)); | |
2414 } | |
2415 else | |
2416 { | |
2417 if (!bignum_fits_ulong_p (XBIGNUM_DATA (count))) | |
2418 args_out_of_range (Qnumber_char_or_marker_p, count); | |
2419 bignum_lshift (scratch_bignum2, XBIGNUM_DATA (value), | |
2420 bignum_to_ulong (XBIGNUM_DATA (count))); | |
2421 } | |
2422 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2)); | |
2423 } | |
2424 #else /* !HAVE_BIGNUM */ | |
428 | 2425 CHECK_INT_COERCE_CHAR (value); |
2426 CONCHECK_INT (count); | |
2427 | |
2428 return make_int (XINT (count) > 0 ? | |
2429 XUINT (value) << XINT (count) : | |
2430 XUINT (value) >> -XINT (count)); | |
1983 | 2431 #endif /* HAVE_BIGNUM */ |
428 | 2432 } |
2433 | |
2434 DEFUN ("1+", Fadd1, 1, 1, 0, /* | |
2435 Return NUMBER plus one. NUMBER may be a number, character or marker. | |
2436 Markers and characters are converted to integers. | |
2437 */ | |
2438 (number)) | |
2439 { | |
2440 retry: | |
2441 | |
1983 | 2442 if (INTP (number)) return make_integer (XINT (number) + 1); |
2443 if (CHARP (number)) return make_integer (XCHAR (number) + 1); | |
2444 if (MARKERP (number)) return make_integer (marker_position (number) + 1); | |
428 | 2445 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) + 1.0); |
1983 | 2446 #ifdef HAVE_BIGNUM |
2447 if (BIGNUMP (number)) | |
2448 { | |
2449 bignum_set_long (scratch_bignum, 1L); | |
2450 bignum_add (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum); | |
2451 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2)); | |
2452 } | |
2453 #endif | |
2454 #ifdef HAVE_RATIO | |
2455 if (RATIOP (number)) | |
2456 { | |
2457 ratio_set_long (scratch_ratio, 1L); | |
2458 ratio_add (scratch_ratio, XRATIO_DATA (number), scratch_ratio); | |
2459 /* No need to canonicalize after adding 1 */ | |
2460 return make_ratio_rt (scratch_ratio); | |
2461 } | |
2462 #endif | |
2463 #ifdef HAVE_BIGFLOAT | |
2464 if (BIGFLOATP (number)) | |
2465 { | |
2466 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number)); | |
2467 bigfloat_set_long (scratch_bigfloat, 1L); | |
2468 bigfloat_add (scratch_bigfloat, XBIGFLOAT_DATA (number), | |
2469 scratch_bigfloat); | |
2470 return make_bigfloat_bf (scratch_bigfloat); | |
2471 } | |
2472 #endif | |
428 | 2473 |
2474 number = wrong_type_argument (Qnumber_char_or_marker_p, number); | |
2475 goto retry; | |
2476 } | |
2477 | |
2478 DEFUN ("1-", Fsub1, 1, 1, 0, /* | |
2479 Return NUMBER minus one. NUMBER may be a number, character or marker. | |
2480 Markers and characters are converted to integers. | |
2481 */ | |
2482 (number)) | |
2483 { | |
2484 retry: | |
2485 | |
1983 | 2486 if (INTP (number)) return make_integer (XINT (number) - 1); |
2487 if (CHARP (number)) return make_integer (XCHAR (number) - 1); | |
2488 if (MARKERP (number)) return make_integer (marker_position (number) - 1); | |
428 | 2489 if (FLOATP (number)) return make_float (XFLOAT_DATA (number) - 1.0); |
1983 | 2490 #ifdef HAVE_BIGNUM |
2491 if (BIGNUMP (number)) | |
2492 { | |
2493 bignum_set_long (scratch_bignum, 1L); | |
2494 bignum_sub (scratch_bignum2, XBIGNUM_DATA (number), scratch_bignum); | |
2495 return Fcanonicalize_number (make_bignum_bg (scratch_bignum2)); | |
2496 } | |
2497 #endif | |
2498 #ifdef HAVE_RATIO | |
2499 if (RATIOP (number)) | |
2500 { | |
2501 ratio_set_long (scratch_ratio, 1L); | |
2502 ratio_sub (scratch_ratio, XRATIO_DATA (number), scratch_ratio); | |
2503 /* No need to canonicalize after subtracting 1 */ | |
2504 return make_ratio_rt (scratch_ratio); | |
2505 } | |
2506 #endif | |
2507 #ifdef HAVE_BIGFLOAT | |
2508 if (BIGFLOATP (number)) | |
2509 { | |
2510 bigfloat_set_prec (scratch_bigfloat, XBIGFLOAT_GET_PREC (number)); | |
2511 bigfloat_set_long (scratch_bigfloat, 1L); | |
2512 bigfloat_sub (scratch_bigfloat, XBIGFLOAT_DATA (number), | |
2513 scratch_bigfloat); | |
2514 return make_bigfloat_bf (scratch_bigfloat); | |
2515 } | |
2516 #endif | |
428 | 2517 |
2518 number = wrong_type_argument (Qnumber_char_or_marker_p, number); | |
2519 goto retry; | |
2520 } | |
2521 | |
2522 | |
2523 /************************************************************************/ | |
2524 /* weak lists */ | |
2525 /************************************************************************/ | |
2526 | |
2527 /* A weak list is like a normal list except that elements automatically | |
2528 disappear when no longer in use, i.e. when no longer GC-protected. | |
2529 The basic idea is that we don't mark the elements during GC, but | |
2530 wait for them to be marked elsewhere. If they're not marked, we | |
2531 remove them. This is analogous to weak hash tables; see the explanation | |
2532 there for more info. */ | |
2533 | |
2534 static Lisp_Object Vall_weak_lists; /* Gemarke es nicht!!! */ | |
2535 | |
2536 static Lisp_Object encode_weak_list_type (enum weak_list_type type); | |
2537 | |
2538 static Lisp_Object | |
2286 | 2539 mark_weak_list (Lisp_Object UNUSED (obj)) |
428 | 2540 { |
2541 return Qnil; /* nichts ist gemarkt */ | |
2542 } | |
2543 | |
2544 static void | |
2286 | 2545 print_weak_list (Lisp_Object obj, Lisp_Object printcharfun, |
2546 int UNUSED (escapeflag)) | |
428 | 2547 { |
2548 if (print_readably) | |
563 | 2549 printing_unreadable_object ("#<weak-list>"); |
428 | 2550 |
800 | 2551 write_fmt_string_lisp (printcharfun, "#<weak-list %s %S>", 2, |
2552 encode_weak_list_type (XWEAK_LIST (obj)->type), | |
2553 XWEAK_LIST (obj)->list); | |
428 | 2554 } |
2555 | |
2556 static int | |
2557 weak_list_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
2558 { | |
2559 struct weak_list *w1 = XWEAK_LIST (obj1); | |
2560 struct weak_list *w2 = XWEAK_LIST (obj2); | |
2561 | |
2562 return ((w1->type == w2->type) && | |
2563 internal_equal (w1->list, w2->list, depth + 1)); | |
2564 } | |
2565 | |
665 | 2566 static Hashcode |
428 | 2567 weak_list_hash (Lisp_Object obj, int depth) |
2568 { | |
2569 struct weak_list *w = XWEAK_LIST (obj); | |
2570 | |
665 | 2571 return HASH2 ((Hashcode) w->type, |
428 | 2572 internal_hash (w->list, depth + 1)); |
2573 } | |
2574 | |
2575 Lisp_Object | |
2576 make_weak_list (enum weak_list_type type) | |
2577 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
2578 Lisp_Object result = ALLOC_LISP_OBJECT (weak_list); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
2579 struct weak_list *wl = XWEAK_LIST (result); |
428 | 2580 |
2581 wl->list = Qnil; | |
2582 wl->type = type; | |
2583 wl->next_weak = Vall_weak_lists; | |
2584 Vall_weak_lists = result; | |
2585 return result; | |
2586 } | |
2587 | |
1204 | 2588 static const struct memory_description weak_list_description[] = { |
1598 | 2589 { XD_LISP_OBJECT, offsetof (struct weak_list, list), |
2551 | 2590 0, { 0 }, XD_FLAG_NO_KKCC }, |
1598 | 2591 { XD_LO_LINK, offsetof (struct weak_list, next_weak), |
2551 | 2592 0, { 0 }, XD_FLAG_NO_KKCC }, |
428 | 2593 { XD_END } |
2594 }; | |
2595 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
2596 DEFINE_LISP_OBJECT ("weak-list", weak_list, |
934 | 2597 mark_weak_list, print_weak_list, |
2598 0, weak_list_equal, weak_list_hash, | |
2599 weak_list_description, | |
2600 struct weak_list); | |
428 | 2601 /* |
2602 -- we do not mark the list elements (either the elements themselves | |
2603 or the cons cells that hold them) in the normal marking phase. | |
2604 -- at the end of marking, we go through all weak lists that are | |
2605 marked, and mark the cons cells that hold all marked | |
2606 objects, and possibly parts of the objects themselves. | |
2607 (See alloc.c, "after-mark".) | |
2608 -- after that, we prune away all the cons cells that are not marked. | |
2609 | |
2610 WARNING WARNING WARNING WARNING WARNING: | |
2611 | |
2612 The code in the following two functions is *unbelievably* tricky. | |
2613 Don't mess with it. You'll be sorry. | |
2614 | |
2615 Linked lists just majorly suck, d'ya know? | |
2616 */ | |
2617 | |
2618 int | |
2619 finish_marking_weak_lists (void) | |
2620 { | |
2621 Lisp_Object rest; | |
2622 int did_mark = 0; | |
2623 | |
2624 for (rest = Vall_weak_lists; | |
2625 !NILP (rest); | |
2626 rest = XWEAK_LIST (rest)->next_weak) | |
2627 { | |
2628 Lisp_Object rest2; | |
2629 enum weak_list_type type = XWEAK_LIST (rest)->type; | |
2630 | |
2631 if (! marked_p (rest)) | |
2632 /* The weak list is probably garbage. Ignore it. */ | |
2633 continue; | |
2634 | |
2635 for (rest2 = XWEAK_LIST (rest)->list; | |
2636 /* We need to be trickier since we're inside of GC; | |
2637 use CONSP instead of !NILP in case of user-visible | |
2638 imperfect lists */ | |
2639 CONSP (rest2); | |
2640 rest2 = XCDR (rest2)) | |
2641 { | |
2642 Lisp_Object elem; | |
2643 /* If the element is "marked" (meaning depends on the type | |
2644 of weak list), we need to mark the cons containing the | |
2645 element, and maybe the element itself (if only some part | |
2646 was already marked). */ | |
2647 int need_to_mark_cons = 0; | |
2648 int need_to_mark_elem = 0; | |
2649 | |
2650 /* If a cons is already marked, then its car is already marked | |
2651 (either because of an external pointer or because of | |
2652 a previous call to this function), and likewise for all | |
2653 the rest of the elements in the list, so we can stop now. */ | |
2654 if (marked_p (rest2)) | |
2655 break; | |
2656 | |
2657 elem = XCAR (rest2); | |
2658 | |
2659 switch (type) | |
2660 { | |
2661 case WEAK_LIST_SIMPLE: | |
2662 if (marked_p (elem)) | |
2663 need_to_mark_cons = 1; | |
2664 break; | |
2665 | |
2666 case WEAK_LIST_ASSOC: | |
2667 if (!CONSP (elem)) | |
2668 { | |
2669 /* just leave bogus elements there */ | |
2670 need_to_mark_cons = 1; | |
2671 need_to_mark_elem = 1; | |
2672 } | |
2673 else if (marked_p (XCAR (elem)) && | |
2674 marked_p (XCDR (elem))) | |
2675 { | |
2676 need_to_mark_cons = 1; | |
2677 /* We still need to mark elem, because it's | |
2678 probably not marked. */ | |
2679 need_to_mark_elem = 1; | |
2680 } | |
2681 break; | |
2682 | |
2683 case WEAK_LIST_KEY_ASSOC: | |
2684 if (!CONSP (elem)) | |
2685 { | |
2686 /* just leave bogus elements there */ | |
2687 need_to_mark_cons = 1; | |
2688 need_to_mark_elem = 1; | |
2689 } | |
2690 else if (marked_p (XCAR (elem))) | |
2691 { | |
2692 need_to_mark_cons = 1; | |
2693 /* We still need to mark elem and XCDR (elem); | |
2694 marking elem does both */ | |
2695 need_to_mark_elem = 1; | |
2696 } | |
2697 break; | |
2698 | |
2699 case WEAK_LIST_VALUE_ASSOC: | |
2700 if (!CONSP (elem)) | |
2701 { | |
2702 /* just leave bogus elements there */ | |
2703 need_to_mark_cons = 1; | |
2704 need_to_mark_elem = 1; | |
2705 } | |
2706 else if (marked_p (XCDR (elem))) | |
2707 { | |
2708 need_to_mark_cons = 1; | |
2709 /* We still need to mark elem and XCAR (elem); | |
2710 marking elem does both */ | |
2711 need_to_mark_elem = 1; | |
2712 } | |
2713 break; | |
2714 | |
442 | 2715 case WEAK_LIST_FULL_ASSOC: |
2716 if (!CONSP (elem)) | |
2717 { | |
2718 /* just leave bogus elements there */ | |
2719 need_to_mark_cons = 1; | |
2720 need_to_mark_elem = 1; | |
2721 } | |
2722 else if (marked_p (XCAR (elem)) || | |
2723 marked_p (XCDR (elem))) | |
2724 { | |
2725 need_to_mark_cons = 1; | |
2726 /* We still need to mark elem and XCAR (elem); | |
2727 marking elem does both */ | |
2728 need_to_mark_elem = 1; | |
2729 } | |
2730 break; | |
2731 | |
428 | 2732 default: |
2500 | 2733 ABORT (); |
428 | 2734 } |
2735 | |
2736 if (need_to_mark_elem && ! marked_p (elem)) | |
2737 { | |
1598 | 2738 #ifdef USE_KKCC |
2645 | 2739 kkcc_gc_stack_push_lisp_object (elem, 0, -1); |
1598 | 2740 #else /* NOT USE_KKCC */ |
428 | 2741 mark_object (elem); |
1598 | 2742 #endif /* NOT USE_KKCC */ |
428 | 2743 did_mark = 1; |
2744 } | |
2745 | |
2746 /* We also need to mark the cons that holds the elem or | |
2747 assoc-pair. We do *not* want to call (mark_object) here | |
2748 because that will mark the entire list; we just want to | |
2749 mark the cons itself. | |
2750 */ | |
2751 if (need_to_mark_cons) | |
2752 { | |
2753 Lisp_Cons *c = XCONS (rest2); | |
2754 if (!CONS_MARKED_P (c)) | |
2755 { | |
2756 MARK_CONS (c); | |
2757 did_mark = 1; | |
2758 } | |
2759 } | |
2760 } | |
2761 | |
2762 /* In case of imperfect list, need to mark the final cons | |
2763 because we're not removing it */ | |
2764 if (!NILP (rest2) && ! marked_p (rest2)) | |
2765 { | |
1598 | 2766 #ifdef USE_KKCC |
2645 | 2767 kkcc_gc_stack_push_lisp_object (rest2, 0, -1); |
1598 | 2768 #else /* NOT USE_KKCC */ |
428 | 2769 mark_object (rest2); |
1598 | 2770 #endif /* NOT USE_KKCC */ |
428 | 2771 did_mark = 1; |
2772 } | |
2773 } | |
2774 | |
2775 return did_mark; | |
2776 } | |
2777 | |
2778 void | |
2779 prune_weak_lists (void) | |
2780 { | |
2781 Lisp_Object rest, prev = Qnil; | |
2782 | |
2783 for (rest = Vall_weak_lists; | |
2784 !NILP (rest); | |
2785 rest = XWEAK_LIST (rest)->next_weak) | |
2786 { | |
2787 if (! (marked_p (rest))) | |
2788 { | |
2789 /* This weak list itself is garbage. Remove it from the list. */ | |
2790 if (NILP (prev)) | |
2791 Vall_weak_lists = XWEAK_LIST (rest)->next_weak; | |
2792 else | |
2793 XWEAK_LIST (prev)->next_weak = | |
2794 XWEAK_LIST (rest)->next_weak; | |
2795 } | |
2796 else | |
2797 { | |
2798 Lisp_Object rest2, prev2 = Qnil; | |
2799 Lisp_Object tortoise; | |
2800 int go_tortoise = 0; | |
2801 | |
2802 for (rest2 = XWEAK_LIST (rest)->list, tortoise = rest2; | |
2803 /* We need to be trickier since we're inside of GC; | |
2804 use CONSP instead of !NILP in case of user-visible | |
2805 imperfect lists */ | |
2806 CONSP (rest2);) | |
2807 { | |
2808 /* It suffices to check the cons for marking, | |
2809 regardless of the type of weak list: | |
2810 | |
2811 -- if the cons is pointed to somewhere else, | |
2812 then it should stay around and will be marked. | |
2813 -- otherwise, if it should stay around, it will | |
2814 have been marked in finish_marking_weak_lists(). | |
2815 -- otherwise, it's not marked and should disappear. | |
2816 */ | |
2817 if (! marked_p (rest2)) | |
2818 { | |
2819 /* bye bye :-( */ | |
2820 if (NILP (prev2)) | |
2821 XWEAK_LIST (rest)->list = XCDR (rest2); | |
2822 else | |
2823 XCDR (prev2) = XCDR (rest2); | |
2824 rest2 = XCDR (rest2); | |
2825 /* Ouch. Circularity checking is even trickier | |
2826 than I thought. When we cut out a link | |
2827 like this, we can't advance the turtle or | |
2828 it'll catch up to us. Imagine that we're | |
2829 standing on floor tiles and moving forward -- | |
2830 what we just did here is as if the floor | |
2831 tile under us just disappeared and all the | |
2832 ones ahead of us slid one tile towards us. | |
2833 In other words, we didn't move at all; | |
2834 if the tortoise was one step behind us | |
2835 previously, it still is, and therefore | |
2836 it must not move. */ | |
2837 } | |
2838 else | |
2839 { | |
2840 prev2 = rest2; | |
2841 | |
2842 /* Implementing circularity checking is trickier here | |
2843 than in other places because we have to guarantee | |
2844 that we've processed all elements before exiting | |
2845 due to a circularity. (In most places, an error | |
2846 is issued upon encountering a circularity, so it | |
2847 doesn't really matter if all elements are processed.) | |
2848 The idea is that we process along with the hare | |
2849 rather than the tortoise. If at any point in | |
2850 our forward process we encounter the tortoise, | |
2851 we must have already visited the spot, so we exit. | |
2852 (If we process with the tortoise, we can fail to | |
2853 process cases where a cons points to itself, or | |
2854 where cons A points to cons B, which points to | |
2855 cons A.) */ | |
2856 | |
2857 rest2 = XCDR (rest2); | |
2858 if (go_tortoise) | |
2859 tortoise = XCDR (tortoise); | |
2860 go_tortoise = !go_tortoise; | |
2861 if (EQ (rest2, tortoise)) | |
2862 break; | |
2863 } | |
2864 } | |
2865 | |
2866 prev = rest; | |
2867 } | |
2868 } | |
2869 } | |
2870 | |
2871 static enum weak_list_type | |
2872 decode_weak_list_type (Lisp_Object symbol) | |
2873 { | |
2874 CHECK_SYMBOL (symbol); | |
2875 if (EQ (symbol, Qsimple)) return WEAK_LIST_SIMPLE; | |
2876 if (EQ (symbol, Qassoc)) return WEAK_LIST_ASSOC; | |
2877 if (EQ (symbol, Qold_assoc)) return WEAK_LIST_ASSOC; /* EBOLA ALERT! */ | |
2878 if (EQ (symbol, Qkey_assoc)) return WEAK_LIST_KEY_ASSOC; | |
2879 if (EQ (symbol, Qvalue_assoc)) return WEAK_LIST_VALUE_ASSOC; | |
442 | 2880 if (EQ (symbol, Qfull_assoc)) return WEAK_LIST_FULL_ASSOC; |
428 | 2881 |
563 | 2882 invalid_constant ("Invalid weak list type", symbol); |
1204 | 2883 RETURN_NOT_REACHED (WEAK_LIST_SIMPLE); |
428 | 2884 } |
2885 | |
2886 static Lisp_Object | |
2887 encode_weak_list_type (enum weak_list_type type) | |
2888 { | |
2889 switch (type) | |
2890 { | |
2891 case WEAK_LIST_SIMPLE: return Qsimple; | |
2892 case WEAK_LIST_ASSOC: return Qassoc; | |
2893 case WEAK_LIST_KEY_ASSOC: return Qkey_assoc; | |
2894 case WEAK_LIST_VALUE_ASSOC: return Qvalue_assoc; | |
442 | 2895 case WEAK_LIST_FULL_ASSOC: return Qfull_assoc; |
428 | 2896 default: |
2500 | 2897 ABORT (); |
428 | 2898 } |
2899 | |
801 | 2900 return Qnil; /* not (usually) reached */ |
428 | 2901 } |
2902 | |
2903 DEFUN ("weak-list-p", Fweak_list_p, 1, 1, 0, /* | |
2904 Return non-nil if OBJECT is a weak list. | |
2905 */ | |
2906 (object)) | |
2907 { | |
2908 return WEAK_LISTP (object) ? Qt : Qnil; | |
2909 } | |
2910 | |
2911 DEFUN ("make-weak-list", Fmake_weak_list, 0, 1, 0, /* | |
2912 Return a new weak list object of type TYPE. | |
2913 A weak list object is an object that contains a list. This list behaves | |
2914 like any other list except that its elements do not count towards | |
456 | 2915 garbage collection -- if the only pointer to an object is inside a weak |
428 | 2916 list (other than pointers in similar objects such as weak hash tables), |
2917 the object is garbage collected and automatically removed from the list. | |
2918 This is used internally, for example, to manage the list holding the | |
2919 children of an extent -- an extent that is unused but has a parent will | |
2920 still be reclaimed, and will automatically be removed from its parent's | |
2921 list of children. | |
2922 | |
2923 Optional argument TYPE specifies the type of the weak list, and defaults | |
2924 to `simple'. Recognized types are | |
2925 | |
2926 `simple' Objects in the list disappear if not pointed to. | |
2927 `assoc' Objects in the list disappear if they are conses | |
2928 and either the car or the cdr of the cons is not | |
2929 pointed to. | |
2930 `key-assoc' Objects in the list disappear if they are conses | |
2931 and the car is not pointed to. | |
2932 `value-assoc' Objects in the list disappear if they are conses | |
2933 and the cdr is not pointed to. | |
442 | 2934 `full-assoc' Objects in the list disappear if they are conses |
2935 and neither the car nor the cdr is pointed to. | |
428 | 2936 */ |
2937 (type)) | |
2938 { | |
2939 if (NILP (type)) | |
2940 type = Qsimple; | |
2941 | |
2942 return make_weak_list (decode_weak_list_type (type)); | |
2943 } | |
2944 | |
2945 DEFUN ("weak-list-type", Fweak_list_type, 1, 1, 0, /* | |
2946 Return the type of the given weak-list object. | |
2947 */ | |
2948 (weak)) | |
2949 { | |
2950 CHECK_WEAK_LIST (weak); | |
2951 return encode_weak_list_type (XWEAK_LIST (weak)->type); | |
2952 } | |
2953 | |
2954 DEFUN ("weak-list-list", Fweak_list_list, 1, 1, 0, /* | |
2955 Return the list contained in a weak-list object. | |
2956 */ | |
2957 (weak)) | |
2958 { | |
2959 CHECK_WEAK_LIST (weak); | |
2960 return XWEAK_LIST_LIST (weak); | |
2961 } | |
2962 | |
2963 DEFUN ("set-weak-list-list", Fset_weak_list_list, 2, 2, 0, /* | |
2964 Change the list contained in a weak-list object. | |
2965 */ | |
2966 (weak, new_list)) | |
2967 { | |
2968 CHECK_WEAK_LIST (weak); | |
2969 XWEAK_LIST_LIST (weak) = new_list; | |
2970 return new_list; | |
2971 } | |
2972 | |
888 | 2973 |
858 | 2974 /************************************************************************/ |
2975 /* weak boxes */ | |
2976 /************************************************************************/ | |
2977 | |
2978 static Lisp_Object Vall_weak_boxes; /* Gemarke es niemals ever!!! */ | |
2979 | |
2980 void | |
2981 prune_weak_boxes (void) | |
2982 { | |
2983 Lisp_Object rest, prev = Qnil; | |
888 | 2984 int removep = 0; |
858 | 2985 |
2986 for (rest = Vall_weak_boxes; | |
2987 !NILP(rest); | |
2988 rest = XWEAK_BOX (rest)->next_weak_box) | |
2989 { | |
2990 if (! (marked_p (rest))) | |
888 | 2991 /* This weak box itself is garbage. */ |
2992 removep = 1; | |
2993 | |
2994 if (! marked_p (XWEAK_BOX (rest)->value)) | |
2995 { | |
2996 XSET_WEAK_BOX (rest, Qnil); | |
2997 removep = 1; | |
2998 } | |
2999 | |
3000 if (removep) | |
3001 { | |
3002 /* Remove weak box from list. */ | |
3003 if (NILP (prev)) | |
3004 Vall_weak_boxes = XWEAK_BOX (rest)->next_weak_box; | |
3005 else | |
3006 XWEAK_BOX (prev)->next_weak_box = XWEAK_BOX (rest)->next_weak_box; | |
3007 removep = 0; | |
3008 } | |
3009 else | |
3010 prev = rest; | |
858 | 3011 } |
3012 } | |
3013 | |
3014 static Lisp_Object | |
2286 | 3015 mark_weak_box (Lisp_Object UNUSED (obj)) |
858 | 3016 { |
3017 return Qnil; | |
3018 } | |
3019 | |
3020 static void | |
2286 | 3021 print_weak_box (Lisp_Object UNUSED (obj), Lisp_Object printcharfun, |
3022 int UNUSED (escapeflag)) | |
858 | 3023 { |
3024 if (print_readably) | |
3025 printing_unreadable_object ("#<weak_box>"); | |
3026 write_fmt_string (printcharfun, "#<weak_box>"); | |
3027 } | |
3028 | |
3029 static int | |
3030 weak_box_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
3031 { | |
888 | 3032 struct weak_box *wb1 = XWEAK_BOX (obj1); |
3033 struct weak_box *wb2 = XWEAK_BOX (obj2); | |
858 | 3034 |
888 | 3035 return (internal_equal (wb1->value, wb2->value, depth + 1)); |
858 | 3036 } |
3037 | |
3038 static Hashcode | |
3039 weak_box_hash (Lisp_Object obj, int depth) | |
3040 { | |
888 | 3041 struct weak_box *wb = XWEAK_BOX (obj); |
858 | 3042 |
888 | 3043 return internal_hash (wb->value, depth + 1); |
858 | 3044 } |
3045 | |
3046 Lisp_Object | |
3047 make_weak_box (Lisp_Object value) | |
3048 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3049 Lisp_Object result = ALLOC_LISP_OBJECT (weak_box); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3050 struct weak_box *wb = XWEAK_BOX (result); |
858 | 3051 |
3052 wb->value = value; | |
3053 result = wrap_weak_box (wb); | |
3054 wb->next_weak_box = Vall_weak_boxes; | |
3055 Vall_weak_boxes = result; | |
3056 return result; | |
3057 } | |
3058 | |
1204 | 3059 static const struct memory_description weak_box_description[] = { |
858 | 3060 { XD_LO_LINK, offsetof (struct weak_box, value) }, |
888 | 3061 { XD_END} |
858 | 3062 }; |
3063 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3064 DEFINE_NONDUMPABLE_LISP_OBJECT ("weak-box", weak_box, mark_weak_box, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3065 print_weak_box, 0, weak_box_equal, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3066 weak_box_hash, weak_box_description, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3067 struct weak_box); |
858 | 3068 |
3069 DEFUN ("make-weak-box", Fmake_weak_box, 1, 1, 0, /* | |
3070 Return a new weak box from value CONTENTS. | |
3071 The weak box is a reference to CONTENTS which may be extracted with | |
3072 `weak-box-ref'. However, the weak box does not contribute to the | |
3073 reachability of CONTENTS. When CONTENTS is garbage-collected, | |
3074 `weak-box-ref' will return NIL. | |
3075 */ | |
3076 (value)) | |
3077 { | |
3078 return make_weak_box(value); | |
3079 } | |
3080 | |
3081 DEFUN ("weak-box-ref", Fweak_box_ref, 1, 1, 0, /* | |
3082 Return the contents of weak box WEAK-BOX. | |
3083 If the contents have been GCed, return NIL. | |
3084 */ | |
888 | 3085 (wb)) |
858 | 3086 { |
888 | 3087 return XWEAK_BOX (wb)->value; |
858 | 3088 } |
3089 | |
3090 DEFUN ("weak-box-p", Fweak_boxp, 1, 1, 0, /* | |
3091 Return non-nil if OBJECT is a weak box. | |
3092 */ | |
3093 (object)) | |
3094 { | |
3095 return WEAK_BOXP (object) ? Qt : Qnil; | |
3096 } | |
3097 | |
888 | 3098 /************************************************************************/ |
3099 /* ephemerons */ | |
3100 /************************************************************************/ | |
3101 | |
993 | 3102 /* The concept of ephemerons is due to: |
3103 * Barry Hayes: Ephemerons: A New Finalization Mechanism. OOPSLA 1997: 176-183 | |
3104 * The original idea is due to George Bosworth of Digitalk, Inc. | |
3105 * | |
3106 * For a discussion of finalization and weakness that also reviews | |
3107 * ephemerons, refer to: | |
3108 * Simon Peyton Jones, Simon Marlow, Conal Elliot: | |
3109 * Stretching the storage manager | |
3110 * Implementation of Functional Languages, 1999 | |
3111 */ | |
3112 | |
888 | 3113 static Lisp_Object Vall_ephemerons; /* Gemarke es niemals ever!!! */ |
1590 | 3114 static Lisp_Object Vnew_all_ephemerons; |
888 | 3115 static Lisp_Object Vfinalize_list; |
3116 | |
1590 | 3117 void |
3118 init_marking_ephemerons(void) | |
3119 { | |
3120 Vnew_all_ephemerons = Qnil; | |
3121 } | |
3122 | |
3123 /* Move all live ephemerons with live keys over to | |
3124 * Vnew_all_ephemerons, marking the values and finalizers along the | |
3125 * way. */ | |
3126 | |
3127 int | |
3128 continue_marking_ephemerons(void) | |
3129 { | |
3130 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil; | |
3131 int did_mark = 0; | |
3132 | |
3133 while (!NILP (rest)) | |
3134 { | |
3135 next = XEPHEMERON_NEXT (rest); | |
3136 | |
3137 if (marked_p (rest)) | |
3138 { | |
3139 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain)); | |
3140 if (marked_p (XEPHEMERON (rest)->key)) | |
3141 { | |
1598 | 3142 #ifdef USE_KKCC |
3143 kkcc_gc_stack_push_lisp_object | |
2645 | 3144 (XCAR (XEPHEMERON (rest)->cons_chain), 0, -1); |
1598 | 3145 #else /* NOT USE_KKCC */ |
1590 | 3146 mark_object (XCAR (XEPHEMERON (rest)->cons_chain)); |
1598 | 3147 #endif /* NOT USE_KKCC */ |
1590 | 3148 did_mark = 1; |
3149 XSET_EPHEMERON_NEXT (rest, Vnew_all_ephemerons); | |
3150 Vnew_all_ephemerons = rest; | |
3151 if (NILP (prev)) | |
3152 Vall_ephemerons = next; | |
3153 else | |
3154 XSET_EPHEMERON_NEXT (prev, next); | |
3155 } | |
3156 else | |
3157 prev = rest; | |
3158 } | |
3159 else | |
3160 prev = rest; | |
3161 | |
3162 rest = next; | |
3163 } | |
3164 | |
3165 return did_mark; | |
3166 } | |
3167 | |
3168 /* At this point, everything that's in Vall_ephemerons is dead. | |
3169 * Well, almost: we still need to run the finalizers, so we need to | |
3170 * resurrect them. | |
3171 */ | |
3172 | |
888 | 3173 int |
3174 finish_marking_ephemerons(void) | |
3175 { | |
1590 | 3176 Lisp_Object rest = Vall_ephemerons, next, prev = Qnil; |
888 | 3177 int did_mark = 0; |
3178 | |
3179 while (! NILP (rest)) | |
3180 { | |
3181 next = XEPHEMERON_NEXT (rest); | |
3182 | |
3183 if (marked_p (rest)) | |
1590 | 3184 /* The ephemeron itself is live, but its key is garbage */ |
888 | 3185 { |
1590 | 3186 /* tombstone */ |
3187 XSET_EPHEMERON_VALUE (rest, Qnil); | |
3188 | |
3189 if (! NILP (XEPHEMERON_FINALIZER (rest))) | |
888 | 3190 { |
1590 | 3191 MARK_CONS (XCONS (XEPHEMERON (rest)->cons_chain)); |
1598 | 3192 #ifdef USE_KKCC |
3193 kkcc_gc_stack_push_lisp_object | |
2645 | 3194 (XCAR (XEPHEMERON (rest)->cons_chain), 0, -1); |
1598 | 3195 #else /* NOT USE_KKCC */ |
1590 | 3196 mark_object (XCAR (XEPHEMERON (rest)->cons_chain)); |
1598 | 3197 #endif /* NOT USE_KKCC */ |
1590 | 3198 |
3199 /* Register the finalizer */ | |
3200 XSET_EPHEMERON_NEXT (rest, Vfinalize_list); | |
3201 Vfinalize_list = XEPHEMERON (rest)->cons_chain; | |
3202 did_mark = 1; | |
888 | 3203 } |
3204 | |
3205 /* Remove it from the list. */ | |
3206 if (NILP (prev)) | |
3207 Vall_ephemerons = next; | |
3208 else | |
3209 XSET_EPHEMERON_NEXT (prev, next); | |
3210 } | |
3211 else | |
3212 prev = rest; | |
3213 | |
3214 rest = next; | |
3215 } | |
1590 | 3216 |
3217 return did_mark; | |
3218 } | |
3219 | |
3220 void | |
3221 prune_ephemerons(void) | |
3222 { | |
3223 Vall_ephemerons = Vnew_all_ephemerons; | |
888 | 3224 } |
3225 | |
3226 Lisp_Object | |
3227 zap_finalize_list(void) | |
3228 { | |
3229 Lisp_Object finalizers = Vfinalize_list; | |
3230 | |
3231 Vfinalize_list = Qnil; | |
3232 | |
3233 return finalizers; | |
3234 } | |
3235 | |
3236 static Lisp_Object | |
2286 | 3237 mark_ephemeron (Lisp_Object UNUSED (obj)) |
888 | 3238 { |
3239 return Qnil; | |
3240 } | |
3241 | |
3242 static void | |
2286 | 3243 print_ephemeron (Lisp_Object UNUSED (obj), Lisp_Object printcharfun, |
3244 int UNUSED (escapeflag)) | |
888 | 3245 { |
3246 if (print_readably) | |
3247 printing_unreadable_object ("#<ephemeron>"); | |
3248 write_fmt_string (printcharfun, "#<ephemeron>"); | |
3249 } | |
3250 | |
3251 static int | |
3252 ephemeron_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
3253 { | |
3254 return | |
3255 internal_equal (XEPHEMERON_REF (obj1), XEPHEMERON_REF(obj2), depth + 1); | |
3256 } | |
3257 | |
3258 static Hashcode | |
3259 ephemeron_hash(Lisp_Object obj, int depth) | |
3260 { | |
3261 return internal_hash (XEPHEMERON_REF (obj), depth + 1); | |
3262 } | |
3263 | |
3264 Lisp_Object | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3265 make_ephemeron (Lisp_Object key, Lisp_Object value, Lisp_Object finalizer) |
888 | 3266 { |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3267 Lisp_Object temp = Qnil; |
888 | 3268 struct gcpro gcpro1, gcpro2; |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3269 Lisp_Object result = ALLOC_LISP_OBJECT (ephemeron); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3270 struct ephemeron *eph = XEPHEMERON (result); |
888 | 3271 |
3272 eph->key = Qnil; | |
3273 eph->cons_chain = Qnil; | |
3274 eph->value = Qnil; | |
3275 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3276 result = wrap_ephemeron (eph); |
888 | 3277 GCPRO2 (result, temp); |
3278 | |
3279 eph->key = key; | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3280 temp = Fcons (value, finalizer); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3281 eph->cons_chain = Fcons (temp, Vall_ephemerons); |
888 | 3282 eph->value = value; |
3283 | |
3284 Vall_ephemerons = result; | |
3285 | |
3286 UNGCPRO; | |
3287 return result; | |
3288 } | |
3289 | |
1598 | 3290 /* Ephemerons are special cases in the KKCC mark algorithm, so nothing |
3291 is marked here. */ | |
1204 | 3292 static const struct memory_description ephemeron_description[] = { |
3293 { XD_LISP_OBJECT, offsetof(struct ephemeron, key), | |
2551 | 3294 0, { 0 }, XD_FLAG_NO_KKCC }, |
1204 | 3295 { XD_LISP_OBJECT, offsetof(struct ephemeron, cons_chain), |
2551 | 3296 0, { 0 }, XD_FLAG_NO_KKCC }, |
1204 | 3297 { XD_LISP_OBJECT, offsetof(struct ephemeron, value), |
2551 | 3298 0, { 0 }, XD_FLAG_NO_KKCC }, |
888 | 3299 { XD_END } |
3300 }; | |
3301 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3302 DEFINE_NONDUMPABLE_LISP_OBJECT ("ephemeron", ephemeron, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3303 mark_ephemeron, print_ephemeron, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3304 0, ephemeron_equal, ephemeron_hash, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3305 ephemeron_description, |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3306 struct ephemeron); |
888 | 3307 |
3308 DEFUN ("make-ephemeron", Fmake_ephemeron, 2, 3, 0, /* | |
1590 | 3309 Return a new ephemeron with key KEY, value VALUE, and finalizer FINALIZER. |
3310 The ephemeron is a reference to VALUE which may be extracted with | |
3311 `ephemeron-ref'. VALUE is only reachable through the ephemeron as | |
888 | 3312 long as KEY is reachable; the ephemeron does not contribute to the |
3313 reachability of KEY. When KEY becomes unreachable while the ephemeron | |
1590 | 3314 itself is still reachable, VALUE is queued for finalization: FINALIZER |
3315 will possibly be called on VALUE some time in the future. Moreover, | |
888 | 3316 future calls to `ephemeron-ref' will return NIL. |
3317 */ | |
3318 (key, value, finalizer)) | |
3319 { | |
3320 return make_ephemeron(key, value, finalizer); | |
3321 } | |
3322 | |
3323 DEFUN ("ephemeron-ref", Fephemeron_ref, 1, 1, 0, /* | |
3324 Return the contents of ephemeron EPHEMERON. | |
3325 If the contents have been GCed, return NIL. | |
3326 */ | |
3327 (eph)) | |
3328 { | |
3329 return XEPHEMERON_REF (eph); | |
3330 } | |
3331 | |
3332 DEFUN ("ephemeron-p", Fephemeronp, 1, 1, 0, /* | |
3333 Return non-nil if OBJECT is an ephemeron. | |
3334 */ | |
3335 (object)) | |
3336 { | |
3337 return EPHEMERONP (object) ? Qt : Qnil; | |
3338 } | |
428 | 3339 |
3340 /************************************************************************/ | |
3341 /* initialization */ | |
3342 /************************************************************************/ | |
3343 | |
3344 static SIGTYPE | |
3345 arith_error (int signo) | |
3346 { | |
3347 EMACS_REESTABLISH_SIGNAL (signo, arith_error); | |
3348 EMACS_UNBLOCK_SIGNAL (signo); | |
563 | 3349 signal_error (Qarith_error, 0, Qunbound); |
428 | 3350 } |
3351 | |
3352 void | |
3353 init_data_very_early (void) | |
3354 { | |
3355 /* Don't do this if just dumping out. | |
3356 We don't want to call `signal' in this case | |
3357 so that we don't have trouble with dumping | |
3358 signal-delivering routines in an inconsistent state. */ | |
3359 if (!initialized) | |
3360 return; | |
613 | 3361 EMACS_SIGNAL (SIGFPE, arith_error); |
428 | 3362 #ifdef uts |
613 | 3363 EMACS_SIGNAL (SIGEMT, arith_error); |
428 | 3364 #endif /* uts */ |
3365 } | |
3366 | |
3367 void | |
3368 init_errors_once_early (void) | |
3369 { | |
442 | 3370 DEFSYMBOL (Qerror_conditions); |
3371 DEFSYMBOL (Qerror_message); | |
428 | 3372 |
3373 /* We declare the errors here because some other deferrors depend | |
3374 on some of the errors below. */ | |
3375 | |
3376 /* ERROR is used as a signaler for random errors for which nothing | |
3377 else is right */ | |
3378 | |
442 | 3379 DEFERROR (Qerror, "error", Qnil); |
3380 DEFERROR_STANDARD (Qquit, Qnil); | |
428 | 3381 |
563 | 3382 DEFERROR_STANDARD (Qinvalid_argument, Qerror); |
3383 | |
3384 DEFERROR_STANDARD (Qsyntax_error, Qinvalid_argument); | |
442 | 3385 DEFERROR_STANDARD (Qinvalid_read_syntax, Qsyntax_error); |
563 | 3386 DEFERROR_STANDARD (Qstructure_formation_error, Qsyntax_error); |
3387 DEFERROR_STANDARD (Qlist_formation_error, Qstructure_formation_error); | |
442 | 3388 DEFERROR_STANDARD (Qmalformed_list, Qlist_formation_error); |
3389 DEFERROR_STANDARD (Qmalformed_property_list, Qmalformed_list); | |
3390 DEFERROR_STANDARD (Qcircular_list, Qlist_formation_error); | |
3391 DEFERROR_STANDARD (Qcircular_property_list, Qcircular_list); | |
428 | 3392 |
442 | 3393 DEFERROR_STANDARD (Qwrong_type_argument, Qinvalid_argument); |
3394 DEFERROR_STANDARD (Qargs_out_of_range, Qinvalid_argument); | |
3395 DEFERROR_STANDARD (Qwrong_number_of_arguments, Qinvalid_argument); | |
3396 DEFERROR_STANDARD (Qinvalid_function, Qinvalid_argument); | |
563 | 3397 DEFERROR_STANDARD (Qinvalid_constant, Qinvalid_argument); |
442 | 3398 DEFERROR (Qno_catch, "No catch for tag", Qinvalid_argument); |
3399 | |
563 | 3400 DEFERROR_STANDARD (Qinvalid_state, Qerror); |
442 | 3401 DEFERROR (Qvoid_function, "Symbol's function definition is void", |
3402 Qinvalid_state); | |
3403 DEFERROR (Qcyclic_function_indirection, | |
3404 "Symbol's chain of function indirections contains a loop", | |
3405 Qinvalid_state); | |
3406 DEFERROR (Qvoid_variable, "Symbol's value as variable is void", | |
3407 Qinvalid_state); | |
3408 DEFERROR (Qcyclic_variable_indirection, | |
3409 "Symbol's chain of variable indirections contains a loop", | |
3410 Qinvalid_state); | |
563 | 3411 DEFERROR_STANDARD (Qstack_overflow, Qinvalid_state); |
3412 DEFERROR_STANDARD (Qinternal_error, Qinvalid_state); | |
3413 DEFERROR_STANDARD (Qout_of_memory, Qinvalid_state); | |
428 | 3414 |
563 | 3415 DEFERROR_STANDARD (Qinvalid_operation, Qerror); |
3416 DEFERROR_STANDARD (Qinvalid_change, Qinvalid_operation); | |
442 | 3417 DEFERROR (Qsetting_constant, "Attempt to set a constant symbol", |
3418 Qinvalid_change); | |
563 | 3419 DEFERROR_STANDARD (Qprinting_unreadable_object, Qinvalid_operation); |
3420 DEFERROR (Qunimplemented, "Feature not yet implemented", Qinvalid_operation); | |
442 | 3421 |
563 | 3422 DEFERROR_STANDARD (Qediting_error, Qinvalid_operation); |
442 | 3423 DEFERROR_STANDARD (Qbeginning_of_buffer, Qediting_error); |
3424 DEFERROR_STANDARD (Qend_of_buffer, Qediting_error); | |
3425 DEFERROR (Qbuffer_read_only, "Buffer is read-only", Qediting_error); | |
3426 | |
3427 DEFERROR (Qio_error, "IO Error", Qinvalid_operation); | |
563 | 3428 DEFERROR_STANDARD (Qfile_error, Qio_error); |
3429 DEFERROR (Qend_of_file, "End of file or stream", Qfile_error); | |
3430 DEFERROR_STANDARD (Qconversion_error, Qio_error); | |
580 | 3431 DEFERROR_STANDARD (Qtext_conversion_error, Qconversion_error); |
442 | 3432 |
3433 DEFERROR (Qarith_error, "Arithmetic error", Qinvalid_operation); | |
3434 DEFERROR (Qrange_error, "Arithmetic range error", Qarith_error); | |
3435 DEFERROR (Qdomain_error, "Arithmetic domain error", Qarith_error); | |
3436 DEFERROR (Qsingularity_error, "Arithmetic singularity error", Qdomain_error); | |
3437 DEFERROR (Qoverflow_error, "Arithmetic overflow error", Qdomain_error); | |
3438 DEFERROR (Qunderflow_error, "Arithmetic underflow error", Qdomain_error); | |
428 | 3439 } |
3440 | |
3441 void | |
3442 syms_of_data (void) | |
3443 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3444 INIT_LISP_OBJECT (weak_list); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3445 INIT_LISP_OBJECT (ephemeron); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3017
diff
changeset
|
3446 INIT_LISP_OBJECT (weak_box); |
442 | 3447 |
3448 DEFSYMBOL (Qquote); | |
3449 DEFSYMBOL (Qlambda); | |
3450 DEFSYMBOL (Qlistp); | |
3451 DEFSYMBOL (Qtrue_list_p); | |
3452 DEFSYMBOL (Qconsp); | |
3453 DEFSYMBOL (Qsubrp); | |
3454 DEFSYMBOL (Qsymbolp); | |
3455 DEFSYMBOL (Qintegerp); | |
3456 DEFSYMBOL (Qcharacterp); | |
3457 DEFSYMBOL (Qnatnump); | |
1983 | 3458 DEFSYMBOL (Qnonnegativep); |
442 | 3459 DEFSYMBOL (Qstringp); |
3460 DEFSYMBOL (Qarrayp); | |
3461 DEFSYMBOL (Qsequencep); | |
3462 DEFSYMBOL (Qbufferp); | |
3463 DEFSYMBOL (Qbitp); | |
3464 DEFSYMBOL_MULTIWORD_PREDICATE (Qbit_vectorp); | |
3465 DEFSYMBOL (Qvectorp); | |
3466 DEFSYMBOL (Qchar_or_string_p); | |
3467 DEFSYMBOL (Qmarkerp); | |
3468 DEFSYMBOL (Qinteger_or_marker_p); | |
3469 DEFSYMBOL (Qinteger_or_char_p); | |
3470 DEFSYMBOL (Qinteger_char_or_marker_p); | |
3471 DEFSYMBOL (Qnumberp); | |
3472 DEFSYMBOL (Qnumber_char_or_marker_p); | |
3473 DEFSYMBOL (Qcdr); | |
563 | 3474 DEFSYMBOL (Qerror_lacks_explanatory_string); |
442 | 3475 DEFSYMBOL_MULTIWORD_PREDICATE (Qweak_listp); |
3476 DEFSYMBOL (Qfloatp); | |
428 | 3477 |
3478 DEFSUBR (Fwrong_type_argument); | |
3479 | |
1983 | 3480 #ifdef HAVE_RATIO |
3481 DEFSUBR (Fdiv); | |
3482 #endif | |
428 | 3483 DEFSUBR (Feq); |
3484 DEFSUBR (Fold_eq); | |
3485 DEFSUBR (Fnull); | |
3486 Ffset (intern ("not"), intern ("null")); | |
3487 DEFSUBR (Flistp); | |
3488 DEFSUBR (Fnlistp); | |
3489 DEFSUBR (Ftrue_list_p); | |
3490 DEFSUBR (Fconsp); | |
3491 DEFSUBR (Fatom); | |
3492 DEFSUBR (Fchar_or_string_p); | |
3493 DEFSUBR (Fcharacterp); | |
3494 DEFSUBR (Fchar_int_p); | |
3495 DEFSUBR (Fchar_to_int); | |
3496 DEFSUBR (Fint_to_char); | |
3497 DEFSUBR (Fchar_or_char_int_p); | |
1983 | 3498 #ifdef HAVE_BIGNUM |
3499 DEFSUBR (Ffixnump); | |
3500 #else | |
428 | 3501 DEFSUBR (Fintegerp); |
1983 | 3502 #endif |
428 | 3503 DEFSUBR (Finteger_or_marker_p); |
3504 DEFSUBR (Finteger_or_char_p); | |
3505 DEFSUBR (Finteger_char_or_marker_p); | |
3506 DEFSUBR (Fnumberp); | |
3507 DEFSUBR (Fnumber_or_marker_p); | |
3508 DEFSUBR (Fnumber_char_or_marker_p); | |
3509 DEFSUBR (Ffloatp); | |
3510 DEFSUBR (Fnatnump); | |
1983 | 3511 DEFSUBR (Fnonnegativep); |
428 | 3512 DEFSUBR (Fsymbolp); |
3513 DEFSUBR (Fkeywordp); | |
3514 DEFSUBR (Fstringp); | |
3515 DEFSUBR (Fvectorp); | |
3516 DEFSUBR (Fbitp); | |
3517 DEFSUBR (Fbit_vector_p); | |
3518 DEFSUBR (Farrayp); | |
3519 DEFSUBR (Fsequencep); | |
3520 DEFSUBR (Fmarkerp); | |
3521 DEFSUBR (Fsubrp); | |
3522 DEFSUBR (Fsubr_min_args); | |
3523 DEFSUBR (Fsubr_max_args); | |
3524 DEFSUBR (Fsubr_interactive); | |
3525 DEFSUBR (Ftype_of); | |
3526 DEFSUBR (Fcar); | |
3527 DEFSUBR (Fcdr); | |
3528 DEFSUBR (Fcar_safe); | |
3529 DEFSUBR (Fcdr_safe); | |
3530 DEFSUBR (Fsetcar); | |
3531 DEFSUBR (Fsetcdr); | |
3532 DEFSUBR (Findirect_function); | |
3533 DEFSUBR (Faref); | |
3534 DEFSUBR (Faset); | |
3535 | |
3536 DEFSUBR (Fnumber_to_string); | |
3537 DEFSUBR (Fstring_to_number); | |
3538 DEFSUBR (Feqlsign); | |
3539 DEFSUBR (Flss); | |
3540 DEFSUBR (Fgtr); | |
3541 DEFSUBR (Fleq); | |
3542 DEFSUBR (Fgeq); | |
3543 DEFSUBR (Fneq); | |
3544 DEFSUBR (Fzerop); | |
3545 DEFSUBR (Fplus); | |
3546 DEFSUBR (Fminus); | |
3547 DEFSUBR (Ftimes); | |
3548 DEFSUBR (Fquo); | |
3549 DEFSUBR (Frem); | |
3550 DEFSUBR (Fmod); | |
3551 DEFSUBR (Fmax); | |
3552 DEFSUBR (Fmin); | |
3553 DEFSUBR (Flogand); | |
3554 DEFSUBR (Flogior); | |
3555 DEFSUBR (Flogxor); | |
3556 DEFSUBR (Flsh); | |
3557 DEFSUBR (Fash); | |
3558 DEFSUBR (Fadd1); | |
3559 DEFSUBR (Fsub1); | |
3560 DEFSUBR (Flognot); | |
3561 | |
3562 DEFSUBR (Fweak_list_p); | |
3563 DEFSUBR (Fmake_weak_list); | |
3564 DEFSUBR (Fweak_list_type); | |
3565 DEFSUBR (Fweak_list_list); | |
3566 DEFSUBR (Fset_weak_list_list); | |
858 | 3567 |
888 | 3568 DEFSUBR (Fmake_ephemeron); |
3569 DEFSUBR (Fephemeron_ref); | |
3570 DEFSUBR (Fephemeronp); | |
858 | 3571 DEFSUBR (Fmake_weak_box); |
3572 DEFSUBR (Fweak_box_ref); | |
3573 DEFSUBR (Fweak_boxp); | |
428 | 3574 } |
3575 | |
3576 void | |
3577 vars_of_data (void) | |
3578 { | |
3579 /* This must not be staticpro'd */ | |
3580 Vall_weak_lists = Qnil; | |
452 | 3581 dump_add_weak_object_chain (&Vall_weak_lists); |
428 | 3582 |
888 | 3583 Vall_ephemerons = Qnil; |
3584 dump_add_weak_object_chain (&Vall_ephemerons); | |
3585 | |
3586 Vfinalize_list = Qnil; | |
3587 staticpro (&Vfinalize_list); | |
3588 | |
858 | 3589 Vall_weak_boxes = Qnil; |
3590 dump_add_weak_object_chain (&Vall_weak_boxes); | |
3591 | |
428 | 3592 #ifdef DEBUG_XEMACS |
3593 DEFVAR_BOOL ("debug-issue-ebola-notices", &debug_issue_ebola_notices /* | |
3594 If non-zero, note when your code may be suffering from char-int confoundance. | |
3595 That is to say, if XEmacs encounters a usage of `eq', `memq', `equal', | |
3596 etc. where an int and a char with the same value are being compared, | |
3597 it will issue a notice on stderr to this effect, along with a backtrace. | |
3598 In such situations, the result would be different in XEmacs 19 versus | |
3599 XEmacs 20, and you probably don't want this. | |
3600 | |
3601 Note that in order to see these notices, you have to byte compile your | |
3602 code under XEmacs 20 -- any code byte-compiled under XEmacs 19 will | |
3603 have its chars and ints all confounded in the byte code, making it | |
3604 impossible to accurately determine Ebola infection. | |
3605 */ ); | |
3606 | |
3607 debug_issue_ebola_notices = 0; | |
3608 | |
3609 DEFVAR_INT ("debug-ebola-backtrace-length", | |
3610 &debug_ebola_backtrace_length /* | |
3611 Length (in stack frames) of short backtrace printed out in Ebola notices. | |
3612 See `debug-issue-ebola-notices'. | |
3613 */ ); | |
3614 debug_ebola_backtrace_length = 32; | |
3615 | |
3616 #endif /* DEBUG_XEMACS */ | |
3617 } |