Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / AST.adoc
CommitLineData
7f918cf1
CE
1AST
2===
3
4<:AST:> is the <:IntermediateLanguage:> produced by the <:FrontEnd:>
5and translated by <:Elaborate:> to <:CoreML:>.
6
7== Description ==
8
9The abstract syntax tree produced by the <:FrontEnd:>.
10
11== Implementation ==
12
13* <!ViewGitFile(mlton,master,mlton/ast/ast-programs.sig)>
14* <!ViewGitFile(mlton,master,mlton/ast/ast-programs.fun)>
15* <!ViewGitFile(mlton,master,mlton/ast/ast-modules.sig)>
16* <!ViewGitFile(mlton,master,mlton/ast/ast-modules.fun)>
17* <!ViewGitFile(mlton,master,mlton/ast/ast-core.sig)>
18* <!ViewGitFile(mlton,master,mlton/ast/ast-core.fun)>
19* <!ViewGitDir(mlton,master,mlton/ast)>
20
21== Type Checking ==
22
23The <:AST:> <:IntermediateLanguage:> has no independent type
24checker. Type inference is performed on an AST program as part of
25<:Elaborate:>.
26
27== Details and Notes ==
28
29=== Source locations ===
30
31MLton makes use of a relatively clean method for annotating the
32abstract syntax tree with source location information. Every source
33program phrase is "wrapped" with the `WRAPPED` interface:
34
35[source,sml]
36----
37sys::[./bin/InclGitFile.py mlton master mlton/control/wrapped.sig 8:19]
38----
39
40The key idea is that `node'` is the type of an unannotated syntax
41phrase and `obj` is the type of its annotated counterpart. In the
42implementation, every `node'` is annotated with a `Region.t`
43(<!ViewGitFile(mlton,master,mlton/control/region.sig)>,
44<!ViewGitFile(mlton,master,mlton/control/region.sml)>), which describes the
45syntax phrase's left source position and right source position, where
46`SourcePos.t` (<!ViewGitFile(mlton,master,mlton/control/source-pos.sig)>,
47<!ViewGitFile(mlton,master,mlton/control/source-pos.sml)>) denotes a
48particular file, line, and column. A typical use of the `WRAPPED`
49interface is illustrated by the following code:
50
51[source,sml]
52----
53sys::[./bin/InclGitFile.py mlton master mlton/ast/ast-core.sig 46:65]
54----
55
56Thus, AST nodes are cleanly separated from source locations. By way
57of contrast, consider the approach taken by <:SMLNJ:SML/NJ> (and also
58by the <:CKitLibrary:CKit Library>). Each datatype denoting a syntax
59phrase dedicates a special constructor for annotating source
60locations:
61[source,sml]
62-----
63datatype pat = WildPat (* empty pattern *)
64 | AppPat of {constr:pat,argument:pat} (* application *)
65 | MarkPat of pat * region (* mark a pattern *)
66----
67
68The main drawback of this approach is that static type checking is not
69sufficient to guarantee that the AST emitted from the front-end is
70properly annotated.