From aea5e2eb1b255457a2411358a4275d473d191536 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Wed, 12 Nov 2014 18:22:02 +0000 Subject: Add basic outline of Forth and explaination of simple concepts. --- forth.html.markdown | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 forth.html.markdown (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown new file mode 100644 index 00000000..de0e18c2 --- /dev/null +++ b/forth.html.markdown @@ -0,0 +1,136 @@ +--- +language: forth +contributors: + - ["Horse M.D.", "http://github.com/HorseMD/"] +filename: learnforth.fs +--- + +Forth was created by Charles H. Moore in the 70s. + +Note: This article focuses predominantly on the Gforth implementation of Forth, but most +of what is written here should work elsewhere. + +> If Lisp is the ultimate high level language, Forth is the ultimate low level language. + +```forth + +\ Forth is an interactive programming language which is comprised of *words*. These are +\ Forth subroutines which are executed once you press , from left to right. + +\ ------------------------------ Precursor ------------------------------ + +\ It's important to know how forth processes instructions. All programming in Forth is +\ done by manipulating what's known as the parameter stack (more commonly just referred +\ to as "the stack"). The stack is a typical last-in-first-out (LIFO) stack. Typing: + +5 2 3 56 76 23 65 + +\ Means 5 gets put on the stack first, then 2, then 3, etc all the way to 65, which +\ is now at the top of the stack. We can see the length and contents of the stack by +\ passing forth the word `.s`: + +.s <7> 5 2 3 56 76 23 65 \ ok + +\ Forth's interpreter interprets what you type in one of two ways: as *words* (i.e. the +\ name of subroutines) or as *numbers*. Words are essentially "symbols that do things". + +\ Finally, as the stack is LIFO, we obviously must use postfix notation to manipulate +\ the stack. This should become clear shortly. + +\ ------------------------------ Basic Arithmetic ------------------------------ + +\ Lets do a simple equation: adding 5 and 4. In infix notation this would be 5 + 4, +\ but as forth works in postfix (see above about stack manipulation) we input it like so: + +5 4 + \ ok + +\ However, this alone yields "ok", yet no answer. Why? The way forth interprets what +\ we typed is as such: 5 gets added to the top of the stack, and then 4. Finally, +\ it runs word `+` on the stack (which pops the top and second value, and adds them), +\ and inserts the result at the top of the stack. Typing the word `.` will yield +\ the result. + +. \ 9 ok + +\ This should illustrate the fundamentals of forth. Lets do a few more arithmetic +\ tests: + +6 7 * . \ 42 ok +1360 23 - . \ 1337 ok +12 12 / . \ 1 ok + +\ And so on. + +\ ------------------------------ More Advanced Stack Maniulation ------------------------------ + +\ Naturally, as we do so much work with the stack, we'll want some useful methods. + +drop \ drop (remove) the item at the top of the stack (note the difference between this and `.`) +dup \ duplicate the item on top the stack +rot \ rotate the top three items (third -> first, first -> second, second -> third) +swap \ swaps the top item with the second item + +\ Examples: + +dup * \ square the top item +2 5 dup * swap / \ half the top item squared +6 4 5 rot * - \ sometimes we just want to reorganize +4 0 drop 2 / \ add 4 and 0, remove 0 and divide the top by 2 + +\ ------------------------------ Extra Stack Manipulation ------------------------------ + +tuck \ acts like dup, except it duplicates the top item into the 3rd* position in the stack +over \ duplicate the second item to the top of the stack +n roll \ where n is a number, *move* the stack item at that position to the top of the stack +n pick \ where n is a number, *duplicate* the item at that position to the top of the stack + +\ 3rd*: when referring to stack indexes, they are zero-based - i.e. the first element is at +\ position 0, the second element is at position 1, etc... Just like indexing arrays in +\ most other languages. + +\ ------------------------------ Creating Words ------------------------------ + +\ Quite often one will want to write their own words. + +: square ( n -- n ) dup * ; \ ok + +\ Lets break this down. The `:` word says to Forth to enter "compile" mode. After that, +\ we tell Forth what our word is called - "square". Between the parentheses we have a +\ comment depicting what this word does to the stack - it takes a number and adds a +\ number. Finally, we have what the word does, until we reach the `;` word which +\ says that you've finished your definition, Forth will add this to the dictionary and +\ switch back into interpret mode. + +\ We can check the definition of a word with the `see` word: + +see square \ dup * ; ok + +\ ------------------------------ Conditionals ------------------------------ + +\ TODO + +\ ------------------------------ Loops ------------------------------ + +\ TODO + +\ ------------------------------ The Return Stack ------------------------------ + +\ TODO + +\ ------------------------------ Variables and Memory ------------------------------ + +\ TODO + +\ ------------------------------ Final Notes ------------------------------ + +\ Booleans +\ Floats +\ Commenting (types) +\ bye + +``` + +##Ready For More? + +* [Starting Forth](http://www.forth.com/starting-forth/) +* [Thinking Forth](http://thinking-forth.sourceforge.net/) -- cgit v1.2.3 From 5b91f96781a153faa5a129514cce8dcca3f24c54 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Wed, 12 Nov 2014 21:56:13 +0000 Subject: Finished conditionals section for now. --- forth.html.markdown | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index de0e18c2..bea7cf38 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -61,7 +61,7 @@ of what is written here should work elsewhere. \ And so on. -\ ------------------------------ More Advanced Stack Maniulation ------------------------------ +\ ------------------------------ Stack Maniulation ------------------------------ \ Naturally, as we do so much work with the stack, we'll want some useful methods. @@ -77,7 +77,7 @@ dup * \ square the top item 6 4 5 rot * - \ sometimes we just want to reorganize 4 0 drop 2 / \ add 4 and 0, remove 0 and divide the top by 2 -\ ------------------------------ Extra Stack Manipulation ------------------------------ +\ ------------------------------ More Advanced Stack Manipulation ------------------------------ tuck \ acts like dup, except it duplicates the top item into the 3rd* position in the stack over \ duplicate the second item to the top of the stack @@ -107,7 +107,28 @@ see square \ dup * ; ok \ ------------------------------ Conditionals ------------------------------ -\ TODO +\ Booleans: +\ In forth, -1 is used to represent truth, and 0 is used to represent false. +\ The idea behind this is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. +\ However, any non-zero value is usually treated as being true. + +42 42 = / -1 ok +12 53 = / 0 ok + +\ `if` is a compile-only word. This means that it can *only* be used when we're compiling a word. +\ when creating conditionals, the format is `if` `then` . + +: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok +100 ?>64 \ Greater than 64! ok + +\ This unimaginative example displays "Greater than 64!" when the number on the stack is greater +\ than 64. However, it does nothing when the test is false. Let's fix that with the `else` word! + +: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; \ ok +100 ?>64 \ Greater than 64! ok +20 ?>64 \ Less than 64! ok + +\ As you can see, conditionals behave more or less like they do in most programming languages. \ ------------------------------ Loops ------------------------------ -- cgit v1.2.3 From c63b9d0153710077d48c3c7ab16b3d848c97f8e9 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Wed, 12 Nov 2014 22:33:04 +0000 Subject: Add outline of loops. --- forth.html.markdown | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index bea7cf38..ee491e7a 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -110,12 +110,12 @@ see square \ dup * ; ok \ Booleans: \ In forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea behind this is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. -\ However, any non-zero value is usually treated as being true. +\ However, any non-zero value is usually treated as being true: 42 42 = / -1 ok 12 53 = / 0 ok -\ `if` is a compile-only word. This means that it can *only* be used when we're compiling a word. +\ `if` is a *compile-only word*. This means that it can *only* be used when we're compiling a word. \ when creating conditionals, the format is `if` `then` . : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok @@ -132,7 +132,41 @@ see square \ dup * ; ok \ ------------------------------ Loops ------------------------------ -\ TODO +\ `do` is like `if` in that it is also a compile-only word, though it uses `loop` as its +\ terminator. + +: myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok +test +\ Hello! +\ Hello! +\ Hello! +\ Hello! +\ Hello! ok + +\ `do` expects two numbers before it: the end number and the index number, respectively. +\ (cr means carraige-return, essentially it a newline). This is equivalent to a for-loop +\ in other languages, with a definite number of times to loop. + +\ So what if we want to get the value of the index as we loop? We use `i`. + +: one-to-15 ( -- ) 15 0 do i . loop ; \ ok +one-to-15 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ok +: squares ( -- ) 10 0 do i DUP * . loop ; \ ok +squares \ 0 1 4 9 16 25 36 49 64 81 ok + +\ Thidly, we can also change how large the step is between each loop iteration with `+loop`. +\ `+loop` reads the number on the top of the stack for how far to move each iteration. + +: threes ( -- ) 15 0 do i . 3 +loop ; \ ok +threes \ 0 3 6 9 12 ok + +\ Finally, while loops: + +: death ( -- ) begin ." Are we there yet?" 0 until ; + +\ Will print "Are we there yet?" forever. While loops are constructed in the format +\ of `begin` `until`. The loop will run until flag is a +\ truthy value (not 0). \ ------------------------------ The Return Stack ------------------------------ -- cgit v1.2.3 From cc8eb6fed6476f1f387b7a3a31d69c61e6e2efb3 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Wed, 12 Nov 2014 23:55:50 +0000 Subject: Finished outline of Variables and Memory. Must be more vague, this is a whirlwind tour! --- forth.html.markdown | 55 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index ee491e7a..06c2c6dc 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -168,17 +168,64 @@ threes \ 0 3 6 9 12 ok \ of `begin` `until`. The loop will run until flag is a \ truthy value (not 0). -\ ------------------------------ The Return Stack ------------------------------ +\ ------------------------------ Variables and Memory ------------------------------ -\ TODO +\ Sometimes we'll be in a situation where we want more permanent variables: +\ First, we use `variable` to declare `age` to be a variable. +variable age -\ ------------------------------ Variables and Memory ------------------------------ +\ Then we write 21 to age with the word `!`. +21 age ! + +\ Finally we can print our variable using the "read" word '@', which adds the value +\ to the stack, or use a handy word called `?` that reads and prints it in one go. +age @ . \ 12 ok +age ? \ 12 ok + +\ What's happening here is that `age` stores the memory address, and we use `!` +\ and `@` to manipulate it. + +\ Constants are quite simiar, except we don't bother with memory addresses: +100 constant WATER-BOILING-POINT \ ok +WATER-BOILING-POINT . \ 100 ok + +\ Arrays! + +\ Set up an array of length 3: +variable mynumbers 2 cells allot + +\ Initialize all the values to 0 +mynumbers 3 cells erase +\ (alternatively we could do `0 fill` instead of `erase`, but as we're setting +\ them to 0 we just use `erase`). + +\ or we can just skip all the above and initialize with specific values: + +create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! + +\ ...which is equivalent to: + +\ [64, 9001, 1337] +64 mynumbers 0 cells + ! +9001 mynumbers 1 cells + ! +1337 mynumbers 2 cells + ! + +\ Reading values at certain array indexes: +0 cells mynumbers + ? \ 64 ok +1 cells mynumbers + ? \ 9001 ok +2 cells mynumbers + ? \ 1337 ok + +\ Of course, you'll probably want to define your own words to manipulate arrays: +: ?mynumbers ( n -- n ) cells mynumbers + ; \ ok +64 mynumbers 2 cells + ! \ ok +2 ?mynumbers ? \ 64 ok + +\ ------------------------------ The Return Stack ------------------------------ \ TODO \ ------------------------------ Final Notes ------------------------------ -\ Booleans \ Floats \ Commenting (types) \ bye -- cgit v1.2.3 From ae2728d58009abfd7d50c2fbc346e46a58e0c2fc Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 00:02:44 +0000 Subject: Be more brief! --- forth.html.markdown | 51 ++++++++++++--------------------------------------- 1 file changed, 12 insertions(+), 39 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 06c2c6dc..7b805057 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -22,13 +22,11 @@ of what is written here should work elsewhere. \ It's important to know how forth processes instructions. All programming in Forth is \ done by manipulating what's known as the parameter stack (more commonly just referred \ to as "the stack"). The stack is a typical last-in-first-out (LIFO) stack. Typing: - 5 2 3 56 76 23 65 \ Means 5 gets put on the stack first, then 2, then 3, etc all the way to 65, which \ is now at the top of the stack. We can see the length and contents of the stack by \ passing forth the word `.s`: - .s <7> 5 2 3 56 76 23 65 \ ok \ Forth's interpreter interprets what you type in one of two ways: as *words* (i.e. the @@ -41,20 +39,13 @@ of what is written here should work elsewhere. \ Lets do a simple equation: adding 5 and 4. In infix notation this would be 5 + 4, \ but as forth works in postfix (see above about stack manipulation) we input it like so: - 5 4 + \ ok -\ However, this alone yields "ok", yet no answer. Why? The way forth interprets what -\ we typed is as such: 5 gets added to the top of the stack, and then 4. Finally, -\ it runs word `+` on the stack (which pops the top and second value, and adds them), -\ and inserts the result at the top of the stack. Typing the word `.` will yield +\ However, this alone yields "ok", yet no answer. Typing the word `.` will yield \ the result. - . \ 9 ok -\ This should illustrate the fundamentals of forth. Lets do a few more arithmetic -\ tests: - +\ This should illustrate how Forth's stack works. Lets do a few more arithmetic tests: 6 7 * . \ 42 ok 1360 23 - . \ 1337 ok 12 12 / . \ 1 ok @@ -84,14 +75,11 @@ over \ duplicate the second item to the top of the stack n roll \ where n is a number, *move* the stack item at that position to the top of the stack n pick \ where n is a number, *duplicate* the item at that position to the top of the stack -\ 3rd*: when referring to stack indexes, they are zero-based - i.e. the first element is at -\ position 0, the second element is at position 1, etc... Just like indexing arrays in -\ most other languages. +\ When referring to stack indexes, they are zero-based. \ ------------------------------ Creating Words ------------------------------ \ Quite often one will want to write their own words. - : square ( n -- n ) dup * ; \ ok \ Lets break this down. The `:` word says to Forth to enter "compile" mode. After that, @@ -102,39 +90,34 @@ n pick \ where n is a number, *duplicate* the item at that position to the top o \ switch back into interpret mode. \ We can check the definition of a word with the `see` word: - see square \ dup * ; ok \ ------------------------------ Conditionals ------------------------------ \ Booleans: \ In forth, -1 is used to represent truth, and 0 is used to represent false. -\ The idea behind this is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. +\ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. \ However, any non-zero value is usually treated as being true: 42 42 = / -1 ok 12 53 = / 0 ok -\ `if` is a *compile-only word*. This means that it can *only* be used when we're compiling a word. -\ when creating conditionals, the format is `if` `then` . +\ `if` is a *compile-only word*. This means that it can only be used when we're compiling a word. +\ when creating conditionals, the format is `if` `then` . : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok -\ This unimaginative example displays "Greater than 64!" when the number on the stack is greater -\ than 64. However, it does nothing when the test is false. Let's fix that with the `else` word! +\ Else: : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok 20 ?>64 \ Less than 64! ok -\ As you can see, conditionals behave more or less like they do in most programming languages. - \ ------------------------------ Loops ------------------------------ \ `do` is like `if` in that it is also a compile-only word, though it uses `loop` as its -\ terminator. - +\ terminator: : myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok test \ Hello! @@ -143,31 +126,21 @@ test \ Hello! \ Hello! ok -\ `do` expects two numbers before it: the end number and the index number, respectively. -\ (cr means carraige-return, essentially it a newline). This is equivalent to a for-loop -\ in other languages, with a definite number of times to loop. - -\ So what if we want to get the value of the index as we loop? We use `i`. +\ `do` expects two numbers on the stack: the end number and the index number, respectively. +\ Get the value of the index as we loop with `i`: : one-to-15 ( -- ) 15 0 do i . loop ; \ ok one-to-15 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ok : squares ( -- ) 10 0 do i DUP * . loop ; \ ok squares \ 0 1 4 9 16 25 36 49 64 81 ok -\ Thidly, we can also change how large the step is between each loop iteration with `+loop`. -\ `+loop` reads the number on the top of the stack for how far to move each iteration. - +\ Change the "step" with `+loop`: : threes ( -- ) 15 0 do i . 3 +loop ; \ ok threes \ 0 3 6 9 12 ok -\ Finally, while loops: - +\ Finally, while loops with `begin` `unil`: : death ( -- ) begin ." Are we there yet?" 0 until ; -\ Will print "Are we there yet?" forever. While loops are constructed in the format -\ of `begin` `until`. The loop will run until flag is a -\ truthy value (not 0). - \ ------------------------------ Variables and Memory ------------------------------ \ Sometimes we'll be in a situation where we want more permanent variables: -- cgit v1.2.3 From e5fbd94af0455cfcac15e398c41a0686b69d38ec Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 00:13:54 +0000 Subject: Refactor section Stack Manipulation. --- forth.html.markdown | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 7b805057..91591ac9 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -56,17 +56,10 @@ of what is written here should work elsewhere. \ Naturally, as we do so much work with the stack, we'll want some useful methods. -drop \ drop (remove) the item at the top of the stack (note the difference between this and `.`) -dup \ duplicate the item on top the stack -rot \ rotate the top three items (third -> first, first -> second, second -> third) -swap \ swaps the top item with the second item - -\ Examples: - -dup * \ square the top item -2 5 dup * swap / \ half the top item squared -6 4 5 rot * - \ sometimes we just want to reorganize -4 0 drop 2 / \ add 4 and 0, remove 0 and divide the top by 2 +3 dup - \ duplicate the top item (1st now equals 2nd): 3 - 3 +2 5 swap / \ swap the top with the second element: 5 / 2 +6 4 5 rot .s \ rotate the top 3 elements: 4 5 6 ok +4 0 drop 2 / \ remove the top item (dont print to screen): 4 / 2 \ ------------------------------ More Advanced Stack Manipulation ------------------------------ @@ -173,7 +166,6 @@ mynumbers 3 cells erase \ them to 0 we just use `erase`). \ or we can just skip all the above and initialize with specific values: - create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ ...which is equivalent to: -- cgit v1.2.3 From ba3bc070c5db0b24f485a1a742c0b3944b3bb7e8 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 00:19:20 +0000 Subject: Improve section Advanced Stack Manipulation's examples. --- forth.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 91591ac9..c61633c2 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -52,7 +52,7 @@ of what is written here should work elsewhere. \ And so on. -\ ------------------------------ Stack Maniulation ------------------------------ +\ ------------------------------ Stack Manipulation ------------------------------ \ Naturally, as we do so much work with the stack, we'll want some useful methods. @@ -63,10 +63,10 @@ of what is written here should work elsewhere. \ ------------------------------ More Advanced Stack Manipulation ------------------------------ -tuck \ acts like dup, except it duplicates the top item into the 3rd* position in the stack -over \ duplicate the second item to the top of the stack -n roll \ where n is a number, *move* the stack item at that position to the top of the stack -n pick \ where n is a number, *duplicate* the item at that position to the top of the stack +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 \ When referring to stack indexes, they are zero-based. -- cgit v1.2.3 From c0774dc8217fc86025a25936bff2f2981ef12026 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 16:10:56 +0000 Subject: Remove/refactor the descriptions. --- forth.html.markdown | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index c61633c2..34416878 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -21,31 +21,26 @@ of what is written here should work elsewhere. \ It's important to know how forth processes instructions. All programming in Forth is \ done by manipulating what's known as the parameter stack (more commonly just referred -\ to as "the stack"). The stack is a typical last-in-first-out (LIFO) stack. Typing: +\ to as "the stack"). Typing: 5 2 3 56 76 23 65 -\ Means 5 gets put on the stack first, then 2, then 3, etc all the way to 65, which -\ is now at the top of the stack. We can see the length and contents of the stack by -\ passing forth the word `.s`: -.s <7> 5 2 3 56 76 23 65 \ ok +\ Makes those numbers get added to the stack, from left to right. +.s \ <7> 5 2 3 56 76 23 65 ok \ Forth's interpreter interprets what you type in one of two ways: as *words* (i.e. the \ name of subroutines) or as *numbers*. Words are essentially "symbols that do things". -\ Finally, as the stack is LIFO, we obviously must use postfix notation to manipulate -\ the stack. This should become clear shortly. - \ ------------------------------ Basic Arithmetic ------------------------------ -\ Lets do a simple equation: adding 5 and 4. In infix notation this would be 5 + 4, -\ but as forth works in postfix (see above about stack manipulation) we input it like so: +\ Arithmetic (in fact most words requiring data) works by manipulating data on +\ the stack. 5 4 + \ ok -\ However, this alone yields "ok", yet no answer. Typing the word `.` will yield -\ the result. +\ This adds 5 and 4 to the stack and then `+` is called, which removes them and +\ adds the result to the stack. We can see it with `.`: . \ 9 ok -\ This should illustrate how Forth's stack works. Lets do a few more arithmetic tests: +\ A few more examples of arithmetic 6 7 * . \ 42 ok 1360 23 - . \ 1337 ok 12 12 / . \ 1 ok @@ -75,19 +70,15 @@ of what is written here should work elsewhere. \ Quite often one will want to write their own words. : square ( n -- n ) dup * ; \ ok -\ Lets break this down. The `:` word says to Forth to enter "compile" mode. After that, -\ we tell Forth what our word is called - "square". Between the parentheses we have a -\ comment depicting what this word does to the stack - it takes a number and adds a -\ number. Finally, we have what the word does, until we reach the `;` word which -\ says that you've finished your definition, Forth will add this to the dictionary and -\ switch back into interpret mode. +\ The `:` word sets forth into compile mode. `(` and `)` are both words which +\ tell forth to ignore between them. Up until the `;` word is what our word +\ does. \ We can check the definition of a word with the `see` word: see square \ dup * ; ok \ ------------------------------ Conditionals ------------------------------ -\ Booleans: \ In forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. \ However, any non-zero value is usually treated as being true: -- cgit v1.2.3 From ade01b4ad8258c9e12f86e2bb9aa5426f60bc500 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 16:26:38 +0000 Subject: More refactoring, get lines below 80 chars. --- forth.html.markdown | 61 +++++++++++++++++++++++++++-------------------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 34416878..8cfa46e4 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -7,28 +7,29 @@ filename: learnforth.fs Forth was created by Charles H. Moore in the 70s. -Note: This article focuses predominantly on the Gforth implementation of Forth, but most -of what is written here should work elsewhere. +Note: This article focuses predominantly on the Gforth implementation of +Forth, but most of what is written here should work elsewhere. -> If Lisp is the ultimate high level language, Forth is the ultimate low level language. +> If Lisp is the ultimate high level lang, Forth is the ultimate low level lang. ```forth -\ Forth is an interactive programming language which is comprised of *words*. These are -\ Forth subroutines which are executed once you press , from left to right. +\ Forth is an interactive programming language which is comprised of +\ *words*. These are Forth subroutines which are executed once you press +, from left to right. \ ------------------------------ Precursor ------------------------------ -\ It's important to know how forth processes instructions. All programming in Forth is -\ done by manipulating what's known as the parameter stack (more commonly just referred -\ to as "the stack"). Typing: +\ It's important to know how forth processes instructions. All +\ programming in Forth is done by manipulating what's known as the parameter +\ stack (more commonly just referred to as "the stack"). Typing: 5 2 3 56 76 23 65 \ Makes those numbers get added to the stack, from left to right. .s \ <7> 5 2 3 56 76 23 65 ok -\ Forth's interpreter interprets what you type in one of two ways: as *words* (i.e. the -\ name of subroutines) or as *numbers*. Words are essentially "symbols that do things". +\ Forth's interpreter interprets what you type in one of two ways: as *words* +\ (i.e. the name of subroutines) or as *numbers*. \ ------------------------------ Basic Arithmetic ------------------------------ @@ -47,16 +48,16 @@ of what is written here should work elsewhere. \ And so on. -\ ------------------------------ Stack Manipulation ------------------------------ +\ ----------------------------- Stack Manipulation ----------------------------- -\ Naturally, as we do so much work with the stack, we'll want some useful methods. +\ Naturally, as we work with the stack, we'll want some useful methods: 3 dup - \ duplicate the top item (1st now equals 2nd): 3 - 3 2 5 swap / \ swap the top with the second element: 5 / 2 6 4 5 rot .s \ rotate the top 3 elements: 4 5 6 ok 4 0 drop 2 / \ remove the top item (dont print to screen): 4 / 2 -\ ------------------------------ More Advanced Stack Manipulation ------------------------------ +\ ---------------------- More Advanced Stack Manipulation ---------------------- 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 @@ -65,7 +66,7 @@ of what is written here should work elsewhere. \ When referring to stack indexes, they are zero-based. -\ ------------------------------ Creating Words ------------------------------ +\ ------------------------------ Creating Words -------------------------------- \ Quite often one will want to write their own words. : square ( n -- n ) dup * ; \ ok @@ -77,7 +78,7 @@ of what is written here should work elsewhere. \ We can check the definition of a word with the `see` word: see square \ dup * ; ok -\ ------------------------------ Conditionals ------------------------------ +\ -------------------------------- Conditionals -------------------------------- \ In forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. @@ -86,22 +87,22 @@ see square \ dup * ; ok 42 42 = / -1 ok 12 53 = / 0 ok -\ `if` is a *compile-only word*. This means that it can only be used when we're compiling a word. -\ when creating conditionals, the format is `if` `then` . +\ `if` is a *compile-only word*. This means that it can only be used when we're +\ compiling a word. The format is `if` `then` . : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok \ Else: -: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; \ ok -100 ?>64 \ Greater than 64! ok -20 ?>64 \ Less than 64! ok +: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; +100 ?>64 \ Greater than 64! ok +20 ?>64 \ Less than 64! ok -\ ------------------------------ Loops ------------------------------ +\ ------------------------------------ Loops ----------------------------------- -\ `do` is like `if` in that it is also a compile-only word, though it uses `loop` as its -\ terminator: +\ `do` is like `if` in that it is also a compile-only word, though it uses +\ `loop` as its terminator: : myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok test \ Hello! @@ -110,11 +111,11 @@ test \ Hello! \ Hello! ok -\ `do` expects two numbers on the stack: the end number and the index number, respectively. +\ `do` expects two numbers on the stack: the end number and the index number: \ Get the value of the index as we loop with `i`: -: one-to-15 ( -- ) 15 0 do i . loop ; \ ok -one-to-15 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ok +: 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 : squares ( -- ) 10 0 do i DUP * . loop ; \ ok squares \ 0 1 4 9 16 25 36 49 64 81 ok @@ -125,7 +126,7 @@ threes \ 0 3 6 9 12 ok \ Finally, while loops with `begin` `unil`: : death ( -- ) begin ." Are we there yet?" 0 until ; -\ ------------------------------ Variables and Memory ------------------------------ +\ ---------------------------- Variables and Memory ---------------------------- \ Sometimes we'll be in a situation where we want more permanent variables: \ First, we use `variable` to declare `age` to be a variable. @@ -134,8 +135,8 @@ variable age \ Then we write 21 to age with the word `!`. 21 age ! -\ Finally we can print our variable using the "read" word '@', which adds the value -\ to the stack, or use a handy word called `?` that reads and prints it in one go. +\ Finally we can print our variable using the "read" word '@', which adds the +\ value to the stack, or use `?` that reads and prints it in one go. age @ . \ 12 ok age ? \ 12 ok @@ -180,7 +181,7 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ TODO -\ ------------------------------ Final Notes ------------------------------ +\ --------------------------------- Final Notes -------------------------------- \ Floats \ Commenting (types) -- cgit v1.2.3 From b2c704deaf460e97e89895f409368879e3f46f60 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 16:29:40 +0000 Subject: Fix missing comment, pull up a few lines. --- forth.html.markdown | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 8cfa46e4..11300159 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -16,7 +16,7 @@ Forth, but most of what is written here should work elsewhere. \ Forth is an interactive programming language which is comprised of \ *words*. These are Forth subroutines which are executed once you press -, from left to right. +\ , from left to right. \ ------------------------------ Precursor ------------------------------ @@ -46,8 +46,6 @@ Forth, but most of what is written here should work elsewhere. 1360 23 - . \ 1337 ok 12 12 / . \ 1 ok -\ And so on. - \ ----------------------------- Stack Manipulation ----------------------------- \ Naturally, as we work with the stack, we'll want some useful methods: @@ -83,18 +81,15 @@ see square \ dup * ; ok \ In forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. \ However, any non-zero value is usually treated as being true: - 42 42 = / -1 ok 12 53 = / 0 ok \ `if` is a *compile-only word*. This means that it can only be used when we're \ compiling a word. The format is `if` `then` . - : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok \ Else: - : ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; 100 ?>64 \ Greater than 64! ok 20 ?>64 \ Less than 64! ok -- cgit v1.2.3 From b1c2d9ef792251b8683b189dd5f24c9c53e4587c Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 22:16:48 +0000 Subject: Add section Return Stack, add Floating Point Operations section. --- forth.html.markdown | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 11300159..2c2c480e 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -18,7 +18,7 @@ Forth, but most of what is written here should work elsewhere. \ *words*. These are Forth subroutines which are executed once you press \ , from left to right. -\ ------------------------------ Precursor ------------------------------ +\ --------------------------------- Precursor --------------------------------- \ It's important to know how forth processes instructions. All \ programming in Forth is done by manipulating what's known as the parameter @@ -174,12 +174,34 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ ------------------------------ The Return Stack ------------------------------ -\ TODO +\ The return stack is used by Forth to the hold pointers to things when +\ words are executing other words, e.g. loops. + +\ We've already seen one use of it: `i`, which duplicates the top of the return +\ stack. `i` is equivalent to `r@`. +: myloop ( -- ) 5 0 do r@ . loop ; + +\ As well as reading, we can add to the return stack and remove from it: +5 6 4 >r swap r> .s \ 6 5 4 + +\ NOTE: Because forth uses the return stack for word pointers, it's essential +\ that you set the return stack back to how it was at the end of your +\ definition. `>r` should always be followed by `r>`. + +\ ------------------------- Floating Point Operations ------------------------- + +\ Most forths tend to dislike the use of floating point operations. We write +\ floating point operations with scientific notation. +8.3e 0.8e f+ f. \ 9.1 ok + +\ Usually we can just prepend arithmetic words with 'f' to use floating point +\ arithmetic: +variable myfloatingvar +4.4e myfloatingvar f! +myfloatingvar f@ f. \ --------------------------------- Final Notes -------------------------------- -\ Floats -\ Commenting (types) \ bye ``` -- cgit v1.2.3 From 70b03c18683f298a8c4b3eb7045c740c9882f343 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 22:25:37 +0000 Subject: Make Arrays its own section, comment float examples. --- forth.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 2c2c480e..76fdf425 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -18,7 +18,7 @@ Forth, but most of what is written here should work elsewhere. \ *words*. These are Forth subroutines which are executed once you press \ , from left to right. -\ --------------------------------- Precursor --------------------------------- +\ --------------------------------- Precursor ---------------------------------- \ It's important to know how forth processes instructions. All \ programming in Forth is done by manipulating what's known as the parameter @@ -142,7 +142,7 @@ age ? \ 12 ok 100 constant WATER-BOILING-POINT \ ok WATER-BOILING-POINT . \ 100 ok -\ Arrays! +\ ----------------------------------- Arrays ----------------------------------- \ Set up an array of length 3: variable mynumbers 2 cells allot @@ -188,7 +188,7 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ that you set the return stack back to how it was at the end of your \ definition. `>r` should always be followed by `r>`. -\ ------------------------- Floating Point Operations ------------------------- +\ ------------------------- Floating Point Operations -------------------------- \ Most forths tend to dislike the use of floating point operations. We write \ floating point operations with scientific notation. @@ -196,9 +196,9 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ Usually we can just prepend arithmetic words with 'f' to use floating point \ arithmetic: -variable myfloatingvar -4.4e myfloatingvar f! -myfloatingvar f@ f. +variable myfloatingvar \ ok +4.4e myfloatingvar f! \ ok +myfloatingvar f@ f. \ 4.4 ok \ --------------------------------- Final Notes -------------------------------- -- cgit v1.2.3 From 4d80a56d2c3311b56e2ccee873bb970abe82e9c4 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 22:51:40 +0000 Subject: Capitalize instances of 'forth'. --- forth.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 76fdf425..77358dcd 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -20,7 +20,7 @@ Forth, but most of what is written here should work elsewhere. \ --------------------------------- Precursor ---------------------------------- -\ It's important to know how forth processes instructions. All +\ It's important to know how Forth processes instructions. All \ programming in Forth is done by manipulating what's known as the parameter \ stack (more commonly just referred to as "the stack"). Typing: 5 2 3 56 76 23 65 @@ -69,8 +69,8 @@ Forth, but most of what is written here should work elsewhere. \ Quite often one will want to write their own words. : square ( n -- n ) dup * ; \ ok -\ The `:` word sets forth into compile mode. `(` and `)` are both words which -\ tell forth to ignore between them. Up until the `;` word is what our word +\ The `:` word sets Forth into compile mode. `(` and `)` are both words which +\ tell Forth to ignore between them. Up until the `;` word is what our word \ does. \ We can check the definition of a word with the `see` word: @@ -78,7 +78,7 @@ see square \ dup * ; ok \ -------------------------------- Conditionals -------------------------------- -\ In forth, -1 is used to represent truth, and 0 is used to represent false. +\ In Forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. \ However, any non-zero value is usually treated as being true: 42 42 = / -1 ok @@ -184,13 +184,13 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ As well as reading, we can add to the return stack and remove from it: 5 6 4 >r swap r> .s \ 6 5 4 -\ NOTE: Because forth uses the return stack for word pointers, it's essential +\ NOTE: Because Forth uses the return stack for word pointers, it's essential \ that you set the return stack back to how it was at the end of your \ definition. `>r` should always be followed by `r>`. \ ------------------------- Floating Point Operations -------------------------- -\ Most forths tend to dislike the use of floating point operations. We write +\ Most Forths tend to dislike the use of floating point operations. We write \ floating point operations with scientific notation. 8.3e 0.8e f+ f. \ 9.1 ok -- cgit v1.2.3 From 879da6be51557b1acba811954bc341afba502edd Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:01:42 +0000 Subject: Fix typos. --- forth.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 77358dcd..9c95f66b 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -81,8 +81,8 @@ see square \ dup * ; ok \ In Forth, -1 is used to represent truth, and 0 is used to represent false. \ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. \ However, any non-zero value is usually treated as being true: -42 42 = / -1 ok -12 53 = / 0 ok +42 42 = \ -1 ok +12 53 = \ 0 ok \ `if` is a *compile-only word*. This means that it can only be used when we're \ compiling a word. The format is `if` `then` . @@ -99,7 +99,7 @@ see square \ dup * ; ok \ `do` is like `if` in that it is also a compile-only word, though it uses \ `loop` as its terminator: : myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok -test +myloop \ Hello! \ Hello! \ Hello! -- cgit v1.2.3 From edba1b39c8dee9f383b4b1dd1732e2b1afc55a40 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:16:26 +0000 Subject: Add a few misc ideas to section Final Notes. --- forth.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 9c95f66b..aec74333 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -202,11 +202,18 @@ myfloatingvar f@ f. \ 4.4 ok \ --------------------------------- Final Notes -------------------------------- -\ bye +\ Loading Forth files: +s" forthfile.fs" included + +\ If you find yourself wanting to clear the stack, typing something that's not +\ a defined word or a number will work. + +\ `bye` closes gforth. ``` ##Ready For More? * [Starting Forth](http://www.forth.com/starting-forth/) +* [Simple Forth](http://www.murphywong.net/hello/simple.htm) * [Thinking Forth](http://thinking-forth.sourceforge.net/) -- cgit v1.2.3 From e001c352caa7babb52c53673a52fffe6a5482da9 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:25:48 +0000 Subject: Slim down comments. --- forth.html.markdown | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index aec74333..04804b60 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -10,18 +10,15 @@ Forth was created by Charles H. Moore in the 70s. Note: This article focuses predominantly on the Gforth implementation of Forth, but most of what is written here should work elsewhere. -> If Lisp is the ultimate high level lang, Forth is the ultimate low level lang. - ```forth -\ Forth is an interactive programming language which is comprised of +\ Forth is a low level interactive programming language which is comprised of \ *words*. These are Forth subroutines which are executed once you press \ , from left to right. \ --------------------------------- Precursor ---------------------------------- -\ It's important to know how Forth processes instructions. All -\ programming in Forth is done by manipulating what's known as the parameter +\ All programming in Forth is done by manipulating what's known as the parameter \ stack (more commonly just referred to as "the stack"). Typing: 5 2 3 56 76 23 65 @@ -78,9 +75,8 @@ see square \ dup * ; ok \ -------------------------------- Conditionals -------------------------------- -\ In Forth, -1 is used to represent truth, and 0 is used to represent false. -\ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary. -\ However, any non-zero value is usually treated as being true: +\ -1 == true, 0 == false. However, any non-zero value is usually treated as +\ being true: 42 42 = \ -1 ok 12 53 = \ 0 ok @@ -123,21 +119,17 @@ threes \ 0 3 6 9 12 ok \ ---------------------------- Variables and Memory ---------------------------- -\ Sometimes we'll be in a situation where we want more permanent variables: -\ First, we use `variable` to declare `age` to be a variable. +\ Use `variable` to declare `age` to be a variable. variable age \ Then we write 21 to age with the word `!`. 21 age ! -\ Finally we can print our variable using the "read" word '@', which adds the +\ Finally we can print our variable using the "read" word `@`, which adds the \ value to the stack, or use `?` that reads and prints it in one go. age @ . \ 12 ok age ? \ 12 ok -\ What's happening here is that `age` stores the memory address, and we use `!` -\ and `@` to manipulate it. - \ Constants are quite simiar, except we don't bother with memory addresses: 100 constant WATER-BOILING-POINT \ ok WATER-BOILING-POINT . \ 100 ok @@ -174,8 +166,8 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ ------------------------------ The Return Stack ------------------------------ -\ The return stack is used by Forth to the hold pointers to things when -\ words are executing other words, e.g. loops. +\ The return stack is used to the hold pointers to things when words are +\ executing other words, e.g. loops. \ We've already seen one use of it: `i`, which duplicates the top of the return \ stack. `i` is equivalent to `r@`. @@ -190,12 +182,11 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ ------------------------- Floating Point Operations -------------------------- -\ Most Forths tend to dislike the use of floating point operations. We write +\ Most Forths tend to eschew the use of floating point operations. We write \ floating point operations with scientific notation. 8.3e 0.8e f+ f. \ 9.1 ok -\ Usually we can just prepend arithmetic words with 'f' to use floating point -\ arithmetic: +\ Usually we simply prepend words with 'f' when dealing with floats: variable myfloatingvar \ ok 4.4e myfloatingvar f! \ ok myfloatingvar f@ f. \ 4.4 ok -- cgit v1.2.3 From 00d03a4cbee3fc87c54b03a2a7d25b82c84ac2ef Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:37:07 +0000 Subject: Tidying up. --- forth.html.markdown | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 04804b60..c60b8d67 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -5,13 +5,14 @@ contributors: filename: learnforth.fs --- -Forth was created by Charles H. Moore in the 70s. +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. Note: This article focuses predominantly on the Gforth implementation of Forth, but most of what is written here should work elsewhere. ```forth - \ Forth is a low level interactive programming language which is comprised of \ *words*. These are Forth subroutines which are executed once you press \ , from left to right. @@ -39,9 +40,9 @@ Forth, but most of what is written here should work elsewhere. . \ 9 ok \ A few more examples of arithmetic -6 7 * . \ 42 ok -1360 23 - . \ 1337 ok -12 12 / . \ 1 ok +6 7 * . \ 42 ok +1360 23 - . \ 1337 ok +12 12 / . \ 1 ok \ ----------------------------- Stack Manipulation ----------------------------- -- cgit v1.2.3 From e4229c618bfc5ad4c0ed1939322e698eb45546c8 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:49:06 +0000 Subject: Add a 'returns' comment for every line of Forth. --- forth.html.markdown | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index c60b8d67..5ffdfbfc 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -50,7 +50,7 @@ Forth, but most of what is written here should work elsewhere. 3 dup - \ duplicate the top item (1st now equals 2nd): 3 - 3 2 5 swap / \ swap the top with the second element: 5 / 2 -6 4 5 rot .s \ rotate the top 3 elements: 4 5 6 ok +6 4 5 rot .s \ rotate the top 3 elements: 4 5 6 4 0 drop 2 / \ remove the top item (dont print to screen): 4 / 2 \ ---------------------- More Advanced Stack Manipulation ---------------------- @@ -103,9 +103,9 @@ myloop \ Hello! \ Hello! ok -\ `do` expects two numbers on the stack: the end number and the index number: +\ `do` expects two numbers on the stack: the end number and the index number. -\ Get the value of the index as we loop with `i`: +\ We can get the value of the index as we loop with `i`: : 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 : squares ( -- ) 10 0 do i DUP * . loop ; \ ok @@ -116,49 +116,49 @@ squares \ 0 1 4 9 16 25 36 49 64 81 ok threes \ 0 3 6 9 12 ok \ Finally, while loops with `begin` `unil`: -: death ( -- ) begin ." Are we there yet?" 0 until ; +: death ( -- ) begin ." Are we there yet?" 0 until ; \ ok \ ---------------------------- Variables and Memory ---------------------------- \ Use `variable` to declare `age` to be a variable. -variable age +variable age \ ok \ Then we write 21 to age with the word `!`. -21 age ! +21 age ! \ ok \ Finally we can print our variable using the "read" word `@`, which adds the \ value to the stack, or use `?` that reads and prints it in one go. -age @ . \ 12 ok -age ? \ 12 ok +age @ . \ 12 ok +age ? \ 12 ok \ Constants are quite simiar, except we don't bother with memory addresses: -100 constant WATER-BOILING-POINT \ ok -WATER-BOILING-POINT . \ 100 ok +100 constant WATER-BOILING-POINT \ ok +WATER-BOILING-POINT . \ 100 ok \ ----------------------------------- Arrays ----------------------------------- \ Set up an array of length 3: -variable mynumbers 2 cells allot +variable mynumbers 2 cells allot \ ok \ Initialize all the values to 0 -mynumbers 3 cells erase +mynumbers 3 cells erase \ ok \ (alternatively we could do `0 fill` instead of `erase`, but as we're setting \ them to 0 we just use `erase`). \ or we can just skip all the above and initialize with specific values: -create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! +create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) \ ...which is equivalent to: \ [64, 9001, 1337] -64 mynumbers 0 cells + ! -9001 mynumbers 1 cells + ! -1337 mynumbers 2 cells + ! +64 mynumbers 0 cells + ! \ ok +9001 mynumbers 1 cells + ! \ ok +1337 mynumbers 2 cells + ! \ ok \ Reading values at certain array indexes: -0 cells mynumbers + ? \ 64 ok -1 cells mynumbers + ? \ 9001 ok -2 cells mynumbers + ? \ 1337 ok +0 cells mynumbers + ? \ 64 ok +1 cells mynumbers + ? \ 9001 ok +2 cells mynumbers + ? \ 1337 ok \ Of course, you'll probably want to define your own words to manipulate arrays: : ?mynumbers ( n -- n ) cells mynumbers + ; \ ok @@ -172,10 +172,10 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! \ We've already seen one use of it: `i`, which duplicates the top of the return \ stack. `i` is equivalent to `r@`. -: myloop ( -- ) 5 0 do r@ . loop ; +: myloop ( -- ) 5 0 do r@ . loop ; \ ok \ As well as reading, we can add to the return stack and remove from it: -5 6 4 >r swap r> .s \ 6 5 4 +5 6 4 >r swap r> .s \ 6 5 4 ok \ NOTE: Because Forth uses the return stack for word pointers, it's essential \ that you set the return stack back to how it was at the end of your @@ -188,9 +188,9 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important! 8.3e 0.8e f+ f. \ 9.1 ok \ Usually we simply prepend words with 'f' when dealing with floats: -variable myfloatingvar \ ok -4.4e myfloatingvar f! \ ok -myfloatingvar f@ f. \ 4.4 ok +variable myfloatingvar \ ok +4.4e myfloatingvar f! \ ok +myfloatingvar f@ f. \ 4.4 ok \ --------------------------------- Final Notes -------------------------------- -- cgit v1.2.3 From 11313c9c005be9979883ca866e4da9aeb3413091 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:50:54 +0000 Subject: Whoops, missed a return comment. --- forth.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 5ffdfbfc..8a24dccc 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -21,7 +21,7 @@ Forth, but most of what is written here should work elsewhere. \ All programming in Forth is done by manipulating what's known as the parameter \ stack (more commonly just referred to as "the stack"). Typing: -5 2 3 56 76 23 65 +5 2 3 56 76 23 65 \ ok \ Makes those numbers get added to the stack, from left to right. .s \ <7> 5 2 3 56 76 23 65 ok -- cgit v1.2.3 From 481261458cf3dad917ae3bdc46b25540b2ca3d05 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:57:02 +0000 Subject: Mention word . Also comment-out the file-include line. --- forth.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 8a24dccc..55286c21 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -195,10 +195,11 @@ myfloatingvar f@ f. \ 4.4 ok \ --------------------------------- Final Notes -------------------------------- \ Loading Forth files: -s" forthfile.fs" included +\ s" forthfile.fs" included -\ If you find yourself wanting to clear the stack, typing something that's not -\ a defined word or a number will work. +\ Typing a non-existent word will empty the stack. However, there's also a word +\ specifically for that: +clearstack \ `bye` closes gforth. -- cgit v1.2.3 From 6cf6ce36684e38a5aaacde58be1cd83d58cebd8f Mon Sep 17 00:00:00 2001 From: HorseMD Date: Thu, 13 Nov 2014 23:59:37 +0000 Subject: Reorder final notes section. --- forth.html.markdown | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 55286c21..0e3cdd2a 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -194,14 +194,15 @@ myfloatingvar f@ f. \ 4.4 ok \ --------------------------------- Final Notes -------------------------------- -\ Loading Forth files: -\ s" forthfile.fs" included - \ Typing a non-existent word will empty the stack. However, there's also a word \ specifically for that: clearstack -\ `bye` closes gforth. +\ Loading Forth files: +\ s" forthfile.fs" included + +\ Exiting Gforth: +\ bye ``` -- cgit v1.2.3 From 601f06e72d48ce0d16f9d34dc1f74418eddae654 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 00:05:48 +0000 Subject: Whoops, make lowercase. --- forth.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 0e3cdd2a..e799655b 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -83,11 +83,11 @@ see square \ dup * ; ok \ `if` is a *compile-only word*. This means that it can only be used when we're \ compiling a word. The format is `if` `then` . -: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" then ; \ ok +: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok \ Else: -: ?>64 ( n -- n ) DUP 64 > if ." Greater than 64!" else ." Less than 64!" then ; +: ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" else ." Less than 64!" then ; 100 ?>64 \ Greater than 64! ok 20 ?>64 \ Less than 64! ok @@ -108,7 +108,7 @@ myloop \ We can get the value of the index as we loop with `i`: : 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 -: squares ( -- ) 10 0 do i DUP * . loop ; \ ok +: squares ( -- ) 10 0 do i dup * . loop ; \ ok squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: -- cgit v1.2.3 From f1688b8000eeb39bc1e89b06c6a55eb74a081b96 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 00:14:38 +0000 Subject: Illustrate parameters via loops. --- forth.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index e799655b..a6b17a5d 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -108,12 +108,12 @@ myloop \ We can get the value of the index as we loop with `i`: : 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 -: squares ( -- ) 10 0 do i dup * . loop ; \ ok -squares \ 0 1 4 9 16 25 36 49 64 81 ok +: squares ( -- ) 0 do i dup * . loop ; \ ok +10 squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: -: threes ( -- ) 15 0 do i . 3 +loop ; \ ok -threes \ 0 3 6 9 12 ok +: threes ( -- ) do i . 3 +loop ; \ ok +15 0 threes \ 0 3 6 9 12 ok \ Finally, while loops with `begin` `unil`: : death ( -- ) begin ." Are we there yet?" 0 until ; \ ok -- cgit v1.2.3 From eb83f36015e6e666603645394aa61d1bd8153dac Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 10:24:39 +0000 Subject: Trim down explainations. --- forth.html.markdown | 55 +++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index a6b17a5d..46b912b4 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -13,21 +13,19 @@ Note: This article focuses predominantly on the Gforth implementation of Forth, but most of what is written here should work elsewhere. ```forth -\ Forth is a low level interactive programming language which is comprised of -\ *words*. These are Forth subroutines which are executed once you press -\ , from left to right. +\ This is a comment +( This is also a comment but it's only used when defining words ) \ --------------------------------- Precursor ---------------------------------- -\ All programming in Forth is done by manipulating what's known as the parameter -\ stack (more commonly just referred to as "the stack"). Typing: +\ All programming in Forth is done by manipulating the parameter stack (more +\ commonly just referred to as "the stack"). 5 2 3 56 76 23 65 \ ok -\ Makes those numbers get added to the stack, from left to right. +\ Those numbers get added to the stack, from left to right. .s \ <7> 5 2 3 56 76 23 65 ok -\ Forth's interpreter interprets what you type in one of two ways: as *words* -\ (i.e. the name of subroutines) or as *numbers*. +\ In Forth, everything is either a word or a number. \ ------------------------------ Basic Arithmetic ------------------------------ @@ -35,14 +33,19 @@ Forth, but most of what is written here should work elsewhere. \ the stack. 5 4 + \ ok -\ This adds 5 and 4 to the stack and then `+` is called, which removes them and -\ adds the result to the stack. We can see it with `.`: +\ `.` pops the top result from the stack: . \ 9 ok -\ A few more examples of arithmetic +\ More examples of arithmetic: 6 7 * . \ 42 ok 1360 23 - . \ 1337 ok 12 12 / . \ 1 ok +13 2 mod . \ 1 ok + +99 negate . \ -99 ok +-99 abs . \ 99 ok +52 23 max . \ 52 ok +52 23 min . \ 23 ok \ ----------------------------- Stack Manipulation ----------------------------- @@ -67,11 +70,8 @@ Forth, but most of what is written here should work elsewhere. \ Quite often one will want to write their own words. : square ( n -- n ) dup * ; \ ok -\ The `:` word sets Forth into compile mode. `(` and `)` are both words which -\ tell Forth to ignore between them. Up until the `;` word is what our word -\ does. +\ The `:` word sets Forth into compile mode until it sees the `;` word. -\ We can check the definition of a word with the `see` word: see square \ dup * ; ok \ -------------------------------- Conditionals -------------------------------- @@ -81,8 +81,7 @@ see square \ dup * ; ok 42 42 = \ -1 ok 12 53 = \ 0 ok -\ `if` is a *compile-only word*. This means that it can only be used when we're -\ compiling a word. The format is `if` `then` . +\ `if` is a compile-only word. `if` `then` . : ?>64 ( n -- n ) dup 64 > if ." Greater than 64!" then ; \ ok 100 ?>64 \ Greater than 64! ok @@ -93,8 +92,7 @@ see square \ dup * ; ok \ ------------------------------------ Loops ----------------------------------- -\ `do` is like `if` in that it is also a compile-only word, though it uses -\ `loop` as its terminator: +\ `do` is also a compile-only word. : myloop ( -- ) 5 0 do cr ." Hello!" loop ; \ ok myloop \ Hello! @@ -108,12 +106,12 @@ myloop \ We can get the value of the index as we loop with `i`: : 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 -: squares ( -- ) 0 do i dup * . loop ; \ ok +: squares ( n -- ) 0 do i dup * . loop ; \ ok 10 squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: -: threes ( -- ) do i . 3 +loop ; \ ok -15 0 threes \ 0 3 6 9 12 ok +: threes ( n n -- ) do i . 3 +loop ; \ ok +15 0 threes \ 0 3 6 9 12 ok \ Finally, while loops with `begin` `unil`: : death ( -- ) begin ." Are we there yet?" 0 until ; \ ok @@ -142,8 +140,9 @@ variable mynumbers 2 cells allot \ ok \ Initialize all the values to 0 mynumbers 3 cells erase \ ok -\ (alternatively we could do `0 fill` instead of `erase`, but as we're setting -\ them to 0 we just use `erase`). + +\ Alternatively we could use `fill`: +mynumbers 3 cells 0 fill \ or we can just skip all the above and initialize with specific values: create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) @@ -177,14 +176,12 @@ create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) \ As well as reading, we can add to the return stack and remove from it: 5 6 4 >r swap r> .s \ 6 5 4 ok -\ NOTE: Because Forth uses the return stack for word pointers, it's essential -\ that you set the return stack back to how it was at the end of your -\ definition. `>r` should always be followed by `r>`. +\ NOTE: Because Forth uses the return stack for word pointers, `>r` should +\ always be followed by `r>`. \ ------------------------- Floating Point Operations -------------------------- -\ Most Forths tend to eschew the use of floating point operations. We write -\ floating point operations with scientific notation. +\ Most Forths tend to eschew the use of floating point operations. 8.3e 0.8e f+ f. \ 9.1 ok \ Usually we simply prepend words with 'f' when dealing with floats: -- cgit v1.2.3 From 13a3c113940c74f9e0847dd2cfd767c07ac0a7b9 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 10:51:22 +0000 Subject: More rewording, mention ?do. --- forth.html.markdown | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 46b912b4..b8b751d9 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -67,11 +67,10 @@ Forth, but most of what is written here should work elsewhere. \ ------------------------------ Creating Words -------------------------------- -\ Quite often one will want to write their own words. -: square ( n -- n ) dup * ; \ ok - \ The `:` word sets Forth into compile mode until it sees the `;` word. +: square ( n -- n ) dup * ; \ ok +\ We can view what a word does too: see square \ dup * ; ok \ -------------------------------- Conditionals -------------------------------- @@ -101,19 +100,22 @@ myloop \ Hello! \ Hello! ok -\ `do` expects two numbers on the stack: the end number and the index number. +\ `do` expects two numbers on the stack: the end number and the start number. \ We can get the value of the index as we loop with `i`: : 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 -: squares ( n -- ) 0 do i dup * . loop ; \ ok -10 squares \ 0 1 4 9 16 25 36 49 64 81 ok + +\ `?do` works similarly, except it will skip the loop if the end and start +\ numbers are equal. +: squares ( n -- ) 0 ?do i dup * . loop ; \ ok +10 squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: -: threes ( n n -- ) do i . 3 +loop ; \ ok +: threes ( n n -- ) ?do i . 3 +loop ; \ ok 15 0 threes \ 0 3 6 9 12 ok -\ Finally, while loops with `begin` `unil`: +\ Indefinite loops with `begin` `unil`: : death ( -- ) begin ." Are we there yet?" 0 until ; \ ok \ ---------------------------- Variables and Memory ---------------------------- @@ -195,9 +197,15 @@ myfloatingvar f@ f. \ 4.4 ok \ specifically for that: clearstack +\ Clear the screen: +page + \ Loading Forth files: \ s" forthfile.fs" included +\ You can list every word that's in Forth's dictionary (but it's a huge list!): +\ words + \ Exiting Gforth: \ bye -- cgit v1.2.3 From a49ab049a54f3961f3e491d48678b421c7662ee1 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 11:13:39 +0000 Subject: Mention nip, rework Arrays section. --- forth.html.markdown | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index b8b751d9..847895c3 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -55,6 +55,7 @@ Forth, but most of what is written here should work elsewhere. 2 5 swap / \ swap the top with the second element: 5 / 2 6 4 5 rot .s \ rotate the top 3 elements: 4 5 6 4 0 drop 2 / \ remove the top item (dont print to screen): 4 / 2 +1 2 3 nip .s \ remove the second item (similar to drop): 1 3 \ ---------------------- More Advanced Stack Manipulation ---------------------- @@ -151,7 +152,7 @@ create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) \ ...which is equivalent to: -\ [64, 9001, 1337] +\ Manually writing values to each index: 64 mynumbers 0 cells + ! \ ok 9001 mynumbers 1 cells + ! \ ok 1337 mynumbers 2 cells + ! \ ok @@ -159,12 +160,14 @@ create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) \ Reading values at certain array indexes: 0 cells mynumbers + ? \ 64 ok 1 cells mynumbers + ? \ 9001 ok -2 cells mynumbers + ? \ 1337 ok -\ Of course, you'll probably want to define your own words to manipulate arrays: -: ?mynumbers ( n -- n ) cells mynumbers + ; \ ok -64 mynumbers 2 cells + ! \ ok -2 ?mynumbers ? \ 64 ok +\ We can simplify it by making a helper word for manipulating arrays: +: arr ( n n -- n ) cells swap + ; +mynumbers 2 arr ? \ 1337 ok + +\ Which we can use for writing too: +20 mynumbers 1 arr ! \ ok +mynumbers 1 arr ? \ 20 ok \ ------------------------------ The Return Stack ------------------------------ -- cgit v1.2.3 From 6b5c06d45a8ee067fcb62bb6e02b6dd95c634292 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 11:23:14 +0000 Subject: Remove unnecessary swap, give array helper word a better name. --- forth.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 847895c3..0521a3ab 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -161,13 +161,13 @@ create mynumbers 64 , 9001 , 1337 , \ ok (the last `,` is important!) 0 cells mynumbers + ? \ 64 ok 1 cells mynumbers + ? \ 9001 ok -\ We can simplify it by making a helper word for manipulating arrays: -: arr ( n n -- n ) cells swap + ; -mynumbers 2 arr ? \ 1337 ok +\ 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 \ Which we can use for writing too: -20 mynumbers 1 arr ! \ ok -mynumbers 1 arr ? \ 20 ok +20 mynumbers 1 of-arr ! \ ok +mynumbers 1 of-arr ? \ 20 ok \ ------------------------------ The Return Stack ------------------------------ -- cgit v1.2.3 From ce1d44b308150c88ee61c819ed4b2242af4fcf14 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 12:12:30 +0000 Subject: Use our word 'square' in the loop example word 'squares'. --- forth.html.markdown | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 0521a3ab..25a9f3b8 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -70,9 +70,10 @@ Forth, but most of what is written here should work elsewhere. \ The `:` word sets Forth into compile mode until it sees the `;` word. : square ( n -- n ) dup * ; \ ok +5 square . \ 25 ok \ We can view what a word does too: -see square \ dup * ; ok +see square \ : square dup * ; ok \ -------------------------------- Conditionals -------------------------------- @@ -109,7 +110,7 @@ one-to-12 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok \ `?do` works similarly, except it will skip the loop if the end and start \ numbers are equal. -: squares ( n -- ) 0 ?do i dup * . loop ; \ ok +: squares ( n -- ) 0 ?do i square . loop ; \ ok 10 squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: -- cgit v1.2.3 From 6e912f4b46b02fbed110502e5b1cf1ecd600ab4c Mon Sep 17 00:00:00 2001 From: HorseMD Date: Fri, 14 Nov 2014 16:49:25 +0000 Subject: Fix return-comment indentation. --- forth.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 25a9f3b8..4c89a5ec 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -110,8 +110,8 @@ one-to-12 \ 0 1 2 3 4 5 6 7 8 9 10 11 12 ok \ `?do` works similarly, except it will skip the loop if the end and start \ numbers are equal. -: squares ( n -- ) 0 ?do i square . loop ; \ ok -10 squares \ 0 1 4 9 16 25 36 49 64 81 ok +: squares ( n -- ) 0 ?do i square . loop ; \ ok +10 squares \ 0 1 4 9 16 25 36 49 64 81 ok \ Change the "step" with `+loop`: : threes ( n n -- ) ?do i . 3 +loop ; \ ok -- cgit v1.2.3 From 1e1ff8bd10990e81cd18b2393a781c5061538bc0 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Tue, 18 Nov 2014 12:01:59 +0000 Subject: Fix typo when demonstrating variables. --- forth.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 4c89a5ec..5db7b51f 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -130,8 +130,8 @@ variable age \ ok \ Finally we can print our variable using the "read" word `@`, which adds the \ value to the stack, or use `?` that reads and prints it in one go. -age @ . \ 12 ok -age ? \ 12 ok +age @ . \ 21 ok +age ? \ 21 ok \ Constants are quite simiar, except we don't bother with memory addresses: 100 constant WATER-BOILING-POINT \ ok -- cgit v1.2.3 From 39ac935c4f2fefa0d63ed8e87bb7142a9c458188 Mon Sep 17 00:00:00 2001 From: HorseMD Date: Tue, 18 Nov 2014 12:19:12 +0000 Subject: Reword arrays introduction. --- forth.html.markdown | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 5db7b51f..570e12ed 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -139,7 +139,10 @@ WATER-BOILING-POINT . \ 100 ok \ ----------------------------------- Arrays ----------------------------------- -\ Set up an array of length 3: +\ 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: variable mynumbers 2 cells allot \ ok \ Initialize all the values to 0 -- cgit v1.2.3 From e059cd98f32fbd7793f2e8622cb6061c1a142146 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Fri, 2 Jan 2015 19:53:49 +0000 Subject: Let's see how factor highlighting looks in forth --- forth.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 570e12ed..4457e607 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -12,7 +12,7 @@ such as Open Firmware. It's also used by NASA. Note: This article focuses predominantly on the Gforth implementation of Forth, but most of what is written here should work elsewhere. -```forth +```factor \ This is a comment ( This is also a comment but it's only used when defining words ) -- cgit v1.2.3 From 732cdbed1ad345f2b826254bc7f0b6283124955e Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Fri, 2 Jan 2015 20:33:47 +0000 Subject: Update forth.html.markdown --- forth.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'forth.html.markdown') diff --git a/forth.html.markdown b/forth.html.markdown index 4457e607..f7c0bf34 100644 --- a/forth.html.markdown +++ b/forth.html.markdown @@ -12,7 +12,7 @@ such as Open Firmware. It's also used by NASA. Note: This article focuses predominantly on the Gforth implementation of Forth, but most of what is written here should work elsewhere. -```factor +``` \ This is a comment ( This is also a comment but it's only used when defining words ) -- cgit v1.2.3