Debian Codegraph @

doc/guide/src/AST.adoc

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