mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Slim down comments.
This commit is contained in:
parent
edba1b39c8
commit
e001c352ca
@ -10,18 +10,15 @@ Forth was created by Charles H. Moore in the 70s.
|
|||||||
Note: This article focuses predominantly on the Gforth implementation of
|
Note: This article focuses predominantly on the Gforth implementation of
|
||||||
Forth, but most of what is written here should work elsewhere.
|
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
|
||||||
|
|
||||||
\ 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
|
\ *words*. These are Forth subroutines which are executed once you press
|
||||||
\ <Cr>, from left to right.
|
\ <Cr>, from left to right.
|
||||||
|
|
||||||
\ --------------------------------- Precursor ----------------------------------
|
\ --------------------------------- Precursor ----------------------------------
|
||||||
|
|
||||||
\ It's important to know how Forth processes instructions. All
|
\ All programming in Forth is done by manipulating what's known as the parameter
|
||||||
\ programming in Forth is done by manipulating what's known as the parameter
|
|
||||||
\ stack (more commonly just referred to as "the stack"). Typing:
|
\ stack (more commonly just referred to as "the stack"). Typing:
|
||||||
5 2 3 56 76 23 65
|
5 2 3 56 76 23 65
|
||||||
|
|
||||||
@ -78,9 +75,8 @@ see square \ dup * ; ok
|
|||||||
|
|
||||||
\ -------------------------------- Conditionals --------------------------------
|
\ -------------------------------- Conditionals --------------------------------
|
||||||
|
|
||||||
\ In Forth, -1 is used to represent truth, and 0 is used to represent false.
|
\ -1 == true, 0 == false. However, any non-zero value is usually treated as
|
||||||
\ The idea is that -1 is 11111111 in binary, whereas 0 is obviously 0 in binary.
|
\ being true:
|
||||||
\ However, any non-zero value is usually treated as being true:
|
|
||||||
42 42 = \ -1 ok
|
42 42 = \ -1 ok
|
||||||
12 53 = \ 0 ok
|
12 53 = \ 0 ok
|
||||||
|
|
||||||
@ -123,21 +119,17 @@ threes \ 0 3 6 9 12 ok
|
|||||||
|
|
||||||
\ ---------------------------- Variables and Memory ----------------------------
|
\ ---------------------------- Variables and Memory ----------------------------
|
||||||
|
|
||||||
\ Sometimes we'll be in a situation where we want more permanent variables:
|
\ Use `variable` to declare `age` to be a variable.
|
||||||
\ First, we use `variable` to declare `age` to be a variable.
|
|
||||||
variable age
|
variable age
|
||||||
|
|
||||||
\ Then we write 21 to age with the word `!`.
|
\ Then we write 21 to age with the word `!`.
|
||||||
21 age !
|
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.
|
\ 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
|
||||||
|
|
||||||
\ 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:
|
\ Constants are quite simiar, except we don't bother with memory addresses:
|
||||||
100 constant WATER-BOILING-POINT \ ok
|
100 constant WATER-BOILING-POINT \ ok
|
||||||
WATER-BOILING-POINT . \ 100 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 ------------------------------
|
||||||
|
|
||||||
\ The return stack is used by Forth to the hold pointers to things when
|
\ The return stack is used to the hold pointers to things when words are
|
||||||
\ words are executing other words, e.g. loops.
|
\ executing other words, e.g. loops.
|
||||||
|
|
||||||
\ We've already seen one use of it: `i`, which duplicates the top of the return
|
\ We've already seen one use of it: `i`, which duplicates the top of the return
|
||||||
\ stack. `i` is equivalent to `r@`.
|
\ stack. `i` is equivalent to `r@`.
|
||||||
@ -190,12 +182,11 @@ create mynumbers 64 , 9001 , 1337 , \ the last `,` is important!
|
|||||||
|
|
||||||
\ ------------------------- Floating Point Operations --------------------------
|
\ ------------------------- 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.
|
\ floating point operations with scientific notation.
|
||||||
8.3e 0.8e f+ f. \ 9.1 ok
|
8.3e 0.8e f+ f. \ 9.1 ok
|
||||||
|
|
||||||
\ Usually we can just prepend arithmetic words with 'f' to use floating point
|
\ Usually we simply prepend words with 'f' when dealing with floats:
|
||||||
\ arithmetic:
|
|
||||||
variable myfloatingvar \ ok
|
variable myfloatingvar \ ok
|
||||||
4.4e myfloatingvar f! \ ok
|
4.4e myfloatingvar f! \ ok
|
||||||
myfloatingvar f@ f. \ 4.4 ok
|
myfloatingvar f@ f. \ 4.4 ok
|
||||||
|
Loading…
Reference in New Issue
Block a user