fcl/README.md

163 lines
3.8 KiB
Markdown
Raw Normal View History

2021-06-19 20:58:54 +02:00
# FCL - Forth Calculator's Language
FCL is the programming language of an Android app called Forth Calculator. It is a Forth dialect with optional local variables, complex data structures, quotations and Java interoperability.
```forth
: fib ( n1 n2 -- n1 n2 n3 ) 2dup + ;
2021-06-19 20:59:25 +02:00
2021-06-19 21:02:03 +02:00
: nfib ( n -- .. ) -> n ( local variable )
2021-06-19 21:12:23 +02:00
0 1 { fib } n times ; ( quotation )
2021-06-19 20:58:54 +02:00
```
2021-06-19 21:08:32 +02:00
Besides all the high-level features, FCL supports the traditional Forth programming structures and uses the same compilation model (compile/interpret mode, dictionary, immediate words, etc.) as traditional Forth systems.
## The Syntax
2021-06-19 21:23:08 +02:00
The syntax is a superset of the Forth language. In FCL there are literal syntax for creaing Lists `[ 1 2 3 ]`, Maps `#[ 'key' 'value' ]#`, Quotations `{ dup + }` and Strings `'Hello World'`.
2021-06-19 21:21:35 +02:00
## Control structures
2021-06-19 21:33:31 +02:00
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
```
2021-06-19 21:34:21 +02:00
The `else` part is optional.
2021-06-19 21:33:31 +02:00
```forth
2021-06-19 21:35:24 +02:00
: abs ( n -- n )
dup 0 < if -1 * then ;
2021-06-19 21:33:31 +02:00
-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
2021-06-19 21:36:46 +02:00
0 <
until
2021-06-19 21:33:31 +02:00
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.
2021-06-19 21:08:32 +02:00
## Locals
2021-06-19 21:43:27 +02:00
```
: example ( a b -- n )
-> b -> a 42 -> c 0 => d
a b + c * d !
d @ ;
```
There are two types of locals in FCL. Local constant `->` and local variable `=>`.
2021-06-19 21:49:40 +02:00
`-> a` loads the top of the stack into the local, called `a`.
2021-06-19 21:43:27 +02:00
2021-06-19 21:49:40 +02:00
`a` pushes the value of the local.
2021-06-19 21:43:27 +02:00
2021-06-19 21:49:40 +02:00
`=> b` loads the top of the stack into the local variable, called `b`.
2021-06-19 21:43:27 +02:00
2021-06-19 21:49:40 +02:00
`b` pushes the reference of the local. `b @` pushes the value of the local.
The `->` and `=>` words can be used anywhere within a word, including loop bodies and quotations. You can initialize a local (`0 -> a`) within the word or use the data that was supplied on the call site (`-> a`).
```
: count-even ( n -- c )
-> n 0 => count
n 0 do
i 2 /mod -> quotient -> remainder
remainder 0 = if
count inc
then
loop
count @ ;
```
2021-06-19 21:08:32 +02:00
## List
2021-06-19 21:43:27 +02:00
## Maps
2021-06-19 21:21:35 +02:00
## Quotations
2021-06-19 21:08:32 +02:00
## HTTP