Used to factorize some code used by the engines.
Serializable::inspect
to show more useful information
more_collections :: more_collections
Highly specific, but useful, collections-related classes.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatsnitc :: toolcontext
Common command-line tool infrastructure than handle options and error messagescore :: union_find
union–find algorithm using an efficient disjoint-set data structurenitc :: modelbuilder
nitc :: light_only
Compiler support for the light FFI only, detects unsupported usage of callbacksnitc
.
nitc :: separate_erasure_compiler
Separate compilation of a Nit program with generic type erasureclone
method of the astbuilder tool
# 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("Additional module to mix-in", "-m", "--mixin")
# --define
var opt_defines = new OptionArray("Define a specific property", "-D", "--define")
redef init
do
super
option_context.add_option(opt_mixins, opt_defines)
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
var defines = opt_defines.value
if not defines.is_empty then
var location = mainmodule.location
var model = mainmodule.model
# Create a fictive module if needed
if mainmodule == mmodules.first then
mainmodule = new MModule(model, null, mainmodule.name + "-d", location)
mainmodule.set_imported_mmodules(mmodules)
mainmodule.is_fictive = true
mainmodule.first_real_mmodule = mmodules.first
end
var recv = mainmodule.sys_type
assert recv != null
var mclassdef = new MClassDef(mainmodule, recv, location)
mclassdef.add_in_hierarchy
for define in defines do
var spl = define.split_once_on('=')
var name = spl.first
var val = null
if spl.length > 1 then val = spl[1]
var prop = mainmodule.try_get_primitive_method(name, recv.mclass)
if prop == null then
error(null, "Error: --define: no top-level function `{name}`.")
continue
end
var ret = prop.intro.msignature.return_mtype
var v
if ret == null then
error(null, "Error: --define: method `{prop}` is not a function.")
continue
else if ret.to_s == "Bool" then
if val == null or val == "true" then
v = true
else if val == "false" then
v = false
else
error(null, "Error: --define: method `{prop}` needs a Bool.")
continue
end
else if ret.to_s == "Int" then
if val != null and val.is_numeric then
v = val.to_i
else
error(null, "Error: --define: method `{prop}` needs a Int.")
continue
end
else if ret.to_s == "String" then
if val != null then
v = val
else
error(null, "Error: --define: method `{prop}` needs a String.")
continue
end
else
error(null, "Error: --define: method `{prop}` returns an unsupported type `{ret}`.")
continue
end
var pd = new MMethodDef(mclassdef, prop, location)
pd.msignature = prop.intro.msignature
pd.constant_value = v
end
check_errors
end
return mainmodule
end
end
src/mixin.nit:15,1--113,3