summaryrefslogtreecommitdiffstats
path: root/eval.c
Commit message (Collapse)AuthorAgeFilesLines
...
* lazy-stream-cons: control close throwing behavior.Kaz Kylheku2021-08-071-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | * eval.c (eval_init): Update registrations of lazy-stream-cons and get-lines with one more optional argument. * lib.c (simple_lazy_stream_func_nt, lazy_stream_func_nt): New static functions. (lazy_stream_cons): Take a new argument, no_throw_close, defaulting it to nil. When calling close_stream directly, pass the inverted value of no_throw_close. Choose the new _nt functions for the lazy list if no_throw_close is true; those functions pass nil as the second argument of close_stream. * lib.h (lazy_stream_cons): Declaration updated. * match.c (v_next_impl, open_data_source, match_fun): Pass down the nothrow value to lazy_stream_cons, or else nil in situations when that is not applicable or there is no such value. Thus the :nothrow feature of v_next will now not only ensure that there is no exception when opening the stream but also when closing it. Unusual situations encountered when the lazy list reads from the stream still throw. * txr.1: Documented.
* gc: problem in environment-copying functions.Kaz Kylheku2021-07-291-7/+12
| | | | | | | | | | | | | * eval.c (copy_env, deep_copy_env): These functions are not following a protocol for object construction that is correct under generational GC. They are allocating a new object with make_obj first, and then calling functions to copy the constituent elements to populate into the object with a direct assignment. This direct assignment is wrong; the set macro is required. A better fix, rather than using the set macro, is to copy the constituent parts first, holding them in local variables, then allocate the new object, and finally, without doing any other memory allocating operations, assign the constituent parts into the new object.
* places: remove unnecessary zap_s variable.Paul A. Patience2021-07-271-2/+1
| | | | | | | | | | | | The zap_s variable is a vestige of op_modplace. It must have been missed when op_modplace was removed in commit 209e731429a0fd890ec6d922c1efc6f02d81a032. * eval.c (zap_s): Delete variable. (eval_init): Remove initialization of zap_s. * protsym.c (zap_s): Remove extern variable declaration. (protected_sym): Remove reference to zap_s.
* hash: change make_hash interface.Kaz Kylheku2021-07-221-20/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The make_hash function now takes the hash_weak_opt_t enumeration instead of a pair of flags. * hash.c (do_make_hash): Take enum argument instead of pair of flags. Just store the option; nothing to calculate. (weak_opt_from_flags): New static function. (tweak_hash): Function removed. (make_seeded_hash): Adjust to new do_make_hash interface with help from weak_opt_from_flags. (make_hash, make_eq_hash): Take enum argument instead of pair of flags. (hashv): Calculate hash_weak_opt_t enum from the extracted flags, pass down to make_eq_hash or make_hash. * hash.h (tweak_hash): Declration removed. (make_hash, make_eq_hash): Declarations updated. * eval.c (me_case, expand_switch): Update make_hash calls to new style. (eval_init): Update make_hash calls and get rid of tweak_hash calls. This renders the tweak_hash function unused. * ffi.c (make_ffi_type_enum, ffi_init): Update make_hash calls to new style. * filter.c (make_trie, trie_add, filter_init): Likewise. * lib.c (make_package_common, obj_init, obj_print): Likewise. * lisplib.c (lisplib_init): Likewise. * match.c (dir_tables_init): Likewise. * parser.c (parser_circ_def, repl, parse_init): Likewise. * parser.l (parser_l_init): Likewise. * struct.c (struct_init, get_slot_syms): Likewise. * sysif.c (get_env_hash): Likewise. * lex.yy.c.shipped, y.tab.c.shipped: Updated.
* hash: support both semantics of weak keys + values.Kaz Kylheku2021-07-211-15/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Hash tables with weak keys and values now support a choice of both possible semantics: under and-semantics, an entry lapses when both the key and value are unreachable. Under or-semantics, an entry lapses if either the key or value is unreachable. The and-semantics is new. Until TXR 266, only or-semantics was supported. This will be the default: when a hash table is specified as :weak-keys and :weak-vals, it will have or-semantics. The keywords :weak-or and :weak-and specify weak keys and values, with the specific semantics. They are utually exclusive, but tolerate the presence of :weak-keys and :weak-vals. The make-hash function is being extended such that if its leftmost argument, <weak-keys>, is specified as one of the keywords :weak-and or :weak-or, then the hash table will have weak keys and values with the specified semantics, and the <weak-vals> argument is ignored (values are weak even if that argument is false). * eval.c (eval_init): Initially register the top_vb, top_mb, top_smb, special and builtin hashes as ordinary hashes: no weak keys or values. Then use tweak_hash to switch to weak keys+vals with and-semantics. We do it this way because the keywords are not yet initialized; we cannot use them. * hash.h (enum hash_flags, hash_flags_t): Moved to header. Member hash_weak_both renamed to hash_weak_or. New member hash_weak_and. (weak_and_k, weak_or_k): New keyword variables. (hash_print_op): Handle hash_weak_and by printing :weak-and. (hash_mark): Handle hash_weak_and by marking nothing, like hash_weak_or. (do_make_hash): Check first argument against the two new keywords and set flags accordingly. This function is called from eval_init before the keywords have been initialized, in which case weak_keys == weak_and_k is true when both are nil; we watch for that. (tweak_hash): Now returns void and takes a hash_flags_t argument which is simply planted. (do_wak_tables): Implement hash_weak_and case. Remove the compat 266 stuff from hash_weak_or. Compatibility is no longer required since we are not changing the default semantics of hash tables. Phew; that's a load of worry off the plate. (hashv): Parse the two new keywords, validate and provide semantics. (hash_init): Initialize weak_and_k and weak_or_k kewyords. * hash.h (enum hash_flags, hash_flags_t): Moved here now. (weak_and_k, weak_or_k): Declared. * lib.c (compat_fixup): Remove call to parse_compat_fixup. * parser.c (parse_init): Create stream_parser_hash with and-semantics. (parse_compat_fixup): Function removed. * parser.h (parse_compat_fixup): Declaration removed. * txr.1: Hash documentation updated.
* compat: fix glaringly broken init-time handling.Kaz Kylheku2021-07-211-22/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We are doing numerous compat_ver checks in various init functions, to enact alternative symbol registrations. Only problem is, compat_ver is always zero during initialization; it is not set until the -C option is processed in txr_main. Registrations must be fixed up after initialization; that's what the compat_fixup mechanism is for. This is an long-standing problem which affects compatibility operation going back over 150 versions. * arith.c (arith_init): Move compat logic to arith_compat_fixup. (arith_compat_fixup): New function. * arith.h (arith_compat_fixup): Declared. * eval.c (eval_init): Move compat logic to eval_compat_fixup. * ffi.c (ffi_init): Move compat logic to ffi_compat_fixup. (ffi_compat_fixup): New function. * ffi.h (ffi_compat_fixup): Declared. * regex.c (regex_init): Move compat logic to regex_compat_fixup. (regex_compat_fixup): New function. * regex.h (regex_compat_fixup): Declared. * stream.c (stream_init): Move compat logic to stream_compat_fixup. (stream_compat_fixup): New function. * stream.h (stream_compat_fixup): Declared. * struct.c (struct_init): Move compat logic to struct_compat_fixup. (struct_compat_fixup): New function. * struct.h (stream_compat_fixup): Declared. * lib.c (compat_fixup): Call arith_compat_fixup, ffi_compat_fixup, regex_compat_fixup, stream_compat_fixup and struct_compat_fixup.
* parse/eval: use weak-both hash tables.Kaz Kylheku2021-07-201-6/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | This addresses the problem that a4c376979d15323ad729e92e41ba43768e8dc163 tried to fix. * eval.c (eval_init): Make all the top-level binding tables, top_fb, top_vb, top_mb, top_smb, special and builtin, weak-both tables: keys and values are weak. This way, the entries disappear if both key and value are unreachable, even if they refer to each other. (eval_compat_fixup): In 266 or earlier compat mode, weak-both tables don't have the right semantics, so we tweak the tables to weak-key tables. * parser.c (parse_init): Same treatment for stream_parser_hash. We want an entry to disappear from the hash if neither the parser nor the stream are reachable. (parse_compat_fixup): New function. * parser.h (parse_compat_function): Declared. * hash.c, hash.h (tweak_hash): New function. * lib.c (compat_fixup): Call parse_compat_fixup.
* lib: new function separate.Paul A. Patience2021-07-091-0/+1
| | | | | | | | | | | | * lib.c (separate): New function. * lib.h (separate): Declared. * eval.c (eval_init): Register separate intrinsic. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* defstruct: diagnose built-in type being redefined.Kaz Kylheku2021-07-091-0/+1
| | | | | | | | | | | | | | | | | | * eval.c (eval_init): Register built-in-type-p intrinsic. * lib.c (buitin_type_p): Rename to built_in_type_p since the word built-in is hyphenated. The function also tests whether the argument is a COBJ class. (cobj_class_exists): Function removed. * stdlib/doc-syms.tl: Updated. * stdlib/struct.tl (defstruct): Add built-in-type-p check. * struct.c (make_struct_type): Call only built_in_type_p; cobj_class_exists is gone. * txr.1: Document built-in-type-p.
* expander: use expand_error everywhere.Kaz Kylheku2021-07-051-66/+66
| | | | | | | | | | | | | | | All expand-time errors go through expand_error for compiler-stylen error reporting. * eval.c (not_bindable_error, dotted_form_error): Only expander helper functions call these helpers so they get switched to expand_error. (expand_opt_params_rec, expand_params_rec, expand_param_macr, expand_lisp1, expand_lisp1_value, expand_lisp1_setq, expand_setqf, expand_qquote_rec, expand_vars, expand_fbind_vars, me_case, me_whilet, me_iflet_whenlet, me_load_for, expand_catch_clause, expand_switch, me_l1_val, me_l1_setq): Use expand_error instead of eval_error.
* macro params: use compiler-style error reporting.Kaz Kylheku2021-07-051-27/+30
| | | | | | | | | | | | | | | | | * eval.c (bind_macro_params): Take the error reporting function as a functional argument which points either to eval_error or expand_error. Update all the recursive calls to pass it down. Replace all calls to eval_error with this function pointer. (me_interp_macro, op_mac_param_bind, op_mac_env_param_bind): Call bind_macro_params with expand_error as the function, so that under the right circumstances, the error will go to standard error, as well as being thrown as an exception. In the case of me_interp_macro, this is clear. The reason we do it for the other two is that their primary use case is inside macros. (op_tree_case, op_tree_bind): Pass eval_error to mac_param_bind, preserving existing behavior.
* expander: tighten up syntax checking.Kaz Kylheku2021-07-051-57/+101
| | | | | | | | | | | | | | | | | | | | | | | | | | * eval.c (eval_exception): New parameter distinguishes whether this function is called from expansion time. If so, it behaves similarly to the compile-error funtion in error.tl: if a recursive load is in effect, a there is no error handler, then deferred warnings are dumped to standard error, followed by the error message. Then the exception is thrown. (eval_error, eval_warn): Pass zero to eval_exception to indicate that this is not expansion time. (expand_error, missing_arg_error, excess_args_error, no_dot_check, syn_check): New static functions. (me_def_variable, me_each, me_for, me_gen, me_gun, me_delay, me_when, me_unless, me_while_until, me_whie_until_star, me_equot, me_case, me_dotimes, me_lcons, me_mlet, me_load_time, me_l1_val, me_l1_setq, me_assert): Add syntactic checks to built-in macros. (do_expand): Add syntactic checks for a number of special operators. * unwind.c (catch_frame_s): New symbol variable. (uw_late_init): Initialize catch_frame_s and use that in registering the catch-frame structure. * unwind.c (catch_frame_s): Declared.
* streams: tightening sloppy argument defaulting.Kaz Kylheku2021-07-011-4/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Numerous functions in TXR Lisp treat a nil argument for an optional parameter as if it were omitted. In the case of streams, this can cause problems. An accidental nil passed to an input function can cause it to read from standard input and hang. In this patch, argument defaulting is tighented for functions that perform I/O. It's mostly stream parameters, but not exclusively. * eval.c (prinl, pprinl): Use default_arg_strict to default the stream argument, and also re-use that value for the put_char call. * lib.c (lazy_stream_cons, print, pprint, put_json): Use default_arg_strict rather than default_arg. * parser.c (regex_parse, lisp_parse_impl, txr_parse): Tighten the defaulting of the input stream and error stream arguments, streamlining the logic at the same time. * stream.c (do_parse_mode): Use default_arg_strict for the mode string argument. (record_adapter, get_line, get_char, get_byte, get_bytes, unget_byte, put_buf, fill_buf, fill_buf_adjust, get_line_as_buf, put_string, put_char, put_byte, put_line, flush_stream, get_string): Use strict defaulting for stream argument. (mkstemp_wrap): Use strict defaulting for suffix.
* New function: find-true.Kaz Kylheku2021-06-291-0/+1
| | | | | | | | | | | | | | | This is like find-if, but returns the value of the predicate function rather than the item. * eval.c (eval_init): Register find-true instrinsic. * lib.c (find_true): New function. * lib.c (find_true): Declared. * stdlib/doc-syms.tl: Updated. * txr.1: Documented.
* constantp: fully expand; recognize functions.Kaz Kylheku2021-06-281-14/+32
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch improves the constantp function dramatically. It now performs a full expansion of its argument, and recognizes all of the constant foldable functions that the compiler recognizes. * eval.c (const_foldable_s): New symbol variable. (const_foldable_hash): New static variable. (constantp_noex): Look up function in the hash table of const foldable functions, including in the case when it appears in a dwim form as in [+ 2 2] which is (dwim + 2 2). In this case, recursively check the arguments for constantp_noex. We get the hash table of foldable functions from the sys:%const-foldable% variable, which comes from an autoloaded module. (constantp): Fully expand the input form, not just m macroexpand. (eval_init): Register the const_foldable_s variable. * lisplib.c (constfun_instantiate, constfun_set_entries): New static functions. (lisplib_init): Register auto-loading of constfun module via new static functions. * stdlib/compiler.tl; Load the constfun module if %const-foldable% is not defined. (%const-foldable-funs%, %const-foldable%): Removed from here. * stdlib/constfun.tl: New file. (%const-foldable-funs%, %const-foldable%): Moved here. * txr.1: Documented changes to constantp.
* expander: use constantp_noex for expanded forms.Kaz Kylheku2021-06-281-4/+4
| | | | | | * eval.c (expand_progn, do_expand): Use the constantp_noex helper function of constantp on arguments that we have already fully expanded.
* limit print depth/width when diagnosing oveflow.Kaz Kylheku2021-06-241-0/+11
| | | | | | | | | * eval.c (error_trace): If the error is a stack overflow, then save the printing depth and width, and set them to stringent values, to minimize recursion in the printer. This minimizes the chances of a segfault or runaway iteration under some conditions. A repro test case is (print '#1=(#1#)) entered into the listener.
* New: stack overflow protection.Kaz Kylheku2021-06-231-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * configure: detect getrlimit, producing HAVE_RLIMIT in config.h. * eval.c (do_eval, do_expand): Call gc_stack_check inline function to check stack pointer against limit. * gc.c (gc_stack_bottom): Static becomes extern, so inline function in gc.h can refer to it. (gc_stack_limit): New global variable. (gc_init): If we have rlimit, then probe RLIMIT_STACK. If the stack is sufficiently large, then enable the stack overflow protection, which kicks in when the stack pointer appears to be within a certain percentage of the limit. (set_stack_limit, get_stack_limit): New static functions. (gc_late_init): Register set-stack-limit and get-stack-limit intrinsics. (gc_stack_overflow): New function. * gc.h (gc_stack_bottom, gc_stack_limit, gc_stack_overflow): Declared. (gc_stack_check): New inline function. * lib.c (stack_overflow_s): New symbol variable. (obj_print_impl): Call gc_stack_check to protect recursive printing againts overflow. * lib.h (stack_overflow_s): Declared. * unwind.c (uw_init): Register stack-overflow symbol as a an exception symbol subtyped from error. (uw_unwind_to_exit_point): When dealing with an unhandled exception, turn off the stack limit, so we can print the messages without triggering it in a loop. * vm.c (vm_execute_closure, vm_funcall_common): Insert gc_stack_check to the top of the execution of every VM function. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated.
* c_str now takes a self argument.Kaz Kylheku2021-06-231-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Adding a self parameter to c_str so that when a non-string occurs, the error is reported against a function. Legend: A - Pass existing self to c_str. B - Define self and pass to c_str and possibly other functions. C - Take new self parameter and pass to c_str and possibly other functions. D - Pass existing self to c_str and/or other functions. E - Define self and pass to other functions, not c_str. X - Pass nil to c_str. * buf.c (buf_strm_put_string, buf_str): B. * chksum.c (sha256_str, md5_str): C. (sha256_hash, md5_hash): D. * eval.c (load): D. * ffi.c (ffi_varray_dynsize, ffi_str_put, ffi_wstr_put, ffi_bstr_put): A. (ffi_char_array_put, ffi_wchar_array_put): C. (ffi_bchar_array_put): A. (ffi_array_put, ffi_array_out, ffi_varray_put): D. * ftw.c (ftw_wrap): A. * glob.c (glob_wrap): A. * lib.c (copy_str, length_str, coded_length,split_str_set, list_str, cmp_str, num_str, out_json_str, out_json_rec, display_width): B. (upcase_str, downcase_str, string_extend, search_str, do_match_str, do_rmatch_str, sub_str, replace_str, cat_str_append, split_str_keep, trim_str, int_str, chr_str, span_str, compl_span_str, break_str, length_str_gt, length_str_ge, length_str_lt, length_str_le, find, rfind, pos, rpos, mismatch, rmismatch): A. (c_str): Add self parameter and use in type mismatch diagnostic. If the parameter is nil, use "internal error". (flo_str): B, and correction to "flot-str" typo. (out_lazy_str, out_quasi_str, obj_print_impl): D. * lib.h (c_str): Declaration updated. * match.c (dump_var): X. (v_load): D. * parser.c (open_txr_file): C. (load_rcfile): E. (find_matching_syms, provide_atom): X. (hist_save, repl): B. * parser.h (open_txr_file): Declaration updated. * parser.y (chrlit): X. * regex.c (search_regex): A. * socket.c (getaddrinfo_wrap, sockaddr_pack): A. (dgram_put_string): B. (open_sockfd): D. (sock_connect): E. * stream.c (stdio_put_string, tail_strategy, vformat_str, open_directory, open_file, open_tail, remove_path, rename_path, tmpfile_wrap, mkdtemp_wrap, mkstemp_wrap): B. (do_parse_mode, parse_mode, make_string_byte_input_stream): B. (normalize_mode, normalize_mode_no_bin): E. (string_out_put_string, formatv, put_string, open_fileno, open_subprocess, open_command, base_name, dir_name, short_suffix, long_suffix): A. (run): D. (win_escape_cmd, win_escape_arg): X. * stream.h (parse_mode, normalize_mode, normalize_mode_no_bin): Declarations updated. * sysif.c (mkdir_wrap, do_utimes, dlopen_wrap, dlsym_wrap, dlvsym_wrap): A. (do_stat, do_lstat): C. (mkdir_nothrow_exists, ensure_dir): E. (chdir_wrap, rmdir_wrap, mkfifo_wrap, chmod_wrap, symlink_wrap, link_wrap, readlink_wrap, exec_wrap, getenv_wrap, setenv_wrap, unsetenv_wrap, getpwnam_wrap, getgrnam_wrap, crypt_wrap, fnmatch_wrap, realpath_wrap, opendir_wrap): B. (stat_impl): statfn pointer-to-function argument now takes self parameter. When calling it, we pass name. * syslog.c (openlog_wrap, syslog_wrapv): A. * time.c (time_string_local, time_string_utc, time_string_meth, time_parse_meth): A. (strptime_wrap): B. * txr.c (txr_main): D. * y.tab.c.shipped: Updated.
* Dubious new functions cxr/cyr.Kaz Kylheku2021-06-211-0/+2
| | | | | | | | | | | | | | * lib.c (cxr, cyr): New functions. * lib.h (cxr, cyr): Declared. * eval.c (eval_init): Intrinsics cxr and cyr registered. * tests/012/cadr.tl: New file. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated.
* expander: bug: atoms in quasiliteral.Kaz Kylheku2021-06-151-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Via macros, atoms can sneak into a quasiliteral which then blow up because they get treated as strings without being converted. Example: (defmacro two () 2) `@(two)xyz` -> ;; error The expansion produces the invalid form, in which the 2 is subsequently treated as a string. (sys:quasi 2 "xyz") On the other hand, symbol macros don't have this problem: (defsymacro two 2) `@{two}xyz` -> "2xyz" The reason is that the (sys:var two) syntax will expand to (sys:var 2), and not 2. The straightforward, consistent fix is to ensure that the first case will also go to (sys:var 2). * eval.c (expand_quasi): If the expanded form is an atom which is not a bindable symbol, wrap it in a sys:var. * tests/012/quasi.tl: Test cases added. Also adding a compilation test for this file, cribbed from patmatch.tl.
* defsymacro: regression: don't expand replacement.Kaz Kylheku2021-06-151-12/+29
| | | | | | | | | | | | | | | | | | | | | | | | | | This is a regression that was introduced in 191. The change in 191 was trying to prevent defsymacro from being expanded immediately by the expander except in 190 compatibility. Unfortunately, this caused the whole defsymacro block not to be entered unless in 190 compatibility, otherwise taking the common exit which returns form_ex, containing the expanded replacement form. * eval.c (do_expand): Split up implementation of defvarl and defsymacro. In the defsymacro block, do not do any expanding on entry. Absent of compatibility mode, we just do some sanity checks and pass the entire form through. In 262 compatibility, we do the expansion to obtain form_ex. Then all the previous compat logic is wrapped in that block. * tests/011/macros-3.tl: Add a test case which confirms that symbol macros are lazily expanded. Weakness in the test suite is how these regressions creep in. * txr.1: Improve defsymacro documentation, spelling out clearly that the unexpanded replacement form is associated with the symbol. Eliminate obsolescent text suggesting that defsymacro is evaluated at macro time.
* errors: avoid premature release of deferred warnings.Kaz Kylheku2021-06-111-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | We don't want to be unconditionally releasing deferred warnings when error exceptions occur in evaluation or compilation. The reason is that the error might be handled, for instance by a speculative expansion. Then unwanted noise occurs, because deferred warnings have been released prematurely. * eval.c (eval_exception): Do not call uw_release_deferred_warnings here. (error_trace): But do call uw_release_deferred_warnings here, before printing anything else. We want to preserve the behavior that when error information is actually being printed to a stream, any deferred warnings are dumped first because they might pertain to the error. (I may revisit this requirement; perhaps deferred warnings rarely, if ever, pertain to an error). * share/txr/stdlib/error.tl (compile-error): Do not dump deferred warnings unconditionally. Only dump them if we are also printing the error message to stderr. Secondly, do not output the error message at all, unless there is no handler for the error.
* lib: new function, fill-vec.Kaz Kylheku2021-06-081-0/+1
| | | | | | | | | | | | | | * eval.c (eval_init): Register fill-vec intrinsic. * lib.c (fill_vec): New function. * lib.h (fill_vec): Declared. * tests/010/vec.tl: New file. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated.
* expander: expand must only ignores unbound warnings.Kaz Kylheku2021-06-071-1/+20
| | | | | | | | | | | | | | | | | | | The expand function must not muffle all deferred warnings. That causes the problem that a form like (inc var a.bar) fails to produce a warning due to bar not being the slot of any structure. The expand function must only muffle warnings about undefined functions and variables. * eval.c (muffle_unbound_warning): New static function. (no_warn_expand): Use muffle_unbound_warning as handler, rather than uw_muffle_warning. * tests/012/struct.tl: Fix two test cases here which test the expand function using a form that references a nonexistent slot. These now generate a warning, so we use the slot name b rather than d, which is defined. * txr.1: Documented change to expand.
* bugfix: do not expand defun body with name in scope.Kaz Kylheku2021-06-041-4/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a revert of November 2016 commit 606132c336dbeb0dd8bb851a64c97f2c11b76a85. The commit claims that it fixes a bug, but in fact it introduces one. There is no discussion in that commit about what motivated it. The commit which follows that one introduces a naivey-implemented diagnostic for catching unbound functions at macro-expansion time, so the likely motivation for this wrong fix was to suppress false positives from that naive diagnostic for recursive functions. That has long since been replaced by a better approach. Because of the bug, we cannot do this very useful thing: we cannot write an inline version of a funtion as a macro first, and then a real function which just calls that macro: (defmacro foo (arg) ...) (defun foo (arg) (foo arg)) The bug causes the (foo arg) call in this function not to be expanded, due to the shadowing. * eval.c (do_expand): When expanding defun, do not introduce the function's name as a lexical function binding, because it isn't one.
* json: functions put-json and put-jsonl.Kaz Kylheku2021-05-291-0/+2
| | | | | | | | | | | | | | | | | | | | | * eval.c (eval_init): Register put-json and put-jsonl intrinsics. * lib.c (out_json_str): Do not output the U+DC01 to U+DCFF code points by masking them and using put_byte. This is unnecessary; if we just send them as-is to the text stream, the UTF-8 encoder does that for us. (put_json, put_jsonl): New functions. * lib.h (put_json, put_jsonl): Declared. * txr.1: Documented. The bulk of tojson is moved under the descriptions of these new functions, and elsewhere where the document pointed to tojson for more information, it now points to put-json. More detailed description of character treatment is given. * share/txr/stdlib/doc-syms.tl: Updated.
* json: get-json function.Kaz Kylheku2021-05-281-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | * eval.c (eval_init): get-json intrinsic registered. * parser.c (prime_parser): Handle prime_json. (lisp_parse_impl): Take enum prime_parser argument directly instead of the interactive flag. (lisp_parse, nread, iread): Pass appropriate prime_parser value instead of the original flag. (get_json): New function. Like nread, but passes prime_json. * parser.h (enum prime_parser): New constant, prime_json. (get_json): Declared. * parser.l (prime_scanner): Handle prime_json. * parser.y (SECRET_ESCAPE_J): New terminal symbol. (spec): New productions around SECRET_ESCAPE_J for parsing JSON. * lex.yy.c.shipped, y.tab.c.shipped, y.tab.h.shipped: Updated. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated.
* json: tojson function.Kaz Kylheku2021-05-281-0/+1
| | | | | | | | | | | | * eval.c (eval_init): tojson intrinsic registered. * lib.c (tojson): New function. * lib.h (tojson): Declared. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated.
* eval: bugfix: expand keys in case{q,ql,qual}*Kaz Kylheku2021-05-211-2/+2
| | | | | | | * eval.c (me_case): When we evaluate the keys of a caseq, caseql* or casequal* construct, we must use expand_eval. I ran into this problem trying to use constants defined as symbol macros as keys.
* compiler: better code for global var definitions.Kaz Kylheku2021-05-141-4/+22
| | | | | | | | | | | | | | | | | | | | | | | | * eval.c (rt_defvarl): More accurate self string. (rt_defv): New static function: like rt_defvarl but ensures that the new variable has a binding cell, and returns that cell instead of the hash cell. (op_defvarl): Take advantage of rt_defv to not have to cons up the binding cell. (eval_init): Register sys:rt-defv intrinsic. * parser.c (read_file_common): Compiled files are now version 7, so we must recognize them. We still load version 6 files because rt:defvarl still exists for them. * share/txr/stdlib/compiler.tl (expand-defvarl): Improve the generated code in two ways. Firstly, use the new sys:rt-defv, which returns the binding cell, so that the value can be stored into it with rplacd without having to cons up anything. Secondly, if there is no value expression, don't emit the code to do the assignment. (%tlo-ver%): Bump compiled file version to (7 0). * txr.1: Add note about TXR 260 loading version 7 and 6.
* lib: basic support for trees as sequences.Kaz Kylheku2021-05-091-12/+18
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | As of this commit, binary search trees can be iterated: mapped over with mapcar and such. * arith.c (poly, rpoly): rpoly won't work with trees. They work just with vectors and lists so let's make the error message more accurate. I noticed the self names of these two are swapped; will fix in another commit. * eval.c (tprint): Refactor to use iterator framework for objects other than lists. The tree case falls into this, so trees are supported. * lib.h (enum seq_kind): New enum constant SEQ_TREELIKE. (seq_iter_init_with_info): Declared. * lib.c (seq_info): Map tree object to SEQ_TREELIKE type. (seq_iter_get_tree, seq_iter_peek_tree): New static functions. (seq_iter_rewind): Support rewinding tree iteration. I think we could reuse the existing iterator here, and in the hash case as well. I made a note to look into this. (seq_iter_init_with_info): Internal linkage changed to external, because tprint in eval.c uses this. Handle SEQ_TREELIKE case here, by setting up iterator with the two new static functions. (seq_iter_mark): Handle SEQ_TREELIKE_CASE. Change switch statement to exhaustively list cases. (ldiff): Add SEQ_TREELIKE to various cases. Idea is that we handle it like SEQ_HASH. (nsort, sort, nshuffle, take, take_while, take_until, drop_while, drop_until, update): Add case for SEQ_TREELIKE, routing to error message. (lazy_where_tree_func): New static function. (where, sel, reject): Support trees.
* mapcar*: fix broken.Kaz Kylheku2021-04-291-1/+1
| | | | | | | | | | * eval.c (lazy_mapcar_func): We must capture the return value of iter_step, since we refer to it in the next statement, expecting it to have stepped. This bug causes a behavior as if the original list had an extra nil. * tests/012/lazy.tl: Tests. Poor test coverage is why this sort of thing comes up and bites us.
* macros: lexical-var-p: don't report t for specials.Kaz Kylheku2021-04-281-8/+30
| | | | | | | | | | | | | | | | | | | | The lexical-var-p function wrongly reports true for locally rebound special variables, which are not lexical. * eval.c (special_var_p): Static function moved to avoid forward declaration. (lexical_var_p): Bail if sym satisfies special_var_p. (old_lexical_var_p): New function, copy of old lexical_var_p before this bugfix. (eval_init): Conditionally register lexical-var-p as either lexical_var_p or old_lexical_var_p depending on the compat value. * txr.1: Update documentation for lexical-var-p to clarify that it doesn't report true for specials. Also make note that it doesn't report true for global lexicals, and likewise that lexical-fun-p doesn't report global functions. Added compat note.
* compile/eval: new operator, mac-env-param-bind.Kaz Kylheku2021-04-211-3/+37
| | | | | | | | | | | | | | | | | | | | | | mac-env-param-bind is like mac-param-bind but also allows the value for the :env parameter to be specified. * eval.c (op_mac_env_param_bind_s): New sy mbol variable. (op_mac_env_param_bind): New static function. (do_expand): Handle mac_env_param_bind_s. (eval_init): Initialize symbol variable and register macro. * share/txr/stdlib/compiler.tl (compiler compile): Add case for mac-env-param-bind. (compiler comp-mac-env-param-bind): New method. * share/txr/stdlib/doc-syms.tl: Updated with new hashes for tree-bind and mac-param-bind, and inclusion of mac-env-param-bind. * tests/012/binding.tl: New file. * txr.1: Documented.
* expander: fun: misleading diagnostic.Kaz Kylheku2021-03-281-1/+1
| | | | | * eval.c (do_expand): argument of fun is not in "operator position"; fixed wording.
* compile/eval: more standard formatting for diags.Kaz Kylheku2021-03-271-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch eliminates parentheses from the error messages, as well as a leading ./ being added to relative paths. The word "warning: " is moved into the error message, so that it does not appear before the location. Example, when doing (compile-file "path/to/foo.tl"). Before patch: warning: (./path/to/foo.tl:37): unbound function foo After: path/to/foo.tl:37: warning: unbound function foo Now when I compile out of Vim, it nicely jumps to errors in Lisp code. * eval.c (eval_exception): Drop parentheses from error location, add colon. (eval_warn): Prepend "warning: " to format string. (eval_defr_warn): Drop parentheses from location, and prepend "warning: " to format string. * parser.c (repl-warning): Drop "warning:" prefix. * share/txr/stdlib/compiler.tl (open-compile-streams): Do not do parent substitution for relative paths if the parent path is the empty string "", to avoid inserting ./ onto relative paths in that case. * share/txr/stdlib/error.tl (sys:loc): Drop parentheses and space from location. (compile-error) Separate location with colon and space. (compile-warning, compile-defr-warning): Likewise and add "warning: " prefix. * unwind.c (uw_rthrow): Drop "warning: " prefix. (uw_warningf): Add "warning: " prefix. (uw_dump_deferred_warnings): Drop "warning: " prefix.
* lib: new functions join, join-with.Kaz Kylheku2021-03-091-1/+6
| | | | | | | | | | | | | | | | | | | | | | | | | That old cat-str function is often a pain, requiring the pieces as a list. We have a sys:fmt-join that is undocumented. That functions is now exposed as usr:join, and documented. Also introducing join-with that takes a separator as the leftmost argument. Thus (op join-with "::") gives us a function that joins pieces with :: in between. * eval.c (eval_init): Regiser fmt_join function under join symbol also. Register join-with. * lib.c (join_with): New function. (fmt_join): Now a wrapper for join_with that passes a nil separator. * lib.h (join_with): Declared. * share/txr/stdlib/optimize.tl (basic-blocks join-blocks): Rename the local function join, which now triggers a warning about a standard function being redefined. * txr.1: Redocumented cat-str, and documented join-with and join.
* expander: improve diagnosis of invalid dotted syntax.Kaz Kylheku2021-02-181-17/+33
| | | | | | | | | | * eval.c (dotted_form_error): Use eval_error instead of throwing direcly, so the error message has location info and consistent formatting. (expand_forms, expand_forms_ss, expand_forms_lisp1): Check for the dotting error one level above, at the cons cell. The cons cell will have line number information attached to it for a better error message.
* compiler: eliminate block from recursive functions.Kaz Kylheku2021-02-121-3/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The block elimination logic doesn't work for self-recursive functions, even if they invoke no block returning, and use only system functions that don't have anything to do with block returns. This is because the recursive call is not recognized, and treated as a call to an unknown function. Let's put in a simple hack. The defun and defmacro operators will use a new secret special operator called sys:blk instead of block to generate the block. The compilation of sys:blk will assume that (sys:blk name ...) is only used in a defun or defmacro by that same name, and include name in the list of OK functions. So that functions created using the interpreter and then dynamically compiled will also benefit, we add this operator to the interpreter. * eval.c (sys_blk_s): New symbol variable. (op_defun): For defun and defmacro, use sys:blk for the block for the block (eval_init): Initialize sys_blk_s with the interned symbol sys:blk. Register the sys:blk operator. * share/txr/stdlib/compiler.tl (compiler compile): Recognize the sys:blk special form and handle via comp-block. (comp-block): If sys:blk is being compiled, then include the block name in the list of functions that do not perform block returns. (If this is false, other checks will fail before use that.) (expand-defun): Use sys:blk for defun and defmacro.
* maprod, maprend: correct self name.Kaz Kylheku2021-01-151-2/+2
| | | | | | * eval.c (maprodv, maprendv): These functions implement maprod and maprend; the v suffix is just in the C code and must not be part of the self name.
* mapcar/maprod: show-stopper bug.Kaz Kylheku2021-01-151-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | Observed wrong result: (mapcar (lambda (. args) (list . args)) '#(1 2 3) '#(4 5 6)) -> #((1 4) nil nil) Correct result: -> #((1 4) (2 5) (3 6)) This is not specific to vector input; it's broken for sequences of all types. Functions affected are mapcar, mapf, mappend, mapdo, maprod, maprend, maprodo. * eval.c (map_common, prod_common): Because we reuse the args structure across iterations, we must reinitialize the fill and list members. These can be modified by the functionw which is called. In particular, the arguments are applied, they may be turned into a list (fill decrements to zero, and a list is produced).
* 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.
* progn, prog1, prog2: now also functions.Kaz Kylheku2021-01-011-0/+19
| | | | | | | | | | | | * eval.c (progn_fun, prog1_fun, prog2_fun): New static functions. (eval_init): Wire progn, prog1 and prog2 function bindings to new functions. * txr.1: Documented. * checkman.txr (check-func): Recognize Macro/function and Operators/functions heading.
* New macro: prog2Kaz Kylheku2021-01-011-1/+14
| | | | | | | | | | | | * eval.c (prog2_s): New symbol variable. (me_prog2): New macro expander function. (eval_init): Initialize prog2_s variable with interned symbol. Register prog2 macro expander. * txr.1: Maintain the documentation for progn and prog1, improving the wording and adding a dialect note . Document prog2.
* eval: use symbol vars for registering if, and or.Kaz Kylheku2021-01-011-3/+3
| | | | | * eval.c (eval_init): Use if_s, or_s, and_s instead of wastefully calling intern.
* shuffle/nshuffle: take optional random state argument.Kaz Kylheku2020-12-291-2/+2
| | | | | | | | | | | | | * 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.
* txr, eval: eliminate some func_n1 calls.Kaz Kylheku2020-12-101-2/+2
| | | | | | | | | * match.c (do_output_line, do_repeat, v_deffiler): Replace func_n1(cdr) and func_n1(rest) with cdr_f. * eval.c (eval_init): Replace func_n1(car) and func_n1(cdr) with car_f and cdr_f. Should have been done in 2011 when this was done for the registrations of car and cdr.
* mapcar: regression: not not converting to left typeKaz Kylheku2020-10-101-1/+2
| | | | | | * eval.c (map_common): Save the leftmost arg in a local variable, and refer to that in the make_like call. The bug here is that the args get zapped to nil.
* time: move time functions out of lib.c into time.c.Kaz Kylheku2020-10-071-14/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * 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.