Translated using translate google. The original language is Russian. Original page https://hi-aga.ru/

Programming is easy and simple!

Don't believe me?! If you do it right, check for yourself how easy it can be if you choose the right language and environment. This programming course is based on solving practical problems.

Here we will begin to learn the Forth programming language from scratch. The syntax of this PL (Programming Language) is so simple that it is practically non-existent. There is only one main rule - all Words (that’s what language operators and user-defined function words are called) and numbers must be separated by at least one space and/or tab and/or line break character (that is, “any empty character, delimiter").

Who is this site for?

It is so easy to start learning programming in this language that the material on this site can be used for children, not to mention teaching this wonderful type of human activity, for schoolchildren, applicants and students. So this material can be used by any interested person, regardless of age and type of human activity. The complexity will increase gradually, from scratch, with the opportunity to tighten up difficult moments and omissions of primitives, already prepared by readers.

The SP-Forth programming environment is perfect for our purposes. You can start programming for free in this console application immediately after quickly downloading and installing the latest version from the official website. We will work in interpreter mode. This means that after starting the SP-Forth programming system, you can enter commands or an entire program, after which, by pressing <Enter>, the application will process the code and produce the intended results. Downloading and installing SP-Forth should not cause any difficulties, and therefore we will omit this process here.

Historically, the basics of programming begin with the first program that displays “Hello, World!”, which in Russian means “Hello, World!”

The code for YAP Forth will be as follows:

.(Hello world!)

or

S "Hello, World!" TYPE

After which the above message will be displayed on the screen. PL (programming language) operators Forth <.(> and <S">: the first takes the text following it up to the closing quote - " (line end sign) and prints it on the screen, the second creates a string whose address with its length is sent to stack. Then the TYPE operator, using these numbers, the address and its size, as parameters, also prints a message. The main “TYPE” must go immediately, in order to avoid execution errors, this is a feature of the system. Pay attention to spaces - in the fort they separate the words of the language Why is the second option needed if the first one is simpler and shorter? Because it is universal, this is how strings are defined in Forth, and they will be useful in the future.

The third version of the same program will look like this:

: Hello_World "Hello, World!" ;
Hello_World

The difference is that we define a word of the same name, which is then called up. The first two options work in interpretation mode, and this one works in compilation mode (a new word is created in the fort dictionary). Being universal, it can be called repeatedly from different places in the program, or even use this code in your libraries.

So we wrote the first full-fledged program. Instead of Hello_World, you can call it Start or whatever you want, the main thing is to adhere to a simple rule - the names should be informative. The programming style in Forth is no different from other languages; it will be useful to read about this in the future.

This is how we defined a new word. The definition of a word begins with a colon, followed by any delimiter character (space, tab, or line feed). After that comes the name of the word that you come up with, then the code is the implementation (a sequence of operators, numbers and other already defined words), separated by spaces. Ends with a semicolon, also separated from the code by a space.

The beginning has already been made. The first program of the Forth programming course for beginners looks quite simple.

Let's add a couple of touches and comments:

It is customary to write a comment after the name in each definition of a new word, indicating what the word takes from the stack as parameters and what it leaves on it. Let's rewrite our first Fort Word:

: Hello_World ( -> ) "Hello, World!" ; Hello_World

Since Hello_World leaves the stack unchanged (does not touch it), then before and after the arrow is empty. The comment is the content of the parentheses.

There is also a second way to comment out the code until the end of the line. This symbol is “\”.

: Hello_World ( -> ) "Hello, World!" ; Hello_World is a program that prints the message "Hello, World!"

Programming yourself is not particularly difficult, even for complete beginners. This is not a C or C++ programming language. Everything is quite clear and simple. You can practice programming online or offline. For the first option, there is a script translator of the Fort language. We will focus on a specific dialect - SP-Forth. It exists for all popular systems (Windows, Linux).

Usually the second task when learning to program is writing a calculator. There is no need to write a calculator in Forth, since it supports basic operations natively, albeit in a somewhat unusual format, which is called reverse Polish notation or postfix. In mathematics, you are used to writing formulas in infix form like (1+2)*5(4-5), where the sign of a binary operation is written between the numbers, for example, in Lisp, the operation comes first, and then the operand or operands, and in Forth it’s the other way around, first we push the operands onto the stack, then the operation performs actions on them, leaving the result there.
The stack is simply a memory location supported by hardware, hence all operations on them are performed very quickly, where our intermediate data will be stored.

This is what working with our calculator will look like:

Operand1 Operand2 Operation. That is, instead of 1+2 in Forth, we should write “1 2 +”.
1 2 +

 Ok ( 3 ) \ 1+2=3 in parentheses is the contents of the stack

12 *

 Ok ( 3 2 ) \ 1*2=2 next result on top of the stack

12 -

 Ok ( 3 2 4294967295(-1) ) \ 1-2=-1 4294967295 – this is an unsigned version of the number -1

12 /

 Ok ( 3 2 4294967295(-1) 0 ) \ 1/2=0 – this is an integer division, so the result is zero

1 2 MOD

Ok ( 3 2 4294967295(-1) 0 1 ) \ remainder of division 1/2

1 2 /MOD

Ok ( [7].. 4294967295(-1) 0 1 1 0 ) \ remainder of division 1/2 and integer part 1/2

You can say programming in Python is much richer in possibilities, and you would be right. But Forth is faster and, as a first programming language, it is much simpler and easier to learn, and most importantly, more understandable. Learning programming in Pascal, in my opinion, is no longer relevant. Although it is good for learning various algorithms, it is no longer a modern programming language, with a lot of redundant constructs and syntax.

Here are examples of programming in SP-Forth.

: ^2 ( A -> A^2 ) DUP * . ; \ squaring a number

: ^3 ( A -> A^3 ) DUP DUP * * . ; \ cube a number

: ^4 ( A -> A^4 ) DUP * DUP * . ; \ raising a number to the fourth power

DUP is a word that simply duplicates the number at the top of the stack.

Let's square 5, to do this we type on the keyboard:

5^2

25 Ok

We get the correct answer. You need to copy the code carefully, so when you copy the first word, squaring it, you get an error message

: ^2 ( A -> A^2 ) DUP * . ;\ squaring a number
                           ^ -2003 WORD OR FILE NOT FOUND

With Copy Paste, the tab characters between the “;” disappeared somewhere. and a comment, as a result we see an error message due to a syntax violation. This is probably a feature of the system.

Also, instead of ^2, you can call the word **2, in python style:

: **2 ( A -> A^2 ) DUP * . ; \ squaring a number
5 **2
25 Ok

As a result, we get the same answer, but the style of the program has changed.

This is enough to get started. Further, when solving specific problems in the SP-Forth programming environment, the process will become more understandable and conscious.