summaryrefslogtreecommitdiffstats
path: root/eval.c
Commit message (Collapse)AuthorAgeFilesLines
* load: release warnings before throwing exception.Kaz Kylheku2020-04-141-3/+4
| | | | | | | | | | | | * eval.c (load): When we parse TXR code, let's not release warnings unconditionally. Let's do that when throwing an exception though due to parse errors. If the load is not recursed it will release warnings at the bottom of the function. * match.c (v_load): Consistently with load, release deferred warnings if throwing exception due to the parse having failed.
* bugfix: definitions must trigger autoload.Kaz Kylheku2020-04-131-1/+6
| | | | | | | | | | | | | | When a function, macro, variale, symbol macro or struct is being defined, we must trigger any auto-load for that symbol. If the definition is redefining a library symbol, then if the autoload is later triggered, it will surprisingly reinstate the library definition. * eval.c (rt_defvarl, op_defsymacro, rt_defsymacro, rt_defun, rt_defmacro): Insert calls to lisplib_try_load against the symbol being defined. * struct.c (make_struct_type): Likewise.
* exceptions: use uw_rthrow for non-error exceptions.Kaz Kylheku2020-04-071-6/+6
| | | | | | | | | | | | | | | | | | | * eval.c (eval_exception): This function is shared by warnings and errors. Use uw_throw. The eval_error caller already has an abort() after its eval_exception call, which makes that code path continue to be equivalent to uw_throw. The behavior changes for the other caller, eval_warn, which will now return if the warning is not handled. (eval_defr_warn, gather_free_refs, gather_free_refs_nw): Throw non-error exception with uw_rthrow. * match.c (v_throw, v_assert, h_assert): Use uw_rthrow for these directives, just like the throw function. * parser.c (repl_intr, repl_warning): Use uw_rthrow. * unwind.c (uw_muffle_warning, uw_release_deferred_warnings): Likewise.
* warning cleanup: remove unused parameters.Kaz Kylheku2020-04-051-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* warning cleanup: add casts for unused parameters.Kaz Kylheku2020-04-051-3/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is the first round of an effort to enable GCC's -Wextra option. All function parameters that are unused an that we cannot eliminate are treated with a cast to void in the function body. * args.c (args_key_check_store): Cast unused param to void. * combi.c (perm_list_gen_fill): Likewise. * eval.c (op_error, op_meta_error, op_quote op_qquote_error, op_unquote_error, op_load_time_lit, me_each, me_for, me_quasilist, me_flet_labels, hash_min_max, me_ignerr, me_whilet, me_iflet_whenlet, me_dotimes, me_mlet, me_load_time, me_load_for): Likewise. * ffi.c (ffi_void_put, ffi_fixed_dynsize, *ffi_fixed_alloc, ffi_noop_free, ffi_void_get, ffi_simple_release, ffi_i8_put, ffi_i8_get, ffi_u8_put, ffi_u8_get, ffi_i16_put, ffi_i16_get, ffi_u16_put, ffi_u16_get, ffi_i32_put, ffi_i32_get, ffi_u32_put, ffi_u32_get, ffi_i64_put, ffi_i64_get, ffi_u64_put, ffi_u64_get, ffi_char_put, ffi_char_get, ffi_uchar_put, ffi_uchar_get, ffi_bchar_get, ffi_short_put, ffi_short_get, ffi_ushort_put, ffi_ushort_get, ffi_int_put, ffi_int_get, ffi_uint_put, ffi_uint_get, ffi_long_put, ffi_long_get, ffi_ulong_put, ffi_ulong_get, ffi_float_put, ffi_float_get, ffi_double_put, ffi_double_get, ffi_val_put, ffi_val_get, ffi_be_i16_put, ffi_be_i16_get, ffi_be_u16_put, ffi_be_u16_get, ffi_le_i16_put, ffi_le_i16_get, ffi_le_u16_put, ffi_le_u16_get, ffi_be_i32_put, ffi_be_i32_get, ffi_be_u32_put, ffi_be_u32_get, ffi_le_i32_put, ffi_le_i32_get, ffi_le_u32_put, ffi_le_u32_get, ffi_be_i64_put, ffi_be_i64_get, ffi_be_u64_put, ffi_be_u64_get, ffi_le_i64_put, ffi_le_i64_get, ffi_le_u64_put, ffi_le_u64_get, ffi_wchar_put, ffi_wchar_get, ffi_sbit_get, ffi_ubit_get, ffi_cptr_get, ffi_str_in, ffi_str_put, ffi_str_get, ffi_str_d_get, ffi_wstr_in, ffi_wstr_get, ffi_wstr_put, ffi_wstr_d_get, ffi_bstr_in, ffi_bstr_put, ffi_bstr_get, ffi_bstr_d_get, ffi_buf_in, ffi_buf_put, ffi_buf_get, ffi_buf_d_in, ffi_buf_d_put, ffi_buf_d_get, ffi_closure_put, ffi_ptr_in_in, ffi_ptr_in_d_in, ffi_ptr_in_out, ffi_ptr_out_in, ffi_ptr_out_out, ffi_ptr_out_null_put, ffi_ptr_out_s_in, ffi_flex_struct_in, ffi_carray_get, ffi_union_get, make_ffi_type_builtin, make_ffi_type_array, ffi_closure_dispatch, ffi_closure_dispatch_safe): Likewise. * gc.c (cobj_destroy_stub_op, cobj_destroy_free_op, cobj_mark_op): Likewise. * lib.c (seq_iter_get_nil, seq_iter_peek_nil): Likewise. * linenoise/linenoise.c (sigwinch_handler): Likewise. * parser.c (repl_intr, read_eval_ret_last, repl_warning, is_balanced_line): Likewise. * parser.y (yydebug_onoff): Likewise. * socket.c (dgram_close): Likewise. * stream.c (unimpl_put_string, unimpl_put_char, unimpl_put_byte, unimpl_unget_char, unimpl_unget_byte, unimpl_put_buf, unimpl_fill_buf, unimpl_seek, unimpl_truncate, unimpl_set_sock_peer, null_put_string, null_put_char, null_put_byte, null_get_line, null_get_char, null_get_byte, null_close, null_flush, null_seek, null_set_prop, null_get_error, null_get_error_str, null_clear_error, null_get_fd, dir_close): Likewise. * struct.c (struct_type_print): Likewise. * unwind.c (me_defex): Likewise.
* New function: txr-parse.Kaz Kylheku2020-04-021-0/+1
| | | | | | | | | | | | txr-parse provides a way for Lisp code to programmatically parse the TXR language and obtain the Lisp represenation. This has hitherto not been available. * eval.c (eval_init): Register txr-parse intrinsic. * parser.c (txr_parse): New function. * parser.h (txr_parse): Declared.
* New contains function: near alias of search.Kaz Kylheku2020-03-231-0/+1
| | | | | | | | | | | | Harmonizes with starts-with and ends-with. * eval.c (eval_init): Register contains intrinsic. * lib.c (contains): New function. * lib.h (contains): Delared. * txr.1: Documented.
* apf and ipf: take arguments that are inserted.Kaz Kylheku2020-03-221-7/+40
| | | | | | | | | | | | | | The apf and ipf functions now take arguments in addition to the function that is being wrapped. If specified, these arguments are inserted to the left the applied arguments. * eval.c (do_args_apf, do_args_ipf): New static functions. (apf, ipf): Use do_args_apf and do_args_ipf, respectively, for handling the case when arguments are present. Passing the stored arguments is done with the help of the new DARG type, instead of consing up a list. * txr.1: Documented new arguments of apf and ipf.
* internals: rename misnamed curry_* functions.Kaz Kyheku2020-03-171-9/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The various curry_xx_yy functions perform partial application, not currying. The curry prefix is being renamed to pa (partially apply). * lib.c (remq_lazy, remql_lazy, remqual_lazy, tree_Find): Updated. (do_curry_12_1, do_curry_12_1_v, do_curry_12_2, do_curry_123_1, do_curry_123_23, do_curry_123_2, do_curry_123_3, do_curry_1234_1, do_curry_1234_34): Renamed to do_pa_12_1, do_pa_12_1_v, do_pa_12_2, do_pa_123_1, do_pa_123_23, do_pa_123_2, do_pa_123_3, do_pa_1234_1, do_pa_1234_34. (curry_12_1, curry_12_1_v, curry_12_2, curry_123_1, curry_123_23, curry_123_2, curry_123_3, curry_1234_1, curry_1234_34): Renamed to pa_12_1, pa_12_1_v, pa_12_2, pa_123_1, pa_123_23, pa_123_2, pa_123_3, pa_1234_1, pa_1234_34. (transposev, do_juxt): Updated. * lib.h: Declarations renamed. * eval.c (subst_vars, qquote_init, expand_catch, weavev): Updated. * filter.c (get_filter, build_filter_from_list, filter_string_tree, filter_init): Updated. * match.c (tx_subst_vars, do_txeval, v_freeform, v_bind, v_throw, v_deffilter, v_assert, h_assert): Updated. * parser.y (gather_clause): Updated. * regex.c (regex_range_full_fun, regex_range_left_fun, regex_range_right_fun, regex_range_search_fun): Updated. * stream.c (open_files, open_files_star): Updated. * txr.c (txr_main): Updated. * unwind.c (me_defex): Updated.
* New function: assq and rassq.Kaz Kyheku2020-02-241-0/+2
| | | | | | | | | | | | | | | | | | | | | TXR Lisp is henceforth a dialect in which (cdr (assq key a-list)) works exactly as shown, without substitution of assql or assoc. * eval.c (eval_init): Register assq and rassq intrinsics. * lib.c (assq, rassq): New functions. * lib.h (assq, rassq): Declared. * txr.1: Documented. * tests/012/ashwin.tl: New file. * tests/012/ashwin.expected: New file.
* New functions: meq, meql and mequal.Kaz Kylheku2020-02-221-0/+3
| | | | | | | | | | * eval.c (eval_init): Register meq, meql an mequal intrinsics. * lib.c (meq, meql, mequal): New functions. * lib.h (meq, meql, mequal): Declared. * txr.1: Documented.
* packages: no default argument in several functions.Kaz Kyheku2020-01-291-3/+3
| | | | | | | * eval.c (eval_init): bugfix: for the functions package-symbols, package-local-symbols and package-foreign-symbols, make the package parameter optional, as is specified in the documentation.
* New function: merge-delete-package.Kaz Kyheku2020-01-291-0/+1
| | | | | | | | | | | | | | | | | | | | | This is a useful function which supports the use of temporary packages over the scope of file compilation units. A file can be read under a temporary package which provides usefully customized symbol visibility consisting of an arrangement of symbols from various other packages. Then, in a single operation, thanks to this new function, that packag can be deleted and all of its local symbols (those having been newly interned over the course of the file) are transferred to some other, more permanent package. * eval.c (eval_init): merge-delete-package intrinsic registered. * lib.c (merge_delete_package): New function. * lib.h (merge_delete_package): Declared. * txr.1: Documented.
* New function: coded-length.Kaz Kylheku2020-01-181-0/+1
| | | | | | | | * eval.c (eval_init): Register coded-length intrinsic. * lib.c (coded_length): New function. * lib.h (coded_length): Declared.
* builtin redefinition: better diagnostic message.Kaz Kylheku2020-01-061-6/+10
| | | | | | | | | | | | | | | | | | | The motivation here is that if we define, say, a macro whose name is the same as a built-in function, we get a warning which misleadingly uses the word "redefining". * eval.c (builtin_reject_test): Add a new parameter which indicates what kind of binding is being defined. This has the same values as the builtin hash. If the builtin hash reports that the symbol is a builtin, we can issue one of two diagnostic messages based on whether the one being defined is of the same kind or noto. (expand_macrolet): Pass the defmacro symbol to builtin_reject_test, since macros are being defined. (expand_fbind_vars): Pass the defun symbol symbol to builtin_reject_test. (do_expand): In the defun/defmacro case, pass the operator in question to builtin_reject_test.
* Copyright year bump 2020.Kaz Kylheku2019-12-311-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * LICENSE, LICENSE-CYG, METALICENSE, 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, 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/debugger.tl, share/txr/stdlib/defset.tl, share/txr/stdlib/doloop.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/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, 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: Extended copyright notices to 2020.
* eval: bugfix: expansion wrongly always in null env.Kaz Kylheku2019-11-181-5/+34
| | | | | | | | | | | | | | | | | | | | | | The eval function uses a null macro environment for expanding a form, even when it's given an environment object. This causes spurious warnings about unbound variables/functions. For instance: (let ((env (make-env '((x . 42)) nil nil))) (eval '(+ x x) env)) ;; warning: unbound variable x To fix this, we have to create a macro version of the incoming environment and expand with that. * eval.c (env_to_menv): Take an evaluation environment chain and convert it to a (flattened) macro environment. (expand_eval): Take a macro environment parameter and use that for expanding the form to be evaluated, rather than nil. (eval_intrinsic): Calculate a macro environment corresponding to the given evaluation environment. Use that for the macroexpand call, and also pass it down to expand_eval to be used for the full expansion.
* lib: use stack-allocated hash iterators everywhere.Kaz Kylheku2019-11-011-2/+5
| | | | | | | | | | | | | | | | | * eval.c (op_dohash): Use hash_iter instead of consing up heap-allocated hash iterator. * filter.c (trie_compress, regex_from_trie): Likewise. * hash.c (hash_equal_op, hash_hash_op, hash_print_op): Likewise. * lib.c (package_local_symbols, package_foreign_symbols, find_max, find_if, rfind_if, populate_obj_hash): Likewise. * parser.c (circ_backpatch, get_visible_syms): Likewise. * struct.c (method_name, get_slot_syms): Likewise.
* expander: bogus undefined warnings from lisp1 values.Kaz Kylheku2019-10-301-8/+5
| | | | | | | | | | | | | Issue: (sys:lisp1-value x) throws a warning even if x is a predefined library function. This is caused by naively using expand to attempt to expand it as a symbol macro. * eval.c (expand_lisp1_value): Use expand_lisp1 instead of expand, just like expand_forms_lisp1. Because I didn't notice this problem when adding those two functions, expand_lisp1 is farther down the file and we need a forward declaration. (expand_lisp1_setq): Likewise, and eliminate the unbound variable check which is done by expand_lisp1.
* expander: allow TTY interrupt.Kaz Kylheku2019-10-281-0/+2
| | | | | * eval.c (expand): Call sig_check_fast so that if the expander gets into some kind of loop, it is interruptible.
* New function: identity*Kaz Kylheku2019-10-281-0/+1
| | | | | | | | | | | | | | | An version of identity with lax argument conventions. * eval.c (eval_init): Register identity* intrinsic. * lib.c (identity_star_f): New symbol variable. (identity_star): New function. (obj_init): gc-protect identity_star_f variable, and initialize it. * lib.h (identity_star_f): Declared. * txr.1: Documented.
* functions: provide accessors for basic properties.Kaz Kylheku2019-10-181-0/+3
| | | | | | | | | | | | | | * eval.c (eval_init): Register intrinsic functions fun-fixparam-count, fun-optparam-count, fun-variadic. * lib.c (get_param_counts): New static function. (fun_fixparam_count, fun_optparam_count, fun_variadic): New functions. * lib.h (fun_fixparam_count, fun_optparam_count, fun_variadic): Declared. * txr.1: Documented.
* New function: copy-tree.Kaz Kylheku2019-10-161-0/+1
| | | | | | | | | | * eval.c (eval_init): Register copy-tree intrinsic. * lib.c (copy_tree): New function. * lib.h (copy_tree): Declared. * txr.1: Documented.
* eval: bugfix: don't pass eval env to macroexpand.Kaz Kylheku2019-10-131-1/+1
| | | | | | | * eval.c (eval_intrinsic): macroexpand must be called with the nil environment. If we had a macro env parameter, we could pass that. In any case, we mustn't pass an eval environment to the expander.
* expander: origin_hash must be eq-based.Kaz Kylheku2019-10-111-1/+1
| | | | | | | * eval.c (eval_init): Initialize origin_hash as eq-based hash table, not eql-based. The main culprit are range #R(x y) objects. eql equality recurses over these, and if it encounters a circular one like #R(#1# #1#), it crashes.
* tree: allow quasiquoting into #T syntax.Kaz Kylheku2019-09-281-1/+7
| | | | | | | | | | | | | | | | | * eval.c (tree_lit_s, tree_construct_s): New symbol variables. (expand_qquote_rec): Handle sys:tree-lit syntax generated by quasi-quoted #T notaton by expanding and converting to sys:tree-constuct call. (eval_init): Initialize tree_lit_s and tree_construct_s. * eval.h (tree_lit_s, tree_construct_s): Declared. * parser.y (tree): Produce sys:tree-lit syntax when #T is quasi-quoted, and unquotes occur inside it. * tree.c (tree_construct_fname, tree_construct): New static functions. (tree_init): Register sys:tree-construct intrinsic function.
* symbol-function: support lambda expressions.Kaz Kylheku2019-09-271-7/+3
| | | | | | | | | | | | | | | | | * eval.c (lookup_fun): Check for a lambda expression and return a faked binding containing the interpreted function. (do_eval, op_fun): Remove checks for lambda that are now being done in lookup_fun. In many other places where lookup_fun is used, we still need lambda checks, like in the expander. * share/txr/stdlib/place.tl (sys:get-fun-getter-setter): Take form argument. Diagnose assignments to lambda, and to unknown function place syntax. (defplace symbol-function): Pass sys:*pl-form* to sys:get-fun-getter-setter as form argument. * txr.1: fboundp and symbol-function doc updated.
* fun operator: don't cons binding when handling lambda.Kaz Kylheku2019-09-261-4/+4
| | | | | | * eval.c (op_fun): Don't cons up a fake fbinding when processing lambda; just return result of func_interp. Test for null fbinding consolidated, too.
* lookup_fun: eliminate recursion.Kaz Kylheku2019-09-261-24/+24
| | | | | | | | * eval.c (lookup_fun); Use iteration to search the nested environments. Put this code ahead of the global search so we fall back on it. Also, let's check for a compound function name first, so we don't search the environments for this.
* func-get-name: fix bogus return for nil argument.Kaz Kylheku2019-09-261-10/+15
| | | | | | | | | | | | * eval.c (func_get_name): when func_get_name has a nil function argument and nil env, it falls back on method_name, which naively searches its space and finds some static slots with a nil value which is then returned as a method name. Let's put in a type check that the argument must be a function. Also, let's drop the recursion in the nested environment search and switch to iteration, so we don't do these wasteful sanity checks on multiple re-entries of the function.
* New data structure: binary search trees.Kaz Kylheku2019-09-251-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Adding binary search trees based on the new tnode cell. The scapegoat algorithm is used, which requires no additional storage in a cell. In the future we may go to something else, like red-black trees, and carve out a bit in the tag field of the cell for the red/black color. Tree cells store only single key objects, not key/value pairs. However, which part of the key object is compared is determined by a custom key function stored in the tree container. For instance, tree nodes can be cons cells, and car can be used as the key function; the cdr then stores an associated value. Trees have a printed notation #T(<props> <key>*) where <props> is a list of up to three items: <props> ::= ([<key-fn> [<less-fn> [<equal-fn>]]]) key-fn, less-fn and equal-fn are function names. If they are missing or nil, they default, respectively, to identity, less and equal. For security, the printed notation is machine-readable only if these options are symbols, not lambda expressions. Furthermore, the symbols must be listed in the special variable *tree-fun-whitelist*. * eval.c (less_s): New symbol variable. (eval_init): Initialize less_s. * eval.h (less_s): Declard. * parser.h (grammar): New #T token recognized, mapped to HASH_T. * parser.y (HASH_T): New terminal symbol. (tree): New non-terminal symbol. (i_expr, n_expr): Add tree to productions. (fname_helper): New static function. (yybadtoken): Map HASH_T to "#T". * protsym.c: Tweaked accidentally; remove. * tree.c (TREE_DEPTH_MAX): New macro. (struct tree): New struct type. (enum tree_iter_state): New enumeration. (struct tree_iter): New struct type. (tree_iter_init): New macro. (tree_s, tree_fun_whitelist_s): New symbol variables. (tn_size, tn_size_one_child, tn_lookup, tn_find_next, tn_flatten, tn_build_tree, tr_rebuild, tr_find_rebuild_scapegoat, tr_insert, tr_lookup, tr_do_delete, tr_delete, tree_insert_node, tree_insert, tree_lookup_node, tree_lookup, tree_delete, tree_root, tree_equal_op, tree_print_op, tree_mark, tree_hash_op): New static functions. (tree_ops): New static struct. (tree): New function. (tree_init): Initialize tree_s and tree_fun_whitelist_s symbol variables. Register intrinsic functions tree, tree-insert-node, tree-insert, tree-lookup-node, tree-lookup, tree-delete, tree-root. Register special variable *tree-fun-whitelist*. * tree.h (tree_s, tree_fun_whitelist_s, tree): Declared. (tree_fun_whitelist): New macro.
* New data type: tnode.Kaz Kylheku2019-09-221-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Binary search tree nodes are being added as a basic heap data type. The C type tag is TNOD, and the Lisp type is tnode. Binary search tree nodes have three elements: a key, a left child and a right child. The printed notation is #N(key left right). Quasiquoting is supported: ^#N(,foo ,bar) but not splicing. Because tnodes have three elements, they they fit into TXR's four-word heap cell, not requiring any additional memory allocation. These nodes are going to be the basis for a binary search tree container, which will use the scapegoat tree algorithm for maintaining balance. * tree.c, tree.h: New files. * Makefile (OBJS): Adding tree.o. * eval.c (expand_qquote_rec): Recurse through tnode cells, so unquotes work inside #N syntax. * gc.c (finalize): Add TNOD to no-op case in switch; tnodes don't require finalization. (mark_obj): Traverse tnode cell. * hash.c (equal_hash): Add TNOD case. * lib.c (tnode_s): New symbol variable. (seq_kind_tab): New entry for TNOD, mapping to SEQ_NOTSEQ. (code2type, equal): Handle TNOD. (obj_init): Initialize tnode_s variable. (obj_print_impl, populate_obj_hash): Handle TNOD. (init): Call tree_init function in tree.c. * lib.h (enum type, type_t): New enumeration TNOD. (struct tnod): New struct type. (union obj, obj_t): New union member tn of type struct tnod. (tnode_s): Declard. * parserc.c (circ_backpatch): Handle TNOD, so circular notation works through tnode cells. * parser.l (grammar): Recognize #N prefix, mapping to HASH_N token. * parser.y (HASH_N): New grammar terminal symbol. (tnode): New nonterminal symbol. (i_expr, n_expr): Add tnode cases to productions. (yybadtoken): Map HASH_N to "#N" string.
* New function: tailp.Kaz Kylheku2019-09-031-0/+1
| | | | | | | | | | * eval.c (eval_init): Register tailp intrinsic. * lib.c (tailp): New function. * lib.h (tailp): Declared. * txr.1: Documented.
* interpreter: trivial let goes through let* case.Kaz Kylheku2019-08-311-1/+1
| | | | | | | * eval.c (bindings_helper): If there are no bindings or just one binding, then go through the sequential case. Thus trivial let is treated like let*. This avoids the continuation-related overheads incurred in the parallel case.
* interpreter: bug between let* and continuations.Kaz Kylheku2019-08-311-11/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | This was discovered by user vapnik spaknik. If a let* form is evaluated in a top-level expression such that the incoming environment is null, and a continuation is captured in the init-form of the first variable, when that continuation is invoked, an exception is thrown: "copy-env: nil is not of type env". Short repro: 1> (block nil (let* ((x (yield 42))))) ** copy-env: nil is not of type env In fact, the bug isn't that we're trying to copy nil. the problem is we are trying to copy an environment we should not be copying at all. In fact, for sequential binding, there is no need for that logic at all; it's only parallel binding which needs it, because all of the init-forms are inserting bindings into the same environment. * eval.c (bindings_helper): Don't bother with registering a copy handler for sequential binding logic, because we don't mutate environments. All of that code is moved into the parallel case.
* New function: cptr-buf.Kaz Kylheku2019-08-211-0/+1
| | | | | | | | | | * eval.c (eval_init): Register cptr-buf intrinsic. * lib.c (cptr_buf): New function. * lib.h (cptr_buf): Declared. * txr.1: Documented.
* New function: intern-fb.Kaz Kylheku2019-08-201-0/+1
| | | | | | | | | | | | | | | To accompany find-symbol-fb, there is intern-fb, which is like intern, but searches the fallback list. * eval.c (eval_init): Register intern-fb intrinsic. * lib.c (intern_fallback_intrinsic): New function. Does defaulting and error checks, then calls intern_fallback, just like intern_intrinsic calls intern. * lib.h (intern_fallback_intrinsic): Declared. * txr.1: Documented.
* lib: streamline interning slightly.Kaz Kylheku2019-08-201-1/+1
| | | | | | | | | | | | | | | | | | | | | We get rid of some defaulting and error checks from interning. This saves a few cycles on startup in the large number of intern calls that are performed. * eval.c (eval_init): Wire the intern intrinsic to the new intern_intrinsic function rather than intern. * lib.c (intern): Remove package lookup and error check on str argument. (intern_intrinsic): New function, which has the package lookup and error check. (intern_fallback): Remove package lookup and error check. * lib.h (intern_intrinsic): Declared. * txr.c (txr_main): Fix one instance of an intern call that relies on defaulting of the second argument, by passing cur_package.
* new functions: find-symbol and find-symbol-fb.Kaz Kylheku2019-08-191-0/+2
| | | | | | | | | | | | | | | | | | Turns out, there is already a find_symbol in lib.c, completely unused. * eval.c (eval_init): Register find-symbol and find-symbol-fb intrinsics. * lib.c (find_symbol): Fix this hitherto unused function to do correct defaulting of the package argument and, to accept an additional argument specifying the not-found value. (find_symbol_fb): New function. * lib.c (find_symbol): Declaration updated. (find_symbol_fb): Declared. * txr.1: Documented.
* bugfix: line number of unbound vars not reported.Kaz Kylheku2019-08-131-2/+5
| | | | | * eval.c (expand): Do not create expansion debug frames for atomic forms, only for compound forms.
* expander: bugfix: spurious lambda form warning.Kaz Kylheku2019-08-071-5/+5
| | | | | | | | | | * eval.c (do_expand): In the expansion logic for function calls, after we have done the dot-to-apply transform, we must thereafter consistently refer to the new front element of the form insym, and not the original element sym. The first element may be a lambda form moved into the second position by dot-to-apply. We then falsely warn about that being in the operator position. Test case: ((lambda ()) . 3).
* txr: regression: lack of file name in error messages.Kaz Kylheku2019-07-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This was broken on April 21, 2019 in commit 2e36e0feae8d1dd75c8410b365d7dc33b30ce66b (April 21, 2019) which changed parse_once in such a way that the name being passed down was no longer stored in the parser. The ensure_parser function being relied upon doesn't take on the responsibility of propagating the name from the stream to the parser, so the parser ends up with a nil name. Let's just smooth this over by having ensure_parser take a name parameter. If ensure_parser creates a new parser, it puts in the name. If the name is nil, then it is taken from the stream. * eval.c (load): Pass name to ensure_parser. * match.c (v_load): Likewise. * parser.c (parser): Take name parameter, and plant into newly constructed parser. (ensure_parser): Take name parameter. When creating a parser, give it that name, or if name is specified as nil, then give it the name taken from the stream. (parser_set_lineno): Pass nil name to ensure_parser. (lisp_parse_impl, read_file_common): Pass name to ensure_parser. * parser.h (parser, ensure_parser): Declarations updated. * parser.y (parse_once): Pass name to ensure_parser. * txr.c (txr_main): Pass spec_file_str to ensure_parser.
* expander: relax sys:setq and lisp-1 stringency.Kaz Kylheku2019-06-171-23/+21
| | | | | | | | | | | | | | | | | | | | | | | | The motivation here is that there are behaviors in the expander which hinder symbol-macro-based renaming techniques. For instance (expand '(symacrolet ((x y)) (sys:setq x x))) throws. The right hand side of the setq is fine, but the left hand one is a forbidden symbol macro. Yet, we would just like this to expand to (sys:setq y y). The original idea was that sys:setq occurs as a result of macro-expansion. Therefore, if its argument is a symbol macro, something must be wrong; it didn't get expanded. That reasoning is wrong in the face of explicit expansion techniques that make multiple expansion passes. For instance (set a b) can become something like (sys:setq #:g0005 #:g0007) when the intent that in another round of renaming these gensyms will be defined as symbol macros which perform one more renaming. * eval.c (expand_lisp1_value, expand_lisp1_setq): Macro-expand the symbol and work with the expanded one. We still keep the check for a symbol macro; these situations can arise if a symbol macro cannot expand due to circularity. (do_expand): When checking sys:setq for a bad symbol or symbol macro, work with the expanded argument.
* Replace lt(x, zero) pattern.Kaz Kylheku2019-06-151-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | This slight inefficiency occurs in some 37 places in the code. In most places we replace lt(x, zero) with minusp(x). In a few places, !plusp(x) is used and surrounding logic is simplified. In one case, the silly pattern lt(x, zero) ? t : nil is replaced with just minusp(x). * buf.c (sub_buf, replace_buf): Replace lt. * combi.c (perm, rperm, comb, rcomb): Likewise. * eval.c (do_format_field): Likewise. * lib.c (listref, sub_list, replace_list, split_func, split_star_func, match_str, lazy_sub-str, sub_str, replace_str, sub_vec, replace_vec): Likewise. * match.c (weird_merge): Likewise. * regex.c (match_regex, match_regex_right_old, match_regex_right, regex_prefix_match, regex_range_left, regex_range_right): Likewise.
* load: always bind *load-path* to actual path.Kaz Kylheku2019-05-201-1/+3
| | | | | | | | | | | * eval.c (load): Bind *load-path* to the real path that was used to open the file, not to the tentative path to which the .tl suffix was added, subject to compatibility option. * txr.1: Documentation for load revised for better wording and to document the above change. Fixed *load-path* being wrongly called a macro, and also load being called a macro. Compat note added.
* interpreter: small code tidying in me_interp_macro.Kaz Kylheku2019-05-021-2/+1
| | | | | | * eval.c (me_interp_macro): Combine initialization and assignment into one. There was previously code between the two that got removed when the old debugger was scrubbed.
* interpreter: don't bother with dot position.Kaz Kylheku2019-05-021-6/+1
| | | | | | * eval.c (do_eval_args): Since function call expressions all undergo the dot-to-apply transform, there is no need look for a form in the dot position.
* bugfix: apply regression.Kaz Kylheku2019-05-021-1/+1
| | | | | | | | | | | | | | | | | | | | | | Refactorings to apply released TXR 192 broke it, causing apply to fail to treat non-list sequences as individual arguments, as documented. This affects dotted application as well. ;; wrong (list . "abc") -> "abc" ;; correct (list . "abc") -> (#\a #\b #\c) With some misgivings, I'm not making the behavior subject to the -C compat option. * eval.c (applyv): Two things are wrong here: we moved the last fixed argument into args->list without turning it into a one-element list. Secondly, we didn't pass this list through apply_intrinsic_frob_args. We can combine both actions into just calling tolist, which is what apply_intrinsic_frob_args will do with the car of a one-element list.
* debugger: expand frames.Kaz Kylheku2019-04-291-33/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch adds special unwind frames for backtracing expansions. With this, we can get rid of the global variable last_form_expanded, since to get the last form expanded, we just search for the most enclosing expand frame. * eval.c (last_form_expanded): Global variable removed. (error_trace): Use uw_last_form_expanded() instead of last_form_expanded. (expand_eval): No need to save and restore last_form_expanded any more. (expand_lisp_setq, expand_setqf, expand_lisp1, do_expand): Use uw_last_form_expanded(). (expand, do_macroexpand_1): Push and pop expand frame. This fixes a bug: do_macroexpand_1 was not recording last_form_expanded. Evaluation of top-level forms uses explicit macroexpansion, therefore top-level evaluation was neglecting to set last_form_expanded. This explains weird behavior I saw in the listener from time to time, when errors would report against the expansion of the wrong form. (eval_init): Remove reference to last_form_expanded variable. * eval.h (last_form_expanded): Declaration removed. * share/txr/stdlib/debug.tl (expand-frame print-trace, expand-frame loc): New methods. (print-backtrace): Include uw-expand frames in the backtrace. * unwind.c (expand_frame_type): New static variable. (uw_find_frames_by_mask): Handle UW_EXPAND. (uw_last_form_expanded, uw_push_expand): New functions. (uw_late_init): Register expand-frame struct type. * unwind.h (enum uw_frtype): New enum member, UW_EXPAND. (uw_last_form_expanded, uw_push_expand): Declared.
* bugfix: source lineno off by one under hash bang.Kaz Kylheku2019-04-211-1/+3
| | | | | | | | | | | | | | * eval.c (load): When we read and discard a hash bang line, we must set the parser line number to two. * parser.c (parser_set_lineno): New function. * parser.h (parser_set_lineno): Declared. * txr.c (check_hash_bang): New argument, occurs. (txr_main): Track whether hash bang has occurred in a new local variable hb_occurs. Then, before parsing, if hash bang has occurred, set the line number to two.