TC-1/2-Parser Chunks
Note
In Tiger, to support recursive types, functions and methods, continuous declarations are considered “simultaneously”. To ease the treatments of such series of declarations, we wrap them into chunks from on the parsing.
This section uses --bindings-compute
, implemented later
(see TC-3, Bindings) as this is when we detect bad declaration referencing.
- Functions
In the following program,
foo
andbar
are visible in each other’s scope, and therefore the following program is correct wrt type checking.foo-bar.tiglet function foo() : int = bar() function bar() : int = foo() in 0 end
tc --bindings-compute foo-bar.tig$ tc --bindings-compute foo-bar.tig $ echo $? 0
In the following sample, because
bar
is not declared in the same bunch of declarations, it is not visible during the declaration offoo
. The program is invalid.foo-stop-bar.tiglet function foo() : int = bar() var stop := 0 function bar() : int = foo() in 0 end
tc --bindings-compute foo-stop-bar.tig$ tc --bindings-compute foo-stop-bar.tig foo-stop-bar.tig:2.26-30: undeclared function: bar $ echo $? 4
The same applies to types.
Warning
A single name cannot be defined more than once in a chunk.
fbfsb.tiglet function foo() : int = 0 function bar() : int = 1 function foo() : int = 2 var stop := 0 function bar() : int = 3 in 0 end
tc --bindings-compute fbfsb.tig$ tc --bindings-compute fbfsb.tig fbfsb.tig:4.3-26: redefinition: foo fbfsb.tig:2.3-26: first definition $ echo $? 4
It behaves exactly as if chunks were part of embedded
let in end
, i.e. as if the previous program was syntactic sugar for the following one (in fact, Tiger 2006 used to desugar it that way).fbfsb-desugared.tiglet function foo() : int = 0 function bar() : int = 1 in let function foo() : int = 2 in let var stop := 0 in let function bar() : int = 3 in 0 end end end end
- Variables
Note
Given the type checking rules for variables, whose definitions cannot be recursive, chunks of variable declarations are reduced to a single variable.
var-chunks.tiglet var x := y var y := x in end
tc --bindings-compute var-chunks.tig$ tc --bindings-compute var-chunks.tig var-chunks.tig:2.12: undeclared variable: y $ echo $? 4