summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-05-30 21:41:03 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-05-30 21:41:03 -0700
commit6846740ce44659a1f31e01054b0f1bcc8dc04c7a (patch)
tree14912dd2d0ecd0702ca9d67d0fdd965d24a75478
parentd87f9b4b380df188a17b6c1b04c4d24d7d79f98e (diff)
downloadtxr-6846740ce44659a1f31e01054b0f1bcc8dc04c7a.tar.gz
txr-6846740ce44659a1f31e01054b0f1bcc8dc04c7a.tar.bz2
txr-6846740ce44659a1f31e01054b0f1bcc8dc04c7a.zip
command line: -e takes multiple forms.
* parser.[ch] (read_objects_from_string): New function. * txr.c (help): Update description of -e, and don't describe -p as being like -e. (txr_main): Implement -e using read_objects_from_string, with progn consed to the front. Don't evaluate if it returns the error value colon_k. * txr.1: Documentation updated.
-rw-r--r--parser.c25
-rw-r--r--parser.h2
-rw-r--r--txr.122
-rw-r--r--txr.c20
4 files changed, 54 insertions, 15 deletions
diff --git a/parser.c b/parser.c
index c61b2875..730edb8a 100644
--- a/parser.c
+++ b/parser.c
@@ -891,6 +891,31 @@ val read_compiled_file(val self, val stream, val error_stream)
return read_file_common(self, stream, error_stream, t);
}
+val read_objects_from_string(val string, val error_stream,
+ val error_return_val, val name_in)
+{
+ val self = lit("read-objects-from-string");
+ val stream = make_string_byte_input_stream(string);
+ val name = default_arg(name_in, lit("string"));
+ val parser = ensure_parser(stream, name);
+ list_collect_decl (out, ptail);
+
+ for (;;) {
+ val form = lisp_parse_impl(self, prime_lisp, t, stream,
+ error_stream, unique_s, name, colon_k);
+
+ if (form == unique_s) {
+ if (parser_errors(parser) != zero)
+ return error_return_val;
+ break;
+ }
+
+ ptail = list_collect(ptail, form);
+ }
+
+ return out;
+}
+
val txr_parse(val source_in, val error_stream,
val error_return_val, val name_in)
{
diff --git a/parser.h b/parser.h
index 8e632c47..fe05b448 100644
--- a/parser.h
+++ b/parser.h
@@ -137,6 +137,8 @@ val get_json(val source_in, val error_stream, val error_return_val,
val name_in, val lineno);
val read_eval_stream(val self, val stream, val error_stream);
val read_compiled_file(val self, val stream, val error_stream);
+val read_objects_from_string(val string, val error_stream,
+ val error_return_val, val name_in);
val txr_parse(val source, val error_stream,
val error_return_val, val name_in);
val repl(val bindings, val in_stream, val out_stream, val env);
diff --git a/txr.1 b/txr.1
index af63b07d..5b35a049 100644
--- a/txr.1
+++ b/txr.1
@@ -815,9 +815,13 @@ no longer specifies the
.metn script-file .
It is an argument to the script, such as the name of an input source.
-.meIP -e < expression
-Evaluates a \*(TL expression for its side effects, without printing
-its value. Can be specified more than once. The
+.meIP -e < expressions
+Evaluates zero or more \*(TL expressions for their side effects, without
+implicitly printing their values. Can be specified more than once.
+The argument may be empty, in which case the argument has no effect,
+since it calls for an empty sequence of forms to be evaluated.
+
+The
.meta script-file
argument becomes optional if at least one
.codn -e ,
@@ -825,7 +829,9 @@ argument becomes optional if at least one
.code -P
or
.code -t
-option is processed. If the evaluation of every
+option is processed.
+
+If the evaluation of every
.meta expression
evaluated this way terminates normally, and there is no
.meta script-file
@@ -835,11 +841,9 @@ instead of entering the interactive listener. The
option can be used to request the listener.
.meIP -p < expression
-Just like
-.code -e
-but prints the value of
-.meta expression
-using the
+The argument must specify exactly one valid \*(TL form.
+If this is successfully parsed and evaluated,
+the value of the expression is printed as if using the
.code prinl
function.
diff --git a/txr.c b/txr.c
index 90036983..9515f937 100644
--- a/txr.c
+++ b/txr.c
@@ -143,9 +143,10 @@ static void help(void)
" instead of as the script-file argument.\n"
" This allows #! scripts to pass options through\n"
" to the txr utility.\n"
-"-e expression Evaluate TXR Lisp expression. Can be specified\n"
-" multiple times. The script-file arg becomes optional.\n"
-"-p expression Like -e, but prints the result of the expression\n"
+"-e expressions Evaluate zero or more TXR Lisp expressions.\n"
+" Can be specified multiple times. The script-file\n"
+" arg becomes optional.\n"
+"-p expression Evaluate a single expression, and print the value\n"
" using the prinl function.\n"
"-P expression Like -p, but prints using pprinl.\n"
"-t expression Like -p, but prints using tprint.\n"
@@ -1054,10 +1055,17 @@ int txr_main(int argc, char **argv)
reg_varl(self_path_s, lit("cmdline-expr"));
reg_var(args_s, or2(orig_args, arg_list));
- eval_intrinsic(lisp_parse(arg, std_error, colon_k,
- lit("cmdline-expr"), colon_k),
- make_env(bindings, nil, nil));
+ {
+ val forms = read_objects_from_string(arg, std_error, colon_k,
+ lit("cmdline-expr"));
+
+ if (forms != colon_k)
+ eval_intrinsic(cons(progn_s, forms),
+ make_env(bindings, nil, nil));
+ }
+
evaled = t;
+
args_new = cdr(lookup_global_var(args_s));
if (args_new != args_saved) {