From 47cd15b970892e2dae29e825fadaf31c6d84835d Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sat, 21 Apr 2018 18:30:13 -0700 Subject: compiler: tighter code for quasiliterals. Give the sys:fmt-simple function argument defaulting so the generated code doesn't have to call it with all five arguments present, four of them nil being much of the time. * eval.c (fmt_simple): Default all but the first four arguments. (eval_init): Re-register sys:fmt-simple as having only one required argument. * parser.c (read_file_common): Load version 1 or 2 files. We are bumping the object file version to 2 because now when we compile files, they won't work with older TXR in which all five arguments to sys:fmt-simple are required. * share/txr/stdlib/compiler.tl (expand-quasi-mods): Generate the sys:fmt-simple call with just enough arguments to express the modifiers that were decoded. (sexpand-quasi-args): Reduce the trivial modifier-less sys:fmt-simple calls to just one argument. (%tlo-ver%): Bump major version to 2. --- eval.c | 13 ++++++++----- parser.c | 4 ++-- share/txr/stdlib/compiler.tl | 13 +++++++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/eval.c b/eval.c index 72f98210..0b374d50 100644 --- a/eval.c +++ b/eval.c @@ -2692,12 +2692,15 @@ val format_field(val obj, val modifier, val filter, val eval_fun) return do_format_field(obj, n, sep, range_ix, plist, filter); } -static val fmt_simple(val obj, val n_in, val sep_in, +static val fmt_simple(val obj, val n, val sep, val range_ix, val plist) { - val n = if3(null(n_in), zero, n_in); - val sep = if3(null(sep_in), lit(" "), sep_in); - return do_format_field(fmt_tostring(obj), n, sep, range_ix, plist, nil); + return do_format_field(fmt_tostring(obj), + default_arg(n, zero), + default_arg(sep, lit(" ")), + default_null_arg(range_ix), + default_null_arg(plist), + nil); } static val fmt_flex(val obj, val plist, struct args *args) @@ -6532,7 +6535,7 @@ void eval_init(void) reg_fun(intern(lit("tprint"), user_package), func_n2o(tprint, 1)); reg_fun(intern(lit("display-width"), user_package), func_n1(display_width)); - reg_fun(intern(lit("fmt-simple"), system_package), func_n5(fmt_simple)); + reg_fun(intern(lit("fmt-simple"), system_package), func_n5o(fmt_simple, 1)); reg_fun(intern(lit("fmt-flex"), system_package), func_n2v(fmt_flex)); reg_fun(intern(lit("fmt-join"), system_package), func_n0v(fmt_join)); diff --git a/parser.c b/parser.c index 14bad41e..1649fc05 100644 --- a/parser.c +++ b/parser.c @@ -629,9 +629,9 @@ static val read_file_common(val stream, val error_stream, val compiled) if (compiled && first) { val major = car(form); - if (neq(major, one)) + if (lt(major, one) || gt(major, two)) uw_throwf(error_s, - lit("cannot load ~s; version number mismatch"), + lit("cannot load ~s: version number mismatch"), stream, nao); big_endian = caddr(form); first = nil; diff --git a/share/txr/stdlib/compiler.tl b/share/txr/stdlib/compiler.tl index 5ae8fb93..087e9848 100644 --- a/share/txr/stdlib/compiler.tl +++ b/share/txr/stdlib/compiler.tl @@ -1143,7 +1143,12 @@ ^(rcons ,rng-ix nil) rng-ix) (nreverse flex)))) - ^(sys:fmt-simple ,obj ,num ,sep, rng-ix ',plist))))))) + (cond + (plist ^(sys:fmt-simple ,obj ,num ,sep, rng-ix ',plist)) + (rng-ix ^(sys:fmt-simple ,obj ,num ,sep, rng-ix)) + (sep ^(sys:fmt-simple ,obj ,num ,sep)) + (num ^(sys:fmt-simple ,obj ,num)) + (t ^(sys:fmt-simple ,obj ,num))))))))) (defun expand-quasi-args (form) (append-each ((el (cdr form))) @@ -1153,9 +1158,9 @@ (sys:var (mac-param-bind form (sym exp : mods) el (list (expand-quasi-mods exp mods)))) (sys:quasi (expand-quasi-mods el)) - (t (list ^(sys:fmt-simple ,el nil nil nil nil))))) + (t (list ^(sys:fmt-simple ,el))))) ((bindable el) - (list ^(sys:fmt-simple ,el nil nil nil nil))) + (list ^(sys:fmt-simple ,el))) (t (list el))))) @@ -1406,7 +1411,7 @@ (defvarl %big-endian% (equal (ffi-put 1 (ffi uint32)) #b'00000001')) -(defvarl %tlo-ver% ^(1 0 ,%big-endian%)) +(defvarl %tlo-ver% ^(2 0 ,%big-endian%)) (defun open-compile-streams (in-path out-path) (let* ((rsuff (r$ %file-suff-rx% in-path)) -- cgit v1.2.3