Documentation

Ficl Documentation

Core reference, features, and API details.


Contents

What is ficl?

Ficl is a lightweight, embeddable scripting language designed to be incorporated into other programs, including memory constrained embedded systems. Ficl conforms to the 1994 ANSI Standard for Forth, and provides several useful extensions including OOP that can wrap compiled code and hardware interfaces. 

Unlike Lua or Python, Ficl acts as a component of your system: you feed it stuff to do, it does the stuff, and comes back to you for more. You can export compiled code to Ficl, execute Ficl code from your compiled code, or interact with a read-execute-print loop. Your choice. Ficl includes a simple but capable object model that can wrap existing data structures. 

Ficl vs. other Interpreters

Where language interpreters usually view themselves as the center of the system, Ficl acts as a component of the system. It is easy to export compiled code to Ficl in the style of TCL, or to invoke Ficl code from a compiled module. This allows you to do incremental development in a way that combines the best features of threaded languages (rapid development, quick code/test/debug cycle, reasonably fast) with the best features of C (everyone knows it, easier to support large blocks of code, efficient, type checking). In addition, Ficl provides a simple and powerful object model that can act as an object oriented adapter for code written in C/C++. 

Ficl Design goals


References: More information on Ficl

Forth and Threaded Interpretive Languages


LICENSE and DISCLAIMER

Copyright (c) 1997-2026 John Sadler, All rights reserved.

I am interested in hearing from anyone who uses ficl. If you have a problem, a success story, a defect, an enhancement request, or if you would like to contribute to a ficl release, please contact me on Sourceforge. 

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
   may be used to endorse or promote products derived from this software 
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
 

Getting Started: Porting Ficl to your system

To install ficl on your target system, you need an ANSI C compiler (C11 or newer) and its runtime library. System dependent code is isolated to a few functions that you need to implement for your system. See sysdep.h for build controls. 

Edit the definitions (in sysdep.c) of ficlMalloc, ficlFree, ficlRealloc, and ficlTextOut for your application and target. Use testmain.c as a guide to installing the ficl system and one or more virtual machines into your code. 

Ficl includes portable double precision math routines that work for 32 and 64 bit machines. You may replace them with machine-dependent versions by #defining PORTABLE_LONGMULDIV to 0 in sysdep.h and implementing your versions of ficlLongMul and ficlLongDiv in sysdep.c.  

Build controls

The file sysdep.h contains default values for build controls. Most of these are written such that if you define them on the compiler command line, the defaults are overridden. I suggest you take the defaults on everything below the "build controls" section until you're confident of your port. Beware of declaring too small a dictionary, for example. You need about 3200 cells for a full system, about 2000 if you strip out the softwords. 

Softwords

Many words from the supported wordsets are written in Forth and stored as a big string that Ficl compiles when it starts. The sources for all of these words are in directory ficl/softwords. There is a Python 3 script (softcore.py) that converts the .fr files into softcore.c. See the makefile in ficl/softwords. 

Application Programming Interface

