Channels ▼

Open Source

A Forth HTML Generator

How It Works

The Forth HTML generator is implemented using MinForth which is an ANS Standard Forth written in C. I chose MinForth because it was possible to port it to my Webster2 web server that doesn't have an operating system per se. I needed dynamic strings (like StringBuffer in Java) for my implementation in addition to the functionality provided by MinForth. For this I turned to the Forth Foundation Library (FFL) instead of reinventing the wheel. It was a small task to port str.fs and stc.fs to the MinForth environment which says something for the Forth standardization process. Dynamic memory words, which are part of ANS Forth, also came in handy.

Tags in the Forth HTML generator are implements as a data structure in dynamic memory which contains storage for a name, for an attribute string, for a series of links to other tags and some flags. Normal tags have a name and optionally an attribute string. Text tags are not named and have their text stored in the attribute portion of the tag structure. Flags in the data structure mark a tag as a normal tag or a text tag and whether or not the tag is closed. As tags are added to other tags, an entry is made in the containing tag for the tag being linked. An HTML document is a network of linked tags.

When a new tag is created, dynamic memory is allocated for the tag data structure, the name of the tag is copied into the structure along with an optional attribute string and the appropriate flags are set. All processing and formatting of attribute strings is performed before copying into the tag structure. The same is true for text strings.

An HTML document is defined by a hierarchical network of tags. The renderpage word is used to convert the network of tags into HTML statements. This is done by performing a depth first traverse of the tag network and rendering the tags along with way. Recursion is used in the code extensively for traversal of the tag network.

Using the Forth HTML Generator

The examples I've presented show how the HTML generator code is used, but a couple of cautions are in order. The HTML generator will always output a properly formed HTML document but whether it is the document you wanted or expected is another matter entirely. For this reason it is important to feed the generated HTML to a browser during the development process to be sure you are on the right path. During your development keep the following in mind:

  • Be careful that all tags created are in fact linked into the tag network. 2. The order tags are linked together is very important. Tags linked into the body of an HTML document appear in that order.
  • Nesting of tags is very important especially for hypertext links. See the code for example three.
  • After you have rendered your HTML page you should free the memory associated with the tag network by calling free-tags on the top-level tag. After you have used the generated HTML you should free the str memory by calling str-free on the str returned from the renderpage word. In MinForth I always execute show-heap to be sure all memory has been freed correctly.


It has been an interesting experience dusting off my old Forth skills and learning ANS Forth as well. Writing the Forth HTML generator got my chops back up and provided me with a useful tool for my on going web server development project.

In doing research into the state of Forth I have concluded that Forth is not a dying language as I first thought but is thriving in many areas. The availability of a standard language definition, foundation, and scientific libraries and a port for almost any processor/operating system you care to mention helps to insure that Forth will continue to have a place in modern system design.

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.