Skip to content

100.Benefits of Forth

Rob Probin edited this page Dec 23, 2023 · 2 revisions

What is Forth?

Forth is a unique and versatile programming language, distinguished by its simplicity, direct hardware manipulation capabilities, and a design philosophy that emphasizes direct control and efficiency.

Unlike many languages, Forth employs a stack-based architecture and a concise, postfix notation (also known as Reverse Polish Notation). This design choice minimizes the language's syntax and allows programmers to write highly efficient and compact code, making it particularly well-suited for systems with limited computational resources.

Forth's unique approach also influenced the development of interactive programming environments. Its interactive shell allows for immediate execution and testing of code, a feature that was innovative at the time and predates the modern Read-Eval-Print Loop (REPL) used in languages like Python and JavaScript.

It is often celebrated for its elegance and the degree of control it offers to programmers, particularly in the context of hardware-oriented and embedded system programming. The Sinclair Spectrum Next, a modern incarnation of the classic Sinclair ZX Spectrum, is one such platform where Forth's capabilities shine, allowing developers to harness the full potential of this retro-inspired hardware with a language that is both historically significant and practically powerful.

As we delve into the specific advantages of using Forth on the Sinclair Spectrum Next, it's important to appreciate this historical context and the distinctive qualities that make Forth a fascinating and enduring choice in the world of programming.

Forth and the ZX Spectrum

The Forth programming language has been popular with the Sinclair ZX Spectrum - and a good number of Forths were produced in the 1980s for the machine, including Artic Spectrum Forth, Abersoft Forth, White lightning and several others.

vForth follows in their footsteps for the Sinclair Spectrum Next. There is also a version for the Spectrum with ZX Microdrive and DISCiPLE MGT.

https://github.com/mattsteeldue/vforth-next https://github.com/mattsteeldue/vforth

Why Forth?

The advantages of using Forth on the Sinclair Spectrum Next are:

  • Forth is interactive.
  • Forth is incremental.
  • Forth is fast to run and to develop.
  • vForth has a built-in Z80 assembler to make Z80 machine code.
  • You can develop in Forth and then later convert parts (or all of it) to Z80 machine code to speed the whole program up.
  • You can test both Forth and Z80 machine code routines as you write it from the Forth command line!

Remember, most of the time it's only necessary to convert the most deepest, used routines into machine code - because this is where the Z80 processor spends most of it's time. Finding which these are is can be tricky - and guesses are usually not correct. Measuring routine times and profiling tell you where. But that's a subject for another day.

Forth is Interactive

You can execute Forth as you go - and test right in the programming environment easily. This can be used to develop a program in a stepwise manner - bottom-up or top-down and test as you go. This interactive approach gets results quickly and allows you to avoid big bugs that are hard to find. The Forth terminal is similar to a command line interpreter or REPL (Read, Evaluate, Print, and Loop) in languages like Lisp and Python.

Forth is Incremental

It allows you to build up your program bit-by-bit.

Forth is Fast - when it runs

Although not as fast as Z80 machine code, it's still very good. This means it's easier to leave more code in a high level language.

Additionally other languages like C have a hard time targetting the Z80 8-bit architecture - so tend to product large, relatively slow binaries. This is a big difference from PC's - either x86 or ARM - which processors were designed with C in mind.

There might be other languages which are good - there are reports of reasonable Z80 Pascal interpreters/compilers for the Z80 - both CP/M and native (Hisoft Pascal, xxx). However Forth is good at small targets because you can drop into Z80 assembly language to speed up a key routine easily - and test it as you go.

Forth is Fast - to develop

Forth allows a very fast programming pace because of:

  • Immediate feedback as you work - make sure everything works ahead of time.
  • Interactive approach - quick testing of words as you go.
  • Easy to make special development tools to allow you to examine what is happening without spending lots amounts of time on custom tooling.
  • You can add easy support of unit tests - so you can ensure deep definitions don't change.
  • Develop a custom DSL, a domain specific language, make your high level language match whatever problem you are solving
  • Easily add features from any programming language - you are not tied to a limited number of keywords or concepts. Want object oriented programming? No problem. Want event loop programming? No problem. The list is as flexible as you want.

Once you know Forth, some people have estimated that is can be 10x faster than C or C++. This can give a similar speed up that languages like Python can give on large machines like PCs.

vForth has a built-in Z80 assembler

You will probably need to make Z80 machine code - or reuse someone elses routine. But programming everything in Z80 machine code reduces the speed you can develop.

The speed of development of languages like BASIC, C, Java, C++ and more recently Python has allowed people to get more done faster. Forth brings that interactive rapid development to the Sinclair ZX Spectrum Next - but also allows you to get power out the machine with Z80 machine code.

Develop in Forth, convert to Z80

and then later convert parts (or all of it) to Z80 machine code to speed the whole program up.

Test Forth and Z80 interactively

You can test machine code routines as you write it from the Forth command line, in the same way you'd encourage you to treat Forth development.