Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ImplementSuffix.adoc
CommitLineData
7f918cf1
CE
1ImplementSuffix
2===============
3
4<:ImplementSuffix:> is a pass for the <:SXML:>
5<:IntermediateLanguage:>, invoked from <:SXMLSimplify:>.
6
7== Description ==
8
9This pass implements the `TopLevel_setSuffix` primitive, which
10installs a function to exit the program.
11
12== Implementation ==
13
14* <!ViewGitFile(mlton,master,mlton/xml/implement-suffix.fun)>
15
16== Details and Notes ==
17
18<:ImplementSuffix:> works by introducing a new `ref` cell to contain
19the function of type `unit -> unit` that should be called on program
20exit.
21
22* The following code (appropriately alpha-converted) is appended to the beginning of the <:SXML:> program:
23+
24[source,sml]
25----
26val z_0 =
27 fn a_0 =>
28 let
29 val x_0 =
30 "toplevel suffix not installed"
31 val x_1 =
32 MLton_bug (x_0)
33 in
34 x_1
35 end
36val topLevelSuffixCell =
37 Ref_ref (z_0)
38----
39
40* Any occurrence of
41+
42[source,sml]
43----
44val x_0 =
45 TopLevel_setSuffix (f_0)
46----
47+
48is rewritten to
49+
50[source,sml]
51----
52val x_0 =
53 Ref_assign (topLevelSuffixCell, f_0)
54----
55
56* The following code (appropriately alpha-converted) is appended to the end of the <:SXML:> program:
57+
58[source,sml]
59----
60val f_0 =
61 Ref_deref (topLevelSuffixCell)
62val z_0 =
63 ()
64val x_0 =
65 f_0 z_0
66----