Link all the page references to their correct places.
[ibg.git] / chapters / 15.rst
1 ===================
2 Compiling your game
3 ===================
4
5 .. only:: html
6
7   .. image:: /images/picA.png
8      :align: left
9
10 .. raw:: latex
11
12    \dropcap{a}
13
14 lmost as rarely as an alchemist producing gold from base metal, the 
15 compilation process turns your source file into a story file (though the 
16 more usual outcome is a reproachful explanation of why -- *again* -- 
17 that hasn't happened). The magic is performed by the compiler program, 
18 which takes your more or less comprehensible code and translates it into 
19 a binary file: a collection of numbers following a specific format 
20 understood only by Z-code interpreters.
21
22 On the surface, compilation is a very simple trick. You just run the 
23 compiler program, indicating which is the source file from which you 
24 wish to generate a game and presto! The magic is done.
25
26 However, the ingredients for the spell must be carefully prepared. The 
27 compiler "reads" your source code, but not as flexibly as a human would. 
28 It needs the syntax to follow some very precise rules, or it will 
29 complain that it cannot do its job under these conditions. The compiler 
30 cares little for meaning, and a lot about orthography, like a most 
31 inflexible teacher; no moist Bambi eyes are going to save you here.
32
33 Although the spell made by the compiler is always the same one, you can 
34 indicate up to a point how you want the magic to happen. There are a few 
35 options to affect the process of compilation; some you define in the 
36 source code, some with ``switches`` and certain commands when you run 
37 the program. The compiler will work with some default options if you 
38 don’t define any, but you may change these if you need to. Many of these 
39 options are provided "just in case" special conditions apply; others are 
40 meant for use of experienced designers with advanced and complex 
41 requirements, and are best left (for now) to those proficient in the 
42 lore.
43
44 Ingredients
45 ===========
46
47 If the source file is not written correctly the compiler will protest, 
48 issuing either a *warning* message or an *error* message. Warnings are 
49 there to tell you that there may be a mistake that could affect the 
50 behaviour of the game at run-time; that won't stop the compiler from 
51 finishing the process and producing a story file. Errors, on the other 
52 hand, reflect mistakes that make it impossible for the compiler to 
53 output such a file. Of these, *fatal* errors stop compilation 
54 immediately, while *non-fatal* errors allow the compiler to continue 
55 reading the source file. (As you’ll see in a minute, this is perhaps a 
56 mixed blessing: while it can be useful to have the compiler tell you 
57 about as many non-fatal errors as it can, you'll often find that many of 
58 them are caused by the one simple slip-up.)
59
60 .. rubric:: Fatal errors
61
62 It's difficult – but not impossible – to cause a fatal error. If you 
63 indicate the wrong
64 file name as source file, the compiler won’t even be able to start, 
65 telling you:
66
67     :samp:`Couldn't open source file {filename}`
68
69 If the compiler detects a large number of non-fatal errors, it may 
70 abandon the whole process with:
71
72     :samp:`Too many errors: giving up`
73
74 Otherwise, fatal errors most commonly occur when the compiler runs out 
75 of memory or disk space; with today's computers, that's pretty unusual. 
76 However, you may hit problems if the story file, which must fit within 
77 the fairly limited resources specified by the Z-machine, becomes too 
78 large. Normally, Inform compiles your source code into a Version 5 file 
79 (that’s what the ``.z5`` extension you see in the output file 
80 indicates), with a maximum size of 256 Kbytes. If your game is larger 
81 than this, you’ll have to compile into Version 8 file (``.z8``), which 
82 can grow up to 512 Kbytes (and you do this very simply by setting the 
83 ``-v8`` switch; more on that in a minute). It takes a surprising amount 
84 of code to exceed these limits; you won’t have to worry about game size 
85 for the next few months, if ever.
86
87
88 .. rubric:: Non-fatal errors
89
90 Non-fatal errors are much more common. You'll learn to be friends with:
91
92     :samp:`Expected {something} but found {something else}`
93
94 This is the standard way of reporting a punctuation or syntax mistake. 
95 If you type a comma instead of a semicolon, Inform will be looking for 
96 something in vain. The good news is that you are pointed to the 
97 offending line of code:
98
99 .. code-block:: transcript
100
101   Tell.inf(76): Error: Expected directive, '[' or class name but found found_in
102   >        found_in
103   Compiled with 1 error (no output)
104
105 You see the line number (76) and what was found there, so you run to the 
106 source file and take a look; any decent editor will display numbers 
107 alongside your lines if you wish, and will usually let you jump to a 
108 given line number. In this case, the error was caused by a semicolon 
109 after the description string, instead of a comma:
110
111 .. code-block:: inform
112
113   Prop    "assorted stalls"
114     with  name 'assorted' 'stalls',
115           description "Food, clothing, mountain gear; the usual stuff.";
116           found_in street below_square,
117   pluralname;
118
119 Here's a rather misleading message which maybe suggests that things in 
120 our source file are in the wrong order, or that some expected 
121 punctuation is missing:
122
123 .. code-block:: transcript
124
125   Fate.inf(459): Error: Expected name for new object or its textual short name
126   but found door
127   > Object door
128   Compiled with 1 error (no output)
129
130 In fact, there's nothing wrong with the ordering or punctuation. The 
131 problem is actually that we've tried to define a new object with an 
132 internal ID of ``door`` -- reasonably enough, you might think, since the 
133 object *is* a door -- but Inform already knows the word (it's the name 
134 of a library attribute). Unfortunately, the error message provides only 
135 the vaguest hint that you just need to choose another name: we used 
136 ``toilet_door`` instead.
137
138 Once the compiler is off track and can't find what was expected, it's 
139 common for the following lines to be misinterpreted, even if there's 
140 nothing wrong with them. Imagine a metronome ticking away in time with a 
141 playing record. If the record has a scratch and the stylus jumps, it may 
142 seem that the rest of the song is out of sync, when it's merely a bit 
143 "displaced" because of that single incident. This also happens with 
144 Inform, which at times will give you an enormous list of things Expected 
145 but not Found. The rule here is: correct the first mistake on the list 
146 and recompile. It may be that the rest of the song was perfect.
147
148 It would be pointless for us to provide a comprehensive list of errors, 
149 because mistakes are numerous and, anyhow, the explanatory text usually 
150 indicates what was amiss. You'll get errors if you forget a comma or a 
151 semicolon. You'll get errors if your quotes or brackets don't pair up 
152 properly. You'll get errors if you use the same name for two things. 
153 You'll get errors -- for many reasons. Just read the message, go to the 
154 line it mentions (and maybe check those just before and after it as 
155 well), and make whatever seems a sensible correction.
156
157 .. rubric:: Warnings
158
159 Warnings are not immediately catastrophic, but you should get rid of them
160 to ensure a good start at finding run-time mistakes (see :doc:`16`). You
161 may declare a variable and then not use it; you may mistake assignment and
162 arithmetic operators (``=`` instead of ``==``); you may forget the comma
163 that separates properties, etc. For all these and many other warnings,
164 Inform has found something which is legal but doubtful.
165
166 One common incident is to return in the middle of a statement block, 
167 before the rest of statements can be reached. This is not always as 
168 evident as it looks, for instance in a case like this:
169
170 .. code-block:: inform
171
172   if (steel_door has open) {
173       print_ret "The breeze blows out your lit match.";
174       give match ~light;
175   }
176
177 In the above example, the ``print_ret`` statement returns true after the 
178 string has been printed, and the ``give match ~light`` line will never 
179 happen. Inform detects the fault and warns you. Probably the designer's 
180 intention was:
181
182 Compiling *à la carte*
183 ======================
184
185 One of the advantages of Inform is its portability between different 
186 systems and machines. Specific usage of the compiler varies accordingly, 
187 but some features should be in all environments. To obtain precise 
188 information about any particular version, run the compiler with the 
189 ``-h1`` switch -- see :ref:`switches`.
190
191 Often the compiler is run with the name of your source file as its only
192 parameter. This tells the compiler to "read this file using Strict mode and
193 from it generate a Version 5 story file of the same name". The source file
194 is mostly full of statements which define how the game is to behave at
195 run-time, but will also include compile-time instructions directed at the
196 compiler itself (although such an instruction looks a lot like a
197 :term:`statement`, it's actually quite different in what it does, and is
198 known as a :term:`directive`). We have already seen the ``Include``
199 directive:
200
201   :samp:`Include "{filename}";`
202
203 When the compiler reaches a line like this, it looks for
204 :samp:`{filename}` -- another file also containing Inform code -- and
205 processes it as if the statements and directives included in
206 :samp:`{filename}` were in that precise spot where the ``Include`` 
207 directive is.
208
209 .. image:: /images/includes.png
210   :align: center
211
212 In every Inform game we Include the library files ``Parser``, 
213 ``VerbLib`` and ``Grammar``, but we may Include other files. For 
214 example, this is the way to incorporate library extensions contributed 
215 by other people, as you saw when we incorporated ``pname.h`` into our 
216 "Captain Fate" game.
217
218 .. note::
219
220   on some machines, a library file is actually called -- for example -- 
221   ``Parser.h``, on others just ``Parser``. The compiler automatically 
222   deals with such differences; you can *always* type simply ``Include 
223   "Parser";`` in your source file.
224
225 As you grow experienced in Inform, and your games become more complex, 
226 you may find that the source file becomes unmanageably large. One useful 
227 technique is then to divide it into a number of sections, each stored in 
228 a separate file, which you Include into a short master game file. For 
229 example:
230
231 .. code-block:: inform
232
233   !============================================================================
234   Constant Story "War and Peace";
235   Constant Headline
236               "^An extended Inform example
237                ^by me and Leo Tolstoy.^";
238
239   Include "Parser";
240   Include "VerbLib";
241
242   Include "1805";
243   Include "1806-11";
244   Include "1812A";
245   Include "1812B";
246   Include "1813-20";
247
248   Include "Grammar";
249
250   Include "Verbski";
251
252   !============================================================================
253
254 .. _switches:
255
256 Switches
257 ========
258
259 When you run the compiler you can set some optional controls; these are 
260 called *switches* because most of them are either on or off (although a 
261 few accept a numeric value 0–9). Switches affect compilation in a 
262 variety of ways, often just by changing the information displayed by the 
263 compiler when it’s running. A typical command line (although this may 
264 vary between machines) would be:
265
266   :samp:`inform {source_file story_file switches}`
267
268 where "``inform``" is the name of the compiler, the
269 :samp:`{story_file}` is optional (so that you can specify a different 
270 name from the 
271 :samp:`{source_file}`) and the switches are also optional. Note that 
272 switches must be preceded by a hyphen ``-``; if you want to set, for 
273 instance, Strict mode, you'd write ``-S`` , while if you want to 
274 deactivate it, you’d write ``-~S``. The tilde sign can, as elsewhere, be 
275 understood as "not". If you wish to set many switches, just write them 
276 one after another separated by spaces and each with its own hyphen, or 
277 merge them with one hyphen and no spaces:
278
279   :samp:`inform MyGame.inf -S -s -X`
280
281   :samp:`inform MyGame.inf -Ssx`
282
283 Although there's nothing wrong with this method, it isn't awfully 
284 convenient should you need to change the switch settings. A more 
285 flexible method is to define the switches at the very start of your 
286 source file, again in either format:
287
288   :samp:`!% -S -s -X`
289
290   :samp:`!% -Ssx`
291
292 Normally, all switches are off by default, except Strict mode (``-S``), 
293 which is on and checks the code for additional mistakes. It's well worth 
294 adding Debug mode (``-D``), thus making the debugging verbs available at 
295 run time. This is the ideal setting while coding, but you should turn 
296 Debug mode off (just remove the ``-D``) when you release your game to 
297 the public. This is fortunately very easy to check, since the game 
298 banner ends with the letter "D" if the game was compiled in Debug mode:
299
300 .. code-block:: transcript
301
302   Captain Fate
303   A simple Inform example
304   by Roger Firth and Sonja Kesserich.
305   Release 3 / Serial number 040804 / Inform v6.30 Library 6/11 SD
306
307 Switches are case sensitive, so you get different effects from ``-x`` 
308 and ``-X``. Some of the more useful switches are:
309
310 :samp:`-~S`
311   Set compiler Strict mode off. This deactivates some additional error 
312   checking features when it reads your source file. Strict mode is on by 
313   default.
314
315 :samp:`-v5 -v8`
316   Compile to this version of story file. Versions 5 (on by default) and 
317   8 are the only ones you should ever care about; they produce, 
318   respectively, story files with the extensions .z5 and .z8 . Version 5 
319   was the Advanced Infocom design, and is the default produced by 
320   Inform. This is the version you'll normally be using, which allows 
321   file sizes up to 256 Kbytes. If your game grows beyond that size, 
322   you'll need to compile to the Version 8 story file, which is very 
323   similar to Version 5 but allows a 512 Kbytes file size.
324
325 :samp:`-D -X`
326   Include respectively the debugging verbs and the Infix debugger in the 
327   story file (see :doc:`16`).
328
329 :samp:`-h1 -h2`
330   Display help information about the compiler. ``-h1`` produces 
331   information about file naming, and ``-h2`` about the available 
332   switches.
333
334 :samp:`-n -j`
335   ``-n`` displays the number of declared attributes, properties and 
336   actions. ``-j`` lists objects as they are being read and constructed 
337   in the story file.
338
339 :samp:`-s`
340   Offer game statistics. This provides a lot of information about your 
341   game, including the number of objects, verbs, dictionary entries, 
342   memory usage, etc., while at the same time indicating the maximum 
343   allowed for each entry. This can be useful to check whether you are 
344   nearing the limits of Inform.
345
346 :samp:`-r`
347   Record all the text of the game into a temporary file, useful to check 
348   all your descriptions and messages by running them through a spelling 
349   checker.
350
351 If you run the compiler with the ``-h2`` switch, you’ll find that there 
352 are many more switches than these, offering mostly advanced or obscure 
353 features which we consider to be of little interest to beginners. 
354 However, feel free to try whatever switches catch your eye; nothing you 
355 try here will affect your source file, which is strictly read-only as 
356 far as the compiler is concerned.
357