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)
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>

1  2 
src/modelbuilder.nit

@@@ -587,12 -586,13 +587,15 @@@ class ModelBuilde
                return nmodule
        end
  
 +      # Injection of a new module without source.
 +      # Used by the interpreted
-       fun load_rt_module(parent: MModule, nmodule: AModule, mod_name: String): nullable AModule
+       fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
        do
                # Create the module
-               var mmodule = new MModule(model, parent.mgroup, mod_name, nmodule.location)
+               var mgroup = null
+               if parent != null then mgroup = parent.mgroup
+               var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
                nmodule.mmodule = mmodule
                nmodules.add(nmodule)
                self.mmodule2nmodule[mmodule] = nmodule