2014-11-12 21:22:02 +03:00
|
|
|
---
|
|
|
|
language: forth
|
|
|
|
contributors:
|
|
|
|
- ["Horse M.D.", "http://github.com/HorseMD/"]
|
|
|
|
filename: learnforth.fs
|
|
|
|
---
|
|
|
|
|
2014-11-14 02:37:07 +03:00
|
|
|
Forth was created by Charles H. Moore in the 70s. It is an imperative,
|
|
|
|
stack-based language and programming environment, being used in projects
|
|
|
|
such as Open Firmware. It's also used by NASA.
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
Note: This article focuses predominantly on the Gforth implementation of
|
|
|
|
Forth, but most of what is written here should work elsewhere.
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2015-01-02 23:33:47 +03:00
|
|
|
```
|
2014-11-14 13:24:39 +03:00
|
|
|
\ This is a comment
|
|
|
|
( This is also a comment but it's only used when defining words )
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 01:25:37 +03:00
|
|
|
\ --------------------------------- Precursor ----------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ All programming in Forth is done by manipulating the parameter stack (more
|
|
|
|
\ commonly just referred to as "the stack").
|
2014-11-14 02:50:54 +03:00
|
|
|
5 2 3 56 76 23 65 \ ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ Those numbers get added to the stack, from left to right.
|
2014-11-13 19:10:56 +03:00
|
|
|
.s \ <7> 5 2 3 56 76 23 65 ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ In Forth, everything is either a word or a number.
|
2014-11-12 21:22:02 +03:00
|
|
|
|
|
|
|
\ ------------------------------ Basic Arithmetic ------------------------------
|
|
|
|
|
2014-11-13 19:10:56 +03:00
|
|
|
\ Arithmetic (in fact most words requiring data) works by manipulating data on
|
|
|
|
\ the stack.
|
2014-11-12 21:22:02 +03:00
|
|
|
5 4 + \ ok
|
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ `.` pops the top result from the stack:
|
2014-11-12 21:22:02 +03:00
|
|
|
. \ 9 ok
|
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ More examples of arithmetic:
|
2014-11-14 02:37:07 +03:00
|
|
|
6 7 * . \ 42 ok
|
|
|
|
1360 23 - . \ 1337 ok
|
|
|
|
12 12 / . \ 1 ok
|
2014-11-14 13:24:39 +03:00
|
|
|
13 2 mod . \ 1 ok
|
|
|
|
|
|
|
|
99 negate . \ -99 ok
|
|
|
|
-99 abs . \ 99 ok
|
|
|
|
52 23 max . \ 52 ok
|
|
|
|
52 23 min . \ 23 ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ ----------------------------- Stack Manipulation -----------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ Naturally, as we work with the stack, we'll want some useful methods:
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 03:13:54 +03:00
|
|
|
3 dup - \ duplicate the top item (1st now equals 2nd): 3 - 3
|
|
|
|
2 5 swap / \ swap the top with the second element: 5 / 2
|
2014-11-14 02:49:06 +03:00
|
|
|
6 4 5 rot .s \ rotate the top 3 elements: 4 5 6
|
2014-11-13 03:13:54 +03:00
|
|
|
4 0 drop 2 / \ remove the top item (dont print to screen): 4 / 2
|
2014-11-14 14:13:39 +03:00
|
|
|
1 2 3 nip .s \ remove the second item (similar to drop): 1 3
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ ---------------------- More Advanced Stack Manipulation ----------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 03:19:20 +03:00
|
|
|
1 2 3 4 tuck \ duplicate the top item into the second slot: 1 2 4 3 4 ok
|
|
|
|
1 2 3 4 over \ duplicate the second item to the top: 1 2 3 4 3 ok
|
|
|
|
1 2 3 4 2 roll \ *move* the item at that position to the top: 1 3 4 2 ok
|
|
|
|
1 2 3 4 2 pick \ *duplicate* the item at that position to the top: 1 2 3 4 2 ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 03:02:44 +03:00
|
|
|
\ When referring to stack indexes, they are zero-based.
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ ------------------------------ Creating Words --------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ The `:` word sets Forth into compile mode until it sees the `;` word.
|
2014-11-14 13:51:22 +03:00
|
|
|
: square ( n -- n ) dup * ; \ ok
|
2014-11-14 15:12:30 +03:00
|
|
|
5 square . \ 25 ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:51:22 +03:00
|
|
|
\ We can view what a word does too:
|
2014-11-14 15:12:30 +03:00
|
|
|
see square \ : square dup * ; ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ -------------------------------- Conditionals --------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 02:25:48 +03:00
|
|
|
\ -1 == true, 0 == false. However, any non-zero value is usually treated as
|
|
|
|
\ being true:
|
2014-11-14 02:01:42 +03:00
|
|
|
42 42 = \ -1 ok
|
|
|
|
12 53 = \ 0 ok
|
2014-11-13 00:56:13 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ `if` is a compile-only word. `if` <stuff to do> `then` <rest of program>.
|
2014-11-14 03:05:48 +03:00
|
|
|
: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" then ; \ ok
|
2014-11-13 00:56:13 +03:00
|
|
|
100 ?>64 \ Greater than 64! ok
|
|
|
|
|
2014-11-13 03:02:44 +03:00
|
|
|
\ Else:
|
2014-11-14 03:05:48 +03:00
|
|
|
: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" else ." Less than 64!" then ;
|
2014-11-13 19:26:38 +03:00
|
|
|
100 ?>64 \ Greater than 64! ok
|
|
|
|
20 ?>64 \ Less than 64! ok
|
2014-11-13 00:56:13 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ ------------------------------------ Loops -----------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ `do` is also a compile-only word.
|
2014-11-13 01:33:04 +03:00
|
|
|
: myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok
|
2014-11-14 02:01:42 +03:00
|
|
|
myloop
|
2014-11-13 01:33:04 +03:00
|
|
|
\ Hello!
|
|
|
|
\ Hello!
|
|
|
|
\ Hello!
|
|
|
|
\ Hello!
|
|
|
|
\ Hello! ok
|
|
|
|
|
2014-11-14 13:51:22 +03:00
|
|
|
\ `do` expects two numbers on the stack: the end number and the start number.
|
2014-11-13 01:33:04 +03:00
|
|
|
|
2014-11-14 02:49:06 +03:00
|
|
|
\ We can get the value of the index as we loop with `i`:
|
2014-11-13 19:26:38 +03:00
|
|
|
: one-to-12 ( -- ) 12 0 do i . loop ; \ ok
|
|
|
|
one-to-12 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok
|
2014-11-14 13:51:22 +03:00
|
|
|
|
|
|
|
\ `?do` works similarly, except it will skip the loop if the end and start
|
|
|
|
\ numbers are equal.
|
2014-11-14 19:49:25 +03:00
|
|
|
: squares ( n -- ) 0 ?do i square . loop ; \ ok
|
|
|
|
10 squares \ 0 1 4 9 16 25 36 49 64 81 ok
|
2014-11-13 01:33:04 +03:00
|
|
|
|
2014-11-13 03:02:44 +03:00
|
|
|
\ Change the "step" with `+loop`:
|
2014-11-14 13:51:22 +03:00
|
|
|
: threes ( n n -- ) ?do i . 3 +loop ; \ ok
|
2014-11-14 13:24:39 +03:00
|
|
|
15 0 threes \ 0 3 6 9 12 ok
|
2014-11-13 01:33:04 +03:00
|
|
|
|
2015-10-09 22:59:25 +03:00
|
|
|
\ Indefinite loops with `begin` <stuff to do> <flag> `until`:
|
2014-11-14 02:49:06 +03:00
|
|
|
: death ( -- ) begin ." Are we there yet?" 0 until ; \ ok
|
2014-11-13 01:33:04 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ ---------------------------- Variables and Memory ----------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 02:25:48 +03:00
|
|
|
\ Use `variable` to declare `age` to be a variable.
|
2014-11-14 02:49:06 +03:00
|
|
|
variable age \ ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 02:55:50 +03:00
|
|
|
\ Then we write 21 to age with the word `!`.
|
2014-11-14 02:49:06 +03:00
|
|
|
21 age ! \ ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
2014-11-14 02:25:48 +03:00
|
|
|
\ Finally we can print our variable using the "read" word `@`, which adds the
|
2014-11-13 19:26:38 +03:00
|
|
|
\ value to the stack, or use `?` that reads and prints it in one go.
|
2014-11-18 15:01:59 +03:00
|
|
|
age @ . \ 21 ok
|
|
|
|
age ? \ 21 ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ Constants are quite simiar, except we don't bother with memory addresses:
|
2014-11-14 02:49:06 +03:00
|
|
|
100 constant WATER-BOILING-POINT \ ok
|
|
|
|
WATER-BOILING-POINT . \ 100 ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
2014-11-14 01:25:37 +03:00
|
|
|
\ ----------------------------------- Arrays -----------------------------------
|
2014-11-13 02:55:50 +03:00
|
|
|
|
2014-11-18 15:19:12 +03:00
|
|
|
\ Creating arrays is similar to variables, except we need to allocate more
|
|
|
|
\ memory to them.
|
|
|
|
|
|
|
|
\ You can use `2 cells allot` to create an array that's 3 cells long:
|
2014-11-14 02:49:06 +03:00
|
|
|
variable mynumbers 2 cells allot \ ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ Initialize all the values to 0
|
2014-11-14 02:49:06 +03:00
|
|
|
mynumbers 3 cells erase \ ok
|
2014-11-14 13:24:39 +03:00
|
|
|
|
|
|
|
\ Alternatively we could use `fill`:
|
|
|
|
mynumbers 3 cells 0 fill
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ or we can just skip all the above and initialize with specific values:
|
2014-11-14 02:49:06 +03:00
|
|
|
create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!)
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ ...which is equivalent to:
|
|
|
|
|
2014-11-14 14:13:39 +03:00
|
|
|
\ Manually writing values to each index:
|
2014-11-14 02:49:06 +03:00
|
|
|
64 mynumbers 0 cells + ! \ ok
|
|
|
|
9001 mynumbers 1 cells + ! \ ok
|
|
|
|
1337 mynumbers 2 cells + ! \ ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ Reading values at certain array indexes:
|
2014-11-14 02:49:06 +03:00
|
|
|
0 cells mynumbers + ? \ 64 ok
|
|
|
|
1 cells mynumbers + ? \ 9001 ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
2014-11-14 14:23:14 +03:00
|
|
|
\ We can simplify it a little by making a helper word for manipulating arrays:
|
|
|
|
: of-arr ( n n -- n ) cells + ; \ ok
|
|
|
|
mynumbers 2 of-arr ? \ 1337 ok
|
2014-11-14 14:13:39 +03:00
|
|
|
|
|
|
|
\ Which we can use for writing too:
|
2014-11-14 14:23:14 +03:00
|
|
|
20 mynumbers 1 of-arr ! \ ok
|
|
|
|
mynumbers 1 of-arr ? \ 20 ok
|
2014-11-13 02:55:50 +03:00
|
|
|
|
|
|
|
\ ------------------------------ The Return Stack ------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 02:25:48 +03:00
|
|
|
\ The return stack is used to the hold pointers to things when words are
|
|
|
|
\ executing other words, e.g. loops.
|
2014-11-14 01:16:48 +03:00
|
|
|
|
|
|
|
\ We've already seen one use of it: `i`, which duplicates the top of the return
|
|
|
|
\ stack. `i` is equivalent to `r@`.
|
2014-11-14 02:49:06 +03:00
|
|
|
: myloop ( -- ) 5 0 do r@ . loop ; \ ok
|
2014-11-14 01:16:48 +03:00
|
|
|
|
|
|
|
\ As well as reading, we can add to the return stack and remove from it:
|
2014-11-14 02:49:06 +03:00
|
|
|
5 6 4 >r swap r> .s \ 6 5 4 ok
|
2014-11-14 01:16:48 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ NOTE: Because Forth uses the return stack for word pointers, `>r` should
|
|
|
|
\ always be followed by `r>`.
|
2014-11-14 01:16:48 +03:00
|
|
|
|
2014-11-14 01:25:37 +03:00
|
|
|
\ ------------------------- Floating Point Operations --------------------------
|
2014-11-14 01:16:48 +03:00
|
|
|
|
2014-11-14 13:24:39 +03:00
|
|
|
\ Most Forths tend to eschew the use of floating point operations.
|
2014-11-14 01:16:48 +03:00
|
|
|
8.3e 0.8e f+ f. \ 9.1 ok
|
|
|
|
|
2014-11-14 02:25:48 +03:00
|
|
|
\ Usually we simply prepend words with 'f' when dealing with floats:
|
2014-11-14 02:49:06 +03:00
|
|
|
variable myfloatingvar \ ok
|
|
|
|
4.4e myfloatingvar f! \ ok
|
|
|
|
myfloatingvar f@ f. \ 4.4 ok
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-13 19:26:38 +03:00
|
|
|
\ --------------------------------- Final Notes --------------------------------
|
2014-11-12 21:22:02 +03:00
|
|
|
|
2014-11-14 02:57:02 +03:00
|
|
|
\ Typing a non-existent word will empty the stack. However, there's also a word
|
|
|
|
\ specifically for that:
|
|
|
|
clearstack
|
2014-11-14 02:16:26 +03:00
|
|
|
|
2014-11-14 13:51:22 +03:00
|
|
|
\ Clear the screen:
|
|
|
|
page
|
|
|
|
|
2014-11-14 02:59:37 +03:00
|
|
|
\ Loading Forth files:
|
|
|
|
\ s" forthfile.fs" included
|
|
|
|
|
2014-11-14 13:51:22 +03:00
|
|
|
\ You can list every word that's in Forth's dictionary (but it's a huge list!):
|
|
|
|
\ words
|
|
|
|
|
2014-11-14 02:59:37 +03:00
|
|
|
\ Exiting Gforth:
|
|
|
|
\ bye
|
2014-11-12 21:22:02 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
##Ready For More?
|
|
|
|
|
|
|
|
* [Starting Forth](http://www.forth.com/starting-forth/)
|
2014-11-14 02:16:26 +03:00
|
|
|
* [Simple Forth](http://www.murphywong.net/hello/simple.htm)
|
2014-11-12 21:22:02 +03:00
|
|
|
* [Thinking Forth](http://thinking-forth.sourceforge.net/)
|