Merge: Niti loves the command line
authorJean Privat <jean@pryen.org>
Thu, 2 Oct 2014 00:13:18 +0000 (20:13 -0400)
committerJean Privat <jean@pryen.org>
Thu, 2 Oct 2014 00:13:18 +0000 (20:13 -0400)
commit0d4de25805565ad4c8d60fd89db03f09c1421cdd
tree20b9368c48c5d4d059bdec6360f061b5430fefac
parent3f5ee5069a2ebed7d05a3539107109854f3d588f
parentf5865550be77b5346adcea1a31c8d329f6b8a184
Merge: Niti loves the command line

Nit is both a good compiled language and a good interpreted language (except that the naive interpreter is slow).

Some people already use nit as a traditional script-language by including a shebang at the first line of their programs and set them executable (`chmod +x`).

~~~nit
print "Hello world"
~~~

This experimental PR make the nit interpreter more script-useful.

First, it introduces the `-e` option to run a program written on the command line.
Like with ruby, perl, bash and other script language.

~~~sh
$ nit -e 'print 5+5'
10
~~~

Second, and this is just wonderful, it adds the `-n` option (from ruby and perl) to automatically iterate over the lines of files given as arguments. The current line is named `sys.line` (instead of `$_` in perl and ruby).

It works for stdin if no argument

~~~sh
$ echo "hello world" | nit -n -e 'print sys.line.capitalized'
Hello World
~~~

or on the arguments (as files) if one or more is given (it is the perl and ruby semantic)

~~~sh
$ nit -n -e 'print sys.line.capitalized' README
Nit Is A Statically Typed Object-Oriented Programming Language.
The Goal Of Nit Is To Propose A Statically Typed Programming Language Where Structure Is Not A Pain.
[...]
~~~

The logic of the `-n` is written in a library loaded at runtime (`niti_runtime.nit`), so can be updated without having to recompile the interpreter.

Pull-Request: #799
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>
src/modelbuilder.nit