Merge: new option --define
authorJean Privat <jean@pryen.org>
Sat, 11 Oct 2014 12:24:15 +0000 (08:24 -0400)
committerJean Privat <jean@pryen.org>
Sat, 11 Oct 2014 12:24:15 +0000 (08:24 -0400)
It is standard in compiled programs to be able to setup some program configuration at compile-time.

Eg. C compilers accept a command-line option `-D` to define macros that will be used inside programs.
It is useful for configuring some string (like `-D PREFIX=/opt/nit/`, or `-D PORT=8081`) or activate some parts (conditional compilation, eg `-D WITH_SSL`).

This PR brings an equivalent capability to Nit engines through the new `-D` (`--define`) option.

The design behind the -D it to enable specific refinement of top-level functions at link-time.
Thus, basically

~~~sh
$ cat my_foo.nit
import foo
redef fun prefix do return "/opt/nit/"
$ nitg my_foo.nit
~~~

can be simplified into

~~~sh
$ nitg foo.nit -D prefix=/opt/nit/
~~~

Like `-m`, the `-D` creates a fictive module that refines the main module of the program.

`-D` also use the return type of the refined method to know how to interpret the text of the value.
Currently only Int, String and Bool is supported.

~~~nit
module foo
fun str: String do return "test"
fun num: Int do return 1
fun flag: Bool do return false
print str
print num
print flag
~~~

~~~sh
$ nitg foo.nit
$ ./foo
test
1
false
$ nitg foo.nit -D str=hello -D num=42 -D flag
$ ./foo
hello
42
true
~~~

The code of the PR is quite straightforward and show again that the new model is quite robust.

As usual, the first commits are some cleanup. The fun stuff is in the latter commits.

Pull-Request: #815
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

1  2 
src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/rapid_type_analysis.nit

Simple merge
Simple merge
Simple merge