summaryrefslogtreecommitdiffstats
path: root/lib.c
Commit message (Collapse)AuthorAgeFilesLines
* Copyright year bump 2021.Kaz Kylheku2021-01-141-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * METALICENSE: 2020 copyrights bumped to 2021. Added note about SHA-256 routines from Colin Percival. * LICENSE, LICENSE-CYG, Makefile, alloca.h, args.c, args.h, arith.c, arith.h, buf.c, buf.h, cadr.c, cadr.h, chksum.c, chksum.h, chksums/crc32.c, chksums/crc32.h, combi.c, combi.h, configure, debug.c, debug.h, eval.c, eval.h, ffi.c, ffi.h, filter.c, filter.h, ftw.c, ftw.h, gc.c, gc.h, glob.c, glob.h, hash.c, hash.h, itypes.c, itypes.h, jmp.S, lex.yy.c.shipped, lib.c, lib.h, linenoise/linenoise.c, linenoise/linenoise.h, lisplib.c, lisplib.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, protsym.c, rand.c, rand.h, regex.c, regex.h, share/txr/stdlib/asm.tl, share/txr/stdlib/awk.tl, share/txr/stdlib/build.tl, share/txr/stdlib/cadr.tl, share/txr/stdlib/compiler.tl, share/txr/stdlib/conv.tl, share/txr/stdlib/copy-file.tl, share/txr/stdlib/debugger.tl, share/txr/stdlib/defset.tl, share/txr/stdlib/doloop.tl, share/txr/stdlib/each-prod.tl, share/txr/stdlib/error.tl, share/txr/stdlib/except.tl, share/txr/stdlib/ffi.tl, share/txr/stdlib/getopts.tl, share/txr/stdlib/getput.tl, share/txr/stdlib/hash.tl, share/txr/stdlib/ifa.tl, share/txr/stdlib/keyparams.tl, share/txr/stdlib/op.tl, share/txr/stdlib/package.tl, share/txr/stdlib/param.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/pmac.tl, share/txr/stdlib/quips.tl, share/txr/stdlib/save-exe.tl, share/txr/stdlib/socket.tl, share/txr/stdlib/stream-wrap.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/tagbody.tl, share/txr/stdlib/termios.tl, share/txr/stdlib/trace.tl, share/txr/stdlib/txr-case.tl, share/txr/stdlib/type.tl, share/txr/stdlib/vm-param.tl, share/txr/stdlib/with-resources.tl, share/txr/stdlib/with-stream.tl, share/txr/stdlib/yield.tl, signal.c, signal.h, socket.c, socket.h, stream.c, stream.h, struct.c, struct.h, strudel.c, strudel.h, sysif.c, sysif.h, syslog.c, syslog.h, termios.c, termios.h, time.c, time.h, tree.c, tree.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h, vm.c, vm.h, vmop.h, win/cleansvg.txr, y.tab.c.shipped: Copyright year bumped to 2021.
* shuffle/nshuffle: take optional random state argument.Kaz Kylheku2020-12-291-4/+4
| | | | | | | | | | | | | * eval.c (eval_init): Register shuffle and nshuffle as two-argument functions with optional argument. * lib.c (nshuffle): Take random-state argument, defaulting to value of random_state special variable. (shuffle): Take random-state argument, pass down to nshuffle. * lib.h (shuffle, nshuffle): Declarations updated. * txr.1: Updated.
* shuffle/nshuffle: eliminate wasteful vector copy.Kaz Kylheku2020-12-291-1/+1
| | | | | | * lib.c (nshuffle): When shuffling the vector that was converted from the list, use nshuffle rather shuffle, because it's a temporary, non-shared object we can mutate.
* printer: no leading zeros in hex chars.Kaz Kylheku2020-11-171-9/+6
| | | | | | | | * lib.c (obj_print_impl): Drop the complicated logic for printing a character as 2, 4 or 6 hex digits. All characters that must print using the #\x notation are jus printed using the ~X format specifier with no precision or leading zeros, which will use just as many digits as are required.
* env: move function to sysif.cKaz Kylheku2020-10-161-35/+1
| | | | | | | | | | | | | | | | | * lib.c (env_list): Static variable removed; now appears in sysif.c (env): Function removed; now in sysif.c. (obj_init): Don't gc-protect env_list here any more. * lib.h (env): Declaration removed. * match.c: Must include "sysif.h" now for env. * sysif.c (env_list): Static variable moved here. (env): Function moved here. (sysif_init): env_list gc-protected here. * sysif.h (env): Declared.
* time: move time functions out of lib.c into time.c.Kaz Kylheku2020-10-071-454/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Makefile (OBJS): Add new time.o. * eval.c (eval_init): Registration of time functions is removed from here; it is done in time_init now, in time.c. * hash.c: Must #include "time.h" now. * lib.c (time_s, time_local_s, time_utc_s, time_string_s, time_parse_s, year_s, month_s, day_s, hour_s, min_s, sec_s, dst_s, gmtoff_s, zone_s): Variable definitions removed. These are now in time.c. Also declared in time.h. (time_sec, time_sec_usec, gmtime_r, localtime_r, string_time, time_string_local, time_string_utc, broken_time_list, tm_to_time_struct, broken_time_struct, time_fields_local, time_fields_utc, time_struct_local, time_struct_utc, time_fields_to_tm, time_struct_to_tm, make_time_impl, make_time, epoch_tm, strptime_wrap, time_parse, setenv, unsetenv, timegm_hack, make_time_utc, time_meth, time_string_meth, time_parse_meth, time_parse_local, time_parse_utc): Functions removed. These are now in time.c. (time_init): Removed, and now in time.c as an external function. * lib.h (time_sec, time_sec_usec, time_string_local, time_string_utc, time_fields_local, time_fields_utc, time_struct_local, time_struct_utc, make_time, make_time_utc, time_parse, time_parse_local, time_parse_utc): Declarations removed. Now in time.h. * rand.c: Must #include "time.h" now. * time.c: New file. * time.h: New file.
* New function: reject.Kaz Kylheku2020-09-011-0/+45
| | | | | | | | | | | * eval.c (eva_init): Register reject intrinsic. * lib.c (appendl): New static function. (reject): New function. * lib.h (reject): Declared. * txr.1: Documented.
* Remove unnecessary forward declarations.Kaz Kylheku2020-08-171-2/+0
| | | | | | * lib.c (length_proper_list): Forward decl removed. * match.c (do_match_line): Likewise.
* cygwin: bugs in string catenation and splitting.Kaz Kylheku2020-08-071-4/+4
| | | | | | | | | | | | | | | | | | This looks like it originates in June 19, 2019 commit 28c6225fec6ce999806e9c077f08ed0e668646c4, before Version 218. The commit introduced the ability to use a character object as a separator in cat_str and split_str_keep. The implementation was broken as written for Cygwin. This finally showed up for me today as a failure in the new in6addr-str test cases. By chance, I noticed at the same time that tab completion in the listener was acting strangely on packag prefix symbols; that issue also went away with this fix. * lib.c (cat_str, vscat, scat3, split_str_keep): We must use wref on the array initialized using the wini macro, because on Cygwin, the string starts at [1] not [0].
* Change noreturn to NORETURN.Kaz Kylheku2020-08-071-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The noreturn macro is respelled to harmonize with the upper-case INLINE and NOINLINE. * lib.h (noreturn): Rename to NORETURN. * arith.c (not_number, not_integer, invalid_ops, invalid_op): Declaration updated. * arith.c (do_mp_error): Likewise. * eval.c (eval_error, no_bindable_error, dotted_form_error): Likewise. * eval.h (eval_error): Likewise. * lib.c (unsup_obj, callerror, wrongargs): Likewise. * match.c (sem_error): Likewise. * stream.c (unimpl, unimpl_put_string, unimpl_put_char, unimpl_put_byte, unimpl_get_line, unimpl_get_char, unimpl_get_byte, unimpl_unget_char, unimpl_unget_byte, unimpl_put_buf, unimpl_fill_buf, unimpl_seek, unimpl_truncate, unimpl_get_sock_family, unimpl_get_sock_type, unimpl_get_sock_peer, unimpl_set_sock_peer): Likewise. * struct.c (no_such_struct, no_such_slot, no_such_static_slot): Likewise. * unwind.h (jmp_restore, uw_throw, uw_throwf, uw_errorf, uw_errorfv, type_mismatch): Likewise.
* New sspl function.Kaz Kylheku2020-07-221-0/+5
| | | | | | | | | | | * eval (eval_init): Register sspl, an argument-reversed interface to split-str-set. * lib.c (sspl): New function. * lib.h (sspl): Declared. * txr.1: Documented.
* Support weak semantics in symbol packages.Kaz Kylheku2020-07-121-11/+12
| | | | | | | | | | | | | | | | | | | | | A package is weak if it holds weak references to symbols, meaning that if there are no references to a symbol other than its entry in a weak package, it can be removed from the package and reclaimed by the garbage collector. * eval.c (eval_init): Update registrations for make-package and sys:make-anon-package to reflect new optional argument. * lib.c (make_package_common): New argument weak. If it is true then both the hashes will have weak values. (make_package, make_anon_package): New optional argument weak. (obj_init): Add nil argument to calls to make_package. All the standard packages are regular, not weak. * lib.h (make_package, make_anon_package): Declarations updated. * txr.1: Documented.
* iter_item: bugfix/omission.Kaz Kylheku2020-07-081-0/+5
| | | | | * lib.c (iter_item): Fix neglect in this function to check for an oop iterator and use its iter-item method.
* uref/qref: read-print consistency issue.Kaz Kylheku2020-07-081-4/+12
| | | | | | | | | | | | | | | | The printer for uref/qref doesn't check for symbols that contain digits in the name, and so for instance (qref a3 3a) comes out as a3.3a, which does not read back, due to looking like a cramped floating-point constant. It looks like the weakest rule we can make is that all symbols that can be preceded by a dot must not have a name starting with a digit. Thus (qref 3a b c) can render as 3a.b.c. But (qref a 3b c) must not render as a.3b.c; it must be (qref a 3b c). * lib.c (simple_qref_args_p): Enact the above rule. In all positions preceded by a dot, if the symbol starts with a digit, then return nil to indicate that the syntax must be rendered in the list form.
* uref/qref: printing bug.Kaz Kylheku2020-07-081-1/+1
| | | | | | | | | | | * lib.c (obj_print_impl): The simple_qref_args_p function is called with reversed values of pos. For uref, we want 1, because the first item occurs after the dot, and for qref we want 0, because the first item occurs before the dot. This mix up means that the degenerate syntax (qref X) incorrectly prints as just X instead of (qref X). Likewise, though harmlessly, (uref X) fails to render as .X, instead coming out as (uref X).
* list_seq: return lazy list, not eager.Kaz Kylheku2020-07-081-8/+18
| | | | | | | | | * lib.c (list_seq_func): New static function. (list_seq): Convert to lazy processing. The iterator is threaded through the lazy cons's car field, so we don't have to mutate the function's environment. * txr.1: Added note about list-seq producing a lazy list.
* New: protocol for iteration with structs.Kaz Kylheku2020-07-071-26/+171
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * lib.c (seq_iterable): Return t if argument is a structure supporting the iter-begin method. (seq_iter_get_oop, seq_iter_peek_oop, seq_iter_get_fast_oop, seq_iter_peek_fast_oop): New static functions. (seq_iter_init_with_info): Handle COBJ case. If the COBJ is a structure which suports the iter-begin method, then retrieve the iterator object by calling it, and then prepare the iterator structure for either the fast or the canonical protocol based on whether the iterator supports iter-more. (seq_iter_mark): Mark the iter member if the iterator is a struct object. (iter_begin): Rearrange tests here to check object type first before sequence kind. If the object is a structure supporting the iter-begin method, then call it and return its value. (iter_more, iter_step): Check for struct object with corresponding special methods and return. (iter_reset): Similar change like in iter_begin. We check for the iter-reset special method and try to use it, otherwise fall back on the regular iter_begin logic. * lib.h (struct seq_iter): New member next of the ul union for caching the result of a peek operation. * struct.c (iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s): New symbol variables; (special_sym): Pointers to new symbol variables added to array. (struct_init): New symbol variables initialized. (get_special_required_slot): New function. * struct.h (iter_begin_s, iter_more_s, iter_item_s, iter_step_s, iter_reset_s): Declared. (enum special_slot): New enum members iter_begin_m, iter_more_m, iter_item_m, iter_step_m, iter_reset_m. (get_special_required_slot): Declared. * txr.1: Documented. * tests/012/oop-seq.expected: New file. * tests/012/oop-seq.tl: New file.
* seq_iter: reverse order of some tests.Kaz Kylheku2020-07-061-131/+129
| | | | | | | | | | | | | * lib.c (seq_iter_rewind, seq_iter_init_with_info): Reverse the order in which we perform some type tests. We check for some special types first, and then for the sequence kind. These situations are mutually exclusive now, so the order doesn't matter. The plan is to introduce the ability for a structure object to be iterable with special methods. Such an object won't necessarily be a sequence (seq_info may identify it as SEQ_NOTSEQ). This code will be prepared for it becuase it's now checking for the type first, before the sequence kind.
* New function: iterable.Kaz Kylheku2020-07-021-4/+43
| | | | | | | | | | | | * eval.c (eval_init): Register iterable intrinsic. * lib.c (seq_iterable): New static function. (nullify): Use seq_iterable to simplify function. (iterable): New function. * lib.h (iterable): Declared. * txr.1: Documented.
* c_num: now takes self argument.Kaz Kylheku2020-06-291-147/+198
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The c_num and c_unum functions now take a self argument for identifying the calling function. This requires changes in a large number of places. In a few places, additional functions acquire a self argument. The ffi module has the most extensive example of this. Some functions mention their name in a larger string, or have scattered literals giving their name; with the introduction of the self local variable, these are replaced by references to self. In the following changelog, the notation TS stands for "take self argument", meaning that the functions acquires a new "val self" argument. The notation DS means "define self": the functions in question defines a self variable, which they pass down. The notation PS means that the functions pass down an existing self variable to functions that now require it. * args.h (args_count): TS. * arith.c (c_unum, c_num): TS. (toint, exptv): DS. * buf.c (buf_check_len, buf_check_alloc_size, buf_check_index, buf_do_set_len, replace_buf, buf_put_buf, buf_put_i8, buf_put_u8, buf_put_char, buf_put_uchar, buf_get_bytes, buf_get_i8, buf_get_u8, buf_get_cptr, buf_strm_get_byte_callback, buf_strm_unget_byte, buf_swap32, str_buf, buf_int, buf_uint, int_buf, uint_buf): PS. (make_duplicate_buf, buf_shrink, sub_buf, buf_print, buf_pprint): DS. * chskum.c (sha256_stream_impl, sha256_buf, crc32_buf, md5_stream_impl, md5_buf): TS. (chksum_ensure_buf, sha256_stream, sha256, sha256_hash, md5_stream, md5, md5_hash): PS. (crc32_stream): DS. * combi.c (perm_while_fun, perm_gen_fun_common, perm_str_gen_fun, rperm_gen_fun, comb_vec_gen_fun, comb_str_gen_fun, rcomb_vec_gen_fun, rcomb_str_gen_fun): DS. * diff.c (dbg_clear, dbg_set, dbg_restore): DS. * eval.c (do_eval, gather_free_refs, maprodv, maprendv, maprodo, do_args_apf, do_args_ipf): DS. (op_dwim, me_op, map_common): PS. (prod_common): TS. * ffi.c (struct txr_ffi_type): release member TS. (make_ffi_type_pointer): PS and release argument TS. (ffi_varray_dynsize, ffi_array_in, ffi_array_put_common, ffi_array_get_common, ffi_varray_in, ffi_varray_null_term): PS. (ffi_simple_release, ffi_ptr_in_release, ffi_struct_release, ffi_wchar_array_get, ffi_array_release_common, ffi_array_release, ffi_varray_release): TS. (ffi_float_put, double_put, ffi_be_i16_put, ffi_be_u16_put, ffi_le_i16_put, ffi_le_u16_put, ffi_be_i32_put, ffi_be_u32_put, ffi_le_i32_put, ffi_sbit_put, ffi_ubit_put, ffi_buf_d_put, make_ffi_type_array, make_ffi_type_enum, ffi_type_compile, make_ffi_type_desc, ffi_make_call_desc, ffi_call_wrap, ffi_closure_dispatch_save, ffi_put_into, ffi_in, ffi_get, ffi_put, carray_set_length, carray_blank, carray_buf, carray_buf_sync, carray_cptr, carray_refset, carray_sub, carray_replace, carray_uint, carray_int): PS. (carray_vec, carray_list): DS. * filter.c (url_encode, url_decode, base64_stream_enc_impl): DS. * ftw.c (ftw_callback, ftw_wrap): DS. * gc.c (mark_obj, gc_set_delta): DS. * glob.c (glob_wrap): DS. * hash.c (equal_hash, eql_hash, eq_hash, do_make_hash, hash_equal, set_hash_traversal_limit, gen_hash_seed): DS. * itypes.c (c_i8, c_u8, c_i16, c_u16, c_i32, c_u32, c_i64, c_u64, c_short, c_ushort, c_int, c_uint, c_long, c_ulong): PS. * lib.c (seq_iter_rewind): TS and becomes internal. (seq_iter_init_with_info, seq_setpos, replace_str, less, replace_vec, diff, isec, obj_print_impl): PS. (nthcdr, equal, mkstring, mkustring, upcase_str, downcase_str, search_str, sub_str, cat_str, scat2, scat3, fmt_join, split_str_keep, split_str_set, trim_str, int_str, chr_int, chr_str, chr_str_set, vector, vecref, vecref_l, list_vec, copy_vec, sub_vec, cat_vec, lazy_str_put, lazy_str_gt, length_str_ge, length_str_lt, length_str_le, cptr_size_hint, cptr_int, out_lazy_str, out_quasi_str, time_string_local_time, time_string_utc, time_fields_local_time, time_fields_utc, time_struct_local, time_struct_utc, make_time, time_meth, time_parse_meth): DS. (init_str, cat_str_init, cat_str_measure, cat_str_append, vscat, time_fields_to_tm, time_struct_to_tm, make_time_impl): TS. * lib.h (seq_iter_rewind): Declaration removed. (c_num, c_unum, init_str): Declarations updated. * match.c (LOG_MISMATCH, LOG_MATCH): PS. (h_skip, h_coll, do_output_line, do_output, v_skip, v_fuzz, v_collect): DS. * parser.c (parser, circ_backpatch, report_security_problem, hist_save, repl, lino_fileno, lino_getch, lineno_getl, lineno_gets, lineno_open): DS. (parser_set_lineno, lisp_parse_impl): PS. * parser.l (YY_INPUT): PS. * rand.c (make_random_state): PS. * regex.c (print_rec): DS. (search_regex): PS. * signal.c (kill_wrap, raise_wrap, get_sig_handler, getitimer_wrap, setitimer_wrap): DS. * socket.c (addrinfo_in, sockaddr_pack, fd_timeout, to_connect, open_sockfd, sock_mark_connected, sock_timeout): TS. (getaddrinfo_wrap, dgram_set_sock_peer, sock_bind, sock_connect, sock_listen, sock_accept, sock_shutdown, sock_send_timeout, sock_recv_timeout, socketpair_wrap): DS. * stream.c (generic_fill_buf, errno_to_string, stdio_truncate, string_out_put_string, open_fileno, open_command, base_name, dir-name): DS. (unget_byte, put_buf, fill_buf, fill_buf_adjust, get_line_as_buf, formatv, put_byte, test_set_indent_mode, test_neq_set_indent_mode, set_indent_mode, set_indent, inc_indent, set_max_length, set_max_depth, open_subprocess, run ): PS. (fds_subst, fds_swizzle): TS. * struct.c (make_struct_type, super, umethod_args_fun): PS. (method_args_fun): DS. * strudel.c (strudel_put_buf, strudel_fill_buf): DS. * sysif.c (errno_wrap, exit_wrap, usleep_wrap, mkdir_wrap, ensure_dir, makedev_wrap, minor_wrap, major_wrap, mknod_wrap, mkfifo_wrap, wait_wrap, wifexited, wexitstatus, wifsignaled, wtermsig, wcoredump, wifstopped, wstopsig, wifcontinued, dup_wrap, close_wrap, exit_star_wrap, umask_wrap, setuid_wrap, seteuid_wrap, setgid_wrap, setegid_wrap, simulate_setuid_setgid, getpwuid_wrap, fnmatch_wrap, dlopen_wrap): DS. (chmod_wrap, do_chown, flock_pack, do_utimes, poll_wrap, setgroups_wrap, setresuid_wrap, setresgid_wrap, getgrgid_wrap): PS. (c_time): TS. * sysif.h (c_time): Declaration updated. * syslog.c (openlog_wrap, syslog_wrap): DS. * termios.c (termios_pack): TS. (tcgetattr_wrap, tcsetattr_wrap, tcsendbreak_wrap, tcdrain_wrap, tcflush_wrap, tcflow_rap, encode_speeds, decode_speeds): DS. * txr.c (compato, array_dim, gc_delta): DS. * unwind.c (uw_find_frames_by_mask): DS. * vm.c (vm_make_desc): PS. (vm_make_closure, vm_swtch): DS.
* New functions: list-seq, ved-seq and str-seq.Kaz Kylheku2020-06-281-0/+46
| | | | | | | | | | | | | | These functions convert any iterable to a list, vector or string. * eval.c (eval_init): Registered list-seq, vec-seq and str-seq intrinsics. * lib.c (list_seq, vec_seq, str_seq): New functions. * lib.h (list_seq, vec_seq, str_seq): Declared. * txr.1: Documented.
* itypes: remove silly itypes_little_endian.Kaz Kylheku2020-06-191-1/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Curiously, the itypes_little_endian variable was introduced in April 2017, in a the first commit implementing buffers. The variable was initialized, but never referenced. About a month after that, I introduced HAVE_LITTLE_ENDIAN config.h macro, which is always present and defined as 0 or 1. This was used right away in the implementation of FFI bitfields, and later on in other FFI work. In spite of HAVE_LITTLE_ENDIAN, almost exactly a year after itypes_little_endian was introduced, I mistakenly put that variable to use instead of recognizing it as superfluous and removing it in favor of using HAVE_LITTLE_ENDIAN. That is what I'm doing now. * itypes.c (itypes_little_endian): Variable removed. (itypes_init): Function removed, since its only job is to initialize this variable. * itypesl.h (itypes_little_endian, itypes_init): Declarations removed. * lib.c (init): Call to itypes_init removed. * parser.c (read_file_common): Instead of itypes_little_indian, use HAVE_LITTLE_ENDIAN, which the configuration script always exists and is defined as 0 or 1.
* seq_iter_rewind: support iterables.Kaz Kylheku2020-06-051-0/+31
| | | | | | | This fixes cases like (isec 0..10 5..15). * lib.c (seq_iter_rewind): Handle the rewinding of non-sequence iterators (ranges, characters, numbers).
* nullify and empty: convert to seq_info.Kaz Kylheku2020-06-051-67/+28
| | | | | | | | | | | | | | | | | Issues: (nullify 1..1) doesn't return nil as it should, and (empty 1) fails. * lib.c (seq_info): Since we would like nullify to use seq_info, seq_info cannot call nullify. Transplant the probing of the nullify method out of nullify and into here. (nullify): Obtain a seq_info_t on the object. If it's not a sequence and is not range, then just return the object. Otherwise obtain a seq_iter_t sequence and peek whether it has a next item. (empty): Similarly, obtain an iterator and peek. Definition of empty is relocated to be next to nullify. * lib.h: Relocate declaration of empty, also.
* transpose: ensure variadic args are a list.Kaz Kylheku2020-06-051-3/+3
| | | | | | | | | * lib.c (transpose): Don't simply copy the input, but convert it to a list with tolist. The transposev function relies on mapcarv, and that function now doesn't simply pull out the trailing object from the args. It processes the args as args, and a non-list causes problems there, such as args_count reporting the wrong value.
* seq_iter: support reverse iteration for ranges.Kaz Kylheku2020-06-041-28/+110
| | | | | | | | | | | | | | | | | | | | | | * lib.h (seq_iter_t): Add some union members to ui and ul for use with ranges. The purpose of this is to have separate naming for the iteration variables for ranges. * lib.c (seq_iter_get_range_cnum, seq_iter_peek_range_cnum, seq_iter_get_range_chr, seq_iter_peek_range_chr, seq_iter_get_range_bignum, seq_iter_peek_range_bignum): Use new union member names for clarity. (seq_iter_get_rev_range_cnum, seq_iter_peek_rev_range_cnum, seq_iter_get_rev_range_chr, seq_iter_peek_rev_range_chr, seq_iter_get_rev_range_bignum, seq_iter_peek_rev_range_bignum): New static functions. (seq_iter_with_init_info): Drop obj argument since the si argument has the object. Add handling for empty and reversed ranges. Also convert unbounded ranges like N..t and N..: to just N, recursively. Use new union member names. (seq_iter_init, seq_iter_init_with_rewind, iter_begin, iter_reset): Follow elimination of argument in seq_iter_init_with_info.
* seq_iter: null list object if rewind not needed.Kaz Kylheku2020-06-041-6/+14
| | | | | | | | | | | | * lib.c (seq_iter_init_with_info): Take new Boolean argument indicating whether the iterator needs to support the rewind operation. If this is false, and the object is a list, then we clobber the object, in order to eliminate the GC root. (seq_iter_init, iter_begin, iter_reset): Pass 0 for the new Boolean parameter. (seq_iter_init_with_rewind): New function. (diff, isec): Use seq_iter_init_with_rewind to request an iterator with rewind support for the second sequence.
* Convert mapping functions to new iterators.Kaz Kylheku2020-06-031-39/+24
| | | | | | | | | | | | | | | | | * eval.c (get_iter_f): Renamed to iter_from_binding_f. (iter_begin_f, iter_more_f, iter_item_f, iter_step_f): New global variables. (op_each): Follow rename of get_iter_f. (mapcarv, mappendv, lazy_mapcar_func, lazy_mapcar, lazy_mapcarv_func, lazy_mapcarv, mapdov, prod_common): Convert from car/cdr/null-test iteration to iter-begin. (eval_init): gc-protect and initialize new variables. * lib.c (mapcar_listout, mappend, mapdo): Convert to seq_iter iteration. List argument renamed to seq. (mapcar): List argument renamed to seq. * lib.h: Declarations updated with renamed arguments.
* seq_iter: integers and ranges become iterable.Kaz Kylheku2020-06-031-14/+172
| | | | | | | | | | | | | | | | | | | | | I think with the iter-begin API, we have the the "Maxwell's Equations of Iteration". This not only works well for conses, but it extends to numbers and ranges. * lib.h (seq_iter_t): New union member ul, containing a new member lim. existing len member moved into ul union. * lib.c (seq_iter_get_vec, seq_iter_peek_vec): Adjust access of len, now wrapped in union. (seq_iter_get_range_cnum, seq_iter_peek_range_cnum, seq_iter_get_range_chr, seq_iter_peek_range_chr, seq_iter_get_range_bignum, seq_iter_peek_range_bignum, seq_iter_get_chr, seq_iter_peek_chr, seq_iter_get_num, seq_iter_peek_num): New static functions. (seq_iter_init_with_info): Support RNG object, further classifying the iteration based on the type of the from element. Support numbers and characters as iterable. (iter_begin, iter_more, iter_item, iter_step, iter_reset): Support numbers directly as unencapsulated iterators.
* New style iteration functions.Kaz Kylheku2020-06-021-2/+109
| | | | | | | | | | | | | | | | | | | | | | | | iter-begin provides a paradigm for iteration that is more compatible with lists. If the sequence is a list, then the list itself is returned as the iterator, and the other functions are basicaly wrappers for car/cdr and null testing. Yet the API is defined in such a way that other objects can be iterated with good efficiency, at the cost of allocating a new iterator object (which can be re-used). * eval.c (eval_init): Register iter-begin, iter-more, iter-item, iter-step and iter-reset. * lib.c (seq_iter_init_with_info): New static function. (seq_iter_init): Now a thin wrapper for seq_iter_init_with_info. (iter_begin, iter_more, iter_item, iter_step, iter_reset): New functions. * lib.h (iter_begin, iter_more, iter_item, iter_step, iter_reset): New functions. * txr.1: Documented.
* Replace trivial format(nil, ...) with simpler ops.Kaz Kylheku2020-05-301-0/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * gencadr.txr (cadr_register): Use scat2 to glue two strings. * cadr.c: Regenerated. * lib.c (scat2, scat3): New functions. * lib.h (scat2, scat3): Declared. * liblib.c (place_instantiate, ver_instantiate, ifa_instantiate, txr_case_instantiate, with_resources_instantiate, path_test_instantiate, struct_instantiate, with_stream_instantiate, hash_instantiate, except_instantiate, type_instantiate, yield_instantiate, sock_instantiate, termios_instantiate, awk_instantiate, build_instantiate, trace_instantiate, getopts_instantiate, package_instantiate, getput_instantiate, tagbody_instantiate, pmac_instantiate, error_instantiate, keyparams_instantiate, ffi_instantiate, doloop_instantiate, stream_wrap_instantiate, asm_instantiate, compiler_instantiate, debugger_instantiate, op_instantiate, save_exe_instantiate, defset_instantiate, copy_file_instantiate): Use scat2 to glue two strings instead of format. * parser.c (find_matching_syms, hist_save, repl): Replace trivial uses of format with scat2 or scat3. * sysif.c (ensure_dir): Likewise. * txr.c (get_self_path, substitute_basename, sysroot, sysroot_init, parse_once_noerr, read_compiled_file_noerr, read_eval_stream_noerr): Likewise. * unwind.c (uw_unwind_to_exit_point): Likewise.
* quasistrings: reduce consing.Kaz Kylheku2020-05-301-6/+34
| | | | | | | | | | | | | | | | | | | Quasistrings compile to code that requires on the sys:fmt-join function to glue strings together. Rewriting that function to avoid converting its arguments from struct args * to a list. * eval.c (fmt_join): Static function removed. * lib.c (cat_str_measure, cat_str_append): more_p parameter changed to int type, which better matches the C style Boolean values it takes. (fmt_join): New external function. * lib.h: Declared. * args.h (args_more_nozap, args_get_nozap): New inline functions allowing multiple iterations over arguments without making a copy.
* search, rsearch: rewrite using seq_info and bugfix.Kaz Kylheku2020-05-251-114/+86
| | | | | | | | | | | | | | | * lib.c (seq_getpos, seq_setpos): New functions. * lib.h (seq_getpos, seq_setpos): Declared. (search_list, rsearch_list): Static functions removed. (search_common): New static function. (search, contains, rsearch): These functions are now trivial wrappers around search_common. A requirement problem is fixed in rsearch: when the key is empty, the length of sequence is returned rather than zero, because zero is obviously not the right most place where an empty key matches. * txr.1: Documentation updated.
* update: convert to seq_info.Kaz Kylheku2020-05-251-14/+11
| | | | | * lib.c (update): Function converted to seq_info classification instead of switching on type.
* rsearch: fix broken.Kaz Kylheku2020-05-251-4/+2
| | | | | | | | | | | The rsearch function is completely broken, returning incorrect values. * lib.c (search_list, rsearch_list): Update the position in the increment part of the loop. This fix only affects research, but the code is copy-and-paste, so it's good to keep them the same, and the position of the pos update is a code smell regardless.
* search/rsearch: wrong object in bad key diagnostic.Kaz Kylheku2020-05-251-2/+2
| | | | | * lib.c (search_list, rsearch_list): When the key has a bad type, don't report the seq object in its place.
* lib: combine cat_str and vscat implementations.Kaz Kylheku2020-05-241-105/+91
| | | | | | | | | | | | | | | | | | | | | | | The vscat function is white-box copy of cat_str, with just the iteration over the inputs done differently, and without the support for separators that are characters instead of strings (which was added to cat_str after vscat was forked. In this patch, the common logic underlying both functions is factored out into a small ad-hoc "struct cat_str" object which maintains the state and provides the operations to measure the pieces of the string, allocate the space, copy the pieces together and produce the resulting object. The motivation here isn't just to reduce duplication. I would like a more efficient function for catenating strings which takes a "struct args *", not requiring a list to be consed up. * lib.c (struct cat_str): New struct type. (cat_str_init, cat_str_measure, cat_str_alloc, cat_str_append, cat_str_get): New static functions. (cat_str, vscat): Considerably shorten by using the above functions.
* New assert macro.Kaz Kylheku2020-05-181-0/+7
| | | | | | | | | | | | * eval.c (rt_assert_fail, me_assert): New static functions. (eval_init): assert macro and sys:rt-assert-fail function registered. * lib.c (func_n4ov): New function. * lib.h (func_n4ov): Declared. * txr.1: Documented.
* lib: sort becomes non-destructive; nsort introduced.Kaz Kylheku2020-05-131-7/+37
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I'm fixing a historic mistake copied from ANSI Lisp, which trips up language newcomers and sometimes even experienced users. The function innocently named sort will now return newly allocated structure. The function previously called sort will be available as nsort (non-consing/allocating sort). The shuffle function also becomes pure, and is accompanied by nshuffle. * eval (me_op): Continue to use destructive sort in this legacy code that is only triggered in very old compat mode. (eval_init): Registered nsort and nshuffle. * lib.c (nsort, nshuffle): New functions introduced, closely based on sort and shuffle. (sort, shuffle): Rewritten to avoid destructive behavior: work by copying the input and calling destructive counterparts. (sort_group): Continue to use destructive sort, which is safe; the structure is locally allocated. The sort_group function has pure semantics. (grade): Likewise. * lib.h (nsort, nshuffle): Declared. * share/txr/stdlib/getopts.tl (opthelp): Replace an instance of the (sort (copy-list ...)) pattern with just (sort ...). * tags.tl (toplevel): Continue to use destructive sort to sort tags before writing the tag file; the lifetime of the tags list ends when the file is written. * tests/010/seq.txr: Switch some sort calls to nsort to keep test case working. * txr.1: Documented.
* lib: use seq-info for sort and shuffle.Kaz Kylheku2020-05-131-19/+25
| | | | | | | | * lib.c (sort, shuffle): Switch to seq_info. For consistency with sort, shuffle now handles hashes in the same peculiar way. * txr.1: Document hash behavior for sort and shuffle.
* funcall: fight spurious retention.Kaz Kylheku2020-05-051-4/+4
| | | | | * lib.c (funcall1, funcall2, funcall3, funcall4): Add forgotten argument zaps in the case that routes to generic_funcall.
* lib: convert counting and predicate quantifying to seq_info.Kaz Kylheku2020-05-051-65/+88
| | | | | | * lib.c (countqual, countql, countq, count_if, some_satisfy, all_satisfy, none_satisfy): Convert from list iteration to seq_info.
* lib/buf: use unsigned integers around allocations.Kaz Kylheku2020-04-251-3/+3
| | | | | | | | | | * buf.c (buf_shrink): Convert len to alloc size using c_unum, which will reject negative values that will implicitly convert to a wrong/huge size. * lib.c (upcase_str, downcase_str): Similar reasoning. (sub_vec): nelem is a size_t, so use unum on it, rather than num.
* printer: add package prefix on symbols with zero-length name.Kaz Kylheku2020-04-221-0/+3
| | | | | | * lib.c (symbol_needs_prefix): If the name is an empty string, the symbol needs a prefix. Otherwise it disappears in the output.
* seq_info: bugfix: uninitialized type field.Kaz Kylheku2020-04-201-1/+1
| | | | | | | | | | | * lib.c (seq_info): Ensure the type field in the returned structure is inintialized. We are neglecting this when the type is COBJ, initializing only the kind field. This makes the in function behave unreliably over arguments that are hash tables, or vector-like sequences (objects with a length function or the carray type). Several other functions will behave unreliably for vector-like sequences: reverse, find, rfind, pos, rpos and tprint.
* warning cleanup: GNU C++ initializer warnings.Kaz Kylheku2020-04-061-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | This is the eight and final round of an effort to enable GCC's -Wextra option. The C++ compiler, with -Wextra, doesn't like C's universal struct initializer { 0 }, individually complaining about all the remaining members not being initialized. What works in C++ is the { } initializer. Conditional definition to the rescue. * lib.h (all_zero_init): New macro which expands to { } under C++, and { 0 } under C. * lib.c (make_time_impl, epoch_tm, time_string_meth, time_parse_meth): Use all_zero_init. * parser.c (prime_parser): Likewise. * socket.c (sock_mark_connected): Likewise. * sysif.c (fcntl_wrap): Likewise. * termios.c (encode_speeds, decode_speeds): Likewise. * configure (diag_flags): Add -Wextra.
* warning cleanup: suspicious switch fallthrough cases.Kaz Kylheku2020-04-051-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the seventh round of an effort to enable GCC's -Wextra option. Warnings about switch fallthrough situations are addressed. GCC now has a diagnostic for this that is enabled by -Wextra in such a way that if a fallthrough comment is present, the diagnostic is suppressed. In much of the code, we have such a comment. It's missing in a few places, or misplaced. There are also some real bugs. * hash.c (hash_buf): Add fallthrough comments to intentional fallthrough cases. (hash_hash_op): bugfix: add break statement. The 32 and 64 bit cases are independent (at compile time). * lib.c (cdr, nullify, list_collect, empty): Add fallthrough comment. (int_str): Add missing break. This has not caused a bug though because setting the octzero flag in the zerox case is harmless to the logic which follows. * linenoise.c (edit): Move misplaced fallthrough. * sysif.c (fcntl_wrap): Bugfix: add missing break, without which errno is tampered to hold EINVAL, in spite of a successful F_SETLK, F_SETLKW or F_GETLK operation. * unwind.h (jmp_restore): Declare noreturn, so that GCC does not issue a false positive warning about a fallthrough in uw_unwind_to_exit_point. * utf8.c (utf8_from_buf, utf8_decode): Move a fallthrough comment outside of preprocessing, so it is properly processed by GCC's diagnostic.
* warning cleanup: missing member initializers.Kaz Kylheku2020-04-051-7/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the sixth round of an effort to enable GCC's -Wextra option. Warnings about uninitialized members are addressed. I am not happy with what had to be done in linenoise.c. We just need a dummy circular list node for the lino_list, which we achieved with a dummy all zero struture, with statially initialized next and prev pointers. There are way too many members to initialize, including one that has struct termios type containing a nonportable set of members. On the plus size, the lino_list structure now moves into the BSS section, reducing the executable size slightly. * lib.c (cptr_ops): Initialize using cobj_ops_init, which has all the initializers already, and should have been used for this in the first place. * linenoise/linenoise.c (lino_list): Remove initializer, which eliminates the warning about some members not being initialized. (lino_init): Initialize the next and prev pointers here. * parser.c (parser_ops): Initialize with cobj_ops_init. * stream.h (stdio_mode_init_blank, stdio_mode_init_r, stdio_mode_init_rpb): Add initializer corresponding to redir array member of struct stdio_mode. * sysif.c (cptr_dl_ops): Use cobj_ops_init. * tree.c (tree_iter_init): Add initializer for the path array member of struct tree_iter. (tr_rebuild): Initialize all fields of the dummy object. Since it's a union, we just have to deal with the any member. There are two layouts for the obj_common fields based on whether CONFIG_GEN_GC is enabled. This is ugly, but occurs in one place only.
* warning cleanup: signed/unsigned in ternaries.Kaz Kylheku2020-04-051-2/+2
| | | | | | | | | | | | | | | | | | | | | This is the third round of an effort to enable GCC's -Wextra option. Instances of signed/unsigned mismatch between the branches of ternary conditionals are addressed. * ffi.c (pad_retval): Add cast into the consequent of the conditional so it yields size_t, like the alternative. * lib.c (split_str_keep): Likewise. (vector): Cast -1 to ucnum so it has the same type as the alloc_plus opposite to it. * parser.c (lino_getch): Add a cast to wint_t to match return value and opposite WEOF operand. * stream.c (generic_get_line): Likewise. * sysif.c (c_time): Convert both consequent and alternative to time_t to silence warning.
* warning cleanup: remove unused parameters.Kaz Kylheku2020-04-051-9/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the second round of an effort to enable GCC's -Wextra option. All function parameters that are unused and are removable are removed. They are eliminated from the function defintions, declarations, callers, and any related function pointer variables or structure members. * arith.c (nary_simple_op): Remove unused self parameter. See lib.c: maxv, minv. * chksum.c (crc32_buf, crc32_str): Remove unused self parameter. (crc32): Don't pass self to the above functions. * eval.c (copy_env_handler, copy_bh_env_handler): Remove unused parent parameter. See unwind.c. (supplement_op_syms): Remove unused max parameter. (me_op): Don't pass max to supplement_op_syms. * lib.c (seq_iter_rewind): Remove unused self parameter. (lazy_flatten_func): Remove unused env parameter. (lazy_flatten): Use func_n1 to create non-environment-carrying funtion out of lazy_flatten_func. (maxv, minv): Don't pass self parameter to nary_simple_op. (middle_pivot): Remove unused lessfun param. (quicksort): Don't pass lessfun to middle_pivot. (diff, isec): Don't pass self to seq_iter_rewind. * lib.h (seq_iter_rewind, nary_simple_op): Declarations updated. * struct.c (get_super_slots): Remove unused self parameteer. (make_struct_type): Don't pass self to get_super_slots. * sysif.c (flock_unpack): Remove unused self parameter. (fcntl_wrap): Don't pass self to flock_unpack. * unwind.c (uw_push_cont_copy): Remove parent parameter from function pointer parameter. See eval.c: copy_env_handler. (call_copy_handlers): Remove parent parameter and don't pass that argument to the indirect call via pointer to the copy handler function. (revive_cont, capture_cont): Don't pass 0 value to removed parent parameter of call_copy_handlers. * unwind.h (struct uw_cont_copy): Function pointer member copy loses parent parameter. (uw_push_cont_copy): Declaration updated.