summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--lib.c57
-rw-r--r--lib.h3
-rw-r--r--match.c9
-rw-r--r--txr.12
-rw-r--r--txr.vim1
6 files changed, 76 insertions, 8 deletions
diff --git a/ChangeLog b/ChangeLog
index c8ebca7a..0e4d5c19 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2012-02-19 Kaz Kylheku <kaz@kylheku.com>
+
+ * lib.c (sub, ref, replace): New functions.
+
+ * lib.h (sub, ref, replace): Declared.
+
+ * match.c (format_field): Generic indexing using new functions.
+
+ * txr.1: Documentation stub.
+
+ * txr.vim: Highlighting for new functions.
+
2012-02-18 Kaz Kylheku <kaz@kylheku.com>
* match.c (extract_vars): If a brace var is actually an
diff --git a/lib.c b/lib.c
index 94cb19d3..fe433d3a 100644
--- a/lib.c
+++ b/lib.c
@@ -3570,7 +3570,62 @@ val length(val seq)
case VEC:
return length_vec(seq);
default:
- type_mismatch(lit("~s is not a sequence"), cons, nao);
+ type_mismatch(lit("length: ~s is not a sequence"), cons, nao);
+ }
+}
+
+val sub(val seq, val from, val to)
+{
+ if (seq == nil)
+ return nil;
+ else switch (type(seq)) {
+ case CONS:
+ case LCONS:
+ return sub_list(seq, from, to);
+ case LIT:
+ case STR:
+ return sub_str(seq, from, to);
+ case VEC:
+ return sub_vec(seq, from, to);
+ default:
+ type_mismatch(lit("sub: ~s is not a sequence"), cons, nao);
+ }
+}
+
+val ref(val seq, val ind)
+{
+ if (seq == nil)
+ return nil;
+ else switch (type(seq)) {
+ case CONS:
+ case LCONS:
+ return listref(seq, ind);
+ case LIT:
+ case STR:
+ return chr_str(seq, ind);
+ case VEC:
+ return vecref(seq, ind);
+ default:
+ type_mismatch(lit("ref: ~s is not a sequence"), cons, nao);
+ }
+}
+
+val replace(val seq, val from, val to, val items)
+{
+ if (seq == nil)
+ goto list;
+ switch (type(seq)) {
+ case CONS:
+ case LCONS:
+ list:
+ return replace_list(seq, from, to, items);
+ case LIT:
+ case STR:
+ return replace_str(seq, from, to, items);
+ case VEC:
+ return replace_vec(seq, from, to, items);
+ default:
+ type_mismatch(lit("replace: ~s is not a sequence"), cons, nao);
}
}
diff --git a/lib.h b/lib.h
index 3b5d4630..e75c5acc 100644
--- a/lib.h
+++ b/lib.h
@@ -557,6 +557,9 @@ val sort(val list, val lessfun, val keyfun);
val find(val list, val key, val testfun, val keyfun);
val set_diff(val list1, val list2, val testfun, val keyfun);
val length(val seq);
+val sub(val seq, val from, val to);
+val ref(val seq, val ind);
+val replace(val seq, val from, val to, val items);
val env(void);
val obj_print(val obj, val stream);
val obj_pprint(val obj, val stream);
diff --git a/match.c b/match.c
index db21e044..3735b07a 100644
--- a/match.c
+++ b/match.c
@@ -1236,16 +1236,11 @@ val format_field(val obj, val modifier, val filter, val eval_fun)
val from = funcall1(eval_fun, second(arg_expr));
val to = funcall1(eval_fun, third(arg_expr));
- obj = if3((vectorp(obj)),
- sub_vec(obj, from, to),
- sub_list(obj, from, to));
+ obj = sub(obj, from, to);
} else {
val arg = funcall1(eval_fun, arg_expr);
if (bignump(arg) || fixnump(arg)) {
- if (vectorp(obj))
- obj = vecref(obj, arg);
- else
- obj = listref(obj, arg);
+ obj = ref(obj, arg);
} else {
uw_throwf(query_error_s, lit("format_field: bad index: ~s"),
arg, nao);
diff --git a/txr.1 b/txr.1
index 98a1387a..d4e2a1e6 100644
--- a/txr.1
+++ b/txr.1
@@ -6617,6 +6617,8 @@ Certain object types have a custom equal function.
.SS Function length
+.SS Functions sub, ref and replace
+
.SS Function symbol-function
.SS Function func-get-form
diff --git a/txr.vim b/txr.vim
index 6f202d14..7ba1c452 100644
--- a/txr.vim
+++ b/txr.vim
@@ -74,6 +74,7 @@ syn keyword txl_keyword contained list-vector copy-vec sub-vec cat-vec
syn keyword txl_keyword contained replace-vec assoc assq acons acons-new
syn keyword txl_keyword contained aconsq-new alist-remove alist-nremove copy-cons
syn keyword txl_keyword contained copy-alist merge sort find set-diff length
+syn keyword txl_keyword contained sub ref replace
syn keyword txl_keyword contained symbol-function func-get-form func-get-env
syn keyword txl_keyword contained functionp interp-fun-p *random-state*