The following is a partial listing of functions that interface your system or program to ficl. For a complete listing, see ficl.h (heavily commented). For examples, see testmain.c and the ficlwin sources (below). See the comments in ficl.c and ficl.h for additional information, and the example in file testmain.c.
FICL_SYSTEM *ficlInitSystem(int nDictCells)
Initializes Ficl's shared system data structures, and creates the dictionary allocating the specified number of CELLs from the heap (by a call to ficlMalloc)
void ficlTermSystem(FICL_SYSTEM *pSys)
Reclaims memory allocated for the ficl system including all dictionaries and all virtual machines created by vmCreate. Any uses of the memory allocation words (allocate and resize) are your problem.
int ficlBuild(FICL_SYSTEM *pSys, char *name, FICL_CODE code, char flags)
Create a primitive word in ficl's main dictionary with the given name, code pointer, and properties (immediate, compile only, etc) as described by the flags (see ficl.h for flag descriptions of the form FW_XXXX)
int ficlExec(FICL_VM *pVM, char *text)
Feed the specified C string ('\0' terminated) to the given virtual machine for evaluation. Returns various exception codes (VM_XXXX in ficl.h) to indicate the reason for returning. Normal exit condition is VM_OUTOFTEXT, indicating that the VM consumed the string successfully and is back for more. ficlExec calls can be nested, and the function itself is re-entrant, but note that a VM is static, so you have to take reasonable precautions (for example, use one VM per thread in a multithreaded system if you want multiple threads to be able to execute commands).
int ficlExecC(FICL_VM *pVM, char *text, int nChars)
Same as ficlExec, but takes a count indicating the length of the supplied string. Setting nChars to -1 is equivalent to ficlExec (expects '\0' termination).
int ficlExecXT(FICL_VM *pVM, FICL_WORD *pFW)
Same as ficlExec, but takes a pointer to a FICL_WORD instead of a string. Executes the word and returns after it has finished. If executing the word results in an exception, this function will re-throw the same code if it is nested under another ficlExec family function, or return the exception code directly if not. This function is useful if you need to execute the same word repeatedly - you save the dictionary search and outer interpreter overhead.
void ficlFreeVM(FICL_VM *pVM)
Removes the VM in question from the system VM list and deletes the  memory allocated to it. This is an optional call, since ficlTermSystem will do this cleanup for you. This function is handy if you're going to do a lot of dynamic creation of VMs.
FICL_VM *ficlNewVM(FICL_SYSTEM *pSys)
Create, initialize, and return a VM from the heap using ficlMalloc. Links the VM into the system VM list for later reclamation by ficlTermSystem.
FICL_WORD *ficlLookup(FICL_SYSTEM *pSys, char *name)
Returns the address (also known as an XT in this case) of the specified word in the main dictionary. If not found, returns NULL. The address can be used in a call to ficlExecXT.
FICL_DICT *ficlGetDict(FICL_SYSTEM *pSys)
Returns a pointer to the main system dictionary, or NULL if the system is uninitialized.
FICL_DICT *ficlGetEnv(FICL_SYSTEM *pSys)
Returns a pointer to the environment dictionary. This dictionary stores information that describes this implementation as required by the Standard.
void ficlSetEnv(FICL_SYSTEM *pSys, char *name, UNS32 value)
Enters a new constant into the environment dictionary, with the specified name and value.
void ficlSetEnvD(FICL_SYSTEM *pSys, char *name, UNS32 hi, UNS32 lo)
Enters a new double-cell constant into the environment dictionary with the specified name and value.
FICL_DICT *ficlGetLoc(FICL_SYSTEM *pSys)
Returns a pointer to the locals dictionary. This function is defined only if FICL_WANT_LOCALS is #defined as non-zero (see sysdep.h). The locals dictionary is the symbol table for local variables.
void ficlCompileCore(FICL_SYSTEM *pSys)
Defined in words.c, this function builds ficl's primitives. 
void ficlCompileSoftCore(FICL_SYSTEM *pSys)
Defined in softcore.c, this function builds ANS required words and ficl extras by evaluating a text string. Python3 script softcore.py generates the string in softcore.c from .fr files in ficl/softcore.  

Ficl Source Files

ficl.h Declares most public functions and all data structures. Includes sysdep.h and math.h
sysdep.h Declares system dependent functions and contains build control macros. Edit this file to suit your target.
dpmath.h Declares functions for double precision integer math. Sizes automatically to 128 bits on a 64 bit machine or 64 bits on a 32 bit machine
dict.c Dictionary
ficl.c System initialization, termination, and ficlExec
float.c Adds precompiled definitions from the optional FLOAT word set. Most of the file is conditioned on FICL_WANT_FLOAT
dpmath.c Double precision integer math support, including portable versions of ficlLongMul and ficlLongDiv
prefix.c The optional prefix parse step (conditioned on FICL_EXTENDED_PREFIX). This parse step handles numeric constructs like 0xa100, for example. See the release notes for more on parse steps.
search.c Contains C implementations of several of the SEARCH and SEARCH EXT words
softcore.c Contains all of the "soft" words - those written in Forth and compiled by Ficl at startup time. Sources for these words are in the softwords directory. The files softwords/softcore.bat and softwords/softcore.pl generate softcore.c from the .fr sources.
softwords/ Directory contains sources and translation scripts for the words defined in softcore.c. Softcore.c depends on most of the files in this directory. See softcore.bat for the actual list of files that contribute to softcore.c. This is where you'll find source code for the object oriented extensions. PERL script softcore.pl converts the .fr files into softcore.c.
stack.c Stack methods
sysdep.c Target dependent functions declared in sysdep.h. Edit this file to suit your target system.
testmain.c The main() function for unix/linux/win32 console applications - use this as an example to integrate ficl into your system. Also runs unit tests and adds a few handy OS interface words.
tools.c Contains C implementations of TOOLS and TOOLS EXT words, the debugger, and debugger support words.
vm.c Virtual Machine methods
unix.c Platform extensions words loaded in ficl.c by ficlCompilePlatform() - conditioned on FICL_WANT_PLATFORM
words.c Exports ficlCompileCore(), the run-time dictionary builder, and contains most precompiled CORE and CORE-EXT words.

