Dr. Dobb's Journal January 1999

Ficl Syntax (and more)


.s				Displays the contents of the stack nondestructively.
.				Pops the top of the stack and displays it.
hex				Sets number base to hex (decimal is the default).
decimal				Sets number base to decimal.

Arithmetic
+ - * / ( a b -- c )		Pops a and b off the stack, perform the operation, and push the result. 
				  Example: 3 2 + (leaves 5 on the stack). 
negate( x -- -x )		Change the sign of the value on top of the stack.

Stack Operations
dup ( x -- x x )		Copy the top of stack.
swap ( x y -- y x )		Swap the top two cells.

Logic
true ( -- -1 )
false ( -- 0 )			False is always zero in Forth, and True can be defined as false invert (all bits set).
				  This unifies logical and bitwise operations. Logical operations view any nonzero 
				  value as True, as in C.
and or xor ( x y -- z )		Perform BITWISE operations on two arguments and push the result.
invert ( x -- ~x )		One's complement the top of stack.
< > <> = ( x y -- flag )	Perform comparison (<> means "not equal") and push True or False.

Fetch and Store
@ w@ c@ ( address -- value )	Fetch 32, 16, or 8 bits, respectively, from address and leave the result on the stack. 
				  Values are zero padded to 32 bits.
! w! c! ( value address -- )	Store 32, 16, or 8 bits, respectively, of value at address.

Creating New Words
constant ( x --) name		Creates a new word that pushes its value (x) when executed. 
				  Example: 0x10000 constant ram-base ram-base @ (fetches 32 bits from 
				  address 0x10000).
variable ( -- ) name		Creates a new word that pushes its address when executed. 
				  Example: variable v (creates a new variable named v), 0 v ! (set v to zero).

Colon Definitions
You can define a new word in terms of existing words using colon and semicolon.
: v++ 1 v +! ; (create new word named v++). Now when you execute v++, it
increments v. In addition to chaining together words, you can also use control
structures in colon definitions. 

Iteration  
: testloop limit index do <insert code here> loop ; 

Conditionals
: signum ( x -- sign )
\ push -1 if negative, 1 if positive, else 0 
 0< if -1 
 else 
  0= if 0 else 1 endif 
 endif 
;

-J.S.

Back to Article


Copyright © 1999, Dr. Dobb's Journal