mirror of
https://github.com/ganelson/inform.git
synced 2024-07-18 06:54:26 +03:00
238 lines
14 KiB
HTML
238 lines
14 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Introduction to Phrases</title>
|
|
<link href="../docs-assets/Breadcrumbs.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<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="../docs-assets/Contents.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<link href="../docs-assets/Progress.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<link href="../docs-assets/Navigation.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<link href="../docs-assets/Fonts.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<link href="../docs-assets/Base.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
<link href="../docs-assets/Colours.css" rel="stylesheet" rev="stylesheet" type="text/css">
|
|
|
|
</head>
|
|
<body class="commentary-font">
|
|
<nav role="navigation">
|
|
<h1><a href="../index.html">
|
|
<img src="../docs-assets/Inform.png" height=72">
|
|
</a></h1>
|
|
<ul><li><a href="../compiler.html">compiler tools</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="../supervisor-module/index.html">supervisor</a></li>
|
|
</ul><h2>Inform7 Modules</h2><ul>
|
|
<li><a href="../core-module/index.html">core</a></li>
|
|
<li><a href="../assertions-module/index.html">assertions</a></li>
|
|
<li><a href="../values-module/index.html">values</a></li>
|
|
<li><a href="../knowledge-module/index.html">knowledge</a></li>
|
|
<li><a href="index.html"><span class="selectedlink">imperative</span></a></li>
|
|
<li><a href="../runtime-module/index.html">runtime</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="../bytecode-module/index.html">bytecode</a></li>
|
|
<li><a href="../building-module/index.html">building</a></li>
|
|
<li><a href="../codegen-module/index.html">codegen</a></li>
|
|
</ul><h2>Services</h2><ul>
|
|
<li><a href="../arch-module/index.html">arch</a></li>
|
|
<li><a href="../calculus-module/index.html">calculus</a></li>
|
|
<li><a href="../html-module/index.html">html</a></li>
|
|
<li><a href="../inflections-module/index.html">inflections</a></li>
|
|
<li><a href="../kinds-module/index.html">kinds</a></li>
|
|
<li><a href="../linguistics-module/index.html">linguistics</a></li>
|
|
<li><a href="../problems-module/index.html">problems</a></li>
|
|
<li><a href="../syntax-module/index.html">syntax</a></li>
|
|
<li><a href="../words-module/index.html">words</a></li>
|
|
<li><a href="../../../inweb/docs/foundation-module/index.html">foundation</a></li>
|
|
|
|
</ul>
|
|
</nav>
|
|
<main role="main">
|
|
<!--Weave of 'Introduction to Phrases' generated by Inweb-->
|
|
<div class="breadcrumbs">
|
|
<ul class="crumbs"><li><a href="../index.html">Home</a></li><li><a href="../compiler.html">Inform7</a></li><li><a href="index.html">imperative</a></li><li><a href="index.html#3">Chapter 3: Phrases</a></li><li><b>Introduction to Phrases</b></li></ul></div>
|
|
<p class="purpose">An exposition of the data structures used inside Inform to hold phrases, rules and rulebooks.</p>
|
|
|
|
<p class="commentary firstcommentary"><a id="SP1" class="paragraph-anchor"></a><b>§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="commentary">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="commentary">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="commentary">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="commentary">"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="commentary">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 —
|
|
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="commentary firstcommentary"><a id="SP2" class="paragraph-anchor"></a><b>§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="commentary">When we compile an invocation of a phrase like "award (N - a number) points",
|
|
it becomes a call to an I6 function, say <span class="extract"><span class="extract-syntax">R_231(N);</span></span>. To make that work, of
|
|
course, we need to turn the definition into a function looking something
|
|
like this:
|
|
</p>
|
|
|
|
<pre class="displayed-code all-displayed-code code-font">
|
|
<span class="plain-syntax"> [ </span><span class="identifier-syntax">R_231</span><span class="plain-syntax"> </span><span class="identifier-syntax">N</span><span class="plain-syntax">;</span>
|
|
<span class="plain-syntax"> </span><span class="identifier-syntax">increase</span><span class="plain-syntax"> </span><span class="identifier-syntax">the</span><span class="plain-syntax"> </span><span class="identifier-syntax">score</span><span class="plain-syntax"> </span><span class="identifier-syntax">by</span><span class="plain-syntax"> </span><span class="identifier-syntax">N</span><span class="plain-syntax">;</span>
|
|
<span class="plain-syntax"> </span><span class="identifier-syntax">say</span><span class="plain-syntax"> </span><span class="string-syntax">"Well done</span><span class="comment-syntax">;</span>
|
|
<span class="string-syntax"> rtrue;</span>
|
|
<span class="string-syntax"> ];</span>
|
|
</pre>
|
|
<p class="commentary">Devising this <span class="extract"><span class="extract-syntax">R_231</span></span> 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="commentary">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="commentary firstcommentary"><a id="SP3" class="paragraph-anchor"></a><b>§3. </b>Each declaration corresponds to a <span class="extract"><span class="extract-syntax">phrase</span></span> 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="commentary">Further structures represent individual local variables (<span class="extract"><span class="extract-syntax">local_variable</span></span>)
|
|
within PHSFs, and individual phrase options (<span class="extract"><span class="extract-syntax">phrase_option</span></span>) within PHODs.
|
|
</p>
|
|
|
|
<p class="commentary">In the case of rules, the <span class="extract"><span class="extract-syntax">phrase</span></span> structure is associated with a <span class="extract"><span class="extract-syntax">rule</span></span>
|
|
structure. Not all rules are placed in rulebooks, but for those that are,
|
|
a structure called a <span class="extract"><span class="extract-syntax">booking</span></span> is used to keep track of this. Rulebooks
|
|
are in turn stored as <span class="extract"><span class="extract-syntax">rulebook</span></span> structures. The next chapter goes into
|
|
this much more fully.
|
|
</p>
|
|
|
|
<p class="commentary firstcommentary"><a id="SP4" class="paragraph-anchor"></a><b>§4. </b>So the <span class="extract"><span class="extract-syntax">phrase</span></span> structure has five substructures, whose names are
|
|
abbreviated as PHUD, PHTD, PHSF, PHRCD and PHOD.
|
|
</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><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="displayed-code all-displayed-code code-font">
|
|
<span class="plain-syntax"> phrase number -> nothing</span>
|
|
</pre>
|
|
<p class="commentary">and that the first "token", the number supplied, is called "N".
|
|
</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><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 — 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><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="commentary">Uniquely of the five substructures of <span class="extract"><span class="extract-syntax">phrase</span></span>, the PHSF can exist
|
|
independently of any <span class="extract"><span class="extract-syntax">phrase</span></span> 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="commentary">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="commentary">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>
|
|
|
|
<nav role="progress"><div class="progresscontainer">
|
|
<ul class="progressbar"><li class="progressprev"><a href="2-act.html">❮</a></li><li class="progresschapter"><a href="P-wtmd.html">P</a></li><li class="progresschapter"><a href="1-im.html">1</a></li><li class="progresschapter"><a href="2-rls.html">2</a></li><li class="progresscurrentchapter">3</li><li class="progresscurrent">itp</li><li class="progresssection"><a href="3-rs.html">rs</a></li><li class="progresssection"><a href="3-cs.html">cs</a></li><li class="progresssection"><a href="3-phr.html">phr</a></li><li class="progresssection"><a href="3-pu.html">pu</a></li><li class="progresssection"><a href="3-prcd.html">prcd</a></li><li class="progresssection"><a href="3-ptd.html">ptd</a></li><li class="progresssection"><a href="3-dptd.html">dptd</a></li><li class="progresssection"><a href="3-po.html">po</a></li><li class="progresssection"><a href="3-pav.html">pav</a></li><li class="progresssection"><a href="3-tph.html">tph</a></li><li class="progresssection"><a href="3-tp.html">tp</a></li><li class="progresssection"><a href="3-pi.html">pi</a></li><li class="progresschapter"><a href="4-lv.html">4</a></li><li class="progresschapter"><a href="5-cfs.html">5</a></li><li class="progresschapter"><a href="6-inv.html">6</a></li><li class="progressnext"><a href="3-rs.html">❯</a></li></ul></div>
|
|
</nav><!--End of weave-->
|
|
|
|
</main>
|
|
</body>
|
|
</html>
|
|
|