Table of Contents

[hemmerling] FORTH 5/7

Related pages:

Implementation of your own FORTH System & Applications

FORTH - The Good, the Bad and the Ugly

Is this a Forth at all? :-)

The Good - Valuable OpenSource FORTHs

⇒ See FORTH 3/4.

The Bad - Commercial FORTH Systems

  1. No source code. Means you can´t learn about the implementation of FORTH words & libraries... for better understanding. The application developers are lost!
  2. Compatibility problems, and nobody takes care of it. The application developers are lost!

The Ugly - Toy FORTH

  1. Not beeing developed by
    1. Senior developers with professional skills in compiler design, embedded systems,.. with 5+y experience ( e.g. college / university professors, engineers with much professional industrial experience,... ).
    2. Senior “assembly language artists” :-) with 5+y of experience.
  2. Without free and immediate / fast / quick support on professional level.
  3. Without comprehensive paper / PDF / HTML,... documentation.
  4. Without comprensive comments in the source code.
  5. Without much sample code & sample projects.
  6. Which might be “not in active development anymore”.
  7. Where the developer gets angry if his / her FORTH is considered to be a “Toy FORTH” :-(.
  8. Do most of the 1000 FORTHs have “more than one user” ? Or is it typical, that either
    1. The FORTH has no killer-application at all ( the only developer develops for all the years just FORTH, not a software solution for a real given application / problem ),
    2. Or the only developer has ONE software / hardware application for which he/she is using the self-developed FORTH with his self-developed software application code on top of the bare FORTH implementation?

Valuable FORTH vs. Toy FORTH

Write your own FORTH Compiler

Slogan "Roll your own programming language - Roll your own Forth"

Implementation Types

Threaded Code
Tethered FORTH
Native FORTH with Threaded Code
Native tethered FORTH
SRT / NCI FORTH ( Subroutine threaded / native Code inlining FORTH )
FORTH which may create Application Executables
Resources

Implementation Suggestions "How to start"

Implementation Ideas

The Input, Input Processing & Compile Process ( Experts told me so )

Inner Interpreter, Outer Parser, Compiler

Word Compilation

How to handle numbers with compiled words?
How to handle parser & interpreter recursion when dealing compiled Forth words?
In Forth interpret mode, what is the proper reaction to a Forth word to be used in Forth compilation mode?
Where is the "thread" to which the instruction pointer is incremented in the so-called "next()" operation of an inner interpreter?
General

Data output according to current BASE Setting

Stack Organisation & Stack Checking

    • User comment “How would you implement such a construct? With #defines or different typedefs that define the CELL size? I suppose you might be able to have a return stack that is 32-bit or 64-bit (for the longer addresses the system requires), but a 16-bit data stack for the calculations. But that would make it hard for the data stack to have an address that is executed (ie - EXECUTE), NONAME, or things like jump tables. Or have a 32/64 bit stack, with a setting that controls the “bit-ness” that the operations would look at?”.

Java VM Bytecode => Forth VM Bytecode Mapping by Chen-hanson Ting

The minimal FORTH System "Minimal Forth Machine" by Mikael Patel

Primitives
Primitive Stack effects Description
>r ( x – )
r> ( – x )
1+ ( x – y )
0= ( x – flag )
nand ( x y – z )
@ ( addr – x )
dup! ( x addr – x )
execute ( addr – )
exit ( – )
May be included because of hardware considerations
drop ( x – )
dup ( x – x x )
swap ( x y – y x )
Resources

The minimal FORTH System "sectorforth"

Primitives
Primitive Stack effects Description
@ ( addr – x ) Fetch memory contents at addr
! ( x addr – ) Store x at addr
sp@ ( – sp ) Get pointer to top of data stack
rp@ ( – rp ) Get pointer to top of return stack
0= ( x – flag ) -1 if top of stack is 0, 0 otherwise
+ ( x y – z ) Sum the two numbers at the top of the stack
nand ( x y – z ) NAND the two numbers at the top of the stack
exit ( r:addr – ) Pop return stack and resume execution at addr
key ( – x ) Read key stroke as ASCII character
emit ( x – ) Print low byte of x as an ASCII character
Variables
Variable Description
state 0: execute words; 1: compile word addresses to the dictionary
tib Terminal input buffer, where input is parsed from
>in Current parsing offset into terminal input buffer
here Pointer to next free position in the dictionary
latest Pointer to most recent dictionary entry
Compiler
What can I do?

The minimal FORTH System of GreenArrays' GA144

#(oct) Primitive FORTH Word #1 FORTH Word #2 Stack effects Description
Jump Instructions
00 Ret ”;” ”;” Jump thru R (destructive)
01 Exec “ex” ” ;:” Jump thru R, save P in R
02 Jmp “jump” “jump” Jump thru I ( The opcode is not used explicitly. The compiler generates it )
03 Call “call” “call” Jump thru I, push current address to R ( The opcode is not used explicitly. Referencing any defined word generates a call )
04 Unext “unext” “unext” Jump to slot 0 ( Discards the address left by for )
05 Next “next” “next” If R is non-zero, jump thru I and decrement R. Otherwise pop R
06 If “if” “if” Jump thru I if T is zero
07 MinusIf ”-if” ”-if” Jump thru I if T is positive
Memory Instructions
10 FetchP ”@p” ”@p+” Fetch thru P, increment P
11 FetchPlus ”@+” ”@+” Fetch thru A, increment A
12 FetchB ”@b” ”@b” Fetch thru B
13 Fetch ”@” ”@” Fetch thru A
14 StoreP ”!p” ”!p+” Store thru P, increment P
15 StorePlus ”!+” ”!+” Store thru A, increment A
16 StoreB ”!b” ”!b” Store thru B
17 Store ”!” ”!” Store thru A
ALU Instructions
20 MultiplyStep ”+*” ”+*” Multiply step: add S to T if A0=1 then shift T and A right
21 Times2 “2*” “2*” Shift T left
22 Div2 “2/” “2/” Shift T right; sign fill
23 Not ”-” ”-” One's complement T
24 Plus ”+” ”+” Add S to T (discard S)
24 Add S to T with carry ( Requires bit 9 of P be set )
25 And “and” “and” Bit-wise and of S and T
26 Or “or” “or” Bit-wise exclusive-or of S and T
27 Drop “drop” “drop” Discard T
Stack Instructions
30 Dup “dup” “dup” Create a working copy of T
31 Pop “pop” “pop” Fetch R (destructive)
32 Over “over” “over” Fetch S (non-destructive)
33 ReadA “a” “a” Fetch A (non-destructive)
34 Nop ”.” ”.” Do nothing
35 Push “push” “push” Push T into R
36 SetB “b!” “b!” Store into B ( Be careful to distinguish b! and !b )
37 SetA “a!” “a!” Store into A

The minimal FORTH System Proposal & Comparison by Peter Knaggs and Paul E. Bennet

The minimal FORTH System Comparison by Paul E. Bennet
Type Ting Brinkhoff Plichota GA-F18 Stack effect Comments
Memory Access C@ C@
C@ C@
@ @ @ @
a
@a
@p
@b
@+
!p
!+
!b
a!
b!
! ! ! !
Math + + + +
- -
+*
2*
* *
/
2/
MOD
Logic AND NAND NAND and
OR or
XOR XOR
INVERT
Stack DUP DUP dup
SWAP SWAP
OVER over
DROP DROP drop
>R >R push
R> R> pop
R@
Control IF 0BRANCH IF if
-if
ELSE
THEN
BEGIN
WHILE
REPEAT
AGAIN
DO
LOOP
EXECUTE ex
<name>;
<name>
unext
next
EXIT EXIT
Defining : :
; ; ;
CONSTANT
VARIABLE
I/O & Comms IN IN i/o
OUT OUT
Other DODOES LIT
data
- - -u
- -l-
–lu
-d- -
-d-u
-dl-
-dlu
r- - -
r-l-
r-lu
rd- -
rdl-
rdlu
The minimal FORTH System Proposal by Peter Knaggs and Paul E. Bennet
Type Word Name Stack effect Comments
1 Memory Access ! store
, comma
@ fetch
ALIGN
ALIGNED
CELL+ cell-plus
CELLS
C! c-store
C, c-comma
C@ c-fetch
CALIGN c-allign
CALIGNED c-alligned
CHAR+ char-plus
CHARS chars
2 Arithmetic + plus
* star
2* two-star
*/MOD star-slash-mod
- minus
/ slash
2/ two-slash
MOD
3 Logic 0= zero-equals
< less-than
AND
INVERT
TRUE
LSHIFT l-shift
= equals
> greater-than
OR
XOR x-or
FALSE
RSHIFT r-shift
4 Stack DUP dupe
SWAP
>R to-r
R@ r-fetch
DROP
OVER
R> r-from
ROT rote
5 Flow Control IF
THEN
WHILE
REPEAT
DO
I
tick
ELSE
BEGIN
AGAIN
UNTIL
LOOP
J
EXECUTE
6 Definitions : colon
CONSTANT
CREATE
; semicolon
VARIABLE
DOES> does
7 Device KEY
EMIT
KEY? key-question
CR c-r
8 Tools ( paren
.S dot-s
\ backslash
The Core Definitions of FIGnition FORTH ( Bytecodes implemented in Assembly Language )
Word Name Stack effect Comments
(lit)
execute
(branch)
(0branch)
(loop)
(+loop)
(do)
i
leave
and
or
xor
«
»
;s
(does)
r>
>r
r
0=
0<
+
d+
minus
dminus
over
drop
swap
dup
@
c@
!
ic!
vram
clock
at
cls
.hex
edit
list
trace
plot
blk>
>blk
>port>
spi
Resources

General Compiler Building Resources

FORTH System Building Resources

Build Tools for FORTH Virtual Machines ( FVMs )
FORTH in LUA
Some other minimal FORTH
Resources

Excursus: C Data Types

Excursus: Use of SQL Databases to store FORTH Words

Write your own FORTH Code - FORTH Code Style

Traditional FORTH

"!", "C!" ( of fig-FORTH ) versus "=" and "\=" ( of pre fig-FORTH )

Readability

Stack-Orientation

Style Conventions

Modern FORTH

1Kbyte Screens / Blocks

Array Words

Batch Processing

Built-in Web Server

  1. Forth2020 will be shipped with a little Web Server.
  2. Server written with RETRO FORTH.
    • The OpenSource FORTH ForthWorks by Charles Childers "RETRO" ( RETRO Forth ).
      • “A clean, elegant, and pragmatic dialect of Forth. It provides a simple alternative for those willing to make a break from legacy systems”.
      • “You can view the glossary via Gopher or HTTP. This is served off the latest documentation in the repository by a server written in RETRO”.

Capitalisation, or not?

Clean Code

Code Quality

Compilation of FORTH Words, Immediate Words

CREATE DOES> ;
Resources

Data Formats

BASE
Input of Values with a specific Data Type ( Single Precision Integer, Double Precision Integer, Floats,.. )
Norms
Implementations
Float versus Fixed Integer
Floating Point Arithmetics

Debugging

Implementations
Resources

Deferred Words ( DEFER, IS )

Exception Handling

ABORT, ABORT", CATCH, THROW, QUIT
Examples of exception handling for FORTH, in Assembly Language
Homebrewn Exception Handling for FORTH, in C
Homebrewn Exception Handling for FORTH, in C++
Homebrewn Exception Handling for FORTH, in Java
Homebrewn Exception Handling for FORTH, in Lua
Resources

Functional Programming with FORTH?

Concept for an in-FORTH Help

Input / Output - CPU / OS specific Memory I/O & Port I/O Access

Standard FORTH Memory I/O
ESP32forth
Open Firmware provides "Register" Access Words meant to access Hardware Devices
Port I/O for Intel 80x86, 808x & Z80
BoardForth
Memory I/O for highly-optimizing FORTH Compilers to avoid Memory Access Optimisation
Port I/O, alternative Syntax
Jupiter Ace ( "FORTH Programming" Manual, Chapter 26 )
Excursus: Intel 8086 Port I/O with MSDOS C Compilers
Excursus: ARM Memory I/O with C Compilers
Resources

Interoperability

Locals

Loops ( DO ... LOOP )

Mocking for Top-Down Development

Multitasking & Interrupt Handling

Concept for Interrupt Handling
Multitasking Implementations
Multitasking Documentation
Resources

Object-Orientation, Object-Oriented Forth

Implementations
Resources

Stack

Stack Juggling vs. Use of Global Variables & Registers

Stack Juggling
Use of Global Variables & Registers

The 3 States - Interpret, Immediate, Compile

Testing - Provision of a Unit Test Framework

Warnings

Word List by VLIST, WORDS

Appropriate OpenDirectory Directory Pages


When this document changes ! Site Navigation ( My Business ! My Topics ! Imprint / Contact ! Privacy Policy ! Keyword Index ! ! Google+ Publisher "hemmerling" )