ficl 2.03 release notes

Forth Inspired Command Language 
Author: John Sadler (john_sadler@alum.mit.edu)
Created: 19 July 1997 
Revision 2.03: 20 May 1999 

Contents


What is ficl?

Ficl (Forth inspired command language) is an ANS Forth interpreter written in C. Unlike traditional Forths, this interpreter is designed to be embedded into other systems as a command/macro/development prototype language. Ficl provides object extensions that can be used to wrap methods and structures of the host system without altering them. See below for examples of software that includes ficl.
Where Forths usually view themselves as the center of the system and expect the rest of the system to be coded in Forth, Ficl acts as a component of the system. It is easy to export code written in C or ASM 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 object model that can act as an object oriented adapter for code written in C (or asm, Forth, C++...). 
Ficl Design goals
  • Target 32 bit processors (version 2.03 targets 64 bit processors too)
  • Scripting, prototyping, and extension language for systems written also in C
  • Supportable - code is as transparent as I can make it
  • Interface to functions written in C
  • Conform to the Forth DPANS 94
  • Minimize porting effort - require an ANSI C runtime environment and minimal glue code
  • Provide object oriented extensions

 

Ficl features

  • Code is written in ANSI C for portability. 
  • Standard: Implements the ANS Forth CORE word set, part of the CORE EXT word set, SEARCH and SEARCH EXT, TOOLS and part of TOOLS EXT, LOCAL and LOCAL EXT, EXCEPTION, MEMORY,  and various extras.
  • Extensible: you can export code written in Forth, C, or asm in a straightforward way. Ficl provides open facilities for extending the language in an application specific way. You can even add new control structures (not surprising if you're familiar with Forth)
  • Ficl and C can interact in two ways: Ficl can wrap C code, and C functions can invoke ficl code.
  • Ficl code is thread safe and re-entrant:  All Ficl VMs share one system dictionary; each Ficl virtual machine has an otherwise complete state, and each can be bound to a separate I/O channel (or none at all). An optional function called ficlLockDictionary() can control exclusive dictionary access. This function is stubbed out by default (See FICL_MULTITHREAD in sysdep.h). As long as there is only one "session" that can compile words into the dictionary, you do not need exclusive dictionary access for multithreading. Note: while the code is re-entrant, there are still restrictions on how you can use it safely in a multithreaded system. Specifically, the VM itself maintains state, so you generally need a VM per thread in a multithreaded system. If interrupt service routines make calls into Ficl code that alters VM state, then these generally need their own VM as well. Alternatively, you could provide a mutual exclusion mechanism to serialize access to a VM from multiple threads.
  • Simple incorporation into existing systems: the sample implementation requires three Ficl function calls (see the example program in testmain.c).
  • ROM able: Ficl is designed to work in RAM based and ROM code / RAM data environments. It does require somewhat more memory than a pure ROM implementation because it builds its system dictionary in RAM at startup time.
  • Written an ANSI C to be as simple as I can make it to understand, support, debug, and port. Compiles without complaint at /Az /W4 (require ANSI C, max. warnings) under Microsoft VC++
  • Does full 32 bit math (but you need to implement two mixed precision math primitives (see sysdep.c))
  • Type 1 indirect threaded interpreter


Porting ficl

To install ficl on your target system, you need an ANSI C compiler and its runtime library. Inspect the system dependent macros and functions in sysdep.h and sysdep.c and edit them to suit your system. For example, INT16 is a short on some compilers and an int on others. Check the default CELL alignment controlled by FICL_ALIGN. If necessary, add new definitions of ficlMalloc, ficlFree, ficlLockDictionary, and ficlTextOut to work with your operating system. Finally, use testmain.c as a guide to installing the ficl system and one or more virtual machines into your code. You do not need to include testmain.c in your build. 

Feel free to stub out the double precision math functions (which are presently implemented as inline assembly because it's so easy on many 32 bit processors) with kludge code that only goes to 32 bit precision. In most applications, you won't notice the difference. If you're doing a lot of number crunching, consider implementing them correctly. 

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 most of the "soft" words. 

To-Do List (target system dependent words)

  • Unimplemented system dependent CORE word: KEY 
  • Kludged CORE word: ACCEPT

 

Application Programming Interface

See the comments in ficl.c and ficl.h for additional information, and the example in file testmain.c.
 
void 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(void)
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(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.
FICL_VM *ficlNewVM(void)
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(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(void)
Returns a pointer to the main system dictionary, or NULL if the system is uninitialized.
FICL_DICT *ficlGetEnv(void)
Returns a pointer to the environment dictionary. This dictionary stores information that describes this implementation as required by the Standard.
void ficlSetEnv(char *name, UNS32 value)
Enters a new constant into the environment dictionary, with the specified name and value.
void ficlSetEnvD(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(void)
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_DICT *dp)
Defined in words.c, this function builds ficl's primitives. 
void ficlCompileSoftCore(FICL_VM *pVM)
Defined in softcore.c, this function builds ANS required words and ficl extras by evaluating a text string (think of it as a memory mapped file ;-) ). The string itself is built from files in the softwords directory by PERL script softcore.pl. 

 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 port to another system.
math.h Declares functions for 64 bit math
words.c Exports ficlCompileCore(), the run-time dictionary builder, and contains all primitive words as static functions.
vm.c Virtual Machine methods
stack.c Stack methods
ficl.c System initialization, termination, and ficlExec
dict.c Dictionary
math64.c Implementation of 64 bit math words (except the two unsigned primitives declared in sysdep.h and implemented in sysdep.c)
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.
sysdep.c Implementation of system dependent functions declared in sysdep.h
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.

What's new in version 2.03

This is the first version of Ficl that includes contributed code. Thanks especially to Daniel Sobral, Michael Gauland for contributions and bug finding. 

New words

  • clock              (FICL)
  • clocks/sec         (FICL)
  • dnegate            (DOUBLE)
  • ms                 (FACILITY EXT - replaces MSEC)
  • throw              (EXCEPTION)
  • catch              (EXCEPTION)
  • allocate           (MEMORY)
  • free               (MEMORY)
  • resize             (MEMORY)
  • within             (CORE EXT)
  • alloc              (class method)
  • alloc-array        (class method)
  • free               (class method)
Bugs Fixed
  • Bug fix in isNumber(): used to treat chars between 'Z' and 'a' as valid in base 10... (harmless, but weird)
  • ficlExec pushes the ip and interprets at the right times so that nested calls to ficlExec behave the way you'd expect them to.
  • evaluate respects count parameter, and also passes exceptional return conditions back out to the calling instance of ficlExec.
  • VM_QUIT now clears the locals dictionary in ficlExec.
Ficlwin Enhancements
  • File Menu: recent file list and Open now load files.
  • Text ouput function is now faster through use of string caching. Cache flushes at the end of each line and each time ficlExec returns.
  • Edit/paste now behaves more reasonably for text. File/open loads the specified file.
  • Registry entries specify dictionary and stack sizes, default window placement, and whether or not to create a splitter for multiple VMs. See HKEY_CURRENT_USER/Software/CodeLab/ficlwin/Settings
Ficl Enhancements
  • This version includes changes to make it 64 bit friendly. This unfortunately meant that I had to tweak some core data types and structures. I've tried to make this transparent to 32 bit code, but a couple of things got renamed. INT64 is now DPINT. UNS64 is now DPUNS. FICL_INT and FICL_UNS are synonyms for INT32 and UNS32 in 32 bit versions, but a are obsolescent. Please use the new data types instead. Typed stack operations on INT32 and UNS32 have been renamed because they operate on CELL scalar types, which are 64 bits wide on 64 bit systems. Added BITS_PER_CELL, which has legal values of 32 or 64. Default is 32.
  • ficl.c: Added ficlExecXT() - executes an xt completely before returning, passing back any exception codes generated in the process. Normal exit code is VM_INNEREXIT.
  • ficl.c: Added ficlExecC() to operate on counted strings as opposed to zero terminated ones.
  • ficlExec pushes ip and executes interpret at the right times so that nested calls to ficlExec behave the way you'd expect them to.
  • ficlSetStackSize() allows specification of stack size at run-time (affects subsequent invocations of ficlNewVM()).
  • vm.c: vmThrow() checks for (pVM->pState != NULL) before longjmping it. vmCreate nulls this pointer initially. 
  • EXCEPTION wordset contributed by Daniel Sobral of FreeBSD
  • MEMORY-ALLOC wordset contributed by Daniel Sobral, too. Added class methods alloc and alloc-array in softwords/oo.fr to allocate objects from the heap.
  • Control structure match check upgraded (thanks to Daniel Sobral for this suggestion). Control structure mismatches are now errors, not warnings, since the check accepts all syntactally legal constructs.
  • Added vmInnerLoop() to vm.h. This function/macro factors the inner  interpreter out of ficlExec so it can be used in other places. Function/macro behavior is conditioned on INLINE_INNER_LOOP in sysdep.h. Default: 1 unless _DEBUG is set. In part, this is because VC++ 5 goes apoplectic when trying to compile it as a function. See 

  • comments in vm.c
  • EVALUATE respects the count parameter, and also passes exceptional return conditions back out to the calling instance of ficlExec.
  • VM_QUIT clears locals dictionary in ficlExec()
  • Added Michael Gauland's ficlLongMul and ficlLongDiv and support routines to math64.c and .h. These routines are coded in C, and are compiled only if PORTABLE_LONGMULDIV == 1 (default is 0).
  • Added definition of ficlRealloc to sysdep.c (needed for memory allocation wordset). If your target OS supports realloc(), you'll probably want to redefine ficlRealloc in those terms. The default version does ficlFree followed by ficlMalloc.
  • testmain.c: Changed gets() in testmain to fgets() to appease the security gods.
  • testmain: msec renamed to ms in line with the ANS
  • softcore.pl now removes comments & spaces at the start and end of lines. As a result: sizeof (softWords) == 7663 bytes (used to be 20000)  and consumes 11384 bytes of dictionary when compiled
  • Deleted license paste-o in readme.txt (oops).

What's new in version 2.02

New words Bugs Fixed
  • forget now adjusts the dictionary pointer to remove the name of the word being forgotten (name chars come before the word header in ficl's dictionary)
  • :noname used to push the colon control marker and its execution token in the wrong order
  • source-id now behaves correctly when loading a file.
  • refill returns zero at EOF (Win32 load). Win32 load command continues to be misnamed. Really ought to be called included, but does not exactly conform to that spec either (because included expects a string signature on the stack, while Ficl's load expects a filename upon invocation). The "real" LOAD is a BLOCK word.
Enhancements (IMHO)
  • dictUnsmudge no longer links anonymous definitions into the dictionary
  • oop is no longer the default compile wordlist at startup, nor is it in the search order. Execute also oop definitions to use Ficl OOP.
  • Revised oo.fr extensively to make more use of early binding
  • Added meta - a constant that pushes the address of metaclass. See oo.fr for examples of use.
  • Added classes: c-ptr  c-bytePtr  c-2bytePtr  c-cellPtr These classes model pointers to non-object data, but each knows the size of its referent.

What's new in version 2.01

  • Bug fix: (local) used to leave a value on the stack between the first and last locals declared. This value is now stored in a static.
  • Added new local syntax with parameter re-ordering. See description below. (No longer compiled in version 2.02, in favor of the Johns Hopkins syntax)

What's new in version 2.0

  • New ANS Forth words: TOOLS and part of TOOLS EXT, SEARCH and SEARCH EXT, LOCALS and LOCALS EXT word sets, additional words from CORE EXT, DOUBLE, and STRING. (See the function ficlCompileCore in words.c for an alphabetical list by word set).
  • Simple USER variable support - a user variable is a virtual machine instance variable. User variables behave as VARIABLEs in all other respects.
  • Object oriented syntax extensions (see below)
  • Optional stack underflow and overflow checking in many CORE words (enabled when FICL_ROBUST >= 2)
  • Various bug fixes

Local Variables

Ficl includes support for LOCALS and LOCALS EXT words (all three of them!). I've implemented both of the local variable syntaxes suggested in DPANS Appendix A.13. Examples: (By the way, Ficl implements -ROT as : -rot   2 -roll ;
    \ Using LOCALS| from LOCALS EXT
    : -rot   ( a b c -- c a b )
        locals| c b a |
        c a b 
    ;
    \ Using LOCAL END-LOCAL
    : -rot   ( a b c -- c a b )
        local c
        local b
        local a
        end-locals
        c a b
    ;
Local variable support is optional because it adds a small amount of overhead to the outer interpreter. You can disable it by setting FICL_WANT_LOCALS to 0 in sysdep.h. Beware: much of the OOP code described below uses local variables, so if you disable locals, you're going to lose other capabilities too. Local variables can make Forth code quite a bit easier to read, so I'd encourage you to experiment with them. 
The default maximum number of local variables is 16. It's controlled by FICL_MAX_LOCALS in sysdep.h. 
Ficl 2.02 includes by default an implementation of the Johns Hopkins local syntax (as best I can determine it from examples on the web). This syntax lets you declare local variables that look very much like a stack comment. Variables in the declaration appear in the "correct" order for a stack comment. Everything after the -- is treated as a comment. In addition, you can insert a | before the -- to declare one or more zero-initialized locals. Example:
:tuck0   { a b c | d -- 0 a b c }
    d a b c ;
The | and -- delimiters can appear at most once, and must appear in the order shown in the example to work correctly. The local declaration ends at the first occurrence of }. The declaration must all be on one line as presently implemented.
Ficl 2.01 added yet another local syntax that models a stack comment. This one is not compiled in the release, but you can add it by editing softwords/softcore.bat to include the file ficllocal.fr. In this case, parameters are re-ordered so that the rightmost initialized param comes from the top of the stack. The syntax is: 
    {{ <initialized params> -- <cleared params> }}
You can omit either the initialized or the cleared parameters. Parameters after the double dash are set to zero initially. Those to the left are initialized from the stack at execution time. Examples (lame ones, admittedly): 
    : -rot   ( a b c -- c a b )
        {{ a b c }}
        c a b 
    ;
    : tuck0  ( a b c -- 0 a b c )
        {{ a b c -- d }}
        d a b c 

Search Order

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. See the list of Ficl extras for details. 
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). 

Soft Words

Many words from all 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 .bat file (softcore.bat) and a PERL 5 script (softcore.pl) that convert Forth files into the file softcore.c, so softcore.c is really dependent on the Forth sources. This is not reflected in the Visual C++ project database. For the time being, it's a manual step. You can edit softcore.bat to change the list of files that contribute to softcore.c. 


Objects in ficl

Ficl is not the first Forth to include Object Oriented extensions. Ficl's OO syntax owes a debt to the work of John Hayes and Dick Pountain, among others. OO Ficl is different from other OO Forths in a few ways, though (some things never change). First, unlike several implementations, the syntax is documented (below) beyond the source code. In Ficl's spirit of working with C code, the OO syntax provides means to adapt existing data structures. I've tried to make Ficl's OO model simple and safe by unifying classes and objects, providing late binding by default, and separating namespaces so that methods and regular Forth words are not easily confused. 

Design goals of Ficl OO syntax

Ficl's object extensions provide the traditional OO benefits of associating data with the code that manipulates it, and reuse through single inheritance. Ficl also has some unusual capabilities that support interoperation with systems written in C. 
  • Ficl objects are normally late bound for safety (late binding guarantees that the appropriate method will always be invoked for a particular object). Early binding is also available, provided you know the object's class at compile-time.
  • Ficl OOP supports single inheritance, aggregation, and arrays of objects.
  • Classes have independent name spaces for their methods: methods are only visible in the context of a class or object. Methods can be overridden or added in subclasses; there is no fixed limit on the number of methods of a class or subclass.
  • Ficl OOP syntax is regular and unified over classes and objects. In ficl, a classes are objects. Class methods include the ability to subclass and instantiate.
  • Ficl can adapt legacy data structures with object wrappers. You can model a structure in a Ficl class, and create an instance that refers to an address in memory that holds an instance of the structure. The ref object can then manipulate the structure directly. This lets you wrap data structures written and instantiated in C.

Ficl Object Model

All classes in Ficl are derived from the common base class OBJECT. All classes are instances of METACLASS. This means that classes are objects, too. METACLASS implements the methods for messages sent to classes. Class methods create instances and subclasses, and give information about the class. Classes have exactly three elements:
  • The address ( .CLASS ) of a parent class, or zero if it's a base class (only OBJECT and METACLASS have this property)
  • The size ( .SIZE ) in address units of an instance of the class
  • A wordlist ID ( .WID ) for the methods of the class
In the figure below, METACLASS and OBJECT are system-supplied classes. The others are contrived to illustrate the relationships among derived classes, instances, and the two system base classes. The dashed line with an arrow at the end indicates that the object/class at the arrow end is an instance of the class at the other end. The vertical line with a triangle denotes inheritance. 

Note for the curious: METACLASS behaves like a class - it responds to class messages and has the same properties as any other class. If you want to twist your brain in knots, you can think of METACLASS as an instance of itself. 
 

Ficl OO Syntax Tutorial

Introduction

Ficl objects associate a class with an instance (really the storage for one set of instance variables). This is done explicitly, in that any Ficl object is represented by the cell pair: 
    ( instance-addr class-addr )
on the stack. Whenever a named Ficl object executes, it leaves this "signature". All methods expect a class and instance on the stack when they execute, too. In many other OO languages, including C++, instances contain information about their classes (a vtable pointer, for example). By making this pairing explicit rather than implicit, Ficl can be OO about chunks of data that don't realize that they are objects, without sacrificing any robustness for native objects. Whenever  you create an object in Ficl, you specify its class. After that, the object always pushes its class and the address of its payload (instance variable space) when invoked by name. 

Classes are special kinds of objects that store the methods of their instances, the size of an instance's payload, and a parent class pointer. Classes themselves are instances of a special base class called METACLASS, and all classes inherit from class OBJECT. This is confusing at first, but it means that Ficl has a very simple syntax for constructing and using objects. Class methods include subclassing (SUB), creating initialized and uninitialized instances (NEW and INSTANCE), and creating reference instances (REF). Classes also have methods for disassembling their methods (SEE), identifying themselves (ID), and listing their pedigree (PEDIGREE). All objects inherit methods for initializing instances and arrays of instances, for performing array operations, and for getting information about themselves. 

Methods and messages

Methods are the chunks of code that objects execute in response to messages. A message is a request to an object for a behavior that the object supports. When it receives a message, the target object looks up a method that performs the behavior for its class, and executes it. Any specific message will be bound to different methods in different objects, according to class. This separation of messages and methods allows objects to behave polymorphically. (In Ficl, methods are words defined in the context of a class, and messages are the names of those words.) Ficl classes associate messages with methods for their instances (a fancy way of saying that each class owns a wordlist). Ficl provides a late-binding operator --> that sends messages to objects at run-time, and an early-binding operator => that compiles a specific class's method. These operators are the only supported way to invoke methods. Regular Forth words are not visible to the method-binding operators,  so there's no chance of confusing a message with a regular word of the same name. 

Tutorial (finally!)

Since this is a tutorial, I'm assuming you're following along by pasting the examples into ficlWin, the Win32 version of Ficl (or some other build that includes the OO part of softcore.c). I also assume that you're familiar with Forth. If not, please see one of the references, below. Ficl's OOP words are in vocabulary OOP. To put OOP in the search order and make it the compilation wordlist from the default search order (as set by ONLY), type: 
    ONLY   ( reset to default search order )
    ALSO OOP DEFINITIONS
To start, we'll work with the two base classes OBJECT and METACLASS. Try this: 
    metaclass --> methods
The line above contains three words. The first is the name of a class, so it pushes its signature on the stack. Since all classes are instances of METACLASS, METACLASS behaves as if it is an instance of itself (this is the only class with this property). It pushes the same address twice: once for the class and once for the payload, since they are the same. The next word finds a method in the context of a class and executes it. In this case, the name of the method is methods. Its job is to list all the methods that a class knows. What you get when you execute this line is a list of all the class methods Ficl provides. 
    object --> sub c-foo
Causes base-class OBJECT to derive from itself a new class called c-foo. Now we'll add some instance variables and methods to the new class... 
    cell: m_cell1
    4 chars: m_chars
    : init   ( inst class -- )
        locals| class inst |
        0 inst class --> m_cell1 !
        inst class --> m_chars 4 0 fill
        ." initializing an instance of c_foo at " inst x. cr
    ;
    end-class
The first two lines add named instance variables to the class, and create a method for each. Untyped instance variable methods (like those created by cell: cells: char: and chars:) just push the address of the corresponding instance variable when invoked on an instance of the class. It's up to you to remember the size of the instance variable and manipulate it with the usual Forth words for fetching and storing (we'll see below how to aggregate objects, which do know their size). We've also defined a method called init that clears the instance variables. Notice that the method expects the addresses of the class and instance when it's called. It stashes those in local variables to avoid stack tricks, and puts them onto the stack whenever it calls a method. In this case, we're storing zero to the two member variables. 

The init method is special for Ficl objects: whenever you create an initialized instance using new or new-array, Ficl calls the class's init method for you on that instance. The default init method supplied by class object clears the instance, so we didn't really need to override it in this case (see the source code in ficl/softwords/oo.fr). 
Now make an instance of the new class: 

    c-foo --> new foo-instance
And try a few things... 
    foo-instance --> methods
    foo-instance --> pedigree
Or you could type this with the same effect: 
    foo-instance 2dup --> methods --> pedigree
Notice that we've overridden the init method supplied by object, and added two more methods for the member variables. If you type WORDS, you'll see that these methods are not visible outside the context of the class that contains them. The method finder --> uses the class to look up methods. You can use this word in a definition, as we did in init, and it performs late binding, meaning that the mapping from message (method name) to method (the code) is deferred until run-time. To see this, you can decompile the init method like this: 
    c-foo --> see init
    or 
    foo-instance --> class --> see init
Ficl also provides early binding, but you have to ask for it. Ficl's early binding operator pops a class off the stack and compiles the method you've named, so that that method executes regardless of the class of object it's used on. This can be dangerous, since it defeats the data-to-code matching mechanism object oriented languages were created to provide, but it does increase run-time speed by binding the method at compile time. In many cases, such as the init method, you can be reasonably certain of the class of thing you're working on. This is also true when invoking class methods, since all classes are instances of metaclass. Here's an example from the definition of metaclass in oo.fr (don't paste this into ficlWin - it's already there): 
    : new   \ ( class metaclass "name" -- )
        metaclass => instance --> init ;
Try this...
    metaclass --> see new
Decompiling the method with SEE shows the difference between the two strategies. The early bound method is compiled inline, while the late-binding operator compiles the method name and code to find and execute it in the context of whatever class is supplied on the stack at  run-time. 
Notice that the early-binding operator requires a class at compile time. For this reason, classes are IMMEDIATE, meaning that they push their signature at compile time or run time. I'd recommend that you avoid early binding until you're very comfortable with Forth, object-oriented programming,  and Ficl's OOP syntax. 

As advertised earlier, Ficl provides ways to objectify existing data structures without changing them. Instead, you can create a Ficl class that models the structure, and instantiate a ref from this class, supplying the address of the structure. After that, the ref instance behaves as a Ficl object, but its instance variables take on the values in the existing structure. Example (from ficlclass.fr): 
 

    object subclass c-wordlist \ OO model of FICL_HASH
     cell: .parent
     cell: .size
     cell: .hash

     : push  drop  >search ;
     : pop   2drop previous ;
     : set-current   drop set-current ;
     : words   --> push  words previous ;
    end-class

    : named-wid   ( "name" -- ) 
        wordlist  postpone c-wordlist  metaclass => ref ;

In this case, c-wordlist describes Ficl's wordlist structure; named-wid creates a wordlist and binds it to a ref instance of c-wordlist. The fancy footwork with POSTPONE and early binding is required because classes are immediate. An equivalent way to define named-wid with late binding is: 
    : named-wid   ( "name" -- )
        wordlist  postpone c-wordlist  --> ref ;
To do the same thing at run-time (and call it my-wordlist): 
    wordlist  c-wordlist --> ref  my-wordlist
Now you can deal with the wordlist through the ref instance: 
    my-wordlist --> push
    my-wordlist --> set-current
    order
Ficl can also model linked lists and other structures that contain pointers to structures of the same or different types. The class constructor word ref: makes an aggregate reference to a particular class. See the instance variable glossary for an example

Ficl can make arrays of instances, and aggregate arrays into class descripions. The class methods array and new-array create uninitialized and initialized arrays, respectively, of a class. In order to initialize an array, the class must define (or inherit) a reasonable init method. New-array invokes it on each member of the array in sequence from lowest to highest. Array instances and array members use the object methods index, next, and prev to navigate. Aggregate a member array of objects using array:. The objects are not automatically initialized in this case - your class initializer has to call array-init explicitly if you want this behavior. 

For further examples of OOP in Ficl, please see the source file ficl/softwords/ficlclass.fr. This file wraps several Ficl internal data structures in objects and gives use examples. 

OOP Glossary

Note: with the exception of the binding operators (the first two definitions here), all of the words in this section are internal factors that you don't need to worry about. These words provide method binding for all classes and instances. Also described are supporting words and execution factors. All are defined in softwords/oo.fr.
-->   ( instance class "method-name" -- xn )
Late binding: looks up and executes the given method in the context of the class on top of the stack. 
=>   comp: ( class meta "method-name" -- )  exec: ( inst class -- xn )
Early binding: compiles code to execute the method of the class specified at compile time.
do-do-instance
When executed, causes the instance to push its ( instance class ) stack signature. Implementation factor of metaclass --> sub. Compiles .do-instance in the context of a class; .do-instance implements the does> part of a named instance. 
exec-method   ( instance class c-addr u -- xn )
Given the address and length of a message (method name) on the stack, finds the method in the context of the specified class and invokes it. Upon entry to the method, the instance and class are on top of the stack, as usual. If unable to find the method, prints an error message and aborts.
find-method-xt   ( class "method-name" -- class xt )
Attempts to map the message to a method in the specified class. If successful, leaves the class and the execution token of the method on the stack. Otherwise prints an error message and aborts.
lookup-method   ( class c-addr u -- class xt )
Given the address and length of a message (method name) on the stack, finds the method in the context of the specified class. If unable to find the method, prints an error message and aborts.
parse-method   comp: ( "method-name" -- )  exec: ( -- c-addr u )
Parse "name" from the input stream and compile code to push its length and address when the enclosing definition runs.

Instance Variable Glossary

Note: these words are only visible when creating a subclass! To create a subclass, use the sub method on object or any class derived from it (not metaclass). Source code for Ficl OOP is in ficl/softwords/oo.fr.
Instance variable words do two things: they create methods that do an action appropriate for the type of instance variable they represent, and they reserve space in the class template for the instance variable. We'll use the term instance variable to refer both to the method that gives access to a particular field of an object, and to the field itself. Rather than give esentially the same example over and over, here's one example that shows several of the instance variable construction words in use:
    object subclass c-example
        cell:          .cell0

        c-4byte   obj: .nCells
      4 c-4byte array: .quad
        char:          .length
     79 chars:         .name
    end-class
This class only defines instance variables, and it inherits some methods from object. Each untyped instance variable (.cell0, .length, .name) pushes its address when executed. Each object instance variable pushes the address and class of the aggregate object. Similar to C, an array instance variable leaves its base address (and its class) when executed. The word subclass is shorthand for "--> sub
 
cell:        ( offset "name" -- offset' )
            Execution:  ( -- cell-addr )
Create an untyped instance variable one cell wide. The instance variable leaves its payload's address when executed. 
cells:       ( offset nCells "name" -- offset' )
             Execution:  ( -- cell-addr )
Create an untyped instance variable n cells wide.
char:        ( offset "name" -- offset' )
             Execution:  ( -- char-addr )
Create an untyped member variable one char wide
chars:       ( offset nChars "name" -- offset' )
             Execution:  ( -- char-addr )
Create an untyped member variable n chars wide.
obj:         ( offset class meta "name" -- offset' )
             Execution:  ( -- instance class )
Aggregate an uninitialized instance of class as a member variable of the class under construction.
array:       ( offset n class meta "name" -- offset' )
             Execution:  ( -- instance class )
Aggregate an uninitialized array of instances of the class specified as a member variable of the class under construction.
ref:         ( offset class meta "name" -- offset' )
             Execution:  ( -- ref-instance ref-class )
Aggregate a reference to a class instance. There is no way to set the value of an aggregated ref - it's meant as a way to manipulate existing data structures with a Ficl OO model. For example, if your system contains a linked list of 4 byte quantities, you can make a class that represents a list element like this: 
object subclass c-4list
c-4list ref: .link
c-4byte obj: .payload
end-class;
address-of-existing-list c-4list --> ref mylist
The last line binds the existing structure to an instance of the class we just created. The link method pushes the link value and the class c_4list, so that the link looks like an object to Ficl and like a struct to C (it doesn't carry any extra baggage for the object model - the Ficl methods alone take care of storing the class information). 
Note: Since a ref: aggregate can only support one class, it's good for modeling static structures, but not appropriate for polymorphism. If you want polymorphism, aggregate a c_ref (see classes.fr for source) into your class - it has methods to set and get an object.
By the way, it is also possible to construct a pair of classes that contain aggregate pointers to each other. Here's a rough example:
object subclass c-fee
object subclass c-fie
    c-fee ref: .fee
end-class         \ done with c-fie
    c-fie ref: .fie

end-class              \ done with c-fee

Class Methods Glossary

These words are methods of metaclass. They define the manipulations that can be performed on classes. Methods include various kinds of instantiation, programming tools, and access to member variables of classes. Source is in softwords/oo.fr.
instance     ( class metaclass "name" -- instance class ) 
Create an uninitialized instance of the class, giving it the name specified. The method leaves the instance 's signature on the stack (handy if you want to initialize). Example:
c_ref --> instance uninit-ref  2drop
new          ( class metaclass "name" -- ) 
Create an initialized instance of class, giving it the name specified. This method calls init to perform initialization. 
array        ( nObj class metaclass "name" -- nObjs instance class ) 
Create an array of nObj instances of the specified class. Instances are not initialized. Example:
10 c_4byte --> array  40-raw-bytes  2drop drop
new-array    ( nObj class metaclass "name" -- ) 
Creates an initialized array of nObj instances of the class. Same syntax as array
alloc   ( class metaclass -- instance class )
Creates an anonymous instance of class from the heap (using a call to ficlMalloc() to get the memory). Leaves the payload and class addresses on the stack. Usage example:
c-ref --> alloc 2constant instance-of-ref
Creates a double-cell constant that pushes the payload and class address of a heap instance of c-ref.
alloc-array   ( nObj class metaclass -- instance class )
Same as new-array, but creates anonymous instances from the heap using a call to ficlMalloc(). Each instance is initialized using the class's init method
ref          ( instance-addr class metaclass "name" -- ) 
Make a ref instance of the class that points to the supplied instance address. No new instance space is allotted. Instead, the instance refers to the address supplied on the stack forever afterward. For wrapping existing structures.
sub          ( class metaclass -- old-wid addr[size] size )
Derive a subclass. You can add or override methods, and add instance variables. Alias: subclass. Examples:
c_4byte --> sub c_special4byte
( your new methods and instance variables here )
end-class
or
c_4byte subclass c_special4byte
( your new methods and instance variables here )
end-class
.size        ( class metaclass -- instance-size ) 
Returns address of the class's instance size field, in address units. This is a metaclass member variable.
.super       ( class metaclass -- superclass ) 
Returns address of the class's superclass field. This is a metaclass member variable.
.wid         ( class metaclass -- wid ) 
Returns the address of the class's wordlist ID field. This is a metaclass member variable.
get-size
Returns the size of an instance of the class in address units. Imeplemented as
: get-size   metaclass => .size @ ;
get-wid
Returns the wordlist ID of the class. Implemented as 
: get-wid   metaclass => .wid @ ;
get-super
Returns the class's superclass. Implemented as
: get-super   metaclass => .super @ ;
id           ( class metaclass -- c-addr u ) 
Returns the address and length of a string that names the class.
methods      ( class metaclass -- ) 
Lists methods of the class and all its superclasses
offset-of    ( class metaclass "name" -- offset )
Pushes the offset from the instance base address of the named member variable. If the name is not that of an instance variable method, you get garbage. There is presently no way to detect this error. Example:
metaclass --> offset-of .wid
pedigree     ( class metaclass -- ) 
Lists the pedigree of the class (inheritance trail)
see          ( class metaclass "name" -- ) 
Decompiles the specified method - obect version of SEE, from the TOOLS wordset.

object base-class Methods Glossary

These are methods that are defined for all instances by the base class object. The methods include default initialization, array manipulations, aliases of class methods, upcasting, and programming tools.
init         ( instance class -- ) 
Default initializer called automatically for all instances created with new or new-array. Zero-fills the instance. You do not normally need to invoke init explicitly.
array-init   ( nObj instance class -- ) 
Applies init to an array of objects created by new-array. Note that array: does not cause aggregate arrays to be initialized automatically. You do not normally need to invoke array-init explicitly.
free   ( instance class -- )
Releases memory used by an instance previously created with alloc or alloc-array. Note - this method is not presently protected against accidentally deleting something from the dictionary. If you do this, Bad Things are likely to happen. Be careful for the moment to apply free only to instances created with alloc or alloc-array.
class        ( instance class -- class metaclass ) 
Convert an object signature into that of its class. Useful for calling class methods that have no object aliases.
super        ( instance class -- instance parent-class ) 
Upcast an object to its parent class. The parent class of object is zero. Useful for invoking an overridden parent class method.
pedigree     ( instance class -- ) 
Display an object's pedigree - its chain of inheritance. This is an alias for the corresponding class method.
size         ( instance class -- sizeof(instance) ) 
Returns the size, in address units, of one instance. Does not know about arrays! This is an alias for the class method get-size
methods      ( instance class -- ) 
Class method alias. Displays the list of methods of the class and all superclasses of the instance.
index        ( n instance class -- instance[n] class ) 
Convert array-of-objects base signature into signature for array element n. No check for bounds overflow. Index is zero-based, like C, so 
0 my-obj --> index 
is equivalent to 
my-obj
Check out the description of -ROT for help in dealing with indices on the stack.
next         ( instance[n] class -- instance[n+1] class ) 
Convert an array-object signature  into the signature of the next object in the array. No check for bounds overflow.
prev         ( instance[n] class -- instance[n-1] class ) 

Convert an object signature into the signature of the previous object in the array. No check for bounds underflow.

Supplied Classes (See classes.fr)

metaclass 
Describes all classes of Ficl. Contains class methods. Should never be directly instantiated or subclassed. Defined in oo.fr. Methods described above.
object 
Mother of all Ficl objects. Defines default initialization and array indexing methods. Defined in oo.fr. Methods described above.
c-ref 
Holds the signature of another object. Aggregate one of these into a data structure or container class to get polymorphic behavior. Methods & members: 
get   ( inst class -- ref-inst ref-class )
set   ( ref-inst ref-class inst class -- )
.instance   ( inst class -- a-addr ) cell member that holds the instance
.class   ( inst class -- a-addr ) cell member that holds the class
c-byte 
Primitive class derived from object, with a 1-byte payload. Set and get methods perform correct width fetch and store. Methods & members:
get   ( inst class -- c )
set   ( c inst class -- )
.payload   ( inst class -- addr ) member holds instance's value
c-2byte 
Primitive class derived from object, with a 2-byte payload. Set and get methods perform correct width fetch and store. Methods & members:
get   ( inst class -- 2byte )
set   ( 2byte inst class -- )
.payload   ( inst class -- addr ) member holds instance's value
c-4byte 
Primitive class derived from object, with a 4-byte (cell) payload. Set and get methods perform correct width fetch and store. Methods & members:
get   ( inst class -- x )
set   ( x inst class -- )
.payload   ( inst class -- addr ) member holds instance's value
c-ptr
Base class derived from object for pointers to non-object types. This class is not complete by itself: several methods depend on a derived class definition of @size. Methods & members:
.addr   ( inst class -- a-addr ) member variable - holds the pointer address
get-ptr   ( inst class -- ptr )
set-ptr   ( ptr inst class -- )
inc-ptr   ( inst class -- ) Adds @size to pointer address
dec-ptr   ( inst class -- ) Subtracts @size from pointer address
index-ptr   ( i inst class -- ) Adds i*@size to pointer address
c-bytePtr
Pointer to byte derived from c-ptr. Methods & members:
@size   ( inst class -- size ) Push size of the pointed-to thing
get   (  inst class -- c ) Fetch the pointer's referent byte
set   ( c inst class -- ) Store c at the pointer address
c-2bytePtr
Pointer to double byte derived from c-ptr. Methods & members:
@size   ( inst class -- size ) Push size of the pointed-to thing
get   (  inst class -- x ) Fetch the pointer's referent 2byte
set   ( x inst class -- ) Store 2byte x at the pointer address
c-cellPtr
Pointer to cell derived from c-ptr. Methods & members:
@size   ( inst class -- size ) Push size of the pointed-to thing
get   (  inst class -- x ) Fetch the pointer's referent cell
set   ( x inst class -- ) Storex at the pointer address
c-string 
Counted string (thin)


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. Example: 
ok> decimal 123 . cr
123 
ok> 0x123 . cr
291 

Search order words

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.
search>   ( -- wid )
Pop wid off the search 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. 
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-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.

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
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)
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

FiclWin Extras (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.

FiclWin Exclusives (no source provided)

!oreg   ( c -- )
Set the value of the simulated LED register as specified (0..255)
@ireg   ( -- c )
Gets the value of the simulated switch block (0..255)
!dac    ( c -- )
Sets the value of the bargraph control as specified. Valid values range from 0..255
@adc    ( -- c )
Fetches the current position of the slider control. Range is 0..255
status"   ( "ccc<quote>" -- )
Set the mainframe window's status line to the text specified, up to the first trailing quote character.
ms   ( u -- )
Causes the running virtual machine to sleep() for the number of milliseconds specified by the top-of-stack value.


ANS Required Information

ANS Forth System
Providing names from the Core Extensions word set 
Providing the Exception word set
Providing names from the Exception Extensions word set
Providing the Locals word set 
Providing the Locals Extensions word set 
Providing the Memory Allocation word set
Providing the Programming-Tools word set
Providing names from the Programming-Tools Extensions word set
Providing the Search-Order word set
Providing the Search-Order Extensions word set

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. 
  • aligned address requirements (3.1.3.3 Addresses); 

  • System dependent. You can change the default address alignment by defining FICL_ALIGN on your compiler's command line. The default value is set to 2 in sysdep.h. This causes dictionary entries and ALIGN and ALIGNED to align on 4 byte boundaries. To align on 2n byte boundaries, set FICL_ALIGN to n
  • behavior of 6.1.1320 EMIT for non-graphic characters

  • Depends on target system, C runtime library, and your implementation of ficlTextOut().
  • character editing of 6.1.0695 ACCEPT and 6.2.1390 EXPECT

  • None implemented in the versions supplied in words.c. Because ficlExec() is supplied a text buffer externally, it's up to your system to define how that buffer will be obtained.
  • character set (3.1.2 Character types, 6.1.1320 EMIT, 6.1.1750 KEY)

  • Depends on target system and implementation of ficlTextOut()
  • character-aligned address requirements (3.1.3.3 Addresses)

  • Ficl characters are one byte each. There are no alignment requirements.
  • character-set-extensions matching characteristics (3.4.2 Finding definition names)

  • No special processing is performed on characters beyond case-folding. Therefore, extended characters will not match their unaccented counterparts.
  • conditions under which control characters match a space delimiter (3.4.1.1 Delimiters); 

  • Ficl uses the Standard C function isspace() to distinguish space characters. The rest is up to your library vendor.
  • format of the control-flow stack (3.2.3.2 Control-flow stack)

  • Uses the data stack
  • conversion of digits larger than thirty-five (3.2.1.2 Digit conversion)

  • The maximum supported value of BASE is 36. Ficl will assertion fail in function ltoa of vm.c if the base is found to be larger than 36 or smaller than 2. There will be no effect if NDEBUG is defined, however, other than possibly unexpected behavior. 
  • display after input terminates in 6.1.0695 ACCEPT and 6.2.1390 EXPECT

  • Target system dependent
  • exception abort sequence (as in 6.1.0680 ABORT")

  • Does ABORT
  • input line terminator (3.2.4.1 User input device); 

  • Target system dependent (implementation of outer loop that calls ficlExec)
  • maximum size of a counted string, in characters (3.1.3.4 Counted strings, 6.1.2450 WORD)

  • 255
  • maximum size of a parsed string (3.4.1 Parsing)

  • Limited by available memory and the maximum unsigned value that can fit in a CELL (232-1). 
  • maximum size of a definition name, in characters (3.3.1.2 Definition names)

  • Ficl stores the first 31 characters of a definition name.
  • maximum string length for 6.1.1345 ENVIRONMENT?, in characters

  • Same as maximum definition name length
  • method of selecting 3.2.4.1 User input device

  • None supported. This is up to the target system 
  • method of selecting 3.2.4.2 User output device

  • None supported. This is up to the target system 
  • methods of dictionary compilation (3.3 The Forth dictionary)
  • number of bits in one address unit (3.1.3.3 Addresses)

  • Target system dependent. Ficl generally supports processors that can address 8 bit quantities, but there is no dependency that I'm aware of.
  • number representation and arithmetic (3.2.1.1 Internal number representation)

  • System dependent. Ficl represents a CELL internally as a union that can hold INT32 (a signed 32 bit scalar value), UNS32 (32 bits unsigned), and an untyped pointer. No specific byte ordering is assumed. 
  • ranges for n, +n, u, d, +d, and ud (3.1.3 Single-cell types, 3.1.4 Cell-pair types)

  • Assuming a 32 bit implementation, range for signed single-cell values is -231..231-1. Range for unsigned single cell values is 0..232-1. Range for signed double-cell values is -263..263-1. Range for unsigned single cell values is 0..264-1. 
  • read-only data-space regions (3.3.3 Data space);

  • None 
  • size of buffer at 6.1.2450 WORD (3.3.3.6 Other transient regions)

  • Default is 255. Depends on the setting of nPAD in ficl.h. 
  • size of one cell in address units (3.1.3 Single-cell types)

  • System dependent, generally four.
  • size of one character in address units (3.1.2 Character types)

  • System dependent, generally one.
  • size of the keyboard terminal input buffer (3.3.3.5 Input buffers)

  • This buffer is supplied by the host program. Ficl imposes no practical limit.
  • size of the pictured numeric output string buffer (3.3.3.6 Other transient regions)

  • Default is 255 characters. Depends on the setting of nPAD in ficl.h. 
  • size of the scratch area whose address is returned by 6.2.2000 PAD (3.3.3.6 Other transient regions)

  • Not presently supported 
  • system case-sensitivity characteristics (3.4.2 Finding definition names)

  • Ficl is not case sensitive
  • system prompt (3.4 The Forth text interpreter, 6.1.2050 QUIT)

  • "ok>"
  • type of division rounding (3.2.2.1 Integer division, 6.1.0100 */, 6.1.0110 */MOD, 6.1.0230 /, 6.1.0240 /MOD, 6.1.1890 MOD)

  • Symmetric
  • values of 6.1.2250 STATE when true

  • One (no others)
  • values returned after arithmetic overflow (3.2.2.2 Other integer operations)

  • System dependent. Ficl makes no special checks for overflow. 
  • whether the current definition can be found after 6.1.1250 DOES> (6.1.0450 :)

  • No. Definitions are unsmudged after ; only, and only then if no control structure matching problems have been detected.

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: 

  • a name is neither a valid definition name nor a valid number during text interpretation (3.4 The Forth text interpreter)

  • Ficl does ABORT and prints the name followed by " not found".
  • a definition name exceeded the maximum length allowed (3.3.1.2 Definition names)

  • Ficl stores the first 31 characters of the definition name, and uses all characters of the name in computing its hash code. The actual length of the name, up to 255 characters, is stored in the definition's length field.
  • addressing a region not listed in 3.3.3 Data Space

  • No problem: all addresses in ficl are absolute. You can reach any 32 bit address in Ficl's address space.
  • argument type incompatible with specified input parameter, e.g., passing a flag to a word expecting an n (3.1 Data types)

  • Ficl makes no check for argument type compatibility. Effects of a mismatch vary widely depending on the specific problem and operands.
  • attempting to obtain the execution token, (e.g., with 6.1.0070 ', 6.1.1550 FIND, etc.) of a definition with undefined interpretation semantics

  • Ficl returns a valid token, but the result of executing that token while interpreting may be undesirable.
  • dividing by zero (6.1.0100 */, 6.1.0110 */MOD, 6.1.0230 /, 6.1.0240 /MOD, 6.1.1561 FM/MOD, 6.1.1890 MOD, 6.1.2214 SM/REM, 6.1.2370 UM/MOD, 8.6.1.1820 M*/);

  • Results are target procesor dependent. Generally, Ficl makes no check for divide-by-zero. The target processor will probably throw an exception.
  • insufficient data-stack space or return-stack space (stack overflow)

  • With FICL_ROBUST (sysdep.h) set >= 2, most parameter stack operations are checked for underflow and overflow. Ficl does not check the return stack.
  • insufficient space for loop-control parameters

  • No check - Evil results.
  • insufficient space in the dictionary

  • Ficl generates an error message if the dictionary is too full to create a definition header. It checks ALLOT as well, but it is possible to make an unchecked allocation request that overflows the dictionary.
  • interpreting a word with undefined interpretation semantics

  • Ficl protects all ANS Forth words with undefined interpretation semantics from being executed while in interpret state. It is possible to defeat this protection using ' (tick) and EXECUTE, though.
  • modifying the contents of the input buffer or a string literal (3.3.3.4 Text-literal regions, 3.3.3.5 Input buffers)

  • Varies depending on the nature of the buffer. The input buffer is supplied by ficl's host function, and may reside in read-only memory. If so, writing the input buffer can ganerate an exception. String literals are stored in the dictionary, and are writable.
  • overflow of a pictured numeric output string;

  • In the unlikely event you are able to construct a pictured numeric string of more than 255 characters, the system will be corrupted unpredictably. The buffer area that holds pictured numeric output is at the end of the virtual machine. Whatever is mapped after the offending VM in memory will be trashed, along with the heap structures that contain it. 
  • parsed string overflow;

  • Ficl does not copy parsed strings unless asked to. Ordinarily, a string parsed from the input buffer during normal interpretation is left in-place, so there is no possibility of overflow. If you ask to parse a string into the dictionary, as in SLITERAL, you need to have enough room for the string, otherwise bad things may happen. This is not usually a problem. 
  • producing a result out of range, e.g., multiplication (using *) results in a value too big to be represented by a single-cell integer (6.1.0090 *, 6.1.0100 */, 6.1.0110 */MOD, 6.1.0570 >NUMBER, 6.1.1561 FM/MOD, 6.1.2214 SM/REM, 6.1.2370 UM/MOD, 6.2.0970 CONVERT, 8.6.1.1820 M*/)

  • Value will be truncated
  • reading from an empty data stack or return stack (stack underflow)

  • Most stack underflows are detected and prevented if FICL_ROBUST (sysdep.h) is set to 2 or greater. Otherwise, the stack pointer and size are likely to be trashed.
  • unexpected end of input buffer, resulting in an attempt to use a zero-length string as a name

  • Ficl returns for a new input buffer until a non-empty one is supplied.
The following specific ambiguous conditions are noted in the glossary entries of the relevant words: 
  • >IN greater than size of input buffer (3.4.1 Parsing)

  • Bad Things occur - unpredictable bacause the input buffer is supplied by the host program's outer loop. 
  • 6.1.2120 RECURSE appears after 6.1.1250 DOES>

  • It finds the address of the definition before DOES>
  • argument input source different than current input source for 6.2.2148 RESTORE-INPUT

  • Not implemented 
  • data space containing definitions is de-allocated (3.3.3.2 Contiguous regions)

  • This is OK until the cells are overwritten with something else. The dictionary maintains a hash table, and the table must be updated in order to de-allocate words without corruption. 
  • data space read/write with incorrect alignment (3.3.3.1 Address alignment)

  • Target processor dependent. Consequences include: none (Intel), address error exception (68K). 
  • data-space pointer not properly aligned (6.1.0150 ,, 6.1.0860 C,)

  • See above on data space read/write alignment 
  • less than u+2 stack items (6.2.2030 PICK, 6.2.2150 ROLL)

  • Ficl detects a stack underflow and reports it, executing ABORT, as long as FICL_ROBUST is two or larger. 
  • loop-control parameters not available ( 6.1.0140 +LOOP, 6.1.1680 I, 6.1.1730 J, 6.1.1760 LEAVE, 6.1.1800 LOOP, 6.1.2380 UNLOOP)

  • Loop initiation words are responsible for checking the stack and guaranteeing that the control parameters are pushed. Any underflows will be detected early if FICL_ROBUST is set to two or greater. Note however that Ficl only checks for return stack underflows at the end of each line of text. 
  • most recent definition does not have a name (6.1.1710 IMMEDIATE)

  • No problem. 
  • name not defined by 6.2.2405 VALUE used by 6.2.2295 TO

  • Ficl's version of TO works correctly with VALUEs, CONSTANTs and VARIABLEs. 
  • name not found (6.1.0070 ', 6.1.2033 POSTPONE, 6.1.2510 ['], 6.2.2530 [COMPILE])

  • Ficl prints an error message and does ABORT
  • parameters are not of the same type (6.1.1240 DO, 6.2.0620 ?DO, 6.2.2440 WITHIN)

  • No check. Results vary depending on the specific problem. 
  • 6.1.2033 POSTPONE or 6.2.2530 [COMPILE] applied to 6.2.2295 TO

  • The word is postponed correctly. 
  • string longer than a counted string returned by 6.1.2450 WORD

  • Ficl stores the first FICL_STRING_MAX-1 chars in the destination buffer. (The extra character is the trailing space required by the standard. Yuck.) 
  • u greater than or equal to the number of bits in a cell (6.1.1805 LSHIFT, 6.1.2162 RSHIFT)

  • Depends on target process or and C runtime library implementations of the << and >> operators on unsigned values. For I386, the processor appears to shift modulo the number of bits in a cell. 
  • word not defined via 6.1.1000 CREATE (6.1.0550 >BODY, 6.1.1250 DOES>)

  • words improperly used outside 6.1.0490 <# and 6.1.0040 #> (6.1.0030 #, 6.1.0050 #S, 6.1.1670 HOLD, 6.1.2210 SIGN)
    Don't. CREATE reserves a field in words it builds for DOES>to fill in. If you use DOES> on a word not made by CREATE, it will overwrite the first cell of its parameter area. That's probably not what you want. Likewise, pictured numeric words assume that there is a string under construction in the VM's scratch buffer. If that's not the case, results may be unpleasant.

Locals Implementation-defined options

  • maximum number of locals in a definition (13.3.3 Processing locals, 13.6.2.1795 LOCALS|)

  • Default is 16. Change by redefining FICL_MAX_LOCALS, defined in sysdep.h

Locals Ambiguous conditions

  • executing a named local while in interpretation state (13.6.1.0086 (LOCAL))

  • Locals can be found in interpretation state while in the context of a definition under construction. Under these circumstances, locals behave correctly. Locals are not visible at all outside the scope of a definition. 
  • name not defined by VALUE or LOCAL (13.6.1.2295 TO)

  • See the CORE ambiguous conditions, above (no change)

Programming Tools Implementation-defined options

  • source and format of display by 15.6.1.2194 SEE

  • SEE de-compiles definitions from the dictionary. Because Ficl words are threaded by their header addresses, it is very straightforward to print the name and other characteristics of words in a definition. Primitives are so noted. Colon definitions are decompiled, but branch target labels are not reconstructed. Literals and string literals are so noted, and their contents displayed.

Search Order Implementation-defined options

  • maximum number of word lists in the search order (16.3.3 Finding definition names, 16.6.1.2197 SET-ORDER) 

  • Defaults to 16. Can be changed by redefining FICL_DEFAULT_VOCS, declared in sysdep.h
  • minimum search order (16.6.1.2197 SET-ORDER, 16.6.2.1965 ONLY) 

  • Equivalent to FORTH-WORDLIST 1 SET-ORDER

Search Order Ambiguous conditions

  • changing the compilation word list (16.3.3 Finding definition names)

  • Ficl stores a link to the current definition independently of the compile wordlist while it is being defined, and links it into the compile wordlist only after the definition completes successfully. Changing the compile wordlist mid-definition will cause the definition to link into the new compile wordlist. 
  • search order empty (16.6.2.2037 PREVIOUS)

  • Ficl prints an error message if the search order underflows, and resets the order to its default state. 
  • too many word lists in search order (16.6.2.0715 ALSO)

  • Ficl prints an error message if the search order overflows, and resets the order to its default state.
 


For more information

Some software that uses ficl


DISCLAIMER OF WARRANTY and LICENSE

Ficl is freeware. Use it in any way that you like, with the understanding that the code is not supported.

Any third party may reproduce, distribute, or modify the ficl software code or any derivative works thereof without any compensation or license, provided that the original author information and this disclaimer text are retained in the source code files. The ficl software code is provided on an "as is" basis without warranty of any kind, including, without limitation, the implied warranties of merchantability and fitness for a particular purpose and their equivalents under the laws of any jurisdiction. 

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 the ficl release (yay!), please send me email