Ficl Internals

Major Data Structures

A running memory image of Ficl consists of one or more FICL_SYSTEMs, each of which owns exactly one dictionary (FICL_DICT), and one or more virtual machines (FICL_VM). Each VM owns two stacks (FICL_STACK) - one for parameters (the parameter stack) and one for return addresses (the return stack). Ficl is a permissive, untyped language by nature, so its fundamental unit of storage is a CELL: a chunk of memory large enough to hold an address or a scalar type.

Ficl major data structures Diagram showing FICL_SYSTEM owning a dictionary and multiple virtual machines. Each VM owns parameter, return, and optional float stacks. The dictionary contains wordlists, FICL_WORD entries, and cell payloads. FICL_SYSTEM FICL_VM param stack return stack float stack (optional) FICL_DICT FICL_WORDLIST FICL_WORD owns owns owns contains

FICL_SYSTEM

The system structure associates one or more virtual machines with a dictionary. All FICL_SYSTEMS include a link pointer that is used to keep track of every allocated system so that memory can be freed by ficlTermSystem. Each system contains a list of virtual machines associated with it. Each system has at least one virtual machine. In a typical implementation, there is one virtual machine per native OS thread, and there may be several VMs sharing a single FICL_SYSTEM, or one FICL_SYSTEM per VM if the implementation needs to support multiple user sessions in a robust way. A FICL_SYSTEM also includes a special dictionary for local variable support (if enabled by FICL_WANT_LOCALS) and another for environment variable support. Environment variables describe the configuration of the system in conformance with American National Standard Forth (ANS Forth).

FICL_DICT

A dictionary manages a fixed-size block of contiguous memory. It serves two roles: to keep track of allocated memory, and to collect symbol tables called wordlists. Each dictionary contains at least one wordlist. The dictionary organized memory (perhaps this is too kind) as an array of CELLs that grows from low memory to high memory within fixed limits determined by the FICL_DEFAULT_DICT parameter in sysdep.h. A wordlist is the controlling structure of a Ficl symbol table. Each wordlist is a hash table containing pointers to FICL_WORDs. Each FICL_WORD associates a pointer to code with one or more CELLs of the dictionay. Each word usually has a name as well, but this is not required. It is possible to create anonymous words using :NONAME. Each word's code pointer determines that word's runtime behavior, and by implication the purpose of its payload data. Some words interpret their payload as a list of Ficl words, and execute them. This is how new behaviors of the language are defined. Other words view their payload field as a location in which one or more CELLs can be stored (VARIABLEs, for example). At runtime, such words push the address of their payload area onto the parameter stack.

FICL_VM

The virtual machine collects state related to execution of Ficl words. Each VM includes registers used by the inner interpreter, some state variables (AKA user variables) such as the current numeric base, and a jmpbuf. A VM has a pointer to the FICL_SYSTEM of which it is a part. It also has a pointer to an incoming text string that it is interpreting. There are VM methods that excute a word given its address (xt), and ones that interpret a text string.

FICL_STACK

Each VM owns a parameter stack, a return stack, and if float support is enabled, a float parameter stack. Parameters, return addresses, and floats are all CELL sized, and values may be moved back and forth among stacks using various Ficl words for that purpose. 

Inner Interpreter: Example Execution

This example shows how the inner interpreter executes a simple colon definition with control flow. The word below computes a factorial using a counted loop and a running accumulator.

: fact ( n -- n! )
  1 swap 1+ 1 ?DO
    I *
  LOOP
;

Ficl stores colon definitions as a payload of execution tokens (XTs). In simplified form, the payload for fact looks like this:

