Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Embedded Systems

Ficl: An Embeddable Extension Language Interpreter


Jan99: Ficl Syntax (and more)

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.

<b>Arithmetic</b>
+ - * / ( 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.

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

<b>Logic</b>
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.

<b>Fetch and Store</b>
@ 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.

<b>Creating New Words</b>
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).

<b>Colon Definitions</b>
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. 

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

<b>Conditionals</b>
: 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.


Copyright © 1999, Dr. Dobb's Journal

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.