- cf is inspired by ColorForth, but it is NOT ColorForth.
- cf has 4 states: DEFINE, COMPILE, INTERPRET, and COMMENT.
- cf uses markers in the source whitespace to control its state.
- cf also supports using the standard words to change the state.
- cf has
aandbregisters like ColorForth. It also has atregister. - Unlike ColorForth, they are actually stacks. See below for more information.
| Byte | Word(s) | New State |
|---|---|---|
| $01 | ":" | DEFINE |
| $02 | "]" or ")" | COMPILE |
| $03 | "[" or "))" | INTERPRET |
| $04 | "(" or "((" | COMMENT |
| ";" | INTERPRET |
- DEFINE changes the state to COMPILE after adding the word to the dictionary.
- ";" compiles EXIT and changes the state to INTERPRET.
- There is no difference between
(and((, they make the code more readable. - CF still supports IMMEDIATE words.
- Flow control words like IF/THEN would be marked as IMMEDIATE.
- They are defined in the source file.
(( A comment in INTERPRET mode ))
: hello ( A comment in COMPILE mode ) ." Hello World!" ;
hello
In cf, a program is a sequence of OPCODEs.
An OPCODE is a CELL-sized unsigned number (32 or 64 bits).
Primitives are assigned numbers sequentially from 0 to (bye).
If an OPCODE is less than or equal to (bye), it is a primitive.
If the top 3 bits are set, it is an unsigned literal with the top 3 bits masked off.
Else, it is the CODE slot of a word to execute.
CODE slots 0-24 are used by cf.
CODE slots 25-(bye) are free CELL-sized slots that can be used for any purpose.
HERE starts at (bye)+1.
In ColorForth, 'a', 'b' and 't' are registers. In CF, they are stacks.
They have operations similar to those for the return stack (>r, r@, r>).
The operations for the 'a' stack are: >a, a@, a>, a!, a@+, and a@-.
The operations for the 'b' and 't' stacks are the same.
CF is really just a Forth VM, upon which any Forth system can be built.
To that end, cf provides a set of primitives and the inner/outer interpreters.
See cf.c for the list of primitives.
The rest of the system is defined by the source code file.
CF takes a source file as its only argument.
If cf is executed without arguments, the default source file is 'boot.fth'.
CF provides a single chunk of memory (see cf.h, MEM_SZ) for data and code.
The CODE area starts at the beginning of the memory.
CF can easily be embedded into a C program.
The cf VM is implemented in cf.c.
The configuration and API for cf are located in cf.h.
File system.c embeds the CF VM into an executable.
Building cf is simple and fast since there are only 3 small source files.
32-bit or 64-bit builds are supported.
Windows
- There is a
cf.slnfile for Visual Studio.
Linux, OpenBSD, and FreeBSD
- There is a makefile, which uses the system C compiler (specified by the CC variable).
- Or you can easily build cf from the command line:
- Example:
# default, 64 bit:
make
# for 32 bit:
ARCH=32 make
# or manually:
$CC -m32 -O3 -o cf *.c
$CC -m64 -O3 -o cf *.c
- Blocks are not native to cf.
- They are implemented in the default source file.
- A block editor is implemented in the default source file.
- The editor has a vi-like feel.
