| 1 |
<!DOCTYPE html> |
| 2 |
<html><head><title>SAL</title> |
| 3 |
<link rel="stylesheet" type="text/css" href="nyquiststyle.css"> |
| 4 |
<link rel="icon" href="nyquist-icon.png" /> |
| 5 |
<link rel="shortcut icon" href="nyquist-icon.png" /> |
| 6 |
</head> |
| 7 |
<body bgcolor="ffffff"> |
| 8 |
<a href = "part6.html">Previous Section</a> | <a href = "part8.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 9 |
<hr> |
| 10 |
<a name = "51"><h2>SAL</h2></a> |
| 11 |
<p>Nyquist supports two languages: XLISP and SAL. In some sense, XLISP and SAL |
| 12 |
are the same language, but with differing syntax. This chapter describes SAL: how it works, SAL syntax and semantics, and the relationship between SAL and XLISP, and differences between Nyquist SAL and Common Music SAL.</p> |
| 13 |
|
| 14 |
<p>Nyquist SAL is based on Rick Taube's SAL language, which is |
| 15 |
part of Common Music. SAL offers the power |
| 16 |
of Lisp but features a simple, Algol-like syntax. SAL is implemented |
| 17 |
in Lisp: Lisp code translates SAL into a Lisp program and uses the |
| 18 |
underlying Lisp engine to evaluate the program. Aside from the translation |
| 19 |
time, which is quite fast, SAL programs execute at about the same speed as |
| 20 |
the corresponding Lisp program. (Nyquist SAL programs run just |
| 21 |
slightly slower than XLISP |
| 22 |
because of some runtime debugging support automatically added to |
| 23 |
user programs by the SAL compiler.)</p> |
| 24 |
|
| 25 |
<p>From the user's perspective, these implementation details are hidden. You |
| 26 |
can enter SAL mode from XLISP by typing <code>(SAL)</code> to the XLISP prompt. |
| 27 |
The SAL input prompt (<code>SAL> </code>) will be displayed. From that point on, |
| 28 |
you simply type SAL commands, and they will be executed. By setting a |
| 29 |
preference in the NyquistIDE program, SAL mode will be entered automatically.</p> |
| 30 |
|
| 31 |
<p>It is possible to encounter errors that will take you from the SAL interpreter |
| 32 |
to an XLISP prompt. In general, the way to get back to SAL is by typing |
| 33 |
<code>(top)</code> to get back to the top level XLISP interpreter and reset the |
| 34 |
Nyquist environment. Then type <code>(sal)</code> to restart the SAL interpreter.</p> |
| 35 |
<a name = "52"><h3>SAL Syntax and Semantics</h3></a><a name="index183"></a> |
| 36 |
<p> The most unusual feature of SAL syntax is that identifiers |
| 37 |
are Lisp-like, including names such as “play-file” and even “*warp*.” |
| 38 |
In SAL, most operators must be separated from identifiers by white space. |
| 39 |
For example, <code>play-file</code> is one identifier, but <code>play - file</code> |
| 40 |
is an expression for “play minus file,” where <code>play</code> and <code>file</code> are |
| 41 |
two separate identifiers. Fortunately, no spaces are needed around commas |
| 42 |
and parentheses.</p> |
| 43 |
|
| 44 |
<p>The set SAL identifiers is difficult to describe due to a number of interacting |
| 45 |
rules designed to prevent surprising or confusing code. The exact details can be |
| 46 |
found in the <code>symbol-token?</code> function in <code>sal-parse.lsp</code>. To determine |
| 47 |
if an identifier is allowed, you can also follow the following example, which tests |
| 48 |
whether <code>$a@</code> is a valid identifier:</p> |
| 49 |
|
| 50 |
<p></p> |
| 51 |
<pre> |
| 52 |
SAL> print type-of(quote($a@)) |
| 53 |
</pre> |
| 54 |
|
| 55 |
<p></p> |
| 56 |
|
| 57 |
<p>If this statement prints <code>SYMBOL</code>, then the quoted expression is a valid identifier. If SAL reports a "parse error" or some other type, then the quoted expression is not a valid identifier.</p> |
| 58 |
|
| 59 |
<p>In SAL, whitespace (any sequence of space, newline, or tab characters) |
| 60 |
is sometimes necessary to separate lexical tokens, but |
| 61 |
otherwise, spaces and indentation are ignored. To make SAL readable, |
| 62 |
it is <i>strongly</i> advised that you indent SAL programs as in the examples |
| 63 |
here. The NyquistIDE program is purposely insistent about SAL indentation, |
| 64 |
so if you use it to edit SAL programs, your indentation should be |
| 65 |
both beautiful and consistent.</p> |
| 66 |
|
| 67 |
<p>As in Lisp (but very unlike C or Java), comments <a name="index184"></a> |
| 68 |
are indicated by |
| 69 |
semicolons. Any text from an unquoted semicolon to the end of the |
| 70 |
line is ignored.</p> |
| 71 |
|
| 72 |
<p></p> |
| 73 |
<pre> |
| 74 |
<i>; this is a comment</i> |
| 75 |
<i>; comments are ignored by the compiler</i> |
| 76 |
print "Hello World" <i>; this is a SAL statement</i> |
| 77 |
</pre> |
| 78 |
|
| 79 |
<p></p> |
| 80 |
|
| 81 |
<p>As in Lisp, identifiers are translated to upper-case, making SAL |
| 82 |
case-insensitive<a name="index185"></a>. For example, the function name <code>autonorm</code> can |
| 83 |
be typed in lower case or as <code>AUTONORM</code>, <code>AutoNorm</code>, or even |
| 84 |
<code>AuToNoRm</code>. All forms denote the same function. The recommended |
| 85 |
approach is to write programs in all lower case.</p> |
| 86 |
|
| 87 |
<p>SAL is organized around statements, most of which |
| 88 |
contain expressions. We will begin with expressions and then look at |
| 89 |
statements.</p> |
| 90 |
<a name = "53"><h4>Expressions</h4></a><a name="index186"></a><a name="index187"></a><a name = "54"><h5>Simple Expressions</h5></a> |
| 91 |
<p>As in XLISP, simple expressions include: |
| 92 |
<ul> |
| 93 |
<li> |
| 94 |
integers (FIXNUM's), such as <code>1215</code>, </li> |
| 95 |
<li>floats (FLONUM's) such as <code>12.15</code>, </li> |
| 96 |
<li>strings (STRING's) such as <code>"Magna Carta"</code>, and</li> |
| 97 |
<li>symbols (SYMBOL's) such as <code>magna-carta</code>. A symbol with a leading colon |
| 98 |
(<code>:</code>) evaluates to itself as in Lisp. Otherwise, a symbol denotes either |
| 99 |
a local variable, a formal parameter, or a global variable. As in Lisp, |
| 100 |
variables do not have data types or type declarations. The type of a |
| 101 |
variable is determined at runtime by its value. |
| 102 |
</li></ul></p> |
| 103 |
|
| 104 |
<p>Additional simple expressions in SAL are: |
| 105 |
<ul> |
| 106 |
<li> |
| 107 |
lists such as <code>{c 60 e 64}</code>. Note that there are no commas to separate list elements, and symbols in lists are not evaluated as variables but stand for themselves. Lists may contain numbers, booleans (which represent XLISP's <code>T</code> or <code>NIL</code>, SAL identifiers (representing XLISP symbols), strings, SAL operators (representing XLISP symbols), and nested lists.</li> |
| 108 |
<li>Booleans: SAL interprets <code>#t</code><a name="index188"></a> as true and <code>#f</code><a name="index189"></a> |
| 109 |
as false. (As far |
| 110 |
as the SAL compiler is concerned, <code>t</code> and <code>nil</code><a name="index190"></a> are just variables. |
| 111 |
Since these are the Lisp versions of true and false, they are interchangeable |
| 112 |
with <code>#t</code> and <code>#f</code>, respectively.) |
| 113 |
</li></ul> |
| 114 |
A curious property of Lisp and Sal is that <i>false</i> and the empty list (<code>nil</code>) are |
| 115 |
the same value. Since SAL is based on Lisp, <code>#f</code> and <code>{}</code> (the empty |
| 116 |
list)<a name="index191"></a> and <code>nil</code> are all equal.</p> |
| 117 |
<a name = "55"><h5>Operators</h5></a> |
| 118 |
<p>Expressions can be formed with unary and binary operators using infix notation. The operators are: |
| 119 |
<dl> |
| 120 |
<dt> |
| 121 |
<a name="index192"></a><code>+</code></dt> |
| 122 |
<dd>addition, including sounds</p> |
| 123 |
<dt><a name="index193"></a><code>-</code></dt> |
| 124 |
<dd>subtraction, including sounds<br><br> |
| 125 |
<dt><a name="index194"></a><code>*</code></dt> |
| 126 |
<dd>multiplication, including sounds<br><br> |
| 127 |
<dt><a name="index195"></a><code>/</code></dt> |
| 128 |
<dd>division (due to divide-by-zero problems, does not operate on sounds)<br><br> |
| 129 |
<dt><a name="index196"></a><code>%</code></dt> |
| 130 |
<dd>modulus (remainder after division)<br><br> |
| 131 |
<dt><a name="index197"></a><code>^</code> </dt> |
| 132 |
<dd>exponentiation<br><br> |
| 133 |
<dt><a name="index198"></a><code>=</code></dt> |
| 134 |
<dd>equal (using Lisp <code>equal</code> for non-lists, and |
| 135 |
comparing lists element-by-element recursively) <a href = "foot.html#foot1">(Footnote 1)</a> <!DOCTYPE html> |
| 136 |
<html><head><title>Footnotes</title> |
| 137 |
<link rel="stylesheet" type="text/css" href="nyquiststyle.css"> |
| 138 |
<link rel="icon" href="nyquist-icon.png" /> |
| 139 |
<link rel="shortcut icon" href="nyquist-icon.png" /> |
| 140 |
</head> |
| 141 |
<br><br> |
| 142 |
<dt><a name="index199"></a><code>!=</code></dt> |
| 143 |
<dd>not equal<br><br> |
| 144 |
<dt><a name="index200"></a><code>></code></dt> |
| 145 |
<dd>greater than<br><br> |
| 146 |
<dt><a name="index201"></a><code><</code></dt> |
| 147 |
<dd>less than<br><br> |
| 148 |
<dt><a name="index202"></a><code>>=</code></dt> |
| 149 |
<dd>greater than or equal<br><br> |
| 150 |
<dt><a name="index203"></a><code><=</code></dt> |
| 151 |
<dd>less than or equal<br><br> |
| 152 |
<dt><a name="index204"></a><code>~=</code></dt> |
| 153 |
<dd>approximately equal. Numbers are approximately |
| 154 |
equal if they are within <code>*~=tolerance*</code> of each |
| 155 |
other. <code>*~=tolerance*</code> is initially 0.000001. Non-numbers are |
| 156 |
compared with the XLISP <code>equal</code> function, and lists are compared |
| 157 |
element-by-element (recursively) using <code>~=</code>. <a href = "foot.html#foot2">(Footnote 2)</a> <br><br> |
| 158 |
<dt><a name="index205"></a><code>&</code></dt> |
| 159 |
<dd>logical and<br><br> |
| 160 |
<dt><a name="index206"></a><code>|</code></dt> |
| 161 |
<dd>logical or<br><br> |
| 162 |
<dt><a name="index207"></a><code>!</code></dt> |
| 163 |
<dd>logical not (unary)<br><br> |
| 164 |
<dt><a name="index208"></a><a name="index209"></a><a name="index210"></a><code>@</code></dt> |
| 165 |
<dd>time shift<br><br> |
| 166 |
<dt><a name="index211"></a><a name="index212"></a><a name="index213"></a><code>@@</code></dt> |
| 167 |
<dd>time shift to absolute time<br><br> |
| 168 |
<dt><a name="index214"></a><a name="index215"></a><code>~</code></dt> |
| 169 |
<dd>time stretch<br><br> |
| 170 |
<dt><a name="index216"></a><a name="index217"></a><code>~~</code></dt> |
| 171 |
<dd>time stretch to absolute stretch factor |
| 172 |
</dd></dl> |
| 173 |
Again, remember that operators <i>must</i> be delimited from their operands using |
| 174 |
spaces or parentheses. Operator precedence is based on the following levels of |
| 175 |
precedence: |
| 176 |
|
| 177 |
<p></p> |
| 178 |
<pre> |
| 179 |
@ @@ ~ ~~ |
| 180 |
^ |
| 181 |
/ * |
| 182 |
% - + |
| 183 |
~= <= >= > ~= = |
| 184 |
! |
| 185 |
& |
| 186 |
| |
| 187 |
</pre> |
| 188 |
|
| 189 |
<p></p> |
| 190 |
<a name = "56"><h5>Function Calls</h5></a><a name="index218"></a> |
| 191 |
<p>A function call is a function name followed by zero or more comma-delimited |
| 192 |
argument expressions |
| 193 |
enclosed within parentheses: |
| 194 |
</p> |
| 195 |
<pre> |
| 196 |
list() |
| 197 |
piano-note(2.0, c4 + interval, 100) |
| 198 |
</pre> |
| 199 |
|
| 200 |
<p> |
| 201 |
Some functions use named parameters, |
| 202 |
in which case the name of the argument with a colon precedes the argument |
| 203 |
expression. |
| 204 |
</p> |
| 205 |
<pre> |
| 206 |
s-save(my-snd(), ny:all, "tmp.wav", play: #t, bits: 16) |
| 207 |
</pre> |
| 208 |
|
| 209 |
<p></p> |
| 210 |
<a name = "57"><h5>Array Notation</h5></a><a name="index219"></a> |
| 211 |
<p>An array reference is a variable identifier followed by an index expression |
| 212 |
in square brackets, e.g.: |
| 213 |
</p> |
| 214 |
<pre> |
| 215 |
x[23] + y[i] |
| 216 |
</pre> |
| 217 |
|
| 218 |
<p></p> |
| 219 |
<a name = "58"><h5>Conditional Values</h5></a><a name="index220"></a><a name="index221"></a> |
| 220 |
<p>The special operator <code>#?</code> evaluates the first argument expression. |
| 221 |
If the result is <i>true</i>, the second expression is evaluated and |
| 222 |
its value is returned. If <i>false</i>, the third expression is evaluated |
| 223 |
and returned (or <i>false</i> is returned if there is no third expression): |
| 224 |
</p> |
| 225 |
<pre> |
| 226 |
#?(random(2) = 0, unison, major-third) |
| 227 |
#?(pitch >= c4, pitch - c4) <i>; returns false if pitch < c4</i> |
| 228 |
</pre> |
| 229 |
|
| 230 |
<p></p> |
| 231 |
<a name = "59"><h4>SAL Statements</h4></a><a name="index222"></a> |
| 232 |
<p>SAL compiles and evaluates <i>statements</i> one at a time. You can type |
| 233 |
statements at the SAL prompt or load a file containing SAL statements. |
| 234 |
SAL statements are described below. The syntax is indicated at the |
| 235 |
beginning of each statement type description: <code>this font</code> indicates |
| 236 |
literal terms such as keywords, <i>the italic font</i> indicates a |
| 237 |
place-holder for some other statement or expression. Bracket [like this] |
| 238 |
indicate optional (zero or one) syntax elements, while braces with a plus |
| 239 |
{like this}+ indicate one or more occurrences of a syntax element. Braces |
| 240 |
with a star {like this}* indicate zero or more occurrences of a syntax element: { <i>non-terminal</i> }* is equivalent to [ {<i>non-terminal</i>}+ ].</p> |
| 241 |
<a name = "60"><h5>begin and end</h5></a><a name="index223"></a><a name="index224"></a><code>begin</code> [<i>with-stmt</i>] {<i>statement</i>}+ <code>end</code> |
| 242 |
<p>A <code>begin</code>-<code>end</code> statement |
| 243 |
consists of a sequence of statements surrounded by |
| 244 |
the <code>begin</code> and <code>end</code> keywords. This form is often used for function |
| 245 |
definitions and after <code>then</code> or <code>else</code> where the syntax demands a |
| 246 |
single statement but you want to perform more than one action. Variables may be |
| 247 |
declared using an optional <code>with</code> statement immediately after <code>begin</code>. |
| 248 |
For example: |
| 249 |
</p> |
| 250 |
<pre> |
| 251 |
begin |
| 252 |
with db = 12.0, |
| 253 |
linear = db-to-linear(db) |
| 254 |
print db, "dB represents a factor of", linear |
| 255 |
set scale-factor = linear |
| 256 |
end |
| 257 |
</pre> |
| 258 |
|
| 259 |
<p></p> |
| 260 |
<a name = "61"><h5>chdir</h5></a><a name="index225"></a><code>chdir</code> <i>expression</i> |
| 261 |
<p>The <code>chdir</code> statement changes the working directory. This statement |
| 262 |
is provided for compatibility with Common Music SAL, but it really |
| 263 |
should be avoided if you use NyquistIDE. The <i>expression</i> following the |
| 264 |
<code>chdir</code> keyword should evaluate to a string that is a directory |
| 265 |
path name. Note that literal strings themselves are valid expressions. |
| 266 |
</p> |
| 267 |
<pre> |
| 268 |
chdir "/Users/rbd/tmp" |
| 269 |
</pre> |
| 270 |
|
| 271 |
<p></p> |
| 272 |
<a name = "62"><h5>define variable</h5></a><a name="index226"></a><a name="index227"></a> |
| 273 |
<p>[<code>define</code>] <code>variable</code> <i>name</i> [= <i>expression</i>] {, <i>name</i> [= <i>expression</i>]}*</p> |
| 274 |
|
| 275 |
<p>Global variables can be declared and initialized. A list of variable names, |
| 276 |
each with an optional initialization follows the <code>define variable</code> |
| 277 |
keywords. (Since <code>variable</code> is a keyword, <code>define</code> is redundant |
| 278 |
and optional in Nyquist SAL, but required in Common Music SAL.) |
| 279 |
If the initialization part is omitted, the variable is initialized |
| 280 |
to false. Global variables do not really need to be declared: just using the |
| 281 |
name implicitly creates the corresponding variable. However, it is an error |
| 282 |
to use a global variable that has not been initialized; |
| 283 |
<code>define variable</code> is a good way to introduce a variable (or constant) |
| 284 |
with an initial value into your program.</p> |
| 285 |
|
| 286 |
<p></p> |
| 287 |
<pre> |
| 288 |
define variable transposition = 2, |
| 289 |
print-debugging-info, <i>; initially false</i> |
| 290 |
output-file-name = "salmon.wav" |
| 291 |
</pre> |
| 292 |
|
| 293 |
<p></p> |
| 294 |
<a name = "63"><h5>define function</h5></a><a name="index228"></a><a name="index229"></a> |
| 295 |
<p>[<code>define</code>] <code>function</code> <i>name</i> <code>(</code> [<i>parameter</i>] {, <i>parameter</i>}* <code>)</code> <i>statement</i></p> |
| 296 |
|
| 297 |
<p>Before a function be called from an expression (as described above), it must |
| 298 |
be defined. A function definition gives the function <i>name</i>, a list of |
| 299 |
<i>parameters</i>, and a <i>statement</i>. When a function is called, the actual |
| 300 |
parameter expressions are evaluated from left to right and the formal parameters |
| 301 |
of the function definition are set to these values. Then, <i>statement</i> is |
| 302 |
evaluated. </p> |
| 303 |
|
| 304 |
<p>The formal parameters may be positional parameters that are matched with |
| 305 |
actual parameters by position from left to right. Syntactically, these are |
| 306 |
symbols and these symbols |
| 307 |
are essentially local variables that exist only until <i>statement</i> completes |
| 308 |
or a <code>return</code> statement causes the function evaluation to end. As in Lisp, |
| 309 |
parameters are passed by value, so assigning a new value to a formal parameter |
| 310 |
has no effect on the actual value. However, lists and arrays are not copied, |
| 311 |
so internal changes to a list or array produce observable side effects.</p> |
| 312 |
|
| 313 |
<p>Alternatively, formal parameters may be keyword parameters. Here the <i>parameter</i> |
| 314 |
is actually a pair: a keyword parameter, which is a symbol followed by a colon, |
| 315 |
and a default value, given by any expression. Within the body of the function, |
| 316 |
the keyword parameter is named by a symbol whose name matches the keyword |
| 317 |
parameter except there is no final colon. |
| 318 |
</p> |
| 319 |
<pre> |
| 320 |
define function foo(x: 1, y: bar(2, 3)) |
| 321 |
display "foo", x, y |
| 322 |
|
| 323 |
exec foo(x: 6, y: 7) |
| 324 |
</pre> |
| 325 |
|
| 326 |
<p> |
| 327 |
In this example, <code>x</code> is bound to the value 6 and <code>y</code> is bound to |
| 328 |
the value 7, so the example prints “<code>foo : X = 6, Y = 7</code>”. Note that |
| 329 |
while the keyword parameters are <code>x:</code> and <code>y:</code>, the corresponding |
| 330 |
variable names in the function body are <code>x</code> and <code>y</code>, respectively.</p> |
| 331 |
|
| 332 |
<p>The <i>parameters</i> are meaningful only within the lexical (static) scope of |
| 333 |
<i>statement</i>. They are not accessible from within other |
| 334 |
functions even if they are called by this function.</p> |
| 335 |
|
| 336 |
<p>Use a <code>begin</code>-<code>end</code> statement if the body of the function should |
| 337 |
contain more than one statement or you need to define local variables. Use |
| 338 |
a <code>return</code> statement to return a value from the function. If <i>statement</i> |
| 339 |
completes without a <code>return</code>, the value false is returned.</p> |
| 340 |
<a name = "64"><h5>exec</h5></a><a name="index230"></a><code>exec</code> <i>expression</i> |
| 341 |
<p>Unlike most other programming languages, you cannot simply type an expression as |
| 342 |
a statement. If you want to evaluate an expression, e.g. call a function, |
| 343 |
you must use an <code>exec</code> statement. The statement simply evaluates |
| 344 |
the <i>expression</i>. For example, |
| 345 |
</p> |
| 346 |
<pre> |
| 347 |
exec set-sound-srate(22050.0) <i>; change default sample rate</i> |
| 348 |
</pre> |
| 349 |
|
| 350 |
<p></p> |
| 351 |
<a name = "65"><h5>if</h5></a><a name="index231"></a><code>if</code> <i>test-expr</i> <code>then</code> <i>true-stmt</i> [<code>else</code> <i>false-stmt</i>] |
| 352 |
<p>An <code>if</code> statement evaluates the expression <i>test-expr</i>. If it is true, |
| 353 |
it evaluates the statement <i>true-stmt</i>. If false, the statement |
| 354 |
<i>false-stmt</i> is evaluated. Use a <code>begin</code>-<code>end</code> statement |
| 355 |
to evaluate more than one statement in then <code>then</code> or <code>else</code> |
| 356 |
parts.</p> |
| 357 |
|
| 358 |
<p></p> |
| 359 |
<pre> |
| 360 |
if x < 0 then x = -x <i>; x gets its absoute value</i> |
| 361 |
|
| 362 |
if x > upper-bound then |
| 363 |
begin |
| 364 |
print "x too big, setting to", upper-bound |
| 365 |
x = upper-bound |
| 366 |
end |
| 367 |
else |
| 368 |
if x < lower-bound then |
| 369 |
begin |
| 370 |
print "x too small, setting to", lower-bound |
| 371 |
x = lower-bound |
| 372 |
end |
| 373 |
</pre> |
| 374 |
|
| 375 |
<p> |
| 376 |
Notice in this example that the <code>else</code> part is another <code>if</code> |
| 377 |
statement. An <code>if</code> may also be the <code>then</code> part of another |
| 378 |
<code>if</code>, so there could be two possible <code>if</code>'s with which to |
| 379 |
associate an <code>else</code>. An <code>else</code> clause always associates |
| 380 |
with the closest previous <code>if</code> that does not already have an |
| 381 |
<code>else</code> clause.</p> |
| 382 |
<a name = "66"><h5>when</h5></a><code>when</code> <i>test</i> <i>statement</i> |
| 383 |
<p>The <code>when</code> statement is similar to <code>if</code>, but there is no <code>else</code> clause.</p> |
| 384 |
|
| 385 |
<p></p> |
| 386 |
<pre> |
| 387 |
when *debug-flag* print "you are here" |
| 388 |
</pre> |
| 389 |
|
| 390 |
<p></p> |
| 391 |
<a name = "67"><h5>unless</h5></a><code>unless</code> <i>test</i> <i>statement</i> |
| 392 |
<p>The <code>unless</code> statement is similar to <code>when</code> (and <code>if</code>) but the |
| 393 |
<i>statement</i> is executed when the <i>test</i> expression is <i>false</i>.</p> |
| 394 |
|
| 395 |
<p></p> |
| 396 |
<pre> |
| 397 |
unless count = 0 set average = sum / count |
| 398 |
</pre> |
| 399 |
|
| 400 |
<p></p> |
| 401 |
<a name = "68"><h5>load</h5></a><a name="index232"></a><code>load</code> <i>path-expression</i> |
| 402 |
<p>The <code>load</code> command loads a file named by <i>path-expression</i>, which must |
| 403 |
be either a literal string or a variable name. When evauated, |
| 404 |
<i>path-expression</i> must result in a string path name for the |
| 405 |
file. The extension <code>.sal</code> is appended to the path if needed. |
| 406 |
To load a file, SAL interprets |
| 407 |
each statement in the file, stopping when the end of the file or an error |
| 408 |
is encountered. If the file name ends in the extension <code>.lsp</code>, the |
| 409 |
file is assumed to |
| 410 |
contain Lisp expressions, which are evaluated by the XLISP interpreter. |
| 411 |
In general, SAL files should end with the extension <code>.sal</code>.</p> |
| 412 |
<a name = "69"><h5>loop</h5></a><a name="index233"></a><code>loop</code> [<i>with-stmt</i>] {<i>stepping</i>}* {<i>stopping</i>}* {<i>action</i>}+ [<i>finally</i>] <code>end</code> |
| 413 |
<p>The <code>loop</code> statement is by far the most complex statement in SAL, but |
| 414 |
it offers great flexibility for just about any kind of iteration. The basic |
| 415 |
function of a loop is to repeatedly evaluate a sequence of <i>action</i>'s which |
| 416 |
are statements. Before the loop begins, local variables may be declared in |
| 417 |
<i>with-stmt</i>, a <code>with</code> statement. </p> |
| 418 |
|
| 419 |
<p>The <i>stepping</i> clauses do several |
| 420 |
things. They introduce and initialize additional local variables similar |
| 421 |
to the <i>with-stmt</i>. |
| 422 |
However, these local variables are updated to new values after the <i>action</i>'s. |
| 423 |
In addition, some <i>stepping</i> clauses have associated stopping conditions, |
| 424 |
which are tested on each iteration <i>before</i> evaluating the <i>action</i>'s.</p> |
| 425 |
|
| 426 |
<p>There are also <i>stopping</i> clauses that provide additional tests to |
| 427 |
stop the iteration. These are also evaluated and tested |
| 428 |
on each iteration before evaluating the <i>action</i>'s.</p> |
| 429 |
|
| 430 |
<p>When some <i>stepping</i> or <i>stopping</i> condition causes the iteration to stop, |
| 431 |
the <i>finally</i> clause is evaluated (if present). Local variables and their |
| 432 |
values can still be accessed in the <i>finally</i> clause. After the <i>finally</i> |
| 433 |
clause, the <code>loop</code> statement completes.</p> |
| 434 |
|
| 435 |
<p>The <i>stepping</i> clauses are the following: |
| 436 |
<dl> |
| 437 |
<dt> |
| 438 |
<code>repeat</code> <i>expression</i></dt> |
| 439 |
<dd>Sets the number of iterations to the value of <i>expression</i>, which should be an integer (FIXNUM).</p> |
| 440 |
<dt><code>for</code> <i>var</i> = <i>expression</i> [ <code>then</code> <i>expr2</i> ]</dt> |
| 441 |
<dd>Introduces a new local |
| 442 |
variable named <i>var</i> and initializes it to <i>expression</i>. Before each subsequent |
| 443 |
iteration, <i>var</i> is set to the value of <i>expr2</i>. If the <code>then</code> part is |
| 444 |
omitted, <i>expression</i> is re-evaluated and assigned to <i>var</i> |
| 445 |
on each subsequent iteration. Note that this differs from a <i>with-stmt</i> where |
| 446 |
expressions are evaluated and variables are only assigned their values once. <b>Warning:</b> All <code>for</code> clauses evaluate <i>before</i> stopping conditions are evaluated, which means if the loop iterates N times, the <code>for</code> clauses run N+1 times. To avoid evaluation after the last iteration, you can often use <code>set</code> <i>var</i> = <i>expr2</i>.<br><br> |
| 447 |
<dt><code>for</code> <i>var</i> <code>in</code> <i>expression</i></dt> |
| 448 |
<dd>Evaluates <i>expression</i> to |
| 449 |
obtain a list and creates a new local variable initialized to the first element |
| 450 |
of the list. After each iteration, <i>var</i> is assigned the next element of the |
| 451 |
list. Iteration stops when <i>var</i> has assumed all values from the list. If the |
| 452 |
list is initially empty, the loop <i>action</i>'s are not evaluated (there are zero |
| 453 |
iterations).<br><br> |
| 454 |
<dt><code>for</code> <i>var</i> [<code>from</code> <i>from-expr</i>] [[<code>to</code> | <code>below</code> | <code>downto</code> | <code>above</code>] <i>to-expr</i>] [<code>by</code> <i>step-expr</i>]</dt> |
| 455 |
<dd>Introduces a new local variable named <i>var</i> and intialized |
| 456 |
to the value of the expression <i>from-expr</i> (with a default value of 0). After |
| 457 |
each iteration of the loop, <i>var</i> is incremented by the value |
| 458 |
of <i>step-expr</i> (with a default value of 1). |
| 459 |
The iteration ends when <i>var</i> is greater than |
| 460 |
the value of <i>to-expr</i> if there is a <code>to</code> clause, |
| 461 |
greater than or equal to the value of <i>to-expr</i> |
| 462 |
if there is a <code>below</code> clause, |
| 463 |
less than the value of <i>to-expr</i> if there is a <code>downto</code> clause, |
| 464 |
or less than or equal to the value of <i>to-expr</i> if there is a <code>above</code> |
| 465 |
clause. (In the cases of <i>downto</i> and <i>above</i>, the default increment value |
| 466 |
is -1. If there |
| 467 |
is no <code>to</code>, <code>below</code>, <code>downto</code>, or <code>above</code> clause, no iteration stop test is created for this |
| 468 |
stepping clause. |
| 469 |
</dd></dl> |
| 470 |
<p>The <i>stopping</i> clauses are the following: |
| 471 |
<dl> |
| 472 |
<dt> |
| 473 |
<code>while</code> <i>expression</i></dt> |
| 474 |
<dd>The iterations are stopped when <i>expression</i> evaluates to <i>false</i>. Anything not false is considered to mean true.</p> |
| 475 |
<dt><code>until</code> <i>expression</i></dt> |
| 476 |
<dd>The iterations are stopped when <i>expression</i> evaluates to <i>true</i>. |
| 477 |
</dd></dl> |
| 478 |
<p>The <i>finally</i> clause is defined as follows: |
| 479 |
<a name="index234"></a> |
| 480 |
<dl> |
| 481 |
<dt> |
| 482 |
<code>finally</code> <i>statement</i></dt> |
| 483 |
<dd>The <i>statement</i> is evaluated when one of the |
| 484 |
<i>stepping</i> or <i>stopping</i> clauses ends the loop. As always, <i>statement</i> may |
| 485 |
be a <code>begin</code>-<code>end</code> statement. If an <i>action</i> evaluates a <code>return</code> |
| 486 |
statement, the <code>finally</code> statement is not executed. |
| 487 |
</dd></dl></p> |
| 488 |
<a name="index235"></a>Loops often fall into common patterns, such as iterating a fixed number of |
| 489 |
times, performing an operation on some range of integers, collecting results |
| 490 |
in a list, and linearly searching for a solution. These forms are illustrated |
| 491 |
in the examples below. |
| 492 |
<p></p> |
| 493 |
<pre> |
| 494 |
<i>; iterate 10 times</i> |
| 495 |
loop |
| 496 |
repeat 10 |
| 497 |
print random(100) |
| 498 |
end |
| 499 |
</pre> |
| 500 |
|
| 501 |
<p> |
| 502 |
</p> |
| 503 |
<pre> |
| 504 |
<i>; print even numbers from 10 to 20 |
| 505 |
; note that 20 is printed. On the next iteration, |
| 506 |
; i = 22, so i >= 22, so the loop exits.</i> |
| 507 |
loop |
| 508 |
for i from 10 below 22 by 2 |
| 509 |
print i |
| 510 |
end |
| 511 |
</pre> |
| 512 |
|
| 513 |
<p> |
| 514 |
</p> |
| 515 |
<pre> |
| 516 |
<i>; collect even numbers in a list</i> |
| 517 |
loop |
| 518 |
with lis |
| 519 |
for i from 0 to 10 by 2 |
| 520 |
set lis @= i <i>; push integers on front of list,</i> |
| 521 |
<i>; which is much faster than append,</i> |
| 522 |
<i>; but list is built in reverse</i> |
| 523 |
finally set result = reverse(lis) |
| 524 |
end |
| 525 |
<i>; since "to" is used, the loop stops when i > 10</i> |
| 526 |
<i>; now, the variable result has a list of evens:</i> {0 2 4 6 8 10} |
| 527 |
</pre> |
| 528 |
|
| 529 |
<p> |
| 530 |
</p> |
| 531 |
<pre> |
| 532 |
<i>; find the first even number in a list</i> |
| 533 |
result = #f <i>; #f means "false"</i> |
| 534 |
loop |
| 535 |
for elem in lis |
| 536 |
until evenp(elem) |
| 537 |
finally result = elem |
| 538 |
end |
| 539 |
<i>; result has first even value in lis (or it is #f)</i> |
| 540 |
</pre> |
| 541 |
|
| 542 |
<p></p> |
| 543 |
<a name = "70"><h5>play</h5></a><a name="index236"></a><code>play</code> <i>expr</i> |
| 544 |
<p>The <code>play</code> statement plays the sound denoted by <i>expr</i>, an expression.</p> |
| 545 |
<a name = "71"><h5>plot</h5></a><a name="index237"></a><a name="index238"></a><code>plot</code> <i>expr</i> {, <i>dur</i>, <i>n</i>} |
| 546 |
<p>The <code>plot</code> statement plots the sound denoted by <i>expr</i>, an |
| 547 |
expression. If you plot a long sound, the <code>plot</code> statement will |
| 548 |
by default truncate the sound to 2.0 seconds and resample the signal |
| 549 |
to 1000 points. The optional <i>dur</i> is an expression that specifies |
| 550 |
the (maximum) duration to be plotted, and the optional <i>n</i> specifies |
| 551 |
the number of points to be plotted. Executing a <code>plot</code> statement |
| 552 |
is equivalent to calling the <code>s-plot</code> function (see Section |
| 553 |
<a href = "part8.html#100">Sound File Input and Output</a>).</p> |
| 554 |
<a name = "72"><h5>print</h5></a><a name="index239"></a><a name="index240"></a><code>print</code> <i>expr</i> {, <i>expr</i>}* |
| 555 |
<p>The <code>print</code> statement prints the values separated by |
| 556 |
spaces and followed by a newline. [Note that in the original |
| 557 |
SAL, the newline is printed <i>before</i> the values, not after.]</p> |
| 558 |
<a name = "73"><h5>display</h5></a><a name="index241"></a><a name="index242"></a><code>display</code> <i>string</i> {, <i>expression</i>}* |
| 559 |
<p>The <code>display</code> statement is handy for debugging. At present, it is only |
| 560 |
implemented in Nyquist SAL. When executed, <code>display</code> prints the <i>string</i> |
| 561 |
followed by a colon and then, for each <i>expression</i>, the expression and its |
| 562 |
value are printed; after the last expression, a newline is printed. For example, |
| 563 |
</p> |
| 564 |
<pre> |
| 565 |
display "In function foo", bar, baz |
| 566 |
</pre> |
| 567 |
|
| 568 |
<p> |
| 569 |
prints |
| 570 |
</p> |
| 571 |
<pre> |
| 572 |
In function foo : bar = 23, baz = 5.3 |
| 573 |
</pre> |
| 574 |
|
| 575 |
<p> |
| 576 |
SAL may print the expressions using Lisp syntax, e.g. if the expression is |
| 577 |
“bar + baz,” do not be surprised if the output is “<code>(sum bar baz) = 28.3</code>.”</p> |
| 578 |
|
| 579 |
<p></p> |
| 580 |
<pre> |
| 581 |
print "The value of x is", x |
| 582 |
</pre> |
| 583 |
|
| 584 |
<p></p> |
| 585 |
<a name = "74"><h5>return</h5></a><a name="index243"></a><code>return</code> <i>expression</i> |
| 586 |
<p>The <code>return</code> statement can only be used inside a function. It evaluates |
| 587 |
<i>expression</i> and then the function returns the value of the expression |
| 588 |
to its caller.</p> |
| 589 |
<a name = "75"><h5>set</h5></a><a name="index244"></a><code>set</code> <i>var</i> <i>op</i> <i>expression</i> {, <i>var</i> <i>op</i> <i>expression</i>}* |
| 590 |
<p>The <code>set</code> statement changes the value of a variable <i>var</i> according |
| 591 |
to the operator <i>op</i> and the value of the <i>expression</i>. The operators are: |
| 592 |
<dl> |
| 593 |
<dt> |
| 594 |
<code>=</code></dt> |
| 595 |
<dd>The value of <i>expression</i> is assigned to <i>var</i>.</p> |
| 596 |
<dt><a name="index245"></a><code>+=</code></dt> |
| 597 |
<dd>The value of <i>expression</i> is added to <i>var</i>.<br><br> |
| 598 |
<dt><a name="index246"></a><code>*=</code></dt> |
| 599 |
<dd>The value of <i>var</i> is multiplied by the value of the expression.<br><br> |
| 600 |
<dt><a name="index247"></a><code>&=</code></dt> |
| 601 |
<dd>The value of <i>expression</i> is inserted as the last element of |
| 602 |
the list referenced by <i>var</i>. If <i>var</i> is the empty list (denoted by <code>#f</code>), |
| 603 |
then <i>var</i> is assigned a newly constructed list of one element, the value |
| 604 |
of <i>expression</i>.<br><br> |
| 605 |
<dt><a name="index248"></a><code>^=</code></dt> |
| 606 |
<dd>The value of <i>expression</i>, a list, is appended to the list referenced |
| 607 |
by <i>var</i>. If <i>var</i> is the empty list (denoted by <code>#f</code>), then <i>var</i> |
| 608 |
is assigned the (list) value of <i>expression</i>.<br><br> |
| 609 |
<dt><a name="index249"></a><code>@=</code></dt> |
| 610 |
<dd>Pushes the value of <i>expression</i> onto the front of the list |
| 611 |
referenced by <i>var</i>. If <i>var</i> is empty (denoted by <code>#f</code>), then <i>var</i> |
| 612 |
is assigned a newly constructed list of one element, the value of <i>expression</i>.<br><br> |
| 613 |
<dt><a name="index250"></a><code><=</code></dt> |
| 614 |
<dd>Sets the new value of <i>var</i> to the minimum of the old value |
| 615 |
of <i>var</i> and the value of <i>expression</i>.<br><br> |
| 616 |
<dt><a name="index251"></a><code>>=</code></dt> |
| 617 |
<dd>Sets the new value of <i>var</i> to the maximum of the old value |
| 618 |
of <i>var</i> and the value of <i>expression</i>. |
| 619 |
</dd></dl> |
| 620 |
<p></p> |
| 621 |
<pre> |
| 622 |
<i>; example from Rick Taube's SAL description</i> |
| 623 |
loop |
| 624 |
with a, b = 0, c = 1, d = {}, e = {}, f = -1, g = 0 |
| 625 |
for i below 5 |
| 626 |
set a = i, b += 1, c *= 2, d &= i, e @= i, f <= i, g >= i |
| 627 |
finally display "results", a, b, c, d, e, f, g |
| 628 |
end |
| 629 |
</pre> |
| 630 |
|
| 631 |
<p></p> |
| 632 |
<a name = "76"><h5>with</h5></a><a name="index252"></a><code>with</code> <i>var</i> [= <i>expression</i>] {, <i>var</i> [= <i>expression</i>]}* |
| 633 |
<p>The <code>with</code> statement declares and initializes local variables. It |
| 634 |
can appear only after <code>begin</code> or <code>loop</code>. If the <i>expression</i> is |
| 635 |
omitted, the initial value is <i>false</i>. The variables are visible only |
| 636 |
inside the <code>begin</code>-<code>end</code> or <code>loop</code> statement where the |
| 637 |
<code>with</code> statement appears. Even in <code>loop</code>'s the variables |
| 638 |
are intialized only when the loop is entered, not on each iteration.</p> |
| 639 |
<a name = "77"><h5>exit</h5></a><a name="index253"></a><code>exit</code> [<code>nyquist</code>] |
| 640 |
<p>The <code>exit</code> statement is unique to Nyquist SAL. It returns from SAL |
| 641 |
mode to the XLISP interpreter. (Return to SAL mode by typing “<code>(sal)</code>”). |
| 642 |
If <code>nyquist</code> is included in the statement, then the entire Nyquist |
| 643 |
process will exit.</p> |
| 644 |
<a name = "78"><h3>Interoperability of SAL and XLISP</h3></a><a name="index254"></a><a name="index255"></a> |
| 645 |
<p>When SAL evaluatas command or loads files, it translates SAL into XLISP. |
| 646 |
You can think of SAL as a program that translates everything you write |
| 647 |
into XLISP and entering it for you. Thus, when you define a SAL function, |
| 648 |
the function actually exists as an XLISP function (created using |
| 649 |
Lisp's <code>defun</code> special form). When you set or evaluate global variables |
| 650 |
in SAL, these are exactly the same Lisp global variables. Thus, XLISP |
| 651 |
functions can call SAL functions and vice-versa. At run time, |
| 652 |
everything is Lisp.</p> |
| 653 |
<a name = "79"><h4>Function Calls</h4></a> |
| 654 |
<p>In general, there is a very simple translation from SAL to Lisp syntax |
| 655 |
and back. A function call is SAL, for example, |
| 656 |
</p> |
| 657 |
<pre> |
| 658 |
osc(g4, 2.0) |
| 659 |
</pre> |
| 660 |
|
| 661 |
<p> |
| 662 |
is translated to Lisp by moving the open parenthesis in front of the |
| 663 |
function name and removing the commas: |
| 664 |
</p> |
| 665 |
<pre> |
| 666 |
(osc g4 2.0) |
| 667 |
</pre> |
| 668 |
|
| 669 |
<p> |
| 670 |
Similarly, if you want to translate a Lisp function call to SAL, just |
| 671 |
reverse the translation.</p> |
| 672 |
<a name = "80"><h4>Symbols and Functions</h4></a> |
| 673 |
<p>SAL translates keywords with trailing colons (such as <code>foo:</code>) |
| 674 |
into Lisp keywords with leading colons (such as <code>:foo</code>), but |
| 675 |
SAL keywords are not treated as expressions as they are in Lisp. |
| 676 |
You cannot write <code>open("myfile.txt", direction: output:)</code> |
| 677 |
because SAL expects an expression after direction. A special form |
| 678 |
<code>keyword</code> is defined to generate a Lisp keyword as an |
| 679 |
expression. The argument is the keyword <i>without</i> a colon, e.g. |
| 680 |
<code>open("myfile.txt", direction: keyword(output))</code>. Alternatively, |
| 681 |
you can write the Lisp-style keyword with the leading colon, e.g. |
| 682 |
<code>open("myfile.txt", direction: :output)</code>.</p> |
| 683 |
|
| 684 |
<p>In Nyquist SAL, the hash character (#), can be used as a prefix to a |
| 685 |
Lisp function name. For example, the following command is not legal |
| 686 |
because <code>print</code> is a SAL command name, not a legal function name: |
| 687 |
</p> |
| 688 |
<pre>set v = append(print(a), print(b))</pre> |
| 689 |
|
| 690 |
<p> |
| 691 |
(Here the intent is to print |
| 692 |
arguments to append). However, you can use the hash character to access |
| 693 |
the Lisp <code>print</code> function: |
| 694 |
</p> |
| 695 |
<pre>set v = append(#print(a), #print(b))</pre> |
| 696 |
|
| 697 |
<p> |
| 698 |
<a name = "81"><h4>Playing Tricks On the SAL Compiler</h4></a></p> |
| 699 |
|
| 700 |
<p>In many cases, the close coupling between SAL and XLISP gives SAL |
| 701 |
unexpected expressive power. A good example is <code>seqrep</code>. This |
| 702 |
is a special looping construct in Nyquist, implemented as a macro in |
| 703 |
XLISP. In Lisp, you would write something like: |
| 704 |
</p> |
| 705 |
<pre> |
| 706 |
(seqrep (i 10) (pluck c4)) |
| 707 |
</pre> |
| 708 |
|
| 709 |
<p> |
| 710 |
One might expect SAL would have to define a special <code>seqrep</code> |
| 711 |
statement to express this, but since statements do not return values, |
| 712 |
this approach would be problematic. The solution (which is already |
| 713 |
fully implemented in Nyquist) is to define a |
| 714 |
new macro <code>sal-seqrep</code> that is equivalent to <code>seqrep</code> |
| 715 |
except that it is called as follows: |
| 716 |
</p> |
| 717 |
<pre> |
| 718 |
(sal-seqrep i 10 (pluck c4)) |
| 719 |
</pre> |
| 720 |
|
| 721 |
<p> |
| 722 |
The SAL compiler automatically translates the identifier <code>seqrep</code> to |
| 723 |
<code>sal-seqrep</code>. Now, in SAL, you can just write |
| 724 |
</p> |
| 725 |
<pre> |
| 726 |
seqrep(i, 10, pluck(c4)) |
| 727 |
</pre> |
| 728 |
|
| 729 |
<p> |
| 730 |
which is translated in a pretty much semantics-unaware fashion to |
| 731 |
</p> |
| 732 |
<pre> |
| 733 |
(sal-seqrep i 10 (pluck c4)) |
| 734 |
</pre> |
| 735 |
|
| 736 |
<p> |
| 737 |
and viola!, we have Nyquist control constructs in SAL even though SAL |
| 738 |
is completely unaware that <code>seqrep</code> is actually a special form.</p> |
| 739 |
<hr> |
| 740 |
<a href = "part6.html">Previous Section</a> | <a href = "part8.html">Next Section</a> | <a href = "title.html#toc">Table of Contents</a> | <a href = "indx.html">Index</a> | <a href = "title.html">Title Page</a> |
| 741 |
</body></html> |