XT (literal) 1
XT swap
XT (literal) 1
XT +
XT (?do) [exit address]
XT I
XT *
XT (loop) [branch address]
XT (;)

When C code calls ficlExecXT with the fact word, it sets up a nested exception frame, pushes the exit-inner sentinel on the IP stack, and then enters the inner loop. The inner loop walks the XT list until (;) triggers VM_INNEREXIT.

If an XT in the payload refers to a colon definition, the inner loop executes its code like any other word. The callee pushes the current instruction pointer, switches to its own payload, and returns to the caller when (;) pops the saved IP.


Ficl extras

Number syntax

You can precede a number with "0x", as in C, and it will be interpreted as a hex value regardless of the value of BASE. Likewise, numbers prefixed with "0d" will be interpreted as decimal values. Example:
ok> decimal 123 . cr
123
ok> 0x123 . cr
291
ok> 0x123 x. cr
123
Note: ficl2.05 and later - this behavior is controlled by the prefix parser defined in prefix.c. You can add other prefixes by defining handlers for them in ficl or C.

The SEARCH wordset and Ficl extensions

Ficl implements many of the search order words in terms of two primitives called >SEARCH and SEARCH>. As their names suggest (assuming you're familiar with Forth), they push and pop the search order stack.

The standard does not appear to specify any conditions under which the search order is reset to a sane state. Ficl resets the search order to its default state whenever ABORT happens. This includes stack underflows and overflows. QUIT does not affect the search order. The minimum search order (set by ONLY) is equivalent to

FORTH-WORDLIST 1 SET-ORDER

There is a default maximum of 16 wordlists in the search order. This can be changed by redefining FICL_DEFAULT_VOCS (declared in sysdep.h).

Note: Ficl resets the search order whenever it does ABORT. If you don't like this behavior, just comment out the dictResetSearchOrder() lines in ficlExec().

>search ( wid -- )
Push wid onto the search order. Many of the other search order words are written in terms of the SEARCH> and >SEARCH primitives. This word can be defined in ANS Forth as follows
: >search   >r get-order 1+ r> swap set-order ;
search>   ( -- wid )
Pop wid off the search order (can be coded in ANS Forth as : search>  get-order nip 1- set-order ; )
ficl-set-current   ( wid -- old-wid )
Set wid as compile wordlist, leaving the previous compile wordlist on the stack
ficl-vocabulary   ( nBins "name" -- )
Creates a ficl-wordlist with the specified number of hash table bins, binds it to the name, and associates the semantics of vocabulary with it (replaces the top wid in the search order list with its own wid when executed)
ficl-wordlist   ( nBins -- wid )
Creates a wordlist with the specified number of hash table bins, and leaves the address of the wordlist on the stack. A ficl-wordlist behaves exactly as a regular wordlist, but it may search faster depending on the number of bins chosen and the number of words it contains at search time. As implemented in ficl, a wordlist is single threaded by default. ficl-named-wordlist takes a name for the wordlist and creates a word that pushes the wid. This is by contrast to VOCABULARY, which also has a name, but replaces the top of the search order with its wid.
forget-wid   ( wid -- )
Iterates through the specified wordlist and unlinks all definitions whose xt addresses are greater than or equal to the value of HERE, the dictionary fill pointer. 
hide   ( -- current-wid-was )
Push the hidden wordlist onto the search order, and set it as the current compile wordlist (unsing ficl-set-current). Leaves the previous compile wordlist ID. I use this word to hide implementation factor words that have low reuse potential so that they don't clutter the default wordlist. To undo the effect of hide, execute  previous set-current
hidden   ( -- wid )
Wordlist for storing implementation factors of ficl provided words. To see what's in there, try:  hide words previous set-current
wid-get-name   ( wid -- c-addr u )
Ficl wordlists (2.05 and later) have a name property that can be assigned. This is used by ORDER to list the names of wordlists in the search order. 
wid-set-name   ( c-addr wid -- )
Ficl wordlists (2.05 and later) have a name property that can be assigned. This is used by ORDER to list the names of wordlists in the search order. The name is assumed to be a \0 terminated string (C style), which conveniently is how Ficl stores word names.  See softwords/softcore.fr definition of brand-wordlist 
wid-set-super   ( wid -- )
Ficl wordlists have a parent wordlist pointer that is not specified in standard Forth. Ficl initializes this pointer to NULL whenever it creates a wordlist, so it ordinarily has no effect. This word sets the parent pointer to the wordlist specified on the top of the stack. Ficl's implementation of SEARCH-WORDLIST will chain backward through the parent link of the wordlist when searching. This simplifies Ficl's object model in that the search order does not need to reflect an object's class hierarchy when searching for a method. It is possible to implement Ficl object syntax in strict ANS Forth, but method finders need to manipulate the search order explicitly.

User variables

user   ( -- ) name
Create a user variable with the given name. User variables are virtual machine local. Each VM allocates a fixed amount of storage for them. You can change the maximum number of user variables allowed by defining FICL_USER_CELLS on your compiiler's command line. Default is 16 user cells. User variables behave like VARIABLEs in all other respects (you use @ and ! on them, for example). Example:
user current-class
0 current-class !

Miscellaneous

-roll   ( xu xu-1 ... x0 u -- x0 xu-1 ... x1 ) 
Rotate u+1 items on top of the stack after removing u. Rotation is in the opposite sense to ROLL
-rot   ( a b c -- c a b )
Rotate the top three stack entries, moving the top of stack to third place. I like to think of this as 11/2swap because it's good for tucking a single cell value behind a cell-pair (like an object). 
.env   ( -- )
List all environment variables of the system
.hash   ( -- )
List hash table performance statistics of the wordlist that's first in the search order
.ver   ( -- )
Display ficl version ID
>name   ( xt -- c-addr u )
Convert a word's execution token into the address and length of its name
body>   ( a-addr -- xt )
Reverses the effect of CORE word >body (converts a parameter field address to an execution token)
compile-only
Mark the most recently defined word as being executable only while in compile state. Many immediate words have this property.
empty   ( -- ) 
Empty the parameter stack
endif
Synonym for THEN
last-word   ( -- xt )
Pushes the xt address of the most recently defined word. This applies to colon definitions, constants, variables, and words that use create. You can print the name of the most recently defined word with 
last-word >name type 
parse-word   ( <spaces>name -- c-addr u )
Skip leading spaces and parse name delimited by a space. c-addr is the address within the input buffer and u is the length of the selected string. If the parse area is empty, the resulting string has a zero length. (From the Standard)
q@   ( addr -- x )
Fetch a 32 bit quantity from the specified address
q!   ( x addr -- )
Store a 32 bit quantity to the specified address 
w@   ( addr -- x )
Fetch a 16 bit quantity from the specified address
w!   ( x addr -- )
Store a 16 bit quantity to the specified address (the low 16 bits of the given value)
x.   ( x -- )
Pop and display the value in hex format, regardless of the current value of BASE

Extra words defined in testmain.c

break   ( -- )
Does nothing - just a handy place to set a debugger breakpoint
cd      ( "directory-name<newline>" -- )
Executes the Win32 chdir() function, changing the program's logged directory.
clock   ( -- now )
Wrapper for the ANSI C clock() function. Returns the number of clock ticks elapsed since process start.
clocks/sec   ( -- clocks_per_sec )
Pushes the number of ticks in a second as returned by clock
load    ( "filename<newline>" -- )
Opens the Forth source file specified and loads it one line at a time, like INCLUDED (FILE)
pwd     ( -- )
Prints the current working directory as set by cd
system  ( "command<newline>" -- )
Issues a command to a shell; implemented with the Win32 system() call.
spewhash   ( "filename<newline>" -- )
Dumps all threads of the current compilation wordlist to the specified text file. This was useful when I thought there might be some point in attempting to optimize the hash function. I no longer harbor those illusions.

ANS Required Information

Implementation-defined Options

The implementation-defined items in the following list represent characteristics and choices left to the discretion of the implementor, provided that the requirements of the Standard are met. A system shall document the values for, or behaviors of, each item. 

Ambiguous Conditions

A system shall document the system action taken upon each of the general or specific ambiguous conditions identified in this Standard. See 3.4.4 Possible actions on an ambiguous condition. 

The following general ambiguous conditions could occur because of a combination of factors: 

The following specific ambiguous conditions are noted in the glossary entries of the relevant words: 

Locals Implementation-defined options

Locals Ambiguous conditions

Programming Tools Implementation-defined options

Search Order Implementation-defined options

Search Order Ambiguous conditions