tiger language manual: misc: wrapping, styling

* source/reference_manual/tiger_language_reference_manual/lexical_specifications/lexical_specifications.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/declarations.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/declarations/import,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/function_declarations/function_declarations.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/method_declarations/method_declarations.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/type_declarations/classes3,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/type_declarations/type_declarations.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/declarations/variable_declarations/variable_declarations.rst,
* source/reference_manual/tiger_language_reference_manual/semantics/expressions/expressions.rst: Here.
parent 0a2fc082
Pipeline #18122 passed with stage
in 1 minute and 22 seconds
......@@ -35,7 +35,7 @@ Strings
following escapes:
``\a``, ``\b``, ``\f``, ``\n``, ``\r``, ``\t``, ``\v``
control characters.
Control characters.
``\num``
The character which code is ``num`` in octal. Valid character codes belong
......
......@@ -4,13 +4,15 @@ Declarations
------------
import
An import clause denote the same expression where it was (recursively)
replaced by the set of declarations its corresponding import-file contains.
:code:`import`
An :code:`import` clause denote the same expression where it was
(recursively) replaced by the set of declarations its corresponding
import-file contains.
An import-file has the following syntax
(see :ref:`Syntactic Specifications` for the definition of the symbols):
.. literalinclude:: declarations/import
:language: ebnf
Because the syntax is different, it is convenient to use another extension.
We use ``*.tih`` for files to import, for instance:
......@@ -45,7 +47,7 @@ import
in the current directory, then in all the directories of the include path,
in order.
name spaces
Name spaces
There are three name spaces: types, variables and functions. The original
language definition features two: variables and functions share the same
name space. The motivation, as noted by Sébastien Carlier, is that in
......
......@@ -4,7 +4,7 @@ Function Declarations
=====================
functions
Functions
To declare a function, provide its return value type:
.. literalinclude:: function.tig
......@@ -15,24 +15,24 @@ functions
.. literalinclude:: procedure.tig
:language: tiger
Functions can be recursive, but mutually recursive functions must be in the
same sequence of function declarations (no other declaration should be
placed between them).
Functions can be recursive, but mutually recursive functions must be
in the same sequence of function declarations (no other declaration
should be placed between them).
See the semantics of function calls for the argument passing policy
(see :ref:`Expressions`).
primitive
A primitive is a built-in function, i.e. a function which body is provided
by the runtime system.
Primitives
A primitive is a built-in function, i.e. a function which body is
provided by the runtime system.
See :ref:`Predefined Functions` for the list of standard primitives.
Aside from the lack of body, and henceforth the absence of translation,
primitive declarations behave as function declarations. They share the same
name space, and obey the same duplicate-name rule. For instance:
primitive declarations behave as function declarations. They share the
same name space, and obey the same duplicate-name rule. For instance:
.. literalinclude:: primitive.tig
:language: tiger
is invalid, and must be rejected with exit status set to 4.
is invalid and must be rejected with the exit status set to 4.
......@@ -5,28 +5,29 @@ Method Declarations
Overriding methods
When a method in a class overrides a method of a super class, the overridden
method (in the super class) is no longer accessible. Dynamic dispatch is
performed, using the exact type of the object (known at run time) to select
the method according to this exact type.
When a method in a class overrides a method of a super class, the
overridden method in the super class is no longer accessible.
Dynamic dispatch is performed, using the exact type of the object
(known at runtime) to select the method according to this exact type.
However, the interface of the accessible attributes and callable methods
remains restricted to the static interface
(i.e. the one of the static type of the object).
However, the interface of the accessible attributes and callable
methods remains restricted to the static interface (i.e. the one
of the static type of the object).
.. literalinclude:: overriding.tig
:language: tiger
Method invariance
Methods are invariant in Tiger: each redefinition of a method in a subclass
shall have the exact same signature as the original (overridden) method.
Methods are invariant in Tiger: each redefinition of a method in
a subclass shall have the exact same signature as the original
overridden method.
This invariance applies to:
1. the number of arguments.
2. the types of the arguments.
3. the type of the return value (2).
3. the type of the return value [#]_.
.. literalinclude:: invariance.tig
:language: tiger
*(2) Which is not the case in C++, where methods have covariant return values.*
.. [#] Which is not the case in C++, where methods have covariant return values.
class Name [ extends Super ] { Classfields }
=> type Name = class [ extends Super ] { Classfields }
class name [ extends super ] { classfields }
=> type name = class [ extends super ] { classfields }
......@@ -4,9 +4,9 @@ Type Declarations
=================
arrays
Arrays
The size of the array does not belong to the type. Index of arrays starts
from 0 and ends at size - 1.
from 0 and ends at :code:`size` - 1.
.. literalinclude:: arrays.tig
:language: tiger
......@@ -22,7 +22,7 @@ arrays
.. literalinclude:: arrays3.tig
:language: tiger
records
Records
Records are defined by a list of fields between braces. Fields are described
as ``fieldname : type-id`` and are separated by a comma. Field names are
unique for a given record type.
......@@ -30,47 +30,51 @@ records
.. literalinclude:: records.tig
:language: tiger
classes
Classes
See also :ref:`Method Declarations`.
Classes define a set of attributes and methods. Empty classes are valid.
Attribute declaration is like variable declaration; method declaration is
similar to function declaration, but uses the keyword method instead of
function.
similar to function declaration, but uses the keyword :code:`method`
instead of :code:`function`.
There are two ways to declare a class. The first version (known as
canonical) uses type, and is similar to record and array declaration:
*canonical*) uses :code:`type`, and is similar to record and array
declaration:
.. literalinclude:: classes.tig
:language: tiger
The second version (known as alternative or Appel’s) doesn’t make use of
type, but introduces classes declarations directly. This is the synta
x described by Andrew Appel in his books:
The second version (known as *alternative* or *Appel’s*) doesn’t make
use of :code:`type`, but introduces classes declarations directly.
This is the syntax described by Andrew Appel in his books:
.. literalinclude:: classes2.tig
:language: tiger
For simplicity reasons, constructs using the alternative syntax are
considered as syntactic sugar for the canonical syntax, and are desugared by
the parser into this first form, using the following transformation:
For simplicity reasons, constructs using the alternative syntax
are considered as syntactic sugar for the canonical syntax, and
are desugared by the parser into this first form, using the
following transformation:
.. literalinclude:: classes3
where ``Name``, ``Super`` and ``Classfields`` are respectively the class
name, the super class name and the contents of the class (attributes and
methods) of the class.
where :code:`name`, :code:`super` and :code:`classfields` are
respectively the class name, the super class name and the contents
of the class (attributes and methods).
In the rest of the section, Appel’s form will be often used, to offer a
uniform reading with his books, but remember that the main syntax is the
other one, and Appel’s syntax is to be desugared into the canonical one.
In the rest of the section, *Appel’s form* will be often used, to
offer a uniform reading with his books, but remember that the main
syntax is the other one, and *Appel’s syntax* is to be desugared
into the canonical one.
Declarations of class members follow the same rules as variable and function
declarations: consecutive method declarations constitute a block (or chunk)
of methods, while a block of attributes contains only a single one attribute
declaration (several attribute declarations thus form several blocks).
An extra rule holds for class members: there shall be no two attributes with
the same name in the same class definition, nor two methods with the name.
Declarations of class members follow the same rules as variable and
function declarations: consecutive method declarations constitute a
chunk of methods, while a block of attributes contains only a single
one attribute declaration (several attribute declarations thus form
several chunks).
An extra rule holds for class members: there shall be no two attributes
nor two methods with the same name within the same class definition.
.. literalinclude:: classes4.tig
:language: tiger
......@@ -81,15 +85,15 @@ classes
.. literalinclude:: classes5.tig
:language: tiger
Objects of a given class are created using the keyword ``new``. There are no
constructors nor destructors in Tiger, so the attributes are always
initialized by the value given at their declaration.
Objects of a given class are created using the keyword :code:`new`.
There are no constructors nor destructors in Tiger, so the attributes
are always initialized by the value given at their declaration.
.. literalinclude:: classes6.tig
:language: tiger
The access to a member (either an attribute or a method) of an object from
outside the class uses the dotted notation (as in C++, Java, C#, etc.).
outside the class uses the dotted notation (as in C++, Java, C#, etc...).
There are no visibility qualifier/restriction (i.e. all attributes of an
object accessible in the current scope are accessible in read and write
modes), and all its methods can be called.
......@@ -97,93 +101,99 @@ classes
.. literalinclude:: classes7.tig
:language: tiger
To access to a member (either an attribute or a method) from within the
class where it is defined, use the ``self`` identifier (equivalent to C++’s
or Java’s ``this``), which refers to the current instance of the object.
To access a member (either an attribute or a method) from within the
class where it is defined, use the :code:`self` identifier (equivalent
to C++’s or Java’s :code:`this`), which refers to the current instance
of the object.
.. literalinclude:: classes8.tig
:language: tiger
The use of ``self`` is mandatory to access a member of the class (or of its
super class(es)) from within the class. A variable or a method not preceded
by ``self.`` won’t be looked up in the scope of the class.
The use of :code:`self` is mandatory to access a member of the class
(or of its super class(es)) from within the class. A variable or a
method not preceded by :code:`self.` won’t be looked up in the scope
of the class.
.. literalinclude:: classes9.tig
:language: tiger
``self`` cannot be used outside a method definition. In this respect,
``self`` cannot appear in a function or a class defined within a method
(except within a method defined therein, of course).
:code:`self` cannot be used outside a method definition. In this
respect, :code:`self` cannot appear in a function or a class defined
within a method (except within a method defined therein, of course).
.. literalinclude:: classes10.tig
:language: tiger
``self`` is a read-only variable and cannot be assigned.
:code:`self` is a read-only variable and cannot be assigned.
The Tiger language supports single inheritance thanks to the keyword
``extends``, so that a class can inherit from another class declared
previously, or declared in the same block of class declarations. A class
with no manifest inheritance (no ``extends`` statement following the class
name) automatically inherits from the built-in class ``Object``
:code:`extends`, so that a class can inherit from another class declared
previously, or declared in the same chunk of class declarations. A class
with no manifest inheritance (no :code:`extends` statement following the
class name) automatically inherits from the built-in class :code:`Object`
(this feature is an extension of Appel’s object-oriented proposal).
Inclusion polymorphism is supported as well: when a class Y inherits from a
class X (directly or through several inheritance links), any object of Y can
be seen as an object of type X. Hence, objects have two types: the static
type, known at compile time, and the dynamic (or exact) type, known at run
time, which is a subtype of (or identical to) the static type. Therefore, an
object of static type Y can be assigned to a variable of type X.
Inclusion polymorphism is supported as well: when a class Y inherits
from a class X (directly or through several inheritance links), any
object of Y can be seen as an object of type X. Hence, objects have
two types: the **static** type, known at compile time, and the
**dynamic** or exact type, known at runtime, which is a subtype of
(or identical to) the static type.
Therefore, an object of static type Y can be assigned to a variable
of type X.
.. literalinclude:: classes11.tig
:language: tiger
As stated before, a class can inherit from a class (1) declared previously
(and visible in the scope), or from a class declared in the same block of
type declarations (recall that a class declaration is in fact a type
declaration). Recursive inheritance is not allowed.
As stated before, a class can inherit from a class [#]_ declared
previously (and visible in the scope), or from a class declared in
the same chunk of type declarations (recall that a class declaration
is in fact a type declaration).
Recursive inheritance is not allowed.
.. literalinclude:: classes12.tig
:language: tiger
All members from the super classes (transitive closure of the “is a”
relationship) are accessible using the dotted notation, and the identifier
``self`` when they are used from within the class.
All members from the super classes (transitive closure of the *is
a* relationship) are accessible using the dotted notation, and the
identifier :code:`self` when they are used from within the class.
Attribute redefinition is not allowed: a class cannot define an attribute
with the same name as an inherited attribute, even if it has the same type.
Attribute redefinition is not allowed: a class cannot define an
attribute with the same name as an inherited attribute, even if
it has the same type.
Regarding method overriding, see :ref:`Method Declarations`.
Let us consider a block of type definitions. For each class of this block,
any of its members (either attributes or methods) can reference any type
introduced in scope of the block, including the class type enclosing the
considered members.
Let us consider a chunk of type definitions. For each class of this
chunk, any of its members (either attributes or methods) can reference
any type introduced in scope of the chunk, including the class type
enclosing the considered members.
.. literalinclude:: classes13.tig
:language: tiger
However, a class member cannot reference another member defined in a class
defined later in the program, in the current class or in a future class
(except if the member referred to is in the same block as the referring
member, hence in the same class, since a block of members cannot obviously
span across two or more classes). And recall that class members can only
reference previously defined class members, or members of the same block of
members (e.g. a chunk of methods).
However, a class member cannot reference another member defined in
a class defined later in the program, in the current class or in a
future class (except if the member referred to is in the same chunk
as the referring member, hence in the same class, since a chunk of
members cannot obviously span across two or more classes).
And recall that class members can only reference previously defined
class members, or members of the same chunk (e.g. a chunk of methods).
.. literalinclude:: classes14.tig
:language: tiger
To put it in a nutshell: within a chunk of types, forward references to
classes are allowed, while forward references to members are limited to the
block of members where the referring entity is defined.
To put it in a nutshell: within a chunk of types, forward references
to classes are allowed, while forward references to members are limited
to the chunk of members where the referring entity is defined.
recursive types
Recursive types
Types can be recursive,
.. literalinclude:: recursive_types.tig
:language: tiger
or mutually recursive (if they are declared in the same chunk) in Tiger,
or mutually recursive, if declared in the same chunk, in Tiger,
.. literalinclude:: recursive_types2.tig
:language: tiger
......@@ -193,9 +203,9 @@ recursive types
.. literalinclude:: recursive_types3.tig
:language: tiger
type equivalence
Two types are equivalent if they are issued from the same type construction
(array or record construction, or primitive type).
Type equivalence
Two types are equivalent if they are issued from the same type
construction (array or record construction, or primitive type).
As in C, unlike Pascal, structural equivalence is rejected.
Type aliases do not build new types, hence they are equivalent.
......@@ -203,6 +213,7 @@ type equivalence
.. literalinclude:: type_equivalence.tig
:language: tiger
is invalid, and must be rejected with exit status set to 5.
This example is invalid, and must be rejected with the exit
status set to 5.
*(1) A super class can only be a class type, and not another kind of type.*
.. [#] A super class can only be a class type, and not another kind of type.
......@@ -4,19 +4,19 @@ Variable Declarations
=====================
variables
Variables
There are two forms of variable declarations in Tiger:
the short one and the long one.
In the short form, only the name of the variable and the initial value of
the variable are specified, the variable type is “inferred”.
In the short form, only the name of the variable and the initial
value of the variable are specified, the variable type is *inferred*.
.. literalinclude:: variables.tig
:language: tiger
In the long form, the type of the variable is specified.
Since one cannot infer a record type for nil, the long form is mandated
when declaring a variable initialized to nil.
Since one cannot infer a record type for :code:`nil`, the long form
is mandated when declaring a variable initialized to :code:`nil`.
.. literalinclude:: variables2.tig
:language: tiger
......@@ -5,52 +5,57 @@ Expressions
L-values
The ``l-values`` (whose value can be read or changed) are: elements of
arrays, fields of records, instances of classes, arguments and variables.
The l-values (whose value can be read or changed) are: elements
of arrays, fields of records, instances of classes, arguments
and variables.
Valueless expressions
Some expressions have no value: procedure calls, assignments, ifs with no
else clause, loops and break. Empty sequences ``()`` and lets with an empty
body are also valueless.
Some expressions have no value: procedure calls, assignments,
*if*\ s with no *else* clause, loops and *break*\ s.
Empty sequences :code:`()` and *let*\ s with an empty body are
also valueless.
Nil
The reserved word ``nil`` refers to a value from a record or a class type.
Do not use ``nil`` where its type cannot be determined.
The reserved word :code:`nil` refers to a value from a record
or a class type.
Do not use :code:`nil` where its type cannot be determined.
.. literalinclude:: nil.tig
:language: tiger
Integers
An integer literal is a series of decimal digits (therefore it is
non-negative). Since the compiler targets 32-bit architectures, since it
needs to handle signed integers, a literal integer value must fit in a
signed 32-bit integer. Any other integer value is a scanner error.
An integer literal is a series of decimal digits (therefore it
is non-negative). Since the compiler targets 32-bit architectures,
since it needs to handle signed integers, a literal integer value
must fit in a signed 32-bit integer.
Any other integer value is a scanner error.
Booleans
There is no Boolean type in Tiger: they are encoded as integers, with the
same semantics as in C, i.e. ``0`` is the only value standing for **false**,
anything else stands for **true**.
There is no Boolean type in Tiger: they are encoded as integers,
with the same semantics as in C, i.e. :code:`0` is the only value
standing for **false**, anything else stands for **true**.
Strings
A string constant is a possibly empty series of printable characters, spaces
or escapes sequences (see :ref:`Lexical Specifications`) enclosed between
double quotes.
A string constant is a possibly empty series of printable characters,
spaces or escapes sequences (see :ref:`Lexical Specifications`)
enclosed between double quotes.
.. literalinclude:: strings.tig
:language: tiger
Record instantiation
A record instantiation must define the value of all the fields and in the
same order as in the definition of the record type.
A record instantiation must define the value of all the fields and
in the same order as in the definition of the record type.
Class instantiation
An object is created with ``new``. There are no constructors in Tiger, so
``new`` takes only one operand, the name of the type to instantiate.
An object is created with :code:`new`. There are no constructors
in Tiger, so :code:`new` takes only one operand, the name of the
type to instantiate.
Function call
Function arguments are evaluated from the left to the right. Arrays and
records arguments are passed by reference, strings and integer are passed
by value.
Function arguments are evaluated from the left to the right.
Arrays and records arguments are passed by reference, strings
and integer are passed by value.
The following example:
......@@ -63,23 +68,25 @@ Function call
:language: tiger
Boolean operators
Tiger Boolean operators normalize their result to ``0`` or ``1``.
For instance, because ``&`` and ``|`` can be implemented as syntactic sugar,
one could easily make ``123 | 456`` return ``1`` or ``123``: make them
return ``1``.
Andrew Appel does not enforce this for ``&`` and ``|``; we do, so that the
following program has a well defined behavior:
Tiger Boolean operators normalize their result to :code:`0`
or :code:`1`.
For instance, because :code:`&` and :code:`|` can be implemented
as syntactic sugar, one could easily make :code:`123 | 456` return
:code:`1` or :code:`123`: make them return :code:`1`.
Andrew Appel does not enforce this for :code:`&` and :code:`|`;
we do, so that the following program has a well defined behavior:
.. literalinclude:: boolean_operators.tig
:language: tiger
Arithmetic
Arithmetic expressions only apply on integers and return integers.
Available operators in Tiger are : ``+``, ``-``, ``*`` and ``/``.
Available operators in Tiger are: :code:`+`, :code:`-`, :code:`*`
and :code:`/`.
Comparison
Comparison operators (``=``, ``<>``, ``<=``, ``<``, ``>=`` and ``>``)
return a Boolean value.
Comparisons
Comparison operators (:code:`=`, :code:`<>`, :code:`<=`, :code:`<`,
:code:`>=` and :code:`>`) return a Boolean value.
Integer and string comparison
All the comparison operators apply to pairs of strings and pairs of
......@@ -89,21 +96,24 @@ Comparison
Comparison of strings is based on the lexicographic order.
Array and record comparison
Pairs of arrays and pairs of records of the same type can be compared
for equality (``=``) and inequality (``<>``). Identity equality applies,
i.e. an array or a record is only equal to itself (shallow equality),
regardless of the contents equality (deep equality). The value ``nil``
can be compared against a value which type is that of a record or a
class, e.g. ``nil = nil`` is invalid.
Arrays, records and objects cannot be ordered: ``<``, ``>``, ``<=`` and
``>=`` are valid only for pairs of strings or integers.
Pairs of arrays and pairs of records of the same type can be
compared for equality (:code:`=`) and inequality (:code:`<>`).
Identity equality applies, i.e. an array or a record is only
equal to itself (*shallow equality*), regardless of the contents
equality (*deep equality*).
The value :code:`nil` can only be compared against a value which
type is that of a record or a class.
As such :code:`nil = nil` is invalid.
Arrays, records and objects cannot be ordered: :code:`<`, :code:`>`,
:code:`<=` and :code:`>=` are valid only for pairs of strings
or integers.
Void comparison
In conformance with A. Appel’s specifications, any two void entities are
equal.
In conformance with Andrew Appel’s specifications, any two void
entities are equal.
Assignment
Assignments
Assignments yield no value. The following code is syntactically correct,
but type incorrect:
......@@ -117,113 +127,119 @@ Assignment
Array and record assignment
Array and record assignments are shallow, not deep, copies.
Therefore aliasing effects arise: if an array or a record variable ``a`` is
assigned another variable ``b`` of the same type, then changes on b will
affect ``a`` and vice versa.
Therefore aliasing effects arise: if an array or a record variable
:code:`a` is assigned another variable :code:`b` of the same type,
then changes on b will affect :code:`a` and vice versa.
.. literalinclude:: array_and_record_assignment.tig
:language: tiger
Polymorphic (object) assignment
Polymorphic (object) assignments
Upcasts are valid for objects because of inclusion polymorphism.
.. literalinclude:: polymorphic_assignment.tig
:language: tiger
Upcasts can be performed when defining a new object variable, by forcing the
type of the declared variable to a super class of the actual object.
Upcasts can be performed when defining a new object variable, by
forcing the type of the declared variable to a super class of the
actual object.
.. literalinclude:: polymorphic_assignment2.tig
:language: tiger
Tiger doesn’t provide a downcast feature performing run time type
identification (RTTI), like C++’s dynamic_cast.
Tiger does not provide a downcast feature performing run time type
identification (RTTI), like C++’s *dynamic_cast*.
.. literalinclude:: polymorphic_assignment3.tig
:language: tiger
Polymorphic (object) branching
| Upcast are performed when branching between two class instantiations.
| Since every class inherits from ``Object``, you will always find a common
root.
| Upcasts are performed when branching between two class
instantiations.
| Since every class itnherits from ``Object``, you will
always find a common root.
.. literalinclude:: polymorphic_branching.tig
:language: tiger
Sequences