This commit is contained in:
Miloslav Ciz 2023-12-21 12:04:09 +01:00
parent 7573a09d17
commit b931aa36c3
10 changed files with 66 additions and 27 deletions

View file

@ -1,24 +1,24 @@
# Forth
Forth is a based [minimalist](minimalism.md) stack-based untyped programming language with [postfix](notation.md) (reverse Polish) notation.
Forth is a very good [minimalist](minimalism.md) [stack](stack.md)-based untyped [programming language](programming_language.md) that uses [postfix](notation.md) (reverse Polish) notation. It is yet much simpler than [C](c.md), it's very [elegant](elegant.md) and its compiler/interpreter can be made very easily, giving it high practical freedom (i.e. not being practically controlled by any central organization); it is used e.g. in space technology and embedded systems as a way to write efficient [low level](low_level.md) programs that are, unlike those written in [assembly](assembly.md), [portable](portability.md) (there even exist computers directly running Forth in hardware). Forth was the main influence for [Comun](comun.md), the [LRS](lrs.md) language.
{ It's kinda like usable [brainfuck](brainfuck.md). ~drummyfish }
It is usually presented as [interpreted](interpreter.md) language but may as well be [compiled](compiler.md), in fact it maps pretty nicely to [assembly](assembly.md).
There are several Forth standard, most notably ANSI Forth from 1994.
There are several Forth standards, most notably ANSI Forth from 1994 (the document is [proprietary](proprietary.md), sharing is allowed, 640 kB as txt).
A free interpreter is e.g. GNU Forth ([gforth](gforth.md)).
A [free](free_software.md) implementation is e.g. GNU Forth ([gforth](gforth.md)) or [pforth](pforth.md) (a possibly better option by LRS standards, favors [portability](portability.md) over performance).
## Language
The language is case-insensitive.
Forth is case-insensitive (this may however not be the case in some implementations).
The language operates on an evaluation **stack**: e.g. the operation + takes the two values at the top of the stack, adds them together and pushed the result back to the stack. Besides this there are also some "advanced" features like variables living outside the stack, if you want to use them.
The language operates on an evaluation **[stack](stack.md)**: e.g. the operation + takes the two values at the top of the stack, adds them together and pushed the result back on the stack. Besides this there are also some "advanced" features like variables living outside the stack, if you want to use them.
The stack is composed of **cells**: the size and internal representation of the cell is implementation defined. There are no data types, or rather everything is just of type signed int.
Basic abstraction of Forth is so called **word**: a word is simply a string without spaces like `abc` or `1mm#3`. A word represents some operation on stack (and possible other effect such as printing to the console), for example the word `1` adds the number 1 on top of the stack, the word `+` performs the addition on top of the stack etc. The programmer can define his own words which can be seen as "functions" or rather procedures or macros (words don't return anything or take any arguments, they all just invoke some operations on the stack). A word is defined like this:
Basic abstraction of Forth is so called **word**: a word is simply a string without spaces like `abc` or `1mm#3`. A word represents some operation on stack (and possible other effect such as printing to the console), for example the word `1` adds the number 1 on top of the stack, the word `+` performs the addition on top of the stack etc. The programmer can define his own words which can be seen as "[functions](function.md)" or rather procedures or macros (words don't return anything or take any arguments, they all just invoke some operations on the stack). A word is defined like this:
```
: myword operation1 operation2 ... ;
@ -77,6 +77,7 @@ do C loop loops from stack top value to stack second from,
begin C until like do/loop but keeps looping as long as top = 0
begin C while like begin/until but loops as long as top != 0
allot allocates memory, can be used for arrays
recurse recursively call the word currently being defined
```
@ -94,3 +95,22 @@ cr ." hey bitch " cr \ prints: hey bitch
: myloop 5 0 do i . loop ; myloop \ prints 0 1 2 3 4
```
## How To
Source code files usually have `.fs` extension. We can use mentioned gforth to run our files. Let's create file `my.fs`; in it we write: { Hope the code is OK, I never actually programmed in Forth before. ~drummyfish }
```
: factorial
dup 1 > if
dup 1 - recurse *
else
drop 1
then
;
5 factorial .
bye
```
We can run this simply with `gforth my.fs`, the programs should write `120`.