1
0
Fork 0
mirror of https://github.com/ganelson/inform.git synced 2024-07-05 08:34:22 +03:00
inform7/docs/core-module/22-itp.html
2020-04-07 01:06:09 +01:00

247 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>21/ac</title>
<meta name="viewport" content="width=device-width initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Language" content="en-gb">
<link href="../inweb.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body>
<nav role="navigation">
<h1><a href="../webs.html">Sources</a></h1>
<ul>
<li><a href="../compiler.html"><b>compiler tools</b></a></li>
<li><a href="../other.html">other tools</a></li>
<li><a href="../extensions.html">extensions and kits</a></li>
<li><a href="../units.html">unit test tools</a></li>
</ul>
<h2>Compiler Webs</h2>
<ul>
<li><a href="../inbuild/index.html">inbuild</a></li>
<li><a href="../inform7/index.html">inform7</a></li>
<li><a href="../inter/index.html">inter</a></li>
</ul>
<h2>Inbuild Modules</h2>
<ul>
<li><a href="../inbuild-module/index.html">inbuild</a></li>
<li><a href="../arch-module/index.html">arch</a></li>
<li><a href="../words-module/index.html">words</a></li>
<li><a href="../syntax-module/index.html">syntax</a></li>
<li><a href="../html-module/index.html">html</a></li>
</ul>
<h2>Inform7 Modules</h2>
<ul>
<li><a href="../core-module/index.html">core</a></li>
<li><a href="../problems-module/index.html">problems</a></li>
<li><a href="../inflections-module/index.html">inflections</a></li>
<li><a href="../linguistics-module/index.html">linguistics</a></li>
<li><a href="../kinds-module/index.html">kinds</a></li>
<li><a href="../if-module/index.html">if</a></li>
<li><a href="../multimedia-module/index.html">multimedia</a></li>
<li><a href="../index-module/index.html">index</a></li>
</ul>
<h2>Inter Modules</h2>
<ul>
<li><a href="../inter-module/index.html">inter</a></li>
<li><a href="../building-module/index.html">building</a></li>
<li><a href="../codegen-module/index.html">codegen</a></li>
</ul>
<h2>Foundation</h2>
<ul>
<li><a href="../../../inweb/docs/foundation-module/index.html">foundation</a></li>
</ul>
</nav>
<main role="main">
<!--Weave of '22/itp' generated by 7-->
<ul class="crumbs"><li><a href="../webs.html">Source</a></li><li><a href="../compiler.html">Compiler Modules</a></li><li><a href="index.html">core</a></li><li><a href="index.html#22">Chapter 22: Phrases</a></li><li><b>Introduction to Phrases</b></li></ul><p class="purpose">An exposition of the data structures used inside Inform to hold phrases, rules and rulebooks.</p>
<p class="inwebparagraph"><a id="SP1"></a><b>&#167;1. </b>A good deal of Inform source text consists of phrase declarations of one
kind or another, consisting of a preamble, then a colon (except in a few
cases where commas are permitted) and then a list of instructions about what
to do, which is normally a series of phrase invocations divided by
semicolons.
</p>
<p class="inwebparagraph">The usual dictionary definition of "phrase" is "a small group of words
standing together as a conceptual unit, typically forming a component of a
clause", but we've used the word "excerpt" for that. Instead, every Inform
phrase definition reads back grammatically as a single complete sentence,
even when quite long and written out in a tabulated, computer language sort
of form: rather as Philip Larkin's poem "MCMIV" is a single sentence
through all four stanzas. For example:
</p>
<blockquote>
<p>To award (N - a number) points: increase the score by N; say "Well done!"</p>
</blockquote>
<p class="inwebparagraph">is a phrase definition. When we talk about "award (N - a number) points",
we will call it a "phrase". When we run into a specific usage of it, like
</p>
<blockquote>
<p>award 21 points;</p>
</blockquote>
<p class="inwebparagraph">this is called an "invocation" of the phrase rather than being the phrase
itself. The difference between a phrase and an invocation is like the
difference in conventional programming languages between a function and
a function call.
</p>
<p class="inwebparagraph">"Award (N - a number) points" is called a "To... phrase", because it is
defined using "To", and takes effect only when it's invoked from another
phrase. But not all phrases look like this. There are also rules:
</p>
<blockquote>
<p>Before eating the cake, say "Look out! Marzipan!"</p>
</blockquote>
<p class="inwebparagraph">This example isn't as different as it looks, and the main difference
is simply that "To..." phrases take effect when invoked whereas rule phrases
take effect when the circumstances laid out in the preamble are found &mdash;
in this case, just before the action "eating the cake" is about be tried.
Inside Inform, "To..." phrases and rules have much in common, and both
are stored in "phrase" structures.
</p>
<p class="inwebparagraph"><a id="SP2"></a><b>&#167;2. </b>Every phrase is defined either as a list of invocations of phrases to
do something, or as a piece of primitive I6 code. The latter case is rare,
and mostly confined to the Standard Rules. But here is an example:
</p>
<blockquote>
<p>To alter score by (N - number): |(- score = score + {N}; -)|.</p>
</blockquote>
<p class="inwebparagraph">When we compile an invocation of a phrase like "award (N - a number) points",
it becomes a call to an I6 function, say <code class="display"><span class="extract">R_231(N);</span></code>. To make that work, of
course, we need to turn the definition into a function looking something
like this:
</p>
<p class="inwebparagraph"></p>
<pre class="display">
<span class="plain">[ R_231 N;</span>
<span class="plain"> increase the score by N;</span>
<span class="plain"> say "Well done!";</span>
<span class="plain"> rtrue;</span>
<span class="plain">];</span>
</pre>
<p class="inwebparagraph">Devising this <code class="display"><span class="extract">R_231</span></code> function is called "compiling" the phrase. On the
other hand, compiling an invocation of "alter score by (some - number)"
is done inline rather than by a function call: the I6 code from its definition
is used directly. So there is no such thing as "compiling" such a phrase.
</p>
<p class="inwebparagraph">Rule phrases cannot be given inline definitions, but they can be defined
as being the same as I6 routines from the template. So both kinds of phrase
can be defined in either a high-level way, invoking a series of other
phrases to get something done, or in a low-level way, as primitives written
directly in I6 code.
</p>
<p class="inwebparagraph"><a id="SP3"></a><b>&#167;3. </b>Each declaration corresponds to a <code class="display"><span class="extract">phrase</span></code> structure. This is an anthology
of five sub-structures for different purposes: PHUD, PHTD, PHSF, PHRCD and
PHOD. The data in these structures is used to represent the information
in the preamble, and also information needed during compilation of the
body of the declaration (local variable names which come and go, for
instance).
</p>
<p class="inwebparagraph">Further structures represent individual local variables (<code class="display"><span class="extract">local_variable</span></code>)
within PHSFs, and individual phrase options (<code class="display"><span class="extract">phrase_option</span></code>) within PHODs.
</p>
<p class="inwebparagraph">In the case of rules, the <code class="display"><span class="extract">phrase</span></code> structure is associated with a <code class="display"><span class="extract">rule</span></code>
structure. Not all rules are placed in rulebooks, but for those that are,
a structure called a <code class="display"><span class="extract">booking</span></code> is used to keep track of this. Rulebooks
are in turn stored as <code class="display"><span class="extract">rulebook</span></code> structures. The next chapter goes into
this much more fully.
</p>
<p class="inwebparagraph"><a id="SP4"></a><b>&#167;4. </b>So the <code class="display"><span class="extract">phrase</span></code> structure has five substructures, whose names are
abbreviated as PHUD, PHTD, PHSF, PHRCD and PHOD.
</p>
<p class="inwebparagraph"></p>
<ul class="items"><li>(i) The phrase usage data (PHUD) contains the results of parsing the
preamble of the declaration to see what kind of phrase is to be created.
For "To..." phrases, little is recorded. For rules, the PHUD contains
all the information needed to place the rule within its rulebook.
</li></ul>
<ul class="items"><li>(ii) Conversely, the phrase type data (PHTD) is primarily useful for
"To..." phrases. It records the pattern of text to be registered as
an excerpt with the excerpt parser, and the kinds required for the
tokens in the definition. For example, it records that the kind of
"alter score by (N - number)" is:
</li></ul>
<pre class="display">
<span class="plain">phrase number -&gt; nothing</span>
</pre>
<p class="inwebparagraph">and that the first "token", the number supplied, is called "N".
</p>
<p class="inwebparagraph"></p>
<ul class="items"><li>(iii) The phrase options data (PHOD) contains the names of phrase options,
if any apply, and whether or not they are mutually exclusive. It is used
only for "To..." phrases, and is separated from the PHTD because it
is parsed separately and isn't used in kind-checking.
</li></ul>
<ul class="items"><li>(iv) The phrase run-time context data (PHRCD) contains data structures
which store the fully-parsed preamble. This is of use only with rules,
and contains (for instance) the parameter to be used when placing the
rule into its rulebook &mdash; an action pattern structure. When such phrases
are created, they sometimes become rules which are bound up into rulebooks.
Rules in rulebooks are sorted into order of specificity, and this judgement
is made on the contents of the PHRCD alone. As we'll see, it's possible
for the same phrase to appear as several different rules in different
rulebooks; or, in the case of "To..." phrases, not to be a rule at all.
</li></ul>
<ul class="items"><li>(v) The phrase stack frame (PHSF) contains the local named values valid in a
given compilation context. (Names of phrase options can also be used locally
and for this the PHOD is used, but they are conditions not values.) These
named values include both local variables created with invocations like
"let F be 10;" and token names such as "N".
</li></ul>
<p class="inwebparagraph">Uniquely of the five substructures of <code class="display"><span class="extract">phrase</span></code>, the PHSF can exist
independently of any <code class="display"><span class="extract">phrase</span></code> structure in more than a temporary way.
Free-standing PHSF stack frames are used when compiling text which
contains substitutions, and for other code-compilation purposes where
code does not arise explicitly from phrase declarations in the source.
</p>
<p class="inwebparagraph">To sum up, a "to..." phrase makes use of a PHUD (minimally), a PHTD
(for sorting, registration and type-checking), a PHOD, and a PHSF.
A rule phrase makes use of a PHUD (more heavily), a PHRCD (for sorting
and to compile checks on applicability), and a PHSF.
</p>
<p class="inwebparagraph">The obvious simplification in this scheme would be to include the
PHRCD within the PHUD, and the PHOD within the PHTD, thus reducing the
picture to just three structures. We do not do this because there are
significant timing difficulties.
</p>
<hr class="tocbar">
<ul class="toc"><li><i>(This section begins Chapter 22: Phrases.)</i></li><li><a href="22-cs.html">Continue with 'Construction Sequence'</a></li></ul><hr class="tocbar">
<!--End of weave-->
</main>
</body>
</html>