summaryrefslogtreecommitdiffstats
path: root/eval.c
Commit message (Collapse)AuthorAgeFilesLines
* combi: fix permi and rpermi; impl combi, rcombi; test.Kaz Kylheku2024-06-241-0/+2
| | | | | | | | | | | | | | | | | | | | | | | | | * combi.c (permi_get, permi_peek): Fix algorithm. (permi_mark): New static function. (permi_ops): Reference permi_mark for mark operation. (permi): Initialize it->ul.next to nao as required by new get/peek algorithm. (rpermi_get, rpermi_peek): Fix algorithm. (rpermi_mark): New static function. (rpermi_ops): Reference permi_mark for mark operation. (rpermi): Initialize it->ul.next to nao as required by new get/peek algorithm. (combi_get, combi_peek, combi_mark, combi_clone): New static functions. (combi_ops): New static structure. (combi): New function. (rcombi_get, rcombi_peek, rcombi_mark, rcombi_clone): New static functions. (rcombi_ops): New static structure. (rcombi): New function. * combi.h (combi, rcombi): Declared. * tests/015/comb.tl: New tests.
* New rpermi: iterator version of rpermKaz Kylheku2024-06-191-0/+1
| | | | | | | | | | | * combi.c (rpermi_get, rpermi_peek, rpermi_clone): New static functions. (rpermi_ops): New static structure. (rpermi): New function. * combi.h (rpermi): Declared. * eval.c (eval_init): Register rpermi intrinsic.
* New function: copy-iter.Kaz Kylheku2024-06-151-0/+1
| | | | | | * eval.c (eval_init): Register copy-iter intrinsic. * lib.[ch] (copy_iter): New function.
* New permi: iterator version of perm.Kaz Kylheku2024-06-151-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | * eval.c (eval_init): Register permi intrinsic. * combi.c (permi_get, permi_peek, permi_clone): New static functions. (permi_ops): New static structure. (permi_iter): New static function. (permi): New function. * combi.h (permi): Declared. * lib.h (struct seq_iter_ops): New function pointer, clone. (seq_iter_ops_init, seq_iter_ops_init_nomark): Initialize new member. (seq_iter_ops_init_clone): New macro. (seq_iter_cls): Existing external name declared. (seq_iter_cobj_ops, seq_iter_mark_op): Previously internal names declared external. * lib.c (seq_iter_mark_op, seq_iter_cobj_ops): Static variables become extern. (seq_iter_clone): New static function. (seq_iter_init_with_info): Use seq_iter_clone instead of assuming we can trivially clone an iterator state bitwise.
* bugfix: one missing case of fmt_cat separator defaulting.Kaz Kylheku2024-05-271-1/+1
| | | | | | | | | | In a recent commit, the defaulting of the separator in quasiliteral variable formatting was moved down into the fmt_cat routine. One stray case remains in subst_vars. * eval.c (subst_vars): A call to fmt_cat is specifying a separator value consisting of a single space. This is wrong, preventing fmt_cat from defaulting it in different ways according to type.
* quasiliterals: buffers in hex, separation for strings and buffers.Kaz Kylheku2024-05-271-2/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | In this commit, output variables in the TXR Pattern language and in TXR Lisp quasiliterals now support separator strings for values that are strings and buffers. Values which are buffers appear differently: they are rendered as a sequence of lower case hex digit pairs. When a string-valued variable specifies a separator, the separator appears between characters of the string value. Previously, the separator was ignored. When a buffer-valued variable specifies a separator. the separator appears between pairs of digits, not between digits. For instance if ethaddr is a variable holding #b'08:00:27:79:c7:f5', then the quasiliteral `@ethaddr` produces "08002779c7f" whereas `@{ethaddr ":"}` produces "08:00:27:79:c7:f5". * buf.[ch] (buf_str_sep): New function. * lib.[ch] (fmt_str_sep): New function. * eval.c (fmt_cat): If the argument is a string, and a separator is present, replace the value with the result of calling fmt_str_sep. If the argument is a buffer, and a separator is present, use buf_str_sep to convert to a string, otherwise use tostringp. * txr.1: Section on Output Variables updated. * tests/012/readprint.tl: New tests.
* quasilit: move separator defaulting to fmt_cat.Kaz Kylheku2024-05-031-4/+5
| | | | | | | | | | | | | | | | | | | | | The motivation here is an upcoming change in which we will support the separator modifier for buffers and strings. Currently, it does nothing. If we write `@{a ":"}`, and a is a buffer or string, the separator is ignored. We don't fix that in this commit, but we fix the problem that some higher level formatting functions are defaulting the separator to " " (single space) and passing it down. We want to control the defaulting based on the type of the object in one place. * eval.c (fmt_cat): Do not assume here that sep has been defaulted; do the defaulting to space here. (format_field, fmt_flex): Initialize the separator to nil, not space. If no separator occurs among the modifiers, it gets passed down as nil through to fmt_cat. (fmt_simple): Don't default the sep argument to space; pass it through to do_format_field which will pass it down to fmt_cat.
* buf: pprint produces hex, not raw bytes.Kaz Kylheku2024-05-031-5/+14
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The pprint semantics of buffers is that the raw bytes are dumped into the stream. This is poor. It was hastily designed based on analogy with strings, which pprint by just sending their contents to the stream; but for strings this is justified because they represent text. We also fix the semantics of buffer values being rendered by quasiliteral notation. Currently, the are treated as sequences, and so they explode into individual decimal integers. * buf.c (buf_pprint): Print the bytes as pairs of lower-case hex digits, with no line breaks. In 294 compatibility or lower, put out bytes as before. * eval.c (fmt_cat): When not in 294 compatibility mode, treat a buffer object via tostringp, which will render it to hexadecimal via buf_pprintf. In compatibility mode, treat it as before, which is as a sequence: the individual values of the buffer are converted to text, thus decimal values in the range 0 to 255, catenated using the given separator. * tests/012/readprint.tl: New tests. * txr.1: Documented. Also expanding on what pretty printing means in TXR.
* New function: iter-cat.Kaz Kylheku2024-04-161-0/+1
| | | | | | | | | | | | | | | | * eval.c (eval_init): Register iter-cat intrinsic. * lib.h (struct seq_iter): New union member dargs. (iter_catv): Declared. * lib.c (seq_iter_get_cat, seq_iter_peek_cat): New static functions. (si_cat_ops): New static structure. (iter_catv): New function. * tests/012/iter.tl: New tests. * txr.1: Documented.
* New function: lcons-force.Kaz Kylheku2024-04-041-0/+1
| | | | | | | | * lib.[ch] (lcons_force): New function. * eval.c (eval_init): Register lcons-force intrinsic. * txr.1: Documented.
* New function: rangeref.Kaz Kylheku2024-03-071-0/+1
| | | | | | | | | | | | | | | | | | | | | Because ranges can be iterated like sequences, and are identified as vector-like, they have to support indexing. However, ranges already have semantics as a function: with a sequence argument, they slice it. Let's put the semantics into a function called rangeref, so it can be coherently documented. * eval.c (eval_init): Register rangeref intrinsic. * lib.c (generic_funcall): Range as a function works in terms of rangeref. (ref): Handle RNG case via rangeref. (rangeref): New function. * lib.h (rangeref): Declared. * tests/012/seq.tl: New tests.
* mapcar: avoid alloca proportional to number of args.Kaz Kylheku2024-03-011-2/+16
| | | | | | | | | | | | | | | * eval.c (MAP_ALLOCA_LIMIT): New preprocessor symbol. (map_common): If the number of args is greater than MAP_ALLOCA_LIMIT, then allocate the array of seq_iter_t structures from chk_malloc rather than alloca. In case an exception might be thrown during the execution of this function, we bind that memory to a buf object. If we return normally, we call the new function buf_free to release it. Otherwise we rely on the garbage collector. * buf.[ch] (buf_free): New function. * tests/012/seq.tl: Test case which hits this behavior.
* zip: make more generic.Kaz Kylheku2024-03-011-7/+54
| | | | | | | | | | | | | | | | * lib.c (do_pa_12_1_v, pa_12_1_v): Static functions removed. (transposev, transpose): Functions removed. * lib.c (transposev, transpose): Declarations removed. * eval.c (join_f): New global variable. (zip_fun, zipv, transpose): New static functions. (eval_init): gc-protect join_f, and initialize it. Registration of zip intrinsic goes to zipv rather than transposev. sys:fmt-join and join registered with help of global join_f rather than local. * tests/012/seq.tl: New zip test cases.
* mapcar, mappend: switch to seq_build.Kaz Kylheku2024-02-271-6/+9
| | | | | | | | | | | | | | * lib.c (mapcar): Implement with seq_iter and seq_build, rather than relying on mapcar_listout and make_like. (mappend): Replace list_collect_decl and make_like with seq_build. * eval.c (map_common): Replace list_collect_decl and make_like with seq_build. The collect_fn is now a pointer to either seq_add or seq_pend rather than list_collect or list_collect_append. (mapcarv, mappendv): Pass seq_add and seq_pend to map_common, rather than list_collect and list_collect_append.
* New function: cons-count.Kaz Kylheku2024-02-091-0/+1
| | | | | | | | | | | | | * eval.c (eval_init): Register cons-count intrinsic. * lib.c (cons_count_rec): New static function. (cons_count): New function. * lib.h (cons_count): Declared. * tests/012/cons.tl: New tests. * txr.1: Documented.
* New function: cons-find.Kaz Kylheku2024-02-091-12/+1
| | | | | | | | | | | | | | | | | * eval.c (cons_find): Static function removed; a new one is implemented in lib.c. (eval_init): Register cons-find intrinsic. * lib.c (cons_find_rec): New static function. (cons_find): New function. * lib.h (cons_find): Declared. * tests/012/cons.tl: New file. * txr.1: Documented cons-find together with tree-find. Document that tree-find's test-fun argument is optional, defaulting to equal.
* New function: hist-sort-by.Kaz Kylheku2024-02-021-0/+1
| | | | | | | | | | | | | * eval.c (eval_init): Register hist-sort-by intrinsic. * lib.c (hist_sort_by): New function. (hist_sort): Wrapper for hist_sort_by now. * lib.h (hist_sort_by): Declared. * tests/012/sort.tl: Tests. * txr.1: Documented.
* We need a length-< special method.Kaz Kylheku2024-01-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Structure objects can be used to implement lazy structures such as sequences. It is undesirable to take the length of a lazy sequence because it forces all of its elements to exist. Moreover, if the sequence is infinite, it is impossible. There are situations in which it is only necessary to know whether the length is less than a certain bound, and for that we have the length-< function. That works on infinite sequence such as lazy lists, requiring them to be forced only so far as to determine the truth value of the test. We need objects that implement lazy sequences to work with this function. * struct.h (enum special_slot): New member length_lt_m. * lib.h (length_lt_s): Symbol variable declared. * struct.c (special_sym): New entry in this table, associating the length_lt_m enum with the length_lt_s symbol variable. * lib.c (length_lt_s): Symbol variable defined. (length_lt): Handle COBJ objects that are structures. we test whether they have a length-< method, or else length method. If they don't have either, we throw. We don't fall back on the default case for objects that don't have a length-< method, because the diagnostic won't be good if they don't have a length method either; the programmer will be informed that the length function couldn't find a length method, without mentioning that it was actually length-< that is being used. * eval.c (eval_init): Register length-< using the length_lt_s symbol variable rather than using intern. * txr.1: Documented. * tests/012/oop-seq.tl: New tests.
* Copyright year bump 2024.Kaz Kylheku2024-01-181-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * LICENSE, LICENSE-CYG, METALICENSE, Makefile, alloca.h, args.c, args.h, arith.c, arith.h, autoload.c, autoload.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, gzio.c, gzio.h, hash.c, hash.h, itypes.c, itypes.h, jmp.S, lib.c, lib.h, linenoise/linenoise.c, linenoise/linenoise.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, psquare.h, rand.c, rand.h, regex.c, regex.h, signal.c, signal.h, socket.c, socket.h, stdlib/arith-each.tl, stdlib/asm.tl, stdlib/awk.tl, stdlib/build.tl, stdlib/cadr.tl, stdlib/compiler.tl, stdlib/constfun.tl, stdlib/conv.tl, stdlib/copy-file.tl, stdlib/csort.tl, stdlib/debugger.tl, stdlib/defset.tl, stdlib/doloop.tl, stdlib/each-prod.tl, stdlib/error.tl, stdlib/except.tl, stdlib/expander-let.tl, stdlib/ffi.tl, stdlib/getopts.tl, stdlib/getput.tl, stdlib/glob.tl, stdlib/hash.tl, stdlib/ifa.tl, stdlib/keyparams.tl, stdlib/load-args.tl, stdlib/match.tl, stdlib/op.tl, stdlib/optimize.tl, stdlib/package.tl, stdlib/param.tl, stdlib/path-test.tl, stdlib/pic.tl, stdlib/place.tl, stdlib/pmac.tl, stdlib/quips.tl, stdlib/save-exe.tl, stdlib/socket.tl, stdlib/stream-wrap.tl, stdlib/struct.tl, stdlib/tagbody.tl, stdlib/termios.tl, stdlib/trace.tl, stdlib/txr-case.tl, stdlib/type.tl, stdlib/vm-param.tl, stdlib/with-resources.tl, stdlib/with-stream.tl, stdlib/yield.tl, 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 2024.
* eval: potential gc problem in binding.Kaz Kylheku2024-01-061-1/+1
| | | | | | | | | | * eval.c (reparent_env): This function is used in bindings helper, when special variables are involved. It makes a new environment the parent of an existing one, just by assigning the pointer. This is wrong under generational GC if it makes a mature object point to a new object. We fix it with the set macro. I've not seen a crash because of this; I caught it by inspection.
* New functions: read-objects, file-get-objects, ...Kaz Kylheku2023-12-191-0/+1
| | | | | | | | | | | | | | | | | | | | | | * parser.c (read_objects_common): New static function, formed from read_objects_from-string. (read_objects_from_string): Now wrapper for read_objects_common. (read_objects): New function. * parser.h (read_objects): Declared. * eval.c (eval_init): Register read-objects intrinsic. * autoload.c (getput_set_entries): Add three new symbols: file-get-objects, file-put-objects and file-append-objects. * stdlib/getput.tl (put-objects): New system function. (file-get-objects, file-put-objects, file-append-objects): New functions. * txr.1: Documented. * tests/018/getput.tl: New file.
* stdlib/error.tl problem rears its head.Kaz Kylheku2023-11-161-8/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There used to be a hack in the Makefile whereby the compilation of stdlib/error.tl was forced to occur earlier. I got rid of it. Now, the issue that was solving reproduced. A situation can occur whereby loading error.tl triggers loading some other files, which end up performing an expansion that needs sys:bind-mac-check: but that function has not yet been defined because error.tl has not yet loaded that far. The issue occurs when stdlib/place.tl is compiled before stdlib/error.tl. The compiled place.tl has a run-time dependency on functions in error.tl, because the compiled version of mac-param-bind and other forms relies on a run-time support function sys:bind-mac-check defined in stdlib/error.tl. * stdlib/error.tl (sys:dig): This function triggers the problem, but it's not the only cause. Here, the problem is because the (set ...) macro is used which triggers loading the stdlib/place module. That brings in the need for bind-mac-params. So here we use sys:setq instead. That is not a complete solution. The changes in eval.c are also required, because built-in macros like whilet expand to code that uses the (set ...) macro. Note how sys:dig uses whilet. (sys:bind-mac-check, sys:bind-mac-error): We move these functions above compile-warning. This addresses remaining circularity problem. The compile-warning function uses the catch macro which brings in stdlib/except.tl, which pulls in stdlib/op.tl due to its use of (do ...), which pulls in stdlib/place.tl. So if we already define sys:bind-mac-check at that point, we are good. * eval.c: Sweep the file for almost all places where macros generate code that invokes (set <symbol> <value>) and replace that with (sys:setq <symbol> <value>) to eliminate the dependency on loading the stdlib/place.tl module. (me_def_variable, me_gun, me_while_until_star, me_case, me_whilet, me_mlet, me_load_for, me_pop_after_load): In all these macro expanders, use sys:setq rather than set in the generated code. * tests/019/load-hook.tl: Some test cases here look for a macro expansion containing (set ...), needing to be fixed to look for (sys:setq ...) due to the change in eval.c.
* New accessor: mref.Kaz Kylheku2023-11-151-0/+1
| | | | | | | | | | | | | | * eval.c (eval_init): Register mref intrinsic. * lib.[ch] (mref): New function. * stdlib/place.tl (sys:mref1): New place. (mref): New place macro, defined in terms of sys:merf1, ref place and mref function. * tests/012/seq.tl: New tests. * txr.1: Documented.
* New: length-list-<, length-<Kaz Kylheku2023-10-051-0/+2
| | | | | | | | | | | | | | | | | | | | | | These are functions for testing whether a list or sequence is shorter than a given integer. This is cheaper than calculating the length of lists, which is in some cases impossible if they are infinite. A length-str-< function already exists, useful with lazy strings. length-< uses length-list-< or length-str-< as appropriate * lib.[ch] (length_list_lt, length_lt): New functions. * eval.c (eval_init): length-list-< and length-< intrinsics registered. * tests/012/seq.tl: New tests. * txr.1: Documented.
* New hist-sort function.Kaz Kylheku2023-09-251-0/+2
| | | | | | | | | | | | | | | * eval.c (eval_init): Register hist-sort intrinsic. * lib.c (gt_f): New global variable. (hist_succ_f): New static variable. (hist_succ): New static function. (hist_sort): New function. * lib.h (gt_f, hist_sort): Declared. * tests/012/sort.tl: New tests. * txr.1: Documented.
* New functions: nested-vec-of and nested-vec.Kaz Kylheku2023-09-211-0/+2
| | | | | | | | | | | | | | | * eval.c (eval_init): Register nestd-vec-of and nested-vec intrinsics. * lib.[ch] (vec_allocate, vec_own, vec_init): New static functions. (vector, copy_vec): Expressed in terms of new functions. (nested_vec_of_v, nested_vec_v): New functions. * args.[ch] (args_cat_from): New function. * tests/010/vec.tl: New tests. * txr.1: Documented.
* Use vargs typedef instead of struct args *.Kaz Kylheku2023-09-051-49/+49
| | | | | | | | | | | | | | | | | | | | | | | | | | | The vargs typedef is underused. Let's use it consistently everywhere. * args.c, * args.h, * args.c, * args.h, * arith.c, * eval.c * ffi.c, * gc.c, * hash.c, * lib.c, * lib.h, * parser.c, * stream.c, * struct.c, * struct.h, * syslog.c, * syslog.h, * unwind.c, * vm.c, * vm.h: All "struct args * declarations replaced with existing "varg" typedef that comes from lib.h.
* New function: str-esc.Kaz Kylheku2023-09-011-0/+1
| | | | | | | | | | * lib.[ch] (str_esc): New function. * eval.c (eval_init): str-esc intrinsic registered. * tests/015/esc.tl: New file. * txr.1: Documented.
* math: tofloat and toint in user-defined arithmetic.Kaz Kylheku2023-08-141-4/+0
| | | | | | | | | | | | | | | | | | * arith.c (tofloat_s, toint_s): New symbol variables. (tofloat, toint): If the argument is a COBJ, handle it via do_unary_method. (arith_init): Initialize new symbol variables. The functions tofloat, toint, tofloatz and tointz. are now registered here, rather than eval_init. * eval.c (eval_init): Remove registrations of tofloat, toint, tofloatz and tointz. * tests/016/ud-arith.tl: New tests. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* New feature: local symbol renaming.Kaz Kylheku2023-08-101-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The new function use-sym-as can bring a foreign symbol into a package under a different name, which is not that symbol's name. This is also featured in a new defpackage clause, :use-syms-as. With this simple relaxation in the package system, we don't require package local nicknames, which is more complicated to implement and less ergonomic, because it doesn't actually vanquish the use of ugly package prefixes on clashing symbols. * eval.c (eval_init): Register use-syms-as. * lib.c (use_sym_as): New function, made out of use_sym. (use_sym): Now a wrapper for use_sym_as. * lib.h (use_sym_as): Declared. * stdlib/package.tl (defpackage): Implement :use-syms-as clause. * tests/012/use-as.tl: New file. * txr.1: Documented, * stdlib/doc-syms.tl: Updated.
* unwind: bind *print-circle* to t in error trace.Kaz Kylheku2023-07-281-0/+5
| | | | | | * eval.c (error_trace): Push a new dynamic environment and bind *print-circle* to t. More could be done here like setting object limits.
* New functions and fixes in lexical introspection.Kaz Kylheku2023-07-271-21/+61
| | | | | | | | | | | | | | | | | | | | | | * evalc (macro_k): New keyword symbol variable. (lexical_binding_kind, lexical_fun_binding_kind) New functions. (lexical_var_p): Bugfix: if the symbol is a special variable, do not short-circuit to a nil answer. Special variables can be shadowed by symbol macros. The function is now defined in terms of lexical_binding_kind. (lexical_symacro_p, lexical_macro_p): New functions. (lexical_fun_p): Now defined using lexical_fun_binding_kind. (lexical_lisp1_binding): Bugfix: check for special variables; do not report special variables as :var. (eval_init): Initialize macro_k. Register new intrinsics: lexical-binding-kind, lexical-fun-binding-kind, lexical-symacro-p, lexical-macro-p. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* Do not unnecessarily invalidate vm binding cache.Kaz Kylheku2023-07-171-7/+18
| | | | | | * eval.c (op_defsymacro, rt_defsymacro, makunbound, fmakunbound): Don't call vm_invalidate_binding if there is no binding for the symbol.
* bug: compiled code keeps seeing var clobbered by symacro.Kaz Kylheku2023-07-171-0/+2
| | | | | | | | * eval.c (op_defsymacro, rt_defsymacro): We must call vm_invalidate_binding so the VM forgets a cached binding for this variable. * tests/019/redef.tl: Test added.
* Simplify top-level macro environments also.Kaz Kylheku2023-07-171-20/+14
| | | | | | | | | | | | Like was done with the function and variable top-level environments, we simplify the macro ones. * eval.c (func_get_name, lookup_mac, lookup_symac, lookup_symac_lisp1, op_defsymacro, rt_defsymacro, rt_defmacro, op_defmacro, reg_mac, reg_symacro): Adjust to simpler representation where the hash cell itself is the binding cell, rather than holding a binding cell in its cdr.
* Simplify top-level variable and function environments.Kaz Kylheku2023-07-161-23/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Since their inception, the top_fb and top_fb hashes associated symbols with bindings cells, causing an extra cons cell to be allocated for each entry. I don't remember why this is. It might have been that way so that gethash(top_fb, sym) would return a cell when the variable exists, or else nil. This was before we had functions like gethash_e and inhash that return the hash cell itself. A hash cell is also a cons and can serve as a binding just fine by itself.Let's make it so. For now, the macro and symbol macro environments stay the way they are; I will likely convert them also. * eval.c (env_fbind, env_vbind, lookup_global_var, lookup_sym_lisp1, lookup_fun, func_get_name, rt_defv, rt_defun, set_symbol_value, reg_fun, reg_varl): Update all these functions so they treat the hash cell from top_vb or top_fb as the binding cell, rather than putting or expecting the cdr of that cell (i.e the hash value) to be a binding cell. * hash.[ch] (gethash_d): New function. Jus gethash_e without the pesky self argument, that would only be needed for error reporting if we pass an object that isn't a hash. * stdlib/place.tl (sys:get-fun-getter-setter, sys:get-vb): These two functions must be adjusted since they refer to the top-fb and top-vb variables. sys:get-vb isn't used anywhere; it continues to exist in order to provide run-time support to files that were compiled with an buggy version of the symbol-value place.
* fix self name of var defining run-time support function.Kaz Kylheku2023-07-161-1/+1
| | | | * eval.c (rt_defv): Report as sys:rt-defv, not sys:defv.
* lib: avoid intern for symbol we already have.Kaz Kylheku2023-07-131-1/+1
| | | | | * eval.c (eval_init): We have a repeat_s variable; no need to call intern(lit("repeat"), user_package).
* Fix diagnostics which call non-symbol a symbol.Kaz Kylheku2023-07-111-0/+3
| | | | | | | | | | * eval.c (me_load_for): An object which is not one of the valid clause symbols is not necessarily a symbol; don't call it one in the diagnostic. * stdlib/struct.tl (sys:check-slot): Similarly, an object that isn't the name of a struct slot isn't necessarily a symbol; don't call it one.
* eval: take macro environment.Kaz Kylheku2023-06-271-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | With this change we fix the bug that the debugger commands yield their Lisp forms rather than evaluating them. * eval.c (eval_intrinsic): Takes one more argument, the macro environment. This is passed into env_to_menv as the root macro environment. (eval_init): Update registration of eval intrinsic to have two optional arguments. * eval.h (eval_intrinsic): Declaration updated. * parser.c (read_file_common, read_eval_ret_last): Pass nil argument to new parameter of eval_intrinsic. (repl): Pass the env parameter as the new menv parameter of eval_intrinsic, rather than the existing env parameter. This fixes the command dispatch in the debugger, since the command table is consists of symbol macros, and not variables. For instance the backtrace command bt is a binding of the bt symbol to the form (sys:print-backtrace), which has to be substituted for it and executed. When that envrionment is used as the ordinary environment, bt looks like a variable whose value is the list (sys:backtrace). * parser.y (elem, check_parse_time_action): Fix eval_intrinsic calls. * txr.c (txr_main): Likewise. * txr.1: Documented. * y.tab.c.shipped: Updated. * stdlib/doc-syms.tl: Updated.
* New functions keep-keys-if, separate-keys.Kaz Kylheku2023-06-071-0/+2
| | | | | | | | | | | * lib.[ch] (keep_keys_if, separate_keys): New functions. * eval.c (eval_init): keep-keys-if, separate-keys intrinsics registered. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* load: now passes args via *load-args*Kaz Kylheku2023-05-311-12/+26
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We can give additional arguments to load, which become arguments of the script, which it can retrieve via the *load-args* special variable. * eval.c (load_args_s): New symbol variable. (loadv): New function, taking over the implementation of load. This takes variadic arguments. Loadv binds the *load-args* variable from the list of variadic arguments. (load): Reduced to wrapper around loadv. (rt_load_for): Each clause in load for can now have arguments after the target name. If that file needs to be loaded, then the arguments are passed. (me_load_for): The macro expander for the load-for macro needs to allow for the load-arg expressions and generate code which passes them to sys:rt-load-for. They all get evaluated. (eval-init): Initialize load_args_s and register the *load-args* variable. Update registration of intrinsic function load to use loadv. * tests/019/load-ret.tl, * tests/019/load-ret/module.tl, * tests/019/load-ret/module2.tl: New files. * txr.1: Documented.
* load: now establishes a block named load.Kaz Kylheku2023-05-311-1/+3
| | | | | | | | | | | | | | | | | | | | | | Files loaded from the command line or via the load function or @(load) directive now have a block named load in scope. Thus (return-from load <expr>) may be used to abort a load when it is finished. The load function will then return the value of <expr>. * eval.c (load): Bind a load block around the whole thing and use the captured return value as the function's return value. * match.c (v_load): Bind the load block here too. Ensure that if the block return is taken, the ret variable contains next_spec_k, so that processing continues with whatever directive follows the @(load). * txr.c (txr_main): Bind the block in severl cases that load code. * txr.1: Documented.
* expander: support param macros in nested macro param lists.Kaz Kylheku2023-05-271-38/+56
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Parameter list macros work in inside macro parameter lists, like they do in function parameter lists. However, they ony work at the top level. Macro parameter lists are nested; they may contain nested parameter lists that match corresponding shapes in the argument list. This patch extends parameter list macros to work in nested macro parameter lists. * eval.c (expand_opt_params_rec, expand_params_rec): These two functions must be extended to take a body argument, and to return not just an expanded parameter list but a parameter list accompanied by a body. We do that by making them return a cons cell, whose car is the expanded parameter list and the cdr is the possibly transformed body. Additionally, these functions now call expand_param_macro on nested macro parameter lists. (expand_params): This function becomes slightly simpler as a result of the above changes. Because expand_params_rec already returns a cons cell holding a parameter list and body, we just return that as-is. * tests/011/keyparams.tl: Added some tests of this, vie the standard :key parameter list macro. A macro is tested which has a nested (:key ...) parameter list in a required parameter position as well as in an optional position. * txr.1: Documented.
* label/flet: bug: empty case wallops symbol macros.Kaz Kylheku2023-05-241-1/+1
| | | | | | | | | | | | | | * eval.c (make_var_shadowing_env): We cannot return the original env in the empty variable case, but earnestly make a new one. This function is used by the expander when walking the lbind/fbind special from emitted by labels/flet. That form clobbers the environment via make_fun_shadowing_env, which calls make_var_shadowing_env and then destructively moves the variable bindings to the function binding slot of the environment. The manifestation is that when we have (symacrolet ((x 1)) (labels () x)), the x fails to expand; it has been wrongly moved to the function bindings area of the macro environment.
* New special operator: compiler-letKaz Kylheku2023-05-161-2/+26
| | | | | | | | | | | | | | | | | | | | | | * eval.c (compiler_let_s): New symbol variable. (op_let): Recognize compiler-let for sequential binding. (do_expand): Traverse and diagnose compiler-let form. (eval_init): Initialize compiler_let_s and register the interpreted version of the operator. * stdlib/compiler.tl (compiler compile): Handle compiler-let form. (compiler comp-compiler-let): New method. (no-dvbind-eval): New function. * autoload.c (compiler-set-entries): Intern the compiler-let symbol in the user package. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* New special operator: progvKaz Kylheku2023-05-151-1/+66
| | | | | | | | | | | | | | | | | | | | | | Adding a progv operator, similar to the Common Lisp one. * eval.c (progv_s): New symbol variable. (op_progv): New static function. (do_expand): Recognize and traverse the progv form. (rt_progv): New static function: run-time support for compiled progv. (eval_init): Initialize progv_s, and register the the op_progv operator interpreting function. * stdlib/compilert (compiler compile): Handle progv operator ... (compiler comp-progv): ... via this new method. * tests/019/progv.tl: New file. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
* vm: bugfix: global lexicals looked up dynamically.Kaz Kylheku2023-05-151-0/+15
| | | | | | | | | | | | | | | | | | | | | | | | | The getlx and setlx VM instructions are using dynamic lookup for uncached bindings, due to using the same lookup_fun search function. They should use lookup_global_fun. That doesn't have an environment parameter though, so the type is not right. However, the VM never uses the environment parameter; it's always passing nil. We will get rid of the environment parameter in the lookup_fn callback and introduce a few wrappers. * eval.c, eval.h (lookup_global_fun, lookup_dynamic_var, lookup_dynamic_sym_lisp1): New functions. * vm.c (vm_stab_slowpath, vm_get_binding): lookup_fn argument loses environment parameter, and so we don't have to pass nil. (vm_gcall, vm_gapply): Use pass lookup_global_fun to to vm_stab. (vm_getsym, vm_getbind, vm_setsym, vm_gettab, vm_settab): lookup_fn argument loses environment parameter. (vm_execute): lookup functions replaced with the appropriate one-argument ones. GETLX and SETLX see a behavior change, due to using lookup_global_var which doesn't search the dynamic environment.
* bug: symbol-value place always global.Kaz Kylheku2023-05-141-0/+13
| | | | | | | | | | | | | | | | | | | | | | | | | | | We have a problem. If v is a dynamic variable, then the form (let (v) (set (symbol-value 'v) 3)) is not behaving correctly; it's updating the top-level value of v not the rebound one. * eval.c (set_symbol_value): New static function. (eval_init): Register sys:set-symbol-value intrinsic. The top-vb variable, though no longer referenced by the symbol-value place, because existing compiled code depends on it. * stdlib/place.tl (symbol-value): Rewrite the place logic to use symbol-value to access the variable, and set-symbol-value to update it, instead of referencing sys:top-vb. (sys:get-vb): This function has to stay, because it provides run-time support for code compiled with the buggy version of the place. * tests/019/symbol-value.tl: New file.
* fix crash if built-in variable is unbound.Kaz Kylheku2023-05-121-1/+3
| | | | | | | | | | | | | | | | | | | We use lookup_var_l in many places to look up the current dynamic value of a built-in variable such as *stdout*. Those places assume that a a valid location is returned which can be subject to a deref. If the application calls makunbound to remove such a variable, that deref will crash due to a null pointer dereference. Possible repro steps are numerous, possible for many variables. One example: (makunbound '*stdout*) (put-line) * eval.c (lookukp_var_l): If the binding is not found, do not return a nulloc, but throw an error exception.