From b58791b726228b97297cc4512476f6016b9346a2 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 26 Jan 2012 12:26:16 -0800 Subject: * 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. --- eval.c | 44 ++++++-------------------------------------- 1 file changed, 6 insertions(+), 38 deletions(-) (limited to 'eval.c') diff --git a/eval.c b/eval.c index 613359a4..8d83f37d 100644 --- a/eval.c +++ b/eval.c @@ -663,14 +663,11 @@ static val *dwim_loc(val form, val env, val op, val newval) val index = first(args); if (consp(index)) { - val from = eval(car(index), env, form); - val to = eval(cdr(index), env, form); - if (op != set_s) eval_error(form, lit("[~s ~s]: slice takes only simple assignments"), obj, index, nao); - replace_vec(obj, from, to, newval); + replace_vec(obj, car(index), cdr(index), newval); return 0; } else { return vecref_l(obj, first(args)); @@ -685,15 +682,8 @@ static val *dwim_loc(val form, val env, val op, val newval) val index = first(args); val cell = obj; if (bignump(index) || fixnump(index)) { - for (; gt(index, zero); index = minus(index, one)) - cell = cdr(cell); - if (lt(index, zero) || !cell) - eval_error(form, lit("[~s ~s]: cannot assign nonexistent location"), - cell, first(args), nao); - return car_l(cell); + return listref_l(obj, index); } else if (consp(index)) { - val from = eval(car(index), env, form); - val to = eval(cdr(index), env, form); val newlist; val tempform; @@ -701,7 +691,7 @@ static val *dwim_loc(val form, val env, val op, val newval) eval_error(form, lit("[~s ~s]: slice takes only simple assignments"), cell, index, nao); - newlist = replace_list(obj, from, to, newval); + newlist = replace_list(obj, car(index), cdr(index), newval); tempform = list(op, second(form), cons(quote_s, cons(newlist, nil)), nao); eval(tempform, env, form); @@ -947,9 +937,7 @@ static val op_dwim(val form, val env) val index = first(args); if (consp(index)) { - val from = eval(car(index), env, form); - val to = eval(cdr(index), env, form); - return sub_vec(obj, from, to); + return sub_vec(obj, car(index), cdr(index)); } else { return vecref(obj, first(args)); } @@ -966,15 +954,9 @@ static val op_dwim(val form, val env) obj, index, nao); if (consp(index)) { - val from = eval(car(index), env, form); - val to = eval(cdr(index), env, form); - return sub_list(obj, from, to); + return sub_list(obj, car(index), cdr(index)); } else { - if (lt(index, zero)) - return nil; - for (; gt(index, zero); index = minus(index, one)) - obj = cdr(obj); - return car(obj); + return listref(obj, first(args)); } } case COBJ: @@ -1500,20 +1482,6 @@ static val lazy_mappendv(val fun, val list_of_lists) return lazy_appendv(lazy_mapcarv(fun, list_of_lists)); } -static val tostring(val obj) -{ - val ss = make_string_output_stream(); - obj_print(obj, ss); - return get_string_from_stream(ss); -} - -static val tostringp(val obj) -{ - val ss = make_string_output_stream(); - obj_pprint(obj, ss); - return get_string_from_stream(ss); -} - static val symbol_function(val sym) { return lookup_fun(nil, sym); -- cgit v1.2.3