0 | ||| State used during JS code generation and when
1 | ||| converting `NamedCExp` to imperative statements.
11 | --------------------------------------------------------------------------------
12 | -- Utilities
13 | --------------------------------------------------------------------------------
15 | ||| Convenient alias for `throw . InternalError`
16 | export
20 | ||| Convenient alias for `error . fastConcat`.
21 | export
25 | --------------------------------------------------------------------------------
26 | -- CG Mode
27 | --------------------------------------------------------------------------------
29 | ||| Specifies the readability of the generated code.
30 | |||
31 | ||| In `Pretty` mode (the default), the codegen will
32 | ||| lift multiline lambdas from argument lists to the
33 | ||| surrounding scope and keep user generated names.
34 | ||| In addition, blocks of code are laid out with indentation
35 | ||| and linebreaks for better readability.
36 | |||
37 | ||| In `Compact` mode, all local variables are replace with
38 | ||| short machine generated ones, and every toplevel
39 | ||| function is printed on a single line without
40 | ||| line-breaks or indentation.
41 | |||
42 | ||| Finally, `Minimal` mode is like `Compact`, but toplevel
43 | ||| function names will be mangled and replaced with
44 | ||| machine generated indices.
48 | ||| We only keep user defined local names and only so
49 | ||| in `Pretty` mode.
50 | export
55 | ||| We mangle toplevel function names only in `Minimal` mode.
56 | export
61 | --------------------------------------------------------------------------------
62 | -- State
63 | --------------------------------------------------------------------------------
68 | ||| Settings and state used during JS code generation.
72 | ||| Whether to always use minimal names
75 | ||| Returns `True`, if the given expression can be used as an
76 | ||| argument in a function call. (If this returns `False`, the
77 | ||| given expression will be lifted to the surrounding scope
78 | ||| and bound to a new local variable).
81 | ||| Returns `True`, if the given expression can be used directly
82 | ||| in a function application. (If this returns `False`, the
83 | ||| given expression will be lifted to the surrounding scope
84 | ||| and bound to a variable).
87 | ||| Current local variable index
90 | ||| Current global variable index
93 | ||| Mapping from local names to minimal expressions
96 | ||| Mapping from toplevel function names to variables
99 | ||| Mappings from name to definitions to be added
100 | ||| to the preamble.
103 | ||| Accepted codegen types in foreign function definitions.
104 | ||| For JS, this is either `["node","javascript"]` or
105 | ||| `["browser","javascript"]`.
108 | ||| %nomangle names
111 | --------------------------------------------------------------------------------
112 | -- Local Variables
113 | --------------------------------------------------------------------------------
115 | ||| Map a local name to the given minimal expression
116 | export
120 | ||| Get and bump the local var index
121 | export
128 | ||| Register a `Name` as a local variable. The name is kept
129 | ||| unchanged if `keepLocalName` returns `True` with the
130 | ||| current name and state, otherwise it is converted to
131 | ||| a new local variable.
132 | export
142 | ||| Look up a name and call `registerLocal` in case it has
143 | ||| not been added to the map of local names.
144 | export
151 | ||| Maps the given list of names (from a pattern match
152 | ||| on a data constructor) to the corresponding
153 | ||| projections on the given scrutinee.
154 | export
163 | --------------------------------------------------------------------------------
164 | -- Toplevel Names
165 | --------------------------------------------------------------------------------
167 | ||| Map a toplevel function name to the given `Var`
168 | export
172 | ||| Get and bump the local ref index
173 | export
191 | ||| Look up a name and call `registerRef` in case it has
192 | ||| not been added to the map of toplevel function names.
193 | ||| The name will be replace with an index if the current
194 | ||| `GCMode` is set to `Minimal`.
195 | export
204 | --------------------------------------------------------------------------------
205 | -- Preamble and Foreign Definitions
206 | --------------------------------------------------------------------------------
208 | ||| Add a new set of definitions under the given name to
209 | ||| the preamble. Fails with an error if a different set
210 | ||| of definitions have already been added under the same name.
211 | export
221 | errorConcat
224 | ]
226 | --------------------------------------------------------------------------------
227 | -- Initialize State
228 | --------------------------------------------------------------------------------
230 | ||| Initial state of the code generator
231 | export
241 | ||| Reset the local state before defining a new toplevel
242 | ||| function.
243 | export