Merge: niti: teach the interpreter to read from the standard input
authorJean Privat <jean@pryen.org>
Fri, 3 Jun 2016 17:17:00 +0000 (13:17 -0400)
committerJean Privat <jean@pryen.org>
Fri, 3 Jun 2016 17:17:00 +0000 (13:17 -0400)
With `-` the program is read from stdin.

~~~
$ echo H4sIAN7lUFcAAysoyswrUVDySM3JyVfiAgCjfiaWDgAAAA== | base64 -d | gunzip | nit -
Hello
~~~

Pull-Request: #2159
Reviewed-by: Alexis Laferrière <alexis.laf@xymus.net>

share/man/nit.md
src/nit.nit
tests/nit.args
tests/nit_args8.inputs [new file with mode: 0644]
tests/niti.skip
tests/nitvm.skip
tests/sav/nit_args8.res [new file with mode: 0644]

index 9503883..fcdec6c 100644 (file)
@@ -6,6 +6,8 @@ nit - interprets and debugs Nit programs.
 
 nit [*options*] FILE [ARG]...
 
+nit [*options*] - [ARG]...
+
 nit [*options*] -e COMMAND [ARG]...
 
 # DESCRIPTION
@@ -16,6 +18,9 @@ It takes the main module of a program as the first argument then the options and
     $ nit examples/hello_world.nit
     hello world
 
+If `-` is used instead of a module, then the program is read from the standard input.
+The whole program is read before its interpretation starts.
+
 The Nit interpreter is usable and valid as a *shebang* interpreted directive.
 It is however recommended to use with `/usr/bin/env` because the location of the executable is not standardized.
 
index 9410b4d..a28bb6f 100644 (file)
@@ -64,6 +64,12 @@ if opt_eval.value then
        modelbuilder.load_rt_module(parent, amodule, "-")
 
        mmodules = [amodule.mmodule.as(not null)]
+else if progname == "-" then
+       var content = stdin.read_all
+       var amodule = toolcontext.parse_module(content)
+       toolcontext.check_errors
+       modelbuilder.load_rt_module(null, amodule, "-")
+       mmodules = [amodule.mmodule.as(not null)]
 else
        mmodules = modelbuilder.parse([progname])
 end
index cbaafd9..ba1a276 100644 (file)
@@ -5,3 +5,4 @@ base_simple3.nit
 -e 'print "hello world"'
 -n -e 'print line' test_prog/README.md test_prog/test_prog.nit
 test_ffi_c_interpreter.nit
+-
diff --git a/tests/nit_args8.inputs b/tests/nit_args8.inputs
new file mode 100644 (file)
index 0000000..c878eeb
--- /dev/null
@@ -0,0 +1 @@
+print "Hello!"
index 4634f40..d93ce27 100644 (file)
@@ -4,6 +4,7 @@ nit_args3
 nit_args4
 nit_args5
 nit_args6
+nit_args8
 nitvm_args1
 nitvm_args3
 nitc_args1
index a68b4cc..b8ae2ec 100644 (file)
@@ -4,6 +4,7 @@ nit_args3
 nit_args4
 nit_args5
 nit_args6
+nit_args8
 nitvm_args1
 nitvm_args3
 nitc_args1
diff --git a/tests/sav/nit_args8.res b/tests/sav/nit_args8.res
new file mode 100644 (file)
index 0000000..10ddd6d
--- /dev/null
@@ -0,0 +1 @@
+Hello!