src: new module mixin to factorize the -m option
authorJean Privat <jean@pryen.org>
Fri, 10 Oct 2014 02:07:03 +0000 (22:07 -0400)
committerJean Privat <jean@pryen.org>
Fri, 10 Oct 2014 22:55:09 +0000 (18:55 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/mixin.nit [new file with mode: 0644]
src/nit.nit

index 40f645c..082d987 100644 (file)
@@ -22,6 +22,7 @@ import semantize
 import platform
 import c_tools
 private import annotation
+import mixin
 
 # Add compiling options
 redef class ToolContext
@@ -3042,9 +3043,6 @@ end
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
 
-var opt_mixins = new OptionArray("Additionals module to min-in", "-m")
-toolcontext.option_context.add_option(opt_mixins)
-
 toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
 
 # We do not add other options, so process them now!
@@ -3063,7 +3061,6 @@ end
 
 # Here we load an process all modules passed on the command line
 var mmodules = modelbuilder.parse(arguments)
-var mixins = modelbuilder.parse(opt_mixins.value)
 
 if mmodules.is_empty then return
 modelbuilder.run_phases
@@ -3071,8 +3068,5 @@ modelbuilder.run_phases
 for mmodule in mmodules do
        toolcontext.info("*** PROCESS {mmodule} ***", 1)
        var ms = [mmodule]
-       if not mixins.is_empty then
-               ms.add_all mixins
-       end
        toolcontext.run_global_phases(ms)
 end
index 4b33914..917606a 100644 (file)
@@ -20,6 +20,7 @@ module naive_interpreter
 import literal
 import semantize
 private import parser::tables
+import mixin
 
 redef class ToolContext
        # --discover-call-trace
diff --git a/src/mixin.nit b/src/mixin.nit
new file mode 100644 (file)
index 0000000..bdbadf0
--- /dev/null
@@ -0,0 +1,44 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Loading and additional module refinements at link-time.
+#
+# Used to factorize some code used by the engines.
+module mixin
+
+import modelbuilder
+
+redef class ToolContext
+       # --mixin
+       var opt_mixins = new OptionArray("Additionals module to min-in", "-m", "--mixin")
+
+       redef init
+       do
+               super
+               option_context.add_option(opt_mixins)
+       end
+
+       redef fun make_main_module(mmodules)
+       do
+               var mixins = opt_mixins.value
+               if not mixins.is_empty then
+                       mmodules.add_all modelbuilder.parse(opt_mixins.value)
+                       modelbuilder.run_phases
+               end
+
+               var mainmodule = super
+
+               return mainmodule
+       end
+end
index 24bb00e..9a95194 100644 (file)
@@ -27,10 +27,9 @@ toolcontext.tooldescription = "Usage: nit [OPTION]... <file.nit>...\nInterprets
 # Add an option "-o" to enable compatibilit with the tests.sh script
 var opt = new OptionString("compatibility (does noting)", "-o")
 toolcontext.option_context.add_option(opt)
-var opt_mixins = new OptionArray("Additionals module to min-in", "-m")
 var opt_eval = new OptionBool("Specifies the program from command-line", "-e")
 var opt_loop = new OptionBool("Repeatedly run the program for each line in file-name arguments", "-n")
-toolcontext.option_context.add_option(opt_mixins, opt_eval, opt_loop)
+toolcontext.option_context.add_option(opt_eval, opt_loop)
 # We do not add other options, so process them now!
 toolcontext.process_options(args)
 
@@ -66,20 +65,11 @@ else
        mmodules = modelbuilder.parse([progname])
 end
 
-mmodules.add_all modelbuilder.parse(opt_mixins.value)
 modelbuilder.run_phases
 
 if toolcontext.opt_only_metamodel.value then exit(0)
 
-var mainmodule: nullable MModule
-
-# Here we launch the interpreter on the main module
-if mmodules.length == 1 then
-       mainmodule = mmodules.first
-else
-       mainmodule = new MModule(model, null, mmodules.first.name, mmodules.first.location)
-       mainmodule.set_imported_mmodules(mmodules)
-end
+var mainmodule = toolcontext.make_main_module(mmodules)
 
 var self_mm = mainmodule
 var self_args = arguments