5c52887aa8
FossilOrigin-Name: 7dd2b1bd7506c2c488b70620ba9d889f3a9a3d58f99b4dba1d6923e0ac0edb8d
133 lines
5.5 KiB
HTML
133 lines
5.5 KiB
HTML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head>
|
|
<title>doc/book/techniques/quotes</title>
|
|
<style type="text/css">
|
|
|
|
* { color: #000; background: #fff; max-width: 700px; }
|
|
tt, pre { background: #dedede; color: #111; font-family: monospace;
|
|
white-space: pre; display: block; width: 100%; }
|
|
.indentedcode { margin-left: 2em; margin-right: 2em; }
|
|
.codeblock {
|
|
background: #dedede; color: #111; font-family: monospace;
|
|
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
|
|
padding: 7px;
|
|
display: block;
|
|
}
|
|
|
|
.indentedlist { margin-left: 2em; color: #000; }
|
|
|
|
span { white-space: pre; }
|
|
.text { color: #000; white-space: pre; background: #dedede; }
|
|
.colon { color: #000; background: #dedede; }
|
|
.note { color: #000; background: #dedede; }
|
|
.str { color: #000; text-decoration: underline; background: #dedede; }
|
|
.num { color: #000; background: #dedede; font-weight: bold; font-style: italic; }
|
|
.fnum { color: #000; font-weight: bold; background: #dedede; }
|
|
.ptr { color: #000; font-weight: bold; background: #dedede; }
|
|
.fetch { color: #000; font-style: italic; background: #dedede; }
|
|
.store { color: #000; font-style: italic; background: #dedede; }
|
|
.char { color: #000; background: #dedede; }
|
|
.inst { color: #000; background: #dedede; }
|
|
.defer { color: #000; background: #dedede; }
|
|
.imm { color: #000; font-weight: bold; background: #dedede; }
|
|
.prim { color: #000; font-weight: bolder; background: #dedede; }
|
|
|
|
.tt { white-space: pre; font-family: monospace; background: #dedede; }
|
|
|
|
.h1, .h2, .h3, .h4 { white-space: normal; }
|
|
.h1 { font-size: 125%; }
|
|
.h2 { font-size: 120%; }
|
|
.h3 { font-size: 115%; }
|
|
.h4 { font-size: 110%; }
|
|
.hr { display: block; height: 2px; background: #000000; }
|
|
</style>
|
|
</head><body>
|
|
<p><span class="h1">Quotations</span>
|
|
<br/><br/>
|
|
Quotes are anonymous functions. RETRO uses these as the basis for
|
|
executable flow control and combinatorial logic.
|
|
<br/><br/>
|
|
<span class="h2">Using Quotations</span>
|
|
<br/><br/>
|
|
To make a quotation, surround the code with square brackets. E.g.,
|
|
<br/><br/>
|
|
<tt class='indentedcode'>#1 #2 eq? [ 'No_match s:put nl ] -if</tt>
|
|
<br/><br/>
|
|
Quotes can be nested:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>[ #3 [ #4 ] dip ] call</tt>
|
|
<br/><br/>
|
|
After creation, a pointer to the quotation is left on the stack
|
|
(or is compiled into the current definition).
|
|
<br/><br/>
|
|
<span class="h2">Combinators</span>
|
|
<br/><br/>
|
|
Words operating on quotations are called combinators; these are
|
|
discussed in <strong>Using Combinators</strong>.
|
|
<br/><br/>
|
|
<span class="h2">Implementation</span>
|
|
<br/><br/>
|
|
A quotation is compiled as:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>... code before quotation ...</tt>
|
|
<tt class='indentedcode'>i liju.... (if_compiling_only)</tt>
|
|
<tt class='indentedcode'>d address after quotation (if_compiling_only)</tt>
|
|
<tt class='indentedcode'>... code for quotation</tt>
|
|
<tt class='indentedcode'>i re...... (this_is_where_the_quote_ends)</tt>
|
|
<tt class='indentedcode'>i li......</tt>
|
|
<tt class='indentedcode'>d address of code for quotation</tt>
|
|
<tt class='indentedcode'>... code after quotation ....</tt>
|
|
<br/><br/>
|
|
<span class="h2">Other Notes</span>
|
|
<br/><br/>
|
|
Quotations are used heavily in RETRO. They give the source a
|
|
feel that's different from traditional Forth, and allow for
|
|
a more consistent syntax.
|
|
<br/><br/>
|
|
For instance, in a traditional Forth, you might have some
|
|
conditionals:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>IF ... THEN</tt>
|
|
<tt class='indentedcode'>IF ... ELSE ... THEN</tt>
|
|
<tt class='indentedcode'>IF ... EXIT THEN</tt>
|
|
<br/><br/>
|
|
RETRO uses conditional combinators for these:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>[ ... ] if</tt>
|
|
<tt class='indentedcode'>[ ... ] [ ... ] choose</tt>
|
|
<tt class='indentedcode'>[ ... ] if;</tt>
|
|
<br/><br/>
|
|
Or loops:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>FOR ... NEXT</tt>
|
|
<br/><br/>
|
|
Is replaced by:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>[ ... ] times</tt>
|
|
<br/><br/>
|
|
This can also extend to stack flow. Sequences like:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>>R ... >R</tt>
|
|
<tt class='indentedcode'>DUP >R ... >R</tt>
|
|
<br/><br/>
|
|
Become:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>[ ... ] dip</tt>
|
|
<tt class='indentedcode'>[ ... ] sip</tt>
|
|
<br/><br/>
|
|
And forms like:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>1 2 3 * swap 3 * swap</tt>
|
|
<br/><br/>
|
|
Can be replaced with a combinator like:
|
|
<br/><br/>
|
|
<tt class='indentedcode'>#1 #2 [ #3 * ] bi@</tt>
|
|
<br/><br/>
|
|
While there is a different set of words to learn, I find that
|
|
overall there's less noise from low level stack shuffling words
|
|
and the added consistency with regards to overall syntax has
|
|
been nice as I was never fond of the multiple forms that existed
|
|
in traditional Forth.
|
|
</p>
|
|
</body></html>
|