-
Notifications
You must be signed in to change notification settings - Fork 3
/
firth.html
503 lines (437 loc) · 13.1 KB
/
firth.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://unpkg.com/[email protected]">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/prism">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>The Little Man Stack Machine</title>
</head>
<body>
<main>
<h1 class="massivetext">🙋 The Little Man Stack Machine</h1>
<nav>
<p class="tool-bar">
<a href="index.html">Intro</a>
<a href="assembly.html">Assembly & Execution</a>
<a href="functions.html">Functions</a>
<a href="firth.html">Firth</a>
<a href="emulator.html">Emulator</a>
<a href="https://github.com/bigskysoftware/littlemanstackmachine.org">Github</a>
</p>
</nav>
<h1>Firth: A High Level Stack Oriented Language</h1>
<p>
A design goal of the LMSM is to provide <em>just</em> enough infrastructure such that a small,
<a href="https://en.wikipedia.org/wiki/High-level_programming_language"> high-level programming language </a>
can be built for it relatively easily. Because of the stack instructions that the LMSM adds to the LMC,
a <a href="https://en.wikipedia.org/wiki/Stack-oriented_programming">stack-oriented</a> programming
language makes the most sense.
</p>
<p>Stack languages are not popular today, but in the past there was a lot of interest in them, and they provide
an interesting, alternative way to think about programming.</p>
<p>
The programming language for the LSMS is called "Firth", which is, to an extent, based on the
<a href="https://en.wikipedia.org/wiki/Forth_(programming_language)">Forth</a> programming language. Similar to
Forth, Firth mainly uses a <a href="https://en.wikipedia.org/wiki/Reverse_Polish_notation">Reverse Polish notation</a>
style of syntax, and the concept of an implicit stack.
</p>
<p>
It is recommended that a Firth compiler is provided with implementations of the LMSM, to show how a high-level
programming language can compile down to LMSM assembly.
</p>
<h2>An Example Firth Program: Squaring A Number</h2>
<p>
Before we delve into the details of Firth, let's look at an example program, based on the <code>SQUARE</code>
assembly program we looked at on the <a href="assembly.html">LMSM Assembly</a> page. The Firth program
that would compile down to this assembly looks like this:
</p>
<pre>
get
square()
.
def square()
dup *
end
</pre>
<p>
This program probably looks a little strange to you, so let's go through it line by line.
</p>
<p>
The first line, <code>get</code>, gets an integer from the user and pushes it onto the top of the value stack.
</p>
<p>
The second line, <code>square()</code>, invokes the function <code>square</code>. Note that the argument to
the <code>square</code> function is passed <em>implicitly</em>, on the value stack. A little crazy but
this is how Forth and many stack-based programming languages work.
</p>
<p>
The third line, <code>.</code>, prints the top of the stack out.
</p>
<p>
Next comes the definition of the function <code>square()</code>. Its body consists of a single line: <code>dup *</code>.
These two "words" are the Firth way of saying "Duplicate the top of the value stack and then replace the top two
values of the value stack with the result of multiplying them together."
</p>
<p>
This is the "Reverse Polish notation" nature of Firth. In a more traditional programming language, this
calculation would look something like this:
</p>
<pre>
def square(x)
x * x
end
</pre>
<p>
In this code the multiplication operator uses <a href="https://en.wikipedia.org/wiki/Infix_notation">Infix notation</a>.
Firth, instead, uses the RPN style, where you "push" values onto a stack and then apply operations to them.
</p>
<p>
Note that there is no explicit <code>return</code> in the definition of the <code>square()</code>
function. There is actually support for the <code>return</code> keyword in Firth, but it isn't necessary here:
the square value is computed by the body of the function and left on the value stack, so when the function returns
the return value is where it should be.
</p>
<p>
Kind of a hack, but it works, and this program will do what we want: ask the user for a number and then print
the square of it.
</p>
<p>
So that's a quick introduction to the Firth programming language. Now let's look at all the elements of
the language in more detail.
</p>
<h2>Elements Of The Firth Programming Langauge</h2>
<p>
The Firth programming language consists of a set of <em>elements</em>. Calling them "expressions" or "statements"
in the traditional sense of those terms is a little misleading since values are stored on a stack, so we will
stick with the term "element".
</p>
<h3>Basic Elements</h3>
<p>Firth supports the following "basic" elements:</p>
<table style="width: 100%">
<thead>
<tr>
<th>
Element
</th>
<th>
Example
</th>
<th>
Assembly
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Numbers
</td>
<td>
<code>3</code>
</td>
<td>
<code>LDI 3 SPUSH</code>
</td>
</tr>
<tr>
<td>
Plus
</td>
<td>
<code>2 3 +</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SADD</code>
</td>
</tr>
<tr>
<td>
Minus
</td>
<td>
<code>2 3 -</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SSUB</code>
</td>
</tr>
<tr>
<td>
Multiplication
</td>
<td>
<code>2 3 *</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SMUL</code>
</td>
</tr>
<tr>
<td>
Division
</td>
<td>
<code>2 3 /</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SDIV</code>
</td>
</tr>
<tr>
<td>
Maximum
</td>
<td>
<code>2 3 max</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SMAX</code>
</td>
</tr>
<tr>
<td>
Minimum
</td>
<td>
<code>2 3 min</code>
</td>
<td>
<code>LDI 2 SPUSH LDI 3 SPUSH SMIN</code>
</td>
</tr>
</tbody>
</table>
<h3>Stack Elements</h3>
<p>Firth supports the following elements to operate on the value stack:</p>
<table style="width: 100%">
<thead>
<tr>
<th>
Element
</th>
<th>
Example
</th>
<th>
Assembly
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Pop The Top Of The Stack
</td>
<td>
<code>pop</code>
</td>
<td>
<code>SPOP</code>
</td>
</tr>
<tr>
<td>
Duplicate Top Of Stack
</td>
<td>
<code>dup</code>
</td>
<td>
<code>SDUP</code>
</td>
</tr>
<tr>
<td>
Swap The Top Two Elements On The Stack
</td>
<td>
<code>swap</code>
</td>
<td>
<code>SSWAP</code>
</td>
</tr>
<tr>
<td>
Drop The Top Element Of The Stack
</td>
<td>
<code>drop</code>
</td>
<td>
<code>SDROP</code>
</td>
</tr>
</tbody>
</table>
<p>
The difference between <code>pop</code> and <code>drop</code> is that <code>pop</code> leaves the top value
of the stack in the accumulator, whereas <code>drop</code> does not.
</p>
<h3>I/O</h3>
<p>Firth supports the following elements for IO:</p>
<table style="width: 100%">
<thead>
<tr>
<th>
Element
</th>
<th>
Example
</th>
<th>
Assembly
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Get
</td>
<td>
<code>get</code>
</td>
<td>
<code>INP SPUSH</code>
</td>
</tr>
<tr>
<td>
Print
</td>
<td>
<code>.</code>
</td>
<td>
<code>SDUP POP OUT</code>
</td>
</tr>
</tbody>
</table>
<h3>Conditional Elements</h3>
<p>
Firth supports two conditional statements: <code>zero?</code> and <code>positive?</code>. These elements
consume the top of the stack. If the condition is true of the top of the stack, they execute the elements in
their body, which is terminated by an <code>end</code> keyword.
</p>
<p>
If the condition is not true, the elements will execute an optional <code>else</code> block, if it is included,
otherwise they will do nothing.
</p>
<p>
Here is a simple "inverter" example Firth program that asks the user for some input and then prints
<code>1</code> if the user enter<code>0</code> and <code>1</code> otherwise.
</p>
<pre>
get
zero?
1
else
0
end
.
</pre>
<h3>The Loop Element</h3>
<p>
Firth supports a simple loop that starts with the <code>do</code> keyword and ends with the <code>loop</code>
keyword. A loop can be exited with the <code>stop</code> keyword.
</p>
<p>
Here is an example of a loop that runs until the user enters a zero value:
</p>
<pre>
do
get
zero?
stop
end
loop
</pre>
<h3>Global Variables</h3>
<p>
Firth supports global variables. They can be declared using the <code>var</code> keyword:
</p>
<code>
var x
</code>
<p>
Variables can be set by appending an exclamation point to their name. This will consume the top of the stack
and set the variable to the value. The following program declares a variable <code>x</code>, saves user input
into it, multiplies the variable by itself and then prints the result:
</p>
<pre>
var x
get
x!
x x *
.
</pre>
<p>Variables must be declared first in Firth programs, and are global. They cannot be declared within functions,
loops or conditionals.</p>
<h3>Functions</h3>
<p>
Functions can be declared using the <code>def</code> keyword, followed by a function name. All function names
must end in the characters <code>()</code>. Note that you cannot declare parameters for a function: they
are passed implicitly on the stack.
</p>
<p>
The implementation of the function consists of a series of elements, followed by the keyword <code>end</code>:
</p>
<pre>
def square()
dup *
end
</pre>
<p>
Functions can return to a caller by using the <code>return</code> element. The <code>return</code> element
is optional: a function will implicitly return when its body completes.
</p>
<p>
Note that functions in Firth can call themselves recursively.
</p>
<h3>Inline Assembly</h3>
<p>
LMSM assembly can be included in a Firth program by using the <code>asm</code> keyword, followed by the
<code>end</code> keyword.
</p>
<p>
Here is some Firth code that uses raw assembly to print out the value stored in the accumulator:
</p>
<pre>
get
pop
asm
OUT
end
</pre>
<h2>Fibbonacci.firth</h2>
<p>
Below is a recursive implementation of the <a href="https://en.wikipedia.org/wiki/Fibonacci_number">fibonacci</a>
algorithm in Firth:
</p>
<pre>
get
fib()
.
def fib()
dup
zero?
return
end
dup 1 -
zero?
return
end
dup 2 -
fib()
swap 1 -
fib()
+
end
</pre>
</main>
<footer>
</footer>
</body>
</html>