summaryrefslogtreecommitdiffstats
path: root/lib.c
Commit message (Collapse)AuthorAgeFilesLines
...
* listref_l: remove.Kaz Kylheku2018-01-061-17/+0
| | | | | | * lib.c (listref_l): Unused function removed. * lib.h (listref_l): Declaration removed.
* refset: implement objects that support car method.Kaz Kylheku2018-01-061-12/+46
| | | | | | | | | | | | | | refset and range assignment is implemented for objects that have no lambda-set but do have a car method. * lib.c (refset): Implementation for lists rewritten to avoid listref_l, and use nthcdr instead. For structs, if there is no lambda-set method, but a car method exists, jump to the list case: the idea is that we can cdr down and then use rplaca. (dwim_set): Error handling streamlined. In this function too, we check whether there is a car method and branch to the list case.
* nthcdr: terminate loop if end of list hit.Kaz Kylheku2018-01-051-1/+1
| | | | | * lib.c (nthcdr): Terminate loop when nil is hit rather than continuing to count down to zero.
* car, cdr: self-identify in error message.Kaz Kylheku2018-01-051-2/+2
| | | | | * lib.c (car, cdr): Type mismatch messages now identify functions.
* car, cdr: fall back on lambda method.Kaz Kylheku2018-01-031-4/+24
| | | | | | * lib.c (car, cdr): Don't fail if the struct object has no car or cdr method. Use it if it is available, otherwise try to fall back on the lambda method if that is available.
* ltail: unused function.Kaz Kylheku2018-01-021-7/+0
| | | | | | | | * lib.c (ltail): Function removed. This was introduced at the same time as lazy_appendv and used only by it. That function was rewritten a few months ago and doesn't use lail. * lib.h (ltail): Declaration removed.
* seq_info: bugfix: wrong object tested obj_struct_p.Kaz Kylheku2018-01-021-1/+1
| | | | | | * lib.c (seq_info): The obj_struct_p test must be applied to obj, not to cls, which is a symbol. Due to this bug, seq_info would always report struct-based sequences as SEQ_NOTSEQ.
* seq_info: whitespace.Kaz Kylheku2018-01-021-1/+1
| | | | | * lib.c (seq_info): Incorrect indentation of else statement fixed.
* last: rewrite using seq_info.Kaz Kylheku2018-01-021-10/+17
| | | | | * lib.c (last): Use seq_info classification rather than relying on listp.
* eliminate cdr_l use from implementation of last.Kaz Kylheku2018-01-021-11/+7
| | | | | | * lib.c (lastcons): Return value is just the last cons rather than a loc. The only caller of this function is last. (last): Adapt to the new lastcons.
* Use rplaca and rplacd instead of set over car_l/cdr_l.Kaz Kylheku2018-01-011-6/+6
| | | | | | | | | | | | | | | | | | | | | | | This reduces the proliferation of car_l and cdr_l. With this change, nreverse should work on chains of objects that implement rplacd. * combi.c (comb_gen_fun_common, rcomb_gen_fun_common): Use rplaca. * eval.c (mappendv, mapdov): Likewise * hash.c (hash_equal_op): Likewise. * lib.c (nreverse, acons_new, aconsql_new, sort_list): Use rplaca and rplacd. * match.c (dest_set, v_gather, v_collect, v_flatten, v_cat, v_output, v_filter): Likewise * parser.c (ensure_parser): Use sys_rplacd. * unwind.c (uw_register_subtype): Use rplacd.
* sub and replace redirect to structure methods.Kaz Kylheku2018-01-011-5/+36
| | | | | | | | | | | | | * lib.c (replace_obj): New static function. (sub): Handle struct case via lambda method. (replace): Handle struct case via replace_obj. * txr.1: Documented. * tests/012/aseq.tl (add): The lambda method now has to handle a range argument. One test case uses the last function, which for non-lists relies on sub, which now calls the lambda method if the object has one.
* New inlined test for struct object.Kaz Kylheku2017-12-311-14/+14
| | | | | | | | | | | * lib.c (seq_info, car, cdr, make_like, nullify, generic_funcall, copy, length, empty, ref, refset, dwim_set, dwim_del, populate_obj_hash): Use new obj_struct_p test when we know that the object is a COBJ. * struct.c (struct_inst_ops): Change from static to extern. * struct.h (ob_struct_p): New inline function.
* New methods rplaca and rplacd.Kaz Kylheku2017-12-301-0/+32
| | | | | | | | | | | | | | | * eval.c (eval_init): Register rplaca and rplacd using new rplaca_s and rplacd_s symbol variables. * lib.c (rplaca_s, rplacd_s): New symbol variables. (rplaca): Handle struct object via rplaca method, if it has one, otherwise lambda-set, if it has that, or else error out. (rplacd): Handle struct object via rplacd method. * lib.h (rplaca_s, rplacd_s): Declared. * txr.1: Documented rplaca and rplacd methods.
* refset: better diagnostics.Kaz Kylheku2017-12-291-1/+3
| | | | | | * lib.c (refset): If structure has no lambda-set method, diagnose it like that, rather than "not a sequence". Also, diagnostics should use refset:, not ref:.
* New feature: structure delegate streams.Kaz Kylheku2017-12-081-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | A new kind of stream object which redirects its operations to the methods of a structure. * Makefile (OBJS): New object file, strudel.o. * lib.c (init): Call new strudel_init function. * lisplib.c (stream_wrap_set_entries, stream_wrap_instantiate): New static functions. (lisplib_init): Arrange for autloading of new stream-wrap.tl. * share/txr/stdlib/stream-wrap.tl: New file. * stream.c (put_string_s, put_char_s, put_byte_s, get_line_s, get_char_s, get_byte_s, unget_char_s, unget_byte_s, put_buf_s, fill_buf_s, flush_s, seek_s, truncate_s, get_prop_s, set_prop_s, get_error_s, get_error_str_s, clear_error_s, get_fd_s): New symbol variables. (stream_init): New symbol variables initialized. Numerous functions registered via these variables now rather than intern(...) expressions. * stream.h (put_string_s, put_char_s, put_byte_s, get_line_s, get_char_s, get_byte_s, unget_char_s, unget_byte_s, put_buf_s, fill_buf_s, flush_s, seek_s, truncate_s, get_prop_s, set_prop_s, get_error_s, get_error_str_s, clear_error_s, get_fd_s): Declared. * strudel.c, strudel.h: New files.
* Rewrite internal mapping function.Kaz Kylheku2017-11-301-4/+21
| | | | | | * lib.c (mapcar_listout): Rework using seq_info for efficient processing of vector-like sequences and objects that implement sequences.
* Fix quoted function name in unsupported object errors.Kaz Kylheku2017-11-231-11/+17
| | | | | | | | | * lib.c (unsup_obj): New static function. (grade, find, rfind, find_max, find_if, rfind_if, pos, rpos, pos_if, rpos_if, pos_max): Replace call to uw_throwf with call to unsup_obj. IN all these functions except grade, the ~s conversion specifier was wrongly used on the function name rather than ~a, resulting in unwanted quoting.
* New function: grade.Kaz Kylheku2017-11-231-0/+43
| | | | | | | | | | | | Inspired by APL. * eval.c (eval_init): Register grade intrinsic. * lib.c (grade): New function. * lib.h (grade): Declared. * txr.1: Documented.
* lastcons: streamline.Kaz Kylheku2017-11-221-1/+1
| | | | | * lib.c (lastcons): Don't wastefully call cdr on an object after called cdr_l; just dereference the cdr_l loc.
* bugfix: tail handles improper list.Kaz Kylheku2017-11-221-1/+1
| | | | | | | | | | | * lib.c (tail): This low-level function is used by the list accumulation routines. Because it doesn't handle improper lists, looking for a null terminator, certain things don't work, like the associativity of append. For instance (append '(1 2) #(3) 4) works but not (append (append '(1 2) #(3)) 4). Fixing tail so that it terminates on any atom, rather than failing trying to cdr through it.
* bugfix: two issues in mappend* and append*.Kaz Kylheku2017-11-211-29/+23
| | | | | | | | | | | | | | | | | | | | | | | Two bugs in these functions, both attributable to the lazy_appendv implementation: They destructively catenate the input lists, much like nconc, even though documented as non-destructive. If any input list is infinite, other than the last input list, that list is forced, resulting in an infinite loop. * lib.c (lazy_appendv_func): Rewritten to use a different algorithm which earnestly allocates a new lazy cons for each element of the output sequence, except for the tail part corresponding to the last list. (lazy_appendv): Set up the lazy cons according to the new representation and just return it. No searching for the tail of the nonempty list, and no destructive manipulation.
* Use fixnum indices for vector iteration.Kaz Kylheku2017-11-161-41/+51
| | | | | | | | | | | * lib.c (find, rfind, find_max, find_if, rfind_if, pos, rpos, pos_if, rpos_if, pos_max): Consistently fixnum indices for iterating over vector. In some functions, a cnum is already used, but could be out of fixnum range; we switch to using c_fixnum for extracting the length and then num_fast on the index. Some functions are converted from using a val index. In the case of rfind_if, a bug is fixed: it was using plusp, which now becomes the correct >= 0.
* pos-max: rewrite.Kaz Kylheku2017-11-161-18/+48
| | | | * lib.c (pos_max): Rewrite using seq_info.
* find-max: bugfix for zero length vectors.Kaz Kylheku2017-11-151-10/+15
| | | | | | * lib.c (find_max): Fix a regression introduced in recent work: only execute the loop when the vector isn't empty.
* pos-if, rpos-if: rewrite.Kaz Kylheku2017-11-151-25/+72
| | | | * lib.c (pos_if, rpos_if): Rewrite using seq_info.
* posq, posql, posqual, rposq, rposql, rposqual: rewriteKaz Kylheku2017-11-151-93/+30
| | | | | | | * lib.c (posq, posql, posqual, rposq, rposql, rposqual): These functions are reduced to wrappers around pos and rpos, respectively, so they generalize properly and efficiently to sequences of all kinds.
* pos, rpos: rewrite with seq_info.Kaz Kylheku2017-11-151-57/+88
| | | | | | | * lib.c (pos, rpos): Functions rewritten to use the seq_info sequence classification mechanism. The rpos function is thereby optimized to work with vectors. Both functions support vector-like struct objects now.
* rfind-if: optimized rewrite and hash support.Kaz Kylheku2017-11-151-9/+47
| | | | | | | | | | * lib.c (rfind_if): Function rewritten to use the seq_info sequence classification mechanism, for much better performance on vector-like objects. Also, supports hash tables just like find_if. * txr.1: Documentation updated regarding hash support of rfind-if.
* find-if: optimized rewrite and hash support.Kaz Kylheku2017-11-151-9/+48
| | | | | | | | | | * lib.c (find_if): Function rewritten to use the seq_info sequence classification mechanism, for much better performance on vector-like objects. Also, supports hash tables just like find_max. * txr.1: Documentation updated regarding hash support of find-if.
* find-max: tiny optimization for vectors.Kaz Kylheku2017-11-151-1/+1
| | | | | | * lib.c (find_max): The vector case must loop from index one, not zero, so as not to wastefully compare the initial max element to itself.
* find_max: convert to use seq_info.Kaz Kylheku2017-10-131-20/+17
| | | | | | | * lib.c (find_max): Sequence classification rewritten to use seq_info. The cases are almost the same, but refer to si.obj rather than seq. Some care is taken in the list case to not hold a reference to the list head.
* rfind: rewrite to be like find.Kaz Kylheku2017-10-131-11/+48
| | | | | | * lib.c (rfind): Instead of treating the sequence as a list, classify with seq_info just like find. Basically the whole function is replaced with an altered copy of find.
* find: convert to seq_info classification.Kaz Kylheku2017-10-131-44/+36
| | | | | | | * lib.c (find): Convert switch statement to use the seq_info function to classify the sequence. For SEQ_VECLIKE, we still check whether the original object is a literal or regular string to treat it specially.
* Fixes in partition, partition*, split and split*.Kaz Kylheku2017-09-291-42/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | Bunch of issues here: broken pre-171 compatibility, non-termination on lazy infinite lists of indices, doc issues. * lib.c (partition_func, split_func, split_star_func): Do the check for negative index values here, with the compat handling for 170 or older. (partition_split_common): Remove code that tries to adjust negative indices, and delete zeros or indices that are still negative after adjustment. The code consumes the entire list of prefixes, so chokes on lazy lists. Also in the compat case, there is complete breakage: the loop doesn't execute, and so out is just nil, and it is taken as the index list. (partition_star_func): Similar change like in partition_func. (partition_star): Similarly to partition_split_common, take out the bogus loop. Also take out loop that tries to remove leading negatives: we cannot do that because we haven't normalized them. * txr.1: Revised doc. Condensed by describing index-list argument in detail under partition. For the other functions, we refer to that one. Conditions for safely handling infinite list of indices spelled out.
* bugfix: fixnum crackdown.Kaz Kylheku2017-09-131-21/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The purpose of this commit is to address certain situations in which code is wrongly relying on a cnum value being in the fixnum range (NUM_MIN to NUM_MAX), so that num_fast can safely be used on it. One wrong pattern is that c_num is applied to some Lisp value, and that value (or one derived from it arithmetically) is then passed to num_fast. The problem is that c_num succeeds on integers outside of the fixnum range. Some bignum values convert to a cnum successfully. Thus either num has to be used instead of num_fast, or else the original c_num attempt must be replaced with something that will fail if the original value isn't a fixnum. (In the latter case, any arithmetic on the fixnum cannot produce value outside of that range). * buf.c (buf_put_bytes): The size argument here is not guaranteed to be in fixnum range: use num. * combi.c (perm_init_common): Throw if the sequence length isn't a fixnum. Thus the num_fast in perm_while_fun is correct, since the ci value is bounded by k, which is bounded by n. * hash.c (hash_grow): Remove dubious assertion which aborts the run-time if the hash table doubling overflows. Simply don't allow the modulus to grow beyond NUM_MAX. If doubling it makes it larger than NUM_MAX, then just don't grow the table. We need the modulus to be in fixnum range, so that uses of num_fast on the modulus value elsewhere are correct. (group_by, group_reduce): Use c_fixnum rather than c_num to extract a value that is later assumed to be a fixnum. * lib.c (c_fixnum): New function. (nreverse, reverse, remove_if, less, window_map_list, sort_vec, unique): Use c_fixnum rather than c_num to extract a value that is later assumed to be a fixnum. (string_extend): Use c_fixnum rather than c_num to extract a value that is later assumed to be a fixnum. Cap the string allocation size to fixnum range rather than INT_PTR_MAX. (cmp_str): The wcscmp function could return values outside of the fixnum range, so we must use num, not num_fast. * lib.h (c_fixnum): Declared.
* bugfix: replace_str uses string_extend incorrectly.Kaz Kylheku2017-08-241-1/+1
| | | | | | | | | | | | | | | | | | | | One test case for this is that (append "ABC" "DEF") returns an "ABCDEF" string whose length reports as 9 instead of the correct 6. This will wreak various havoc. The bug was introduced in the very first version of replace_str, in commit d011fda9b6b078f09027eb65d500c8beffc99414 on January 26, 2012. In the same commit, the string_extend behavior is introduced of supporting an integer value specifying the number of characters by which to extend the string. This feature of string_extend is used in replace_str, but wrongly. * lib.c (replace_str): Pass just the size delta to string_extend; do not add the old length to the delta such that the total size is wrongly passed.
* Revising out-of-memory handling.Kaz Kylheku2017-08-181-13/+14
| | | | | | | | | | | | | | | | | | | | | We don't want to be aborting on OOM, but throwing an exception. * lib.c (alloc_error_s): New symbol variable. (oom_realloc): Global variable removed. (oom): New static function. (chk_malloc, chk_malloc_gc_more, chk_calloc, chk_realloc): Call oom instead of removed oom_realloc handler. (env): Throw alloc-error rather than error by calling oom. (obj_init): Initialize alloc_error_s. (init): Drop function pointer argument; do not initialize removed oom_realloc. * lib.h (alloc_error_s): Declared. (oom_realloc): Declaration removed. (init): Declaration updated. * txr.1: Type tree diagram includes alloc-error.
* vec-set-length maintenance.Kaz Kylheku2017-08-171-4/+13
| | | | | | | * lib.c (vec_set_length): Check new length against INT_PTR_MAX rather than size_t limit. We want to keep the length a fixnum. If the allocation needs to increase, grow it by 25%, not by doubling it.
* Rewriting string-extend.Kaz Kylheku2017-08-171-26/+25
| | | | | | | | | | * lib.c (string_extend): Restructure internals with these goals: no loop: calculate needed space in one step; if the allocation needs to grow, then grow it by 25% or step it up to exactly the needed size, whichever of the two is larger. Overflow check against INT_PTR_MAX, since len and alloc fields of string are not fixnums but integers extracted with c_num.
* New spl and tok: variants of tok-str and split-str.Kaz Kylheku2017-08-071-0/+14
| | | | | | | | * eval.c (eval_init): Register spl and tok intrinsics. * lib.c (spl, tok): New functions. * txr.1: Documented.
* bugfix: n-ary arith functions must check single arg.Kaz Kylheku2017-08-051-8/+57
| | | | | | | | | | | | | | | | We are allowing calls like (* "a") and (+ "a") without diagnosing that the argument isn't of a valid type. Note that (max "a") is fine beacause min and max use the less function; they are not strictly numeric. * lib.c (nary_op): Beef up function with additional argument for type checking the unary case. (unary_num, unary_arith, unary_int): New static functions. (plusv, mulv, logandv, logiorv): Use new nary_op interface. (gtv, ltv, gev, lev, numeqv, numneq): Check the first number. * lib.c (nary_op): Declaration updated.
* Add sum and prod convenience functions.Kaz Kylheku2017-08-051-0/+12
| | | | | | | | | | * eval.c (eval_init): prod and sum intrinsics registered. * lib.c (sum, prod): New functions. * lib.h (sum, prod): Declared. * txr.1: Documented.
* lib: deprecate set-diff; extend set operations.Kaz Kylheku2017-07-261-0/+59
| | | | | | | | | | | | | | * eval.c (eval_init): Register set-diff under two names: set-diff and diff. Register new isec and uni intrinsics. * lib.c (isec, uni): New functions. * lib.h (isec, uni): Declared. * txr.1: Documented new uni and isec functions, new diff function name, and the deprecation of set-diff and its order guarantee w.r.t the left sequence.
* find, pos: optimize and support objects properly.Kaz Kylheku2017-07-181-21/+114
| | | | | | | | | | | * lib.c (find, pos): Provide specialized behavior based on type and test and key functions. Lists and list-like objects are treated by marching down with cdr. Vectors are traversed with numeric index, as are vector-like objects which exhibit a length function. A special optimization is put in for non-lazy strings which use identity as their key function, and one of the built-in equality operators for the test function: wcschr is used on the underlying C string.
* new function: nthKaz Kylheku2017-07-181-0/+5
| | | | | | | | | | | | | | | | Just the ANSI CL nth for lists. * eval.c (eval_init): Register nth intrinsic. * lib.c (nth): New function. * lib.h (nth): Declared. * share/txr/stdlib/place.tl (nth): New place macro, trivially takes care of making nth an accessor. Place macros are terrific! * txr.1: Documented.
* lib: new function, relate.Kaz Kylheku2017-07-171-0/+25
| | | | | | | | | | | * eval.c (eval_init): Register new intrinsic relate. * lib.c (do_relate, do_relate_dfl): New static functions. (relate): New function. * lib.h (relate): Declared. * txr.1: Documented.
* ffi: relaxation in cptr put semantics.Kaz Kylheku2017-07-021-5/+10
| | | | | | | | | | | | For convenience, we allow a cptr to be converted to foreign representation even if its tag doesn't match the FFI type being used for the conversion. This is allowed only in the case that the cptr is a null pointer, and its tag is nil. * lib.c (cptr_handle): Defeat the type check if the pointer is null, and its tag is nil. Thus, the FFI variable cptr-null will conveniently convert to any cptr type in the ffi_cptr_put operation and others.
* New cptr functions cptr-cast and int-cptr.Kaz Kylheku2017-06-261-0/+11
| | | | | | | | | | | * eval.c (eval_init): Register new intrinsics cptr-cast and int-cptr. * lib.c (cptr_cast, int_cptr): New functions. * lib.h (cptr_cast, int_cptr): Declared. * txr.1: Documented.
* cptr-int and cptr-obj can make typed cptr objects.Kaz Kylheku2017-06-191-4/+6
| | | | | | | | | | | | | | | | | * eval.c (eval_init): Update registration of cptr-int and cptr-obj with one optional argument. * lib.c (cptr_int): New type symbol argument, defaulting to nil. Also, don't bother defaulting the integer argument; the function isn't registered for that being optional. (cptr_obj): New type symbol argument, defaulting to nil. * lib.h (cptr_int, cptr_obj): Declarations updated. * txr.1: Documented cptr-int and cptr-obj function changes. Added discussion of type tag to introductory paragraph. Also added neglected documentation of the FFI cptr type, both unparametrized and parametrized.