mirror of
https://github.com/zeroflag/fcl.git
synced 2025-01-12 20:01:25 +01:00
Update README.md
This commit is contained in:
parent
9d2d11dcc8
commit
dd9aaa04de
1 changed files with 103 additions and 0 deletions
103
README.md
103
README.md
|
@ -17,6 +17,109 @@ The syntax is a superset of the Forth language. In FCL there are literal syntax
|
||||||
|
|
||||||
## Control structures
|
## Control structures
|
||||||
|
|
||||||
|
FCL supports the traditional Forth conditional and loop control structures.
|
||||||
|
|
||||||
|
General form of `if else then`.
|
||||||
|
|
||||||
|
```forth
|
||||||
|
<bool> if <consequent> else <alternative> then
|
||||||
|
```
|
||||||
|
|
||||||
|
For example:
|
||||||
|
```forth
|
||||||
|
: max ( a b -- max )
|
||||||
|
2dup < if nip else drop then ;
|
||||||
|
|
||||||
|
10 100 max . \ prints 100
|
||||||
|
```
|
||||||
|
|
||||||
|
The else part is optional.
|
||||||
|
|
||||||
|
```forth
|
||||||
|
: abs ( n -- absn )
|
||||||
|
dup 0< if -1 * then ;
|
||||||
|
|
||||||
|
-10 abs . \ prints 10
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Case statement
|
||||||
|
|
||||||
|
FCL supports switch-case like flow control logic as shown in the following example.
|
||||||
|
|
||||||
|
```forth
|
||||||
|
: day ( n -- )
|
||||||
|
case
|
||||||
|
1 of print: 'Monday' endof
|
||||||
|
2 of print: 'Tuesday' endof
|
||||||
|
3 of print: 'Wednesday' endof
|
||||||
|
4 of print: 'Thursday' endof
|
||||||
|
5 of print: 'Friday' endof
|
||||||
|
6 of print: 'Saturday' endof
|
||||||
|
7 of print: 'Sunday' endof
|
||||||
|
drop 'Unknown'
|
||||||
|
endcase ;
|
||||||
|
````
|
||||||
|
|
||||||
|
#### Count-controlled loops
|
||||||
|
|
||||||
|
The `limit` and `start` before the word `do` defines the number of times the loop will run.
|
||||||
|
|
||||||
|
```forth
|
||||||
|
<limit> <start> do <loop-body> loop
|
||||||
|
```
|
||||||
|
|
||||||
|
*Do* loops iterate through integers by starting at *start* and incrementing until you reach the *limit*. The word *i* pushes the loop index onto the stack. In a nested loop, the inner loop may access the loop variable of the outer loop by using the word *j*.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
```forth
|
||||||
|
5 0 do i . loop \ prints 0 1 2 3 4
|
||||||
|
```
|
||||||
|
|
||||||
|
It is important to understand the implementation details of this loop. `DO` loops store the loop index on the return stack. You can break the semantics of *i* and *j* if you use the return stack to store temporary data. Exiting from the loop requires clearing up the return stack by using the `unloop` word.
|
||||||
|
|
||||||
|
#### Condition-controlled loops
|
||||||
|
|
||||||
|
##### until loop
|
||||||
|
|
||||||
|
```forth
|
||||||
|
begin <loop-body> <bool> until
|
||||||
|
```
|
||||||
|
The *begin*...*until* loop repeats until a condition is true. This loop always executes at least one time.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
```forth
|
||||||
|
: countdown ( n -- )
|
||||||
|
begin
|
||||||
|
dup .
|
||||||
|
1- dup
|
||||||
|
0 < until
|
||||||
|
drop ;
|
||||||
|
|
||||||
|
5 countdown \ prints 5 4 3 2 1 0
|
||||||
|
```
|
||||||
|
|
||||||
|
##### while loop
|
||||||
|
|
||||||
|
```forth
|
||||||
|
begin .. <bool> while <loop-body> repeat
|
||||||
|
```
|
||||||
|
For example:
|
||||||
|
```forth
|
||||||
|
: countdown ( n -- )
|
||||||
|
begin
|
||||||
|
dup 0 >=
|
||||||
|
while
|
||||||
|
dup . 1-
|
||||||
|
repeat
|
||||||
|
drop ;
|
||||||
|
|
||||||
|
5 countdown \ prints 5 4 3 2 1 0
|
||||||
|
```
|
||||||
|
|
||||||
|
Control structres are compile time words with no interpretation semantics.
|
||||||
|
|
||||||
|
|
||||||
## Locals
|
## Locals
|
||||||
|
|
||||||
## Maps
|
## Maps
|
||||||
|
|
Loading…
Reference in a new issue