summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2012-01-26 12:26:16 -0800
committerKaz Kylheku <kaz@kylheku.com>2012-01-26 12:26:16 -0800
commitb58791b726228b97297cc4512476f6016b9346a2 (patch)
tree2ed1b157bb29f6f90c97565adac4b2d54c2e1912 /lib.c
parentb7aae46f879bfc8a43781a08aaef9f506f7211bf (diff)
downloadtxr-b58791b726228b97297cc4512476f6016b9346a2.tar.gz
txr-b58791b726228b97297cc4512476f6016b9346a2.tar.bz2
txr-b58791b726228b97297cc4512476f6016b9346a2.zip
* eval.c (dwim_loc, dwim_op): Eliminated redundant re-evaluation
of range arguments. They are already evaluated since the cons expression is evaluates as part of the dwim arglist. Replaced some open code with function calls to the new listref and listref_l functions. (tostring, tostringp): made extern and moved to lib.c. * lib.c (listref, listref_l): New functions. (tostring, tostringp): moved here from eval.c. * lib.h (listref, listref_l, tostring, tostringp): Declared. * match.c (format_field): Handle index and range references. * txr.1: Documented new output variable syntax.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 78e77f57..e71b87cd 100644
--- a/lib.c
+++ b/lib.c
@@ -307,6 +307,29 @@ val sixth(val cons)
return car(cdr(cdr(cdr(cdr(cdr(cons))))));
}
+val listref(val list, val ind)
+{
+ if (lt(ind, zero))
+ return nil;
+ for (; gt(ind, zero); ind = minus(ind, one))
+ list = cdr(list);
+ return car(list);
+}
+
+val *listref_l(val list, val ind)
+{
+ val olist = list;
+ val oind = ind;
+
+ for (; gt(ind, zero) && list; ind = minus(ind, one))
+ list = cdr(list);
+ if (consp(list))
+ return car_l(list);
+
+ uw_throwf(error_s, lit("~s has no assignable location at ~s"),
+ olist, oind, nao);
+}
+
val *tail(val cons)
{
while (cdr(cons))
@@ -3793,6 +3816,20 @@ val obj_pprint(val obj, val out)
return obj;
}
+val tostring(val obj)
+{
+ val ss = make_string_output_stream();
+ obj_print(obj, ss);
+ return get_string_from_stream(ss);
+}
+
+val tostringp(val obj)
+{
+ val ss = make_string_output_stream();
+ obj_pprint(obj, ss);
+ return get_string_from_stream(ss);
+}
+
void init(const wchar_t *pn, mem_t *(*oom)(mem_t *, size_t),
val *stack_bottom)
{