If you do not want to submit code for some reason, you are always welcome to ask questions about the language or related topics via the issue system.
You may also proof-read and correct documentation if you are willing!
-All the documents on this repository are written in English, however most of our contributors are not native anglophones.
+All the documents in this repository are written in English. However, most of our contributors are not native anglophones.
Therefore we encourage people, especially those coming of an english-speaking culture to read the documentation available and submit patches to correct bad formulations, typos or related mistakes on our part through the usual system.
* lib/ Nit standard library
* LICENCE License of the software
* Makefile Bootstrap the Nit tools
- * misc/ Some additional files for commons text editors and tools
+ * misc/ Some additional files for common text editors and tools
* NOTICE.md List of the authors
* README This file
* share/ Common resources used by tools
Literal strings are enclosed within quotes (`"`).
To insert a value
inside a literal string, include the values inside braces (`{}`).
-Braces has to be escaped.
-`+` is the concatenation operator but is less efficient than the brace form.
+Braces have to be escaped.
+`+` is the concatenation operator, but is less efficient than the brace form.
~~~
var j = 5
~~~
Multi-line strings are enclosed with triple quotes (`"""`).
-Values are inserted with a triple braces (`{{{value}}}`).
+Values are inserted with triple braces (`{{{value}}}`).
The multi-line form thus allows verbatim new-lines, quotes and braces
~~~
All objects have a `to_s` method that converts the object to a String.
`print` is a top-level method that takes any number of arguments and
-prints to the standard output. `print` always add a newline, another
-top-level method, `printn`, does not add the newline.
+prints them to the standard output. `print` always adds a newline to the output, another
+top-level method, `printn`, does not add a newline.
~~~
var x: String
`<`, `>`, `<=`, `>=` and `<=>` on `Comparable` objects (which include
`Int`, `String` and others).
-- `==`, `<`, `>`, `<=`, `>=` and `<=>` are standard Nit operators (so they are redefinable).
+- `==`, `<`, `>`, `<=`, `>=` and `<=>` are standard Nit operators thus are redefinable.
- `and`, `or` and `not` are not standard Nit operators: they are not
redefinable, also they are lazy and have adaptive typing flow
- `==` is not for reference equality but for value equality (like
`equals` in Java). There is a special reference equality operator,
`is`, but it cannot be redefined and its usage is not recommended.
- Note also that while `==` is redefinable, it has a special adaptive
+ Note that while `==` is redefinable, it has a special adaptive
typing flow effect when used with `null`.
- `!=` is not a standard Nit operator. In fact `x != y` is
- enums (e.g. `Int` or `Bool`) can only specialize interfaces, cannot have attributes, cannot have constructors, have proper instances but they are not instantiated by the programmer—it means no `new Int`. Note that at this point there is no user-defined enums.
-All kinds of classes must have a name, can have some superclasses and can have some definitions of properties. Properties are methods, attributes, constructors and virtual types. All kinds of classes can also be generic. When we talk about “classes” in general, it means all these four kinds. We say “concrete classes” to designate only the classes declared with the `class` keyword alone.
+All kinds of classes must have a name, can have some superclasses and can have some definitions of properties. Properties are methods, attributes, constructors and virtual types. All kinds of classes can also be generic. When documentation refers to “classes” , it generally refers to all four kinds. The term “concrete classes” is used to designate the classes declared with the `class` keyword alone.
## Class Specialization
- adding new superclasses.
-Note that the kind or the visibility of a class cannot be changed by a refinement. Therefore, it is allowed to just write `redef class X` whatever is the kind or the visibility of `X`.
+Note that the kind or the visibility of a class cannot be changed by a refinement. Therefore, it is allowed to just write `redef class X` regardless of the kind or the visibility of `X`.
In programs, the real instantiated classes are always the combination of all their refinements.
Classes in OO models are often a simple aggregates of attributes and methods.
-By default, the `new` construction require a value for each attribute defined in a class without a default value.
+By default, the `new` construction requires a value for each attribute defined in a class without a default value.
~~~
class Product
## Uncollected attributes
-There is three cases for an attributes to not be collected in the `new`.
+There are three cases for which an attribute is not collected in a `new` construction.
* Attributes with a default value
* Attributes with the annotation `noinit`
## Generalized initializers
-Initializers are methods that are automatically invoked by the new.
-In fact, by default, the setter of an attribute is used as a initializer.
+Initializers are methods that are automatically invoked by `new`.
+In fact, by default, the setter of an attribute is used as an initializer.
`autoinit` is used to register a method as a setter.
assert fp.z == 13
~~~
-Generalized setters are a powerful tool but often needed in only rare specific cases.
-In most case, there is no reason that an argument of a `new` construction is not stored in the object as a real attribute.
+Generalized setters are a powerful tool, but only needed in rare specific cases.
+In most cases, there is no reason for an argument of a `new` construction to not be stored in the object as a real attribute.
## Inheritance
## `new` factories
-`new` factories permit to completely shortcut the class instantiation mechanim.
+`new` factories allow to completely shortcut the class instantiation mechanism.
It could be used to provide `new` syntax on non-concrete class (mainly `extern class`).
`new` factories behave like a top-level function that return the result of the construction.
`super` calls the “previous” definition of the method. It is used in a redefinition of a method in a subclass or in a refinement, It can be used with or without arguments; in the latter case, the original arguments are implicitly used.
-The `super` of Nit behave more like the `call-next-method` of CLOS that the `super` of Java or Smalltalk. It permits the traversal of complex class hierarchies and refinement. Basically, `super` is polymorphic: the method called by `super` is not only determined by the class of
-definition of the method but also by the dynamic type of `self`.
+The `super` of Nit behaves more like the `call-next-method` of CLOS than the `super` of Java or Smalltalk. It permits the traversal of complex class hierarchies and refinement. Basically, `super` is polymorphic: the method called by `super` is not only determined by the class of
+definition of the method, but also by the dynamic type of `self`.
-The principle it to produce a strict order of the redefinitions of a method (the linearization). Each call to `super` call the next method definition in the linearization. From a technical point of view, the linearization algorithm used is based on C3. It ensures that:
+The principle is to produce a strict order of the redefinitions of a method (the linearization). Each call to `super` call the next method definition in the linearization. From a technical point of view, the linearization algorithm used is based on C3. It ensures that:
- A definition comes after its redefinition.
- bracket operator: `[]`. Its definition requires one parameter or more and a return value. Its invocation is done with `x[y, z]` where `x` is the receiver, `y` the first argument and `z` the second argument.
-- setters: `something=` where `something` can be any valid method identifier. Their definitions require one parameter or more and no return value. If there is only one parameter, the invocation is done with `x.something = y` where `x` is the receiver and y the argument. If there is more that one parameter, the invocation is done with `x.something(y, z) = t` where `x` is the receiver, `y` the first argument, `z` the second argument and `t` the last argument.
+- setters: `something=` where `something` can be any valid method identifier. Their definitions require one parameter or more and no return value. If there is only one parameter, the invocation is done with `x.something = y` where `x` is the receiver and `y` the argument. If there is more that one parameter, the invocation is done with `x.something(y, z) = t` where `x` is the receiver, `y` the first argument, `z` the second argument and `t` the last argument.
- bracket setter: `[]=`. Its definition requires two parameters or more and no return value. Its invocation is done with `x[y, z] = t` where `x` is the receiver, `y` the first argument, `z` the second argument and `t` the last argument.
# Modules
-`module` declares the name of a module. While optional it is recommended to use it, at least for documentation purpose. The basename of the source file must match the name declared with `module`. The extension of the source file must be `nit`.
+`module` declares the name of a module. While optional, it is recommended to use it, at least for documentation purposes. The basename of the source file must match the name declared with `module`. The extension of the source file must be `nit`.
A module is made of, in order:
- `private import` indicates a private importation. Importers of a given module will not automatically import its privately imported modules. An analogy is using `#include` in a body file (`.c`) in C/C++.
-- `intrude import` indicates an intrusive importation. `intrude` `import` bypasses the `private` visibility and gives to the importer module a full access on the imported module. Such an import may only be considered when modules are strongly bounded and developed together. The closest, but insufficient, analogy is something like including a body file in a body file in C/C++.
+- `intrude import` indicates an intrusive importation. `intrude` `import` bypasses the `private` visibility and gives to the importer module full access on the imported module. Such an import may only be considered when modules are strongly bounded and developed together. The closest, but insufficient, analogy is something like including a body file in a body file in C/C++.
## Visibility
\noindent\textbf{A Concise Reference of the Nit Language}
-This document attempts to be as short as possible while covering all features of the language in deepth.
+This document attempts to be as short as possible while covering all features of the language in depth.
It is not a real manual to learn the language since concepts are covered when required.
%Forward and backward references about concepts are written like this~\goto{redef} which means Section~\ref*{redef}.
%An index\goto{index} also lists concepts and keywords for an improved navigation.
- Adaptive typing.
-Some structures alter the control flow but are not described in this
+Some structures alter the control flow, but are not described in this
section: `and`, `or`, `not`, `or else` and `return`.
Note that the control flow is determined only from the position, the
end
~~~
-Note that `loop` is different from `while true` because the control flow does not consider the values of expression.
+Note that `loop` is different from `while true` because the control flow does not consider the values of expressions.
## do
-Single `do` are used to create scope for variables or to be attached with labeled breaks.
+Single `do` are used to create scoped variables or to be attached with labeled breaks.
~~~
do
# Local Variables and Static Typing
-`var` declares local variables. In fact there is no global variable in Nit, so in this document *variable* always refers to a local variable. A variable is visible up to the end of the current
+`var` declares local variables. In fact, there is no global variable in Nit, so in this document *variable* always refers to a local variable. A variable is visible up to the end of the current
control structure. Two variables with the same name cannot coexist: no nesting nor masking.
Variables are bound to values. A variable cannot be used unless it has a value in all control flow paths (à la Java).
## Variable Upper Bound
-An optional type information can be added to a variable declaration. This type is used as an upper bound of the type of the variable. When a initial value is given in a variable declaration without a specific type information, the static type of the initial value is used as an upper bound. If no type and no initial value are given, the upper bound is set to `nullable Object`.
+An optional type information can be added to a variable declaration. This type is used as an upper bound of the type of the variable. When an initial value is given in a variable declaration without a specific type information, the static type of the initial value is used as an upper bound. If no type and no initial value are given, the upper bound is set to `nullable Object`.
~~~nitish
var e: Int # Upper bound is Int
print max # outputs 11
~~~
-Note that type adaptation occurs only in an `isa` if the target type is more specific that the current type.
+Note that type adaptation occurs only in an `isa` if the target type is more specific than the current type.
~~~
var col: Collection[Int] = [1, 2, 3]
import counter
import pkgconfig
private import explain_assert_api
+import contracts
# Add compiling options
redef class ToolContext
# option --global
var opt_global = new OptionBool("Use global compilation", "--global")
- var global_compiler_phase = new GlobalCompilerPhase(self, null)
+ var global_compiler_phase = new GlobalCompilerPhase(self, [contracts_phase])
redef init do
super
end
end
- var separate_compiler_phase = new SeparateCompilerPhase(self, null)
+ var separate_compiler_phase = new SeparateCompilerPhase(self, [contracts_phase])
end
class SeparateCompilerPhase
end
end
- var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
+ var erasure_compiler_phase = new ErasureCompilerPhase(self, [contracts_phase])
end
class ErasureCompilerPhase
private class ContractsPhase
super Phase
- # The entry point of the contract phase
- # In reality, the contract phase is executed on each module
- # FIXME: Actually all method is checked to add method if any contract is inherited
redef fun process_nmodule(nmodule)do
# Check if the contracts are disabled
if toolcontext.opt_no_contract.value then return
nmodule.do_contracts(self.toolcontext)
end
+
+ redef fun process_mainmodule(mainmodule: MModule, given_mmodules: SequenceRead[MModule]) do
+ # Visit all loaded modules `toolcontext.nmodules` to do contract weaving
+ for nmodule in toolcontext.modelbuilder.nmodules do
+ nmodule.do_weaving_contracts(self.toolcontext)
+ end
+ end
end
redef class AModule
- # Compile all contracts
- #
- # The implementation of the contracts is done in two visits.
- #
- # - First step, the visitor analyzes and constructs the contracts
- # for each class (invariant) and method (expect, ensure).
- #
- # - Second step the visitor analyzes each `ASendExpr` to see
- # if the callsite calls a method with a contract. If this is
- # the case the callsite is replaced by another callsite to the contract method.
+
+ # Entry point to generate the entire contract infrastructure.
+ # Once this method is called we must call the `do_weaving_contracts` method (see it for more information).
fun do_contracts(toolcontext: ToolContext) do
var ast_builder = new ASTBuilder(mmodule.as(not null))
#
var contract_visitor = new ContractsVisitor(toolcontext, toolcontext.modelbuilder.identified_modules.first, self, ast_builder)
contract_visitor.enter_visit(self)
- #
+ end
+
+ # Entry point to execute the weaving in order to redirect the calls to the contract sides if it's needed.
+ fun do_weaving_contracts(toolcontext: ToolContext)
+ do
+ var ast_builder = new ASTBuilder(mmodule.as(not null))
var callsite_visitor = new CallSiteVisitor(toolcontext, ast_builder)
callsite_visitor.enter_visit(self)
end
if not exist_contract_facet then
# If has no contract facet in intro just create it
if classdef != intro_mclassdef then
- create_facet(v, intro_mclassdef, contract_facet, self)
+ var n_intro_face = create_facet(v, intro_mclassdef, contract_facet, self)
+ n_intro_face.location = self.intro.location
+ n_intro_face.do_all(v.toolcontext)
end
n_contract_facet = create_facet(v, classdef, contract_facet, self)
else
var actual_callsite = callsite
if actual_callsite != null then
callsite = v.drive_callsite_to_contract(actual_callsite)
+ # Set the signature mapping with the old value, this avoids having to re-check the callsite.
+ callsite.signaturemap = actual_callsite.signaturemap
end
end
end
var actual_callsite = callsite
if actual_callsite != null then
callsite = v.drive_callsite_to_contract(actual_callsite)
+ # Set the signature mapping with the old value, this avoids having to re-check the callsite
+ callsite.signaturemap = actual_callsite.signaturemap
end
end
end
module mclassdef_collect
# We usualy need specific phases
-# NOTE: `frontend` is sufficent in most case (it is often too much)
+# NOTE: `frontend` is sufficent in most cases (it is often too much)
import frontend
import model_collect
module method_analyze_metrics
# We usualy need specific phases
-# NOTE: `frontend` is sufficent in most case (it is often too much)
+# NOTE: `frontend` is sufficent in most cases (it is often too much)
import nitsmell_toolcontext
import mclassdef_collect
end
modelbuilder.run_phases
+toolcontext.run_global_phases(modelbuilder.parsed_modules)
if toolcontext.opt_only_metamodel.value then toolcontext.quit
import test_phase
# We usualy need specific phases
-# NOTE: `frontend` is sufficient in most case (it is often too much)
+# NOTE: `frontend` is sufficient in most cases (it is often too much)
import frontend
# The body of the specific work.
--- /dev/null
+# 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.
+
+class A
+
+ var null_attribut: nullable Int
+
+ fun set_null_attribut(i: nullable Int)
+ is
+ ensure(null_attribut == i)
+ do
+ null_attribut = i
+ end
+end
+
+
+var a = new A
+a.set_null_attribut
+a.set_null_attribut(10)
+a.set_null_attribut
\e[0;33mtest_keep_going.nit:44,18--21\e[0m: Error: method `fail` does not exists in `Sys`.
var a = new Sys.\e[1;31mfail\e[0m
^
+Errors: 6. Warnings: 0.
1
2
3