Noeud « Next »: T2 Error Recovery, Noeud « Previous »: T2 Pretty-Printing Samples, Noeud « Up »: T2 Samples
In Tiger, to support recursive types and functions, continuous
declarations of functions and continuous declarations of types are
considered “simultaneously”. For instance in the following program,
foo
and bar
are visible in each other's scope, and
therefore the following program is correct wrt type checking.
let function foo () : int = bar ()
function bar () : int = foo ()
in
0
end
File 31: foo-bar.tig
$ tc -T foo-bar.tig Example 32: tc -T foo-bar.tig
In the following sample, because bar
is not declared in the same
bunch of declarations, it is not visible during the declaration of
foo
. The program is invalid.
let function foo () : int = bar ()
var stop := 0
function bar () : int = foo ()
in
0
end
File 33: foo-stop-bar.tig
$ tc -T foo-stop-bar.tig error-->foo-stop-bar.tig:1.28-33: unknown function: bar =>4 Example 34: tc -T foo-stop-bar.tig
The same applies to types.
We shall name chunk a continuous series of type (or function) declaration.
Within a chunk, duplicate names are invalid, while they are valid for separated chunks:
let function foo () : int = 0
function bar () : int = 1
function foo () : int = 2
var stop := 0
function bar () : int = 3
in
0
end
File 35: fbfsb.tig
$ tc -T fbfsb.tig error-->fbfsb.tig:3.4-28: function redefinition: foo error-->fbfsb.tig:1.4-28: first definition =>4 Example 36: tc -T fbfsb.tig
It behaves exactly as if chunks were part of embedded let in end
.
This is why our Tiger compilers will treat chunks as syntactic sugar: an
internal let
(i.e., a LetExp
) may only have a single chunk
of declarations. If the input has several chunks, they must be split
into several let
:
$ tc -A fbfsb.tig /* == Abstract Syntax Tree. == */ let function foo () : int = 0 function bar () : int = 1 function foo () : int = 2 in let var stop := 0 in let function bar () : int = 3 in 0 end end end Example 37: tc -A fbfsb.tig
Given the type checking rules for variables, whose definitions cannot be recursive, chunks of variable declarations are reduced to a single variable.
let var foo := 1
var foo := foo + 1
var foo := foo + 1
in
foo
end
File 38: fff.tig
$ tc -A fff.tig /* == Abstract Syntax Tree. == */ let var foo := 1 in let var foo := (foo + 1) in let var foo := (foo + 1) in foo end end end Example 39: tc -A fff